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



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

假设相同的卡片详细信息可以作为多张卡片存储多次。

我想使用Stripe令牌检查新输入的卡是否已经存在。如果它已经在那里,它会使用它,如果没有,它会创建一个新的卡片

不幸的是,当我今天在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 

条带存储卡的指纹始终是唯一的

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

对于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)&&(card.exp_month==card_exp_month)&&(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

听起来像是在本地缓存卡片数据以便能够向客户显示。

如果这是正确的,Stripe为每张卡/令牌提供一个指纹,你可以开始存储在卡记录中(如果你还没有)。每张卡的指纹都是唯一的,因此在为客户存储其他卡之前,您可以简单地通过指纹搜索用户的卡。

作为一个简单的例子,假设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

如果你没有在本地缓存卡片数据,Stripe会为你处理重复的数据,你不需要做任何事情。

最新更新