Raj*_*ula 15 ruby-on-rails stripe-payments
我已将条带客户ID保存在我的数据库中,以便以后付款.客户将拥有多张卡,我希望使用现有卡检查/验证新客户卡.
假设相同的卡详细信息可以作为多个卡存储多次.
我想使用Stripe令牌检查新输入的卡是否已存在.如果它已经存在,它将使用它,否则它将创建一张新卡.
Sah*_*har 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
Run Code Online (Sandbox Code Playgroud)
存储有条纹的卡的指纹始终是唯一的
如果您想减少对条带的调用,建议您在本地存储所有卡的指纹并使用它们来检查唯一性.在本地存储卡的指纹是安全的,并且它唯一地识别卡.
Edu*_*ass 14
对于2016年阅读此内容的人:Sahil Dhankhar的答案仍然正确,尽管Stripe显然已经改变了他们的API语法:
customer.cards
Run Code Online (Sandbox Code Playgroud)
就是现在:
customer.sources
Run Code Online (Sandbox Code Playgroud)
所以,正确的语法现在是:
#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
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人!
听起来您正在本地缓存卡数据,以便能够将其显示给客户.
如果这是正确的,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
Run Code Online (Sandbox Code Playgroud)
如果您没有在本地缓存卡数据,Stripe会为您处理重复数据,您无需执行任何操作.
卡指纹仅在匹配卡号时有用。您还必须检查以确保到期日期也没有更改。如果客户具有相同的卡号,但是更新了到期日期
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9932 次 |
最近记录: |