Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Customer's default and active card from Stripe

I am trying to retrieve the default and active card of a Customer. (Also keep in mind that with the coding I have, the customer can always have one card which means if there is a way around it it can help).

Some months ago I used this code segment which was working fine. It seems Stripe made some updates and I can't get it to work now.

current_user.stripe_card_id = customer.active_card.id

The error I get is

undefined method `active_card' for #Stripe::Customer

If you need any more information please let me know.

edit: customer.default_card.id does not work either.

like image 792
Stefanos.Ioannou Avatar asked Dec 09 '22 02:12

Stefanos.Ioannou


2 Answers

I used customer.methods to check the methods and found this (default_source):

current_user.stripe_card_id = customer.default_source

Works fine now. Thank you

like image 72
Stefanos.Ioannou Avatar answered Dec 10 '22 14:12

Stefanos.Ioannou


default card id will available in customer object's "default_source" key

{
  "id": "cus_GACkqbqFD8RQw4",
  "object": "customer",
  "default_source": <DEFAULT CARD ID HERE> 
   ...
}

read more here : https://stripe.com/docs/api/customers

[EDIT] Additionally, It's worth noting that when you request a list of all the cards belonging to a particular customer, the default card is always at the top of the result. So you could also get the default card by requesting the customers cards and adding a limit of 1.

Information on how to achieve this: https://stripe.com/docs/api/cards/list

like image 43
Saurabh Mistry Avatar answered Dec 10 '22 15:12

Saurabh Mistry