#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
#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
token = Stripe::Token.retrieve("tok_a1b2c3d4")
unless current_user.cards.find_by(fingerprint: token.card.fingerprint)
current_user.cards.create( ... # data from token )
end
4条答案
按热度按时间hk8txs481#
不幸的是,当我今天在Stripe上工作时,我注意到它确实允许存储重复的卡。为了避免这种情况,我做了以下步骤:
与条带存储在一起的卡的指纹始终是唯一的
如果要减少对条带化的调用,建议您在本地存储所有卡的指纹,并使用它们检查唯一性。在本地存储卡的指纹是安全的,并且它唯一地标识卡。
3htmauhk2#
现在是:
因此,正确的语法现在应该是:
希望这对某人有帮助!
zazmityj3#
卡指纹仅用于匹配卡号。您还必须检查以确保到期日期也未更改。如果客户的卡号相同,但到期日期已更新
trnvg8h34#
听起来像是在本地缓存卡数据以便能够向客户显示。
如果这是正确的,Stripe为每张卡/令牌提供指纹,您可以开始将其存储在卡记录中(如果您还没有)。每张指纹对于一张卡是唯一的,因此在为客户存储其他卡之前,您可以简单地按指纹搜索用户的卡。
举个简单的例子,假设
User has_many :cards
:如果您没有在本地缓存卡数据,Stripe会为您处理重复数据,您无需执行任何操作。