提问者:小点点

在添加新卡之前,我可以检查 Stripe 客户是否已经拥有特定卡吗?


我已经在数据库中保存了条纹客户ID,以便以后付款。一个客户将拥有多张卡,我想用他们现有的卡检查/验证新的客户卡。

假设相同的卡片细节可以像多张卡片一样多次存储。

我想使用条纹标记检查新输入的卡是否已经存在。如果它已经存在,它将使用它,如果没有,它将创建一张新卡。


共3个答案

匿名用户

不幸的是,在今天处理Stripe时,我注意到它确实允许存储重复的卡。为了避免这种情况,我采取了以下步骤:

#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 

用条纹存储的卡的指纹总是唯一的

如果您想减少对条纹的调用,建议您将所有卡的指纹存储在本地,并使用它们检查唯一性。在本地存储卡的指纹是安全的,它可以唯一标识卡。

匿名用户

对于在2016年阅读这篇文章的人来说:Sahil Dhankhar的答案仍然是正确的,尽管Stripe显然已经改变了他们的API语法:

customer.cards

现在是:

customer.sources

因此,正确的语法现在是:

#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 

希望这能帮助某人!

匿名用户

卡指纹仅用于匹配卡号。您还必须检查以确保到期日期也没有更改。如果客户具有相同的卡号,但到期日期已更新

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