Stripe - Add new card to existing customer

chenxi17 picture chenxi17 · Mar 7, 2015 · Viewed 25k times · Source

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?

Answer

koopajah picture koopajah · Mar 7, 2015

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'