user2911232
user2911232

Reputation:

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.

Upvotes: 10

Views: 17147

Answers (5)

bkrnetic
bkrnetic

Reputation: 31

Relying on customers' default_source is safe no matter the changes. You can see here that subscriptions will still use customers' default_source if both invoice_settings.default_payment_method and subscription.default_payment_method are not set.

Upvotes: 0

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

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

Upvotes: 7

luigi7up
luigi7up

Reputation: 5962

PaymentMethods API - 2020 update

If you have switched from old Sources API to the new Payment Methods API then you should know that unlike old Sources there's no default Payment Method for a Customer.

Now, you can attach a Payment Method as default one to a subscription object:

Stripe::Subscription.update(
  'sub_8epEF0PuRhmltU',
  {
    default_payment_method: 'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
  }
)

or as a customer.invoice_settings.default_payment_method

Stripe::Customer.update(
  'cus_FOcc5sbh3ZQpAU',
  {
    invoice_settings: {default_payment_method: 'pm_1vvc9v2eZvKYlo2CJDeTrB4n'},
  }
)

Here is the whole Stripe documentation on that

Upvotes: 2

user2911232
user2911232

Reputation:

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

Upvotes: 15

Nermin
Nermin

Reputation: 6100

customer = Stripe::Customer.retrieve(customer_id_on_stripe)

first_3_cards = customer.sources.all(limit: 3, object: 'card')[:data]

Will return array of cards, if you want to fetch bank_accounts

first_3_bank_accounts = customer.sources.all(limit: 3, object: 'bank_account')[:data]

Upvotes: -2

Related Questions