As I have saved stripe customer id's in my db for later payments. here customer will have multiple cards where I would like to check/validate customer card with existed cards from that particular customer.

Suppose same card details storing multiple times as multiple cards.

In this process, I want check with token whether this card already existed or not. will use it If already there, If not will create new card.

3 Answers 11

It sounds like you're caching the card data locally to be able to display it to the customer.

If that is correct, Stripe provides a fingerprint for each card/token which you can begin storing in the card records (if you're not already). 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.

As a simple example, assuming a User has_many :cards:

token = Stripe::Token.retrieve("tok_a1b2c3d4")

unless current_user.cards.find_by(fingerprint: token.card.fingerprint)
  current_user.cards.create( ... # data from token )
end

If you're not caching the card data locally, Stripe handles duplicates for you and you don't need to do anything.

3 upvote
  flag
Here, I just creating the card with new token for the particular customer. Next will get fingerprint for that card and will check with this fingerprint to rest of cards from that customer. If any found already, will delete this card. If not found any duplicate then will keep this card as usual. So that here, we won't get multiple cards with same card details like duplicate cards for particular customer. – Raju akula
2 upvote
  flag
As of now, stripe creating duplicate cards as If i enter same card details multiple times it will create multiple cards with that same details for that particular customer. So I done as above. Please let me know your comments If I am wrong anything. – Raju akula
1 upvote
  flag
@colinm unfortunately stripe currently not handling duplicates – Sahil Dhankhar
upvote
  flag
@Rajuakula i added an answer with the steps i used to validate if the card already existed. lmk if thats helpful. i agree that stripe is not handling uniqueness of cards at its end currently – Sahil Dhankhar
2 upvote
  flag
This answer is only partially complete. The card fingerprint is only useful for matching the card number. You must also check to make sure the expiration date hasn't changed as well. If a customer has the same card number, but an updated expiration date, this answer will not create a new card. – Sean S

Unfortunately while working on Stripe today i noticed that it do allows storing of duplicate cards. To avoid this, i did following steps:

#fetch the customer 
customer = Stripe::Customer.retrieve(stripe_customer_token)
#Retrieve the card fingerprint using the stripe_card_token  
card_fingerprint = Stripe::Token.retrieve(stripe_card_token).try(:card).try(:fingerprint) 
# check whether a card with that fingerprint already exists
default_card = customer.cards.all.data.select{|card| card.fingerprint ==  card_fingerprint}.last if card_fingerprint 
#create new card if do not already exists
default_card = customer.cards.create({:card => stripe_card_token}) unless default_card 
#set the default card of the customer to be this card, as this is the last card provided by User and probably he want this card to be used for further transactions
customer.default_card = default_card.id 
# save the customer
customer.save 

fingerprint of a card stored with stripe is always unique

If you want to make less calls to stripe, it is recommended that you store the fingerprints of all the cards locally and use them for checking uniqueness. Storing fingerprints of cards locally is secure and it uniquely identifies a card.

upvote
  flag
Are you storing card_id's in your db? – Batman
4 upvote
  flag
This answer is only partially complete. The card fingerprint is only useful for matching the card number. You must also check to make sure the expiration date hasn't changed as well. If a customer has the same card number, but an updated expiration date, this answer will not create a new card. – Sean S

For people reading this in 2016: Sahil Dhankhar answer is still correct, although Stripe have apparently changed their API syntax:

customer.cards

is now:

customer.sources

So, correct syntax would now be:

#fetch the customer 
customer = Stripe::Customer.retrieve(stripe_customer_token)
#Retrieve the card fingerprint using the stripe_card_token  
card_fingerprint = Stripe::Token.retrieve(stripe_card_token).try(:card).try(:fingerprint) 
# check whether a card with that fingerprint already exists
default_card = customer.sources.all.data.select{|card| card.fingerprint ==  card_fingerprint}.last if card_fingerprint 
#create new card if do not already exists
default_card = customer.sources.create({:card => stripe_card_token}) unless default_card 
#set the default card of the customer to be this card, as this is the last card provided by User and probably he want this card to be used for further transactions
customer.default_card = default_card.id 
# save the customer
customer.save 

Hope this helps someone!

Not the answer you're looking for? Browse other questions tagged or ask your own question.