Justin Mullis

Joined

60 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Stripe payments course getting out of date

Ok excellent, can't wait for the new version!

Posted in Stripe payments course getting out of date

Chris just wanted to mention that the Stripe master course is getting a little out of date (mostly just API changes on Stripe's end). Maybe a good idea to enable commenting on each section/video in the course so people can see there what they need to change.

To start, it looks like you can pass the Stripe token as the "source" when creating a customer. Passing it when creating a subscription gives me an invalid parameter error from the Stripe gem.

I updated my user.rb from

def stripe_customer
    if stripe_id?
        Stripe::Customer.retrieve(stripe_id)
    else
        stripe_customer = Stripe::Customer.create(email: email)
        update(stripe_id: stripe_customer.id)
        stripe_customer
    end
end

to

def stripe_customer(stripeToken = nil)
    if stripe_id?
        Stripe::Customer.retrieve(stripe_id)
    else
        stripe_customer = Stripe::Customer.create(email: email, source: stripeToken)
        update(stripe_id: stripe_customer.id)
        stripe_customer
    end
end

And then the controller code for subscription create goes from

customer = current_user.stripe_customer

subscription = customer.subscriptions.create(
    source: params[:stripeToken],
    plan: params[:plan]
)

to

customer = current_user.stripe_customer(params[:stripeToken])

subscription = Stripe::Subscription.create({
    customer: customer.id,
    items: [{plan: params[:plan]}],
})

I think the code is a little gross looking following the changes, since stripeToken needs to be passed to the model to create the Stripe customer. But its working now.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.