small medium large xlarge

Back to: All Forums  Take My Money
Generic-user-small
06 May 2017, 00:37
Rails Engineer (11 posts)

Where is Purchase defined?

It is used in your source code to create a transaction in this file:

code/subscription/01/app/workflows/creates_subscription_via_stripe.rb: Purchase.transaction do

Head_shot_pragsmall
06 May 2017, 01:53
Noel Rappin (48 posts)

Typo, it should read Payment.transaction – apparently its not erring because the spec file doesn’t end in spec, looks like it got caught when I updated the naming at one point. (The name of the workflow class is also wrong in the spec file.

Thanks for catching this!

Generic-user-small
06 May 2017, 19:10
Rails Engineer (11 posts)

After applying the suggested changes, rspec generates the following errors:

Failures:

1) CreatesSubscriptionViaStripe happy path creates a customer Got 3 failures:

 1.1) Failure/Error: expect(subscription).to be_pending_initial_payment
        expected `#<Subscription id: 1, user_id: 5, plan_id: 1, start_date: nil, end_date: nil, status: "waiting", paym..., remote_id: nil, string: nil, created_at: "2017-05-06 19:07:48", updated_at: "2017-05-06 19:07:48">.pending_initial_payment?` to return true, got false
      # ./spec/workflows/creates_subscription_via_stripe_spec.rb:29:in `block (3 levels) in <top (required)>'

 1.2) Failure/Error: expect(subscription.payment_method).to eq("stripe")
      
        expected: "stripe"
             got: nil
      
        (compared using ==)
      # ./spec/workflows/creates_subscription_via_stripe_spec.rb:31:in `block (3 levels) in <top (required)>'

 1.3) Failure/Error: expect(subscription.remote_id).to be_present
        expected `nil.present?` to return true, got false
      # ./spec/workflows/creates_subscription_via_stripe_spec.rb:32:in `block (3 levels) in <top (required)>'

2) purchasing a subscription plan can add a plan to a cart Failure/Error: expect(users(:buyer).stripe_id).not_to be_nil

   expected: not nil
        got: nil
 # ./spec/features/subscriptions/subscribe_to_plan_spec.rb:33:in `block (2 levels) in <top (required)>'

Finished in 15.27 seconds (files took 6.72 seconds to load) 51 examples, 2 failures

Failed examples:

rspec ./spec/workflows/creates_subscription_via_stripe_spec.rb:26 # CreatesSubscriptionViaStripe happy path creates a customer rspec ./spec/features/subscriptions/subscribe_to_plan_spec.rb:24 # purchasing a subscription plan can add a plan to a cart

Randomized with seed 21079

Head_shot_pragsmall
08 May 2017, 11:38
Noel Rappin (48 posts)

This is hard to diagnose without seeing your actual code, one possibility is that the VCR cassettes need to be regenerated (your line numbers don’t match with mine, so something is different…)

Generic-user-small
08 May 2017, 17:51
Rails Engineer (11 posts)

Your source code is generating the errors listed above. The path to the project that is generating the errors in your compressed archive is listed in the initial post. I deleted the vcr_cassettes directory prior to running rspec.

Head_shot_pragsmall
08 May 2017, 18:12
Noel Rappin (48 posts)

Okay, I’m puzzled.

This is my working test file:

require "rails_helper"

RSpec.describe CreatesSubscriptionViaStripe, :aggregate_failures, :vcr do

  describe "happy path", :vcr do

    let(:user) { create(:user, id: 5) }
    let(:subscription) { Subscription.create!(
        user: user, plan: plan, status: :waiting) }
    let(:plan) { create(:plan,
        name: "vip_monthly", remote_id: "vip_monthly") }
    let(:workflow) { CreatesSubscriptionViaStripe.new(
        user: user, expected_subscription_id: [subscription.id], token: token) }
    let(:token) { StripeToken.new(
        credit_card_number: "4242424242424242", expiration_month: "12",
        expiration_year: Time.zone.now.year + 1, cvc: "123") }

    it "creates a customer" do
      workflow.run
      subscription.reload
      expect(subscription).to be_pending_initial_payment
      expect(user.stripe_id).to be_present
      expect(subscription.payment_method).to eq("stripe")
      expect(subscription.remote_id).to be_present
    end

  end

end

and my working workflow file:

class CreatesSubscriptionViaStripe

  attr_accessor :user, :expected_subscription_id, :token, :success

  def initialize(user:, expected_subscription_id:, token:)
    @user = user
    @expected_subscription_id = expected_subscription_id
    @token = token
    @successs = false
  end

  def subscription
    @subscription ||= user.subscriptions_in_cart.first
  end

  def expected_plan_valid?
    expected_subscription_id.first.to_i == subscription.id.to_i
  end

  def run
    Payment.transaction do
      return unless expected_plan_valid?
      stripe_customer = StripeCustomer.new(user: user)
      return unless stripe_customer.valid?
      stripe_customer.source = token
      subscription.make_stripe_payment(stripe_customer)
      stripe_customer.add_subscription(subscription)
      @success = true
    end
  rescue Stripe::StripeError => exception
    Rollbar.error(exception)
  end

  def redirect_on_success_url
    user
  end

end
You must be logged in to comment