0

I started development on the payment system for our subscription website using Stripe's custom form: https://stripe.com/docs/custom-form. I have the development site working such that when somebody chooses a subscription, it pops up a form to collect the credit card data, that data is submitted to Stripe, and stripe sends back a token.

In the "Next steps" at the bottom, it seems to indicate that I can then use this token to sign the user up for a subscription. However, the subscription page doesn't show how to use that token when creating a subscription. It appears from that page that when a subscription is created, Stripe will create an invoice and email it to the user.

I'd really like to handle the first payment directly on the website rather than lose users in an email flow. I think it can be done, but I can't find an example. How do I use the Stripe token from the collected credit card to charge the user for the first period of the subscription? The only code for creating a subscription is:

Stripe::Subscription.create(
  :customer => customer.id,
  :plan => "basic-monthly",
)

Which doesn't pass the token in. Where do I pass the token?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • The closest solution I've been able to find is from [Stripe Error: 'Token is Not Supported' when trying to sign up to subscription](http://stackoverflow.com/questions/34754142/stripe-error-token-is-not-supported-when-trying-to-sign-up-to-subscription) where is seems that `source: stripeToken.id` can be passed in *somewhere* (but I'm not sure where). – Stephen Ostermiller Dec 09 '16 at 17:30

2 Answers2

1

You need to use the token to create a customer object, and you can then use the resulting customer object to create the subscription.

This tutorial explains the subscriptions flow with Stripe: https://stripe.com/docs/subscriptions/quickstart.

Ywain
  • 16,854
  • 4
  • 51
  • 67
  • Yes, I've done all that. However, that documentation doesn't say how to pass in the credit card details that I have already collected before the process. When I create the customer and create the subscription, how do I use the credit card token to mark the invoice paid? – Stephen Ostermiller Dec 09 '16 at 20:08
  • You use the token in the customer creation request (by passing it in the `source` parameter). The token carries all the card details, so when you use it to create the customer, the resulting customer object will have the card as its default payment source. – Ywain Dec 10 '16 at 01:04
0

The token can be passed as the source attribute when creating a subscription:

Stripe::Subscription.create(
  :customer => customer.id,
  :plan => "basic-monthly",
  :source => token,
)

I end up doing that for existing customers. Since I am collecting their credit card information again, this allows Stripe to use whatever they just entered.

For new customers, set the source field when creating the customer. The payment source stored with the customer end up getting used when the subscription is created.

You can't create a customer with a source token and then use the same token on the subscription too. I've found out that Stripe considers this to be re-using the token and it throws an exception. You have to conditionally use it in one place or the other depending on whether or not the customer is newly created.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109