David Kimura PRO
Joined 7/18/2015
Drifting Ruby Owner
Ruby Rogues Panelist
David Kimura PRO said almost 4 years ago on Payment Gateway Basics with Stripe :
I'm not sure why the stripe_customer is nil. Are you getting any errors from the stripe_customer method in the user model. The guard clause that you have may be problematic as you're likely returning nil since the user has a stripe_id (assuming). If the stripe_id exists, but you're getting nil returned when looking up the customer, you may have some other kind of problem going on. Looking a bit closer at it. you're trying to return a subscription on the stripe_customer method. You should be retrieving the customer (https://stripe.com/docs/api/customers/create?lang=ruby and also see the retrieve customer section) 

The second issue you'll run into is the customer.subscriptions. Once you do have a customer object, you'll get the undefined method subscriptions for the stripe customer. You'll need to look up the subscription based on the subscription_id. https://stripe.com/docs/api/subscriptions/retrieve?lang=ruby

David Kimura PRO said almost 4 years ago on Payment Gateway Basics with Stripe :
The stripe_customer method returns nil?

Did you change your stripe_customer method to pull the customer instead of the subscription (i.e., return Stripe::Customer.retrieve(stripe_id) if stripe_id?
)

David Kimura PRO said almost 4 years ago on Payment Gateway Basics with Stripe :
This needs to be changed. You cannot scope subscriptions on the customer anymore.
subscription = customer.subscriptions.create(source: params[:stripeToken], plan: 'starter')

to something like this. I haven't tested this, but this is likely the basic flow where you attach the source to the customer (i.e., the payment method) and then you create the subscription.

customer.update({ default_source: params[:stripeToken] })
Stripe::Subscription.create({
  customer: customer,
  plan: 'starter'
})



David Kimura PRO said almost 4 years ago on Payment Gateway Basics with Stripe :
That's strange. The docs, https://stripe.com/docs/api/customers/update?lang=ruby , show the customer object using the update method.



David Kimura PRO said almost 4 years ago on Payment Gateway Basics with Stripe :
  Maybe try

customer.default_source = params[:stripeToken]
customer.save