可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
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!
回答3:
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.
回答4:
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
customer = Stripe::Customer.retrieve(customer_stripe_id) #Retrieve the card fingerprint using the stripe_card_token newcard = Stripe::Token.retrieve(source_token) card_fingerprint = newcard.try(:card).try(:fingerprint) card_exp_month = newcard.try(:card).try(:exp_month) card_exp_year = newcard.try(:card).try(:exp_year) # check whether a card with that fingerprint already exists default_card = customer.sources.all(:object => "card").data.select{|card| ((card.fingerprint==card_fingerprint)and(card.exp_month==card_exp_month)and(card.exp_year==card_exp_year))}.last default_card = customer.sources.create(source: source_token) if !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