Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe - Add new card to existing customer

I have a need to add a card to a pre-existing customer. Here's what I did:

1. obtain token from user submission

card_token = request.POST('stripeToken')

2. retrieve a customer

customer =  stripe.Customer.retrieve('cus_xxxxxxxxxx')

3. add card to this customer

customer.Cards.create(card=card_token)

It is # 3 that I'm having trouble because it looks like the customer doesn't have method Cards, but I've seen people done it elsewhere.

How should I achieve this?

like image 354
chenxi17 Avatar asked Mar 07 '15 07:03

chenxi17


People also ask

How does a customer update their card in Stripe?

Once the customer clicks on the link you send them, they will first see a button to 'Update Card Details'. Once they click on that they will be prompted to enter their new card details. This then becomes the new card from which they are billed.

Does Stripe automatically update expired cards?

Stripe works with card networks and automatically attempts to update saved card details whenever a customer receives a new card (for example, replacing an expired card or one that was reported lost or stolen).

Can I check whether a Stripe customer already has a specific card before adding a new one?

Each fingerprint is unique to a card, so before storing additional cards for a customer, you can simply search the user's cards by fingerprint. If you're not caching the card data locally, Stripe handles duplicates for you and you don't need to do anything.


2 Answers

If you are on the 2015-02-18 API version or later then the cards attribute has been changed to sources as you can see in the changelog

The documentation on the Create Card API shows the following code now:

customer = stripe.Customer.retrieve('cus_xxxxxxxxxx')
customer.sources.create(card=card_token)

You can find your API version in the API keys settings in the dashboard and you can also use the Stripe-Version header to force your API request to an older API version so that cards still work as explained in the Versioning documentation:

stripe.api_version = '2015-01-26'
like image 79
koopajah Avatar answered Oct 17 '22 22:10

koopajah


2019 Update: Things have changed a bit with Strong Customer Authentication (SCA) requirements in Europe; you'll now probably want to use the Setup Intents API to collect card details upfront for future payments.

This new API is both PCI and SCA compliant. You can learn more here
or check out this sample code on GitHub: https://github.com/stripe-samples/saving-card-without-payment.

You can also do this entirely with Checkout now as well!

like image 4
floatingLomas Avatar answered Oct 17 '22 23:10

floatingLomas