I have a need to add a card to a pre-existing customer. Here's what I did:
card_token = request.POST('stripeToken')
customer = stripe.Customer.retrieve('cus_xxxxxxxxxx')
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?
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.
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).
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.
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'
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With