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

时间:2014-05-16 15:05:39

标签: ruby-on-rails stripe-payments

我已将条带客户ID保存在我的数据库中,以便以后付款。客户将拥有多张卡,我希望使用现有卡检查/验证新客户卡。

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

我想使用Stripe令牌检查新输入的卡是否已存在。如果它已经存在,它将使用它,否则它将创建一张新卡。

5 个答案:

答案 0 :(得分:24)

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

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

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

答案 1 :(得分:14)

对于2016年阅读此内容的人:Sahil Dhankhar的答案仍然正确,尽管Stripe have apparently changed their API syntax

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 

希望这有助于某人!

答案 2 :(得分:6)

听起来您正在本地缓存卡数据,以便能够将其显示给客户。

如果这是正确的,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会为您处理重复项,您不需要做任何事情。

答案 3 :(得分:5)

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

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

答案 4 :(得分:0)

fingerprint是检查重复卡的方法。您可以在卡对象或令牌对象中检查fingerprint。 请遵循此: stripe api checking for existing card