Can I check whether a Stripe customer already has a specific card before adding a new one?

懵懂的女人 提交于 2019-12-17 23:08:01

问题


I have saved stripe customer id's in my db for later payments. A customer will have multiple cards and I would like to check/validate new customer cards with their existing cards.

Suppose the same card details can be stored multiple times as multiple cards.

I want to check using the Stripe token whether a newly entered card already exists or not. It will use it if it's already there, if not it will create a 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 wants this card to be used for further transactions
customer.default_card = default_card.id

# Save the customer
customer.save



回答5:


fingerprint is the way to check duplicate card. you can check fingerprint in card object or token object. please follow this : stripe api checking for existing card



来源:https://stackoverflow.com/questions/23698614/can-i-check-whether-a-stripe-customer-already-has-a-specific-card-before-adding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!