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

6tqwzwtp  于 2023-02-26  发布在  Ruby
关注(0)|答案(4)|浏览(171)

我已将stripe客户ID保存在数据库中以备以后付款。客户将拥有多张卡,我希望检查/验证新客户卡与其现有卡。
假设相同的卡详细信息可以作为多张卡存储多次。
我想使用Stripe令牌检查新输入的卡是否已经存在。如果已经存在,它将使用它,如果没有,它将创建一个新卡。

hk8txs48

hk8txs481#

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

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

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

3htmauhk

3htmauhk2#

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

希望这对某人有帮助!

zazmityj

zazmityj3#

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

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
trnvg8h3

trnvg8h34#

听起来像是在本地缓存卡数据以便能够向客户显示。
如果这是正确的,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会为您处理重复数据,您无需执行任何操作。

相关问题