2

How do I save a card to a customer with Stripe.js?

I don't want to change them at this point. I just want to save the credit card info to their stripe account so I can use it later.

It seems like I'd need to use createToken from Stipe.js. But my understanding is that this is a one time use token. I want to save the credit card info for later use.

This seems to be a similar question: Stripe Payment: Save token and customer and make payment later from token but the solution isn't clear. I'm not sure if it means for the customer I need to save card=token and everything will work fine.

Community
  • 1
  • 1
brian
  • 632
  • 4
  • 8
  • 20

3 Answers3

3

Though the question is an old one and solution of this problem is now pretty straightforward in the current Stripe API, I'm just answering for those who accidentally reached or will reach here without reading the Official Stripe Doc properly(like me) searching for this question.

To make a stripe payment You first need to make a call to the Stripe API(Using Stripe's Checkout widget, Elements or Mobile SDKs) with the User's card information. As a response, you will get a token. Then you can charge your customer immediately using Stripe's Charge API. This is for just one-time payment. You will find an example here.

If you want to save customer's information for later payment, you need to create a 'Customer' first using Stripe's API and then using that customer's ID (returned as a response from the previous API call) you can charge this customer. Example here.

I just described the process briefly to show the idea at a glance. But you should really need to read this quickstart guide in Stripe's documentation. This explains the process very well.

sha-1
  • 1,550
  • 1
  • 15
  • 23
2

I just want to save the credit card info to their stripe account so I can use it later.

Then that's exactly what you can do!

If you're already passing the token back into your server-side code, you just need to update that server-side code to retrieve the customer and create the card on that customer record using the token.

Not knowing what language you're using I can't provide relevant sample code, but the Stripe API reference has functional examples for Ruby, Python, PHP, Java, and Node.js.

Note that if the customer has any outstanding invoices, this card will be used the next time they attempt to settle—so while simply adding the card won't create a charge by itself, it's possible the card may still be billed.

colinm
  • 4,062
  • 20
  • 17
0

A key point that the prior answers seems to dance around but do not explicitly state is that you can't simply save the credit card (token) in Stripe. Stripe's API's don't save credit cards per se, however, they can save a customer and attached to the customer you can save one more credit cards (or payment sources). So a credit card (or payment source) is not a stand alone entity in the Stripe storage system, it's a child entity of a Customer.

RonC
  • 23,270
  • 13
  • 67
  • 104
  • I am having some issues here. I need to use the generated token to create a new customer, but then I cannot reuse the token to create a source to attach to the customer. So do I generate two tokens, one for customer creation and one for source to add to customer? The API docs say I can create a customer with a source, but when I try I find that "cannot create customer with source type source" – 1pocketaces1 Jul 13 '19 at 01:19
  • Unfortunately I’m on an international trip and won’t be back where I can look at my code for 4 weeks. My recollection is that you can create the customer straight away and then use a generated token to attach the card to the customer. But that is just my recollection from many months ago when I was working on that part of our system. – RonC Jul 13 '19 at 11:20