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

Ruby on rails 在添加新卡之前,我是否可以检查Stripe客户是否已经拥有特定卡?,ruby-on-rails,stripe-payments,Ruby On Rails,Stripe Payments,我已将客户id保存在数据库中,以便以后付款。一个客户将有多张卡,我想用他们现有的卡检查/验证新客户卡 假设同一张卡的详细信息可以作为多张卡存储多次 我想使用条带令牌检查新输入的卡是否已经存在。如果它已经存在,它将使用它,如果没有,它将创建一张新卡。听起来您正在本地缓存卡数据,以便能够将其显示给客户 如果这是正确的,Stripe会为每个卡/令牌提供一个指纹,您可以开始将其存储在卡记录中(如果尚未存储)。每个指纹都是卡的唯一特征,因此在为客户存储其他卡之前,您只需按指纹搜索用户的卡即可 举个简单的例

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

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


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

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

如果这是正确的,Stripe会为每个卡/令牌提供一个指纹,您可以开始将其存储在卡记录中(如果尚未存储)。每个指纹都是卡的唯一特征,因此在为客户存储其他卡之前,您只需按指纹搜索用户的卡即可

举个简单的例子,假设一个
用户有很多:卡片:

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将为您处理重复的卡,您无需执行任何操作。

不幸的是,今天在使用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的答案仍然正确,尽管:

现在是:

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

指纹
是检查重复卡的方法。您可以在卡对象或令牌对象中检查指纹。 请按此办理:

在这里,我只是为特定客户创建带有新令牌的卡。接下来将获取该卡的指纹,并使用该指纹检查该客户的其余卡。如果已经找到任何,将删除此卡。如果未发现任何副本,则将一如既往地保留此卡。因此,在这里,我们不会像为特定客户创建重复卡那样获得具有相同卡详细信息的多张卡。现在,创建重复卡就像我多次输入相同的卡详细信息一样,它将为该特定客户创建具有相同详细信息的多张卡。所以我做了如上所述。如果我有任何错误,请告诉我您的意见。@colim不幸的是,stripe目前不处理重复项stripe仍然不处理重复项您是否在数据库中存储卡id?此答案仅部分完成。卡指纹仅用于匹配卡号。您还必须检查以确保到期日期也没有更改。如果客户有相同的卡号,但有更新的到期日期,此答案将不会创建新卡。这是正确的做法(在2018年),所有其他答案都会遗漏一两件事。但是,变量名
default\u card
是错误的/令人困惑的,因为该代码没有检索默认卡,而是返回一个布尔值,显示该卡是否已经存在。
Stripe::Token.retrieve(Stripe\u card\u Token)。try(:card)。try(:fingerprint)
!谢谢
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