在添加新卡之前,我可以检查 Stripe 客户是否已经拥有特定卡吗? [英] Can I check whether a Stripe customer already has a specific card before adding a new one?

查看:55
本文介绍了在添加新卡之前,我可以检查 Stripe 客户是否已经拥有特定卡吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将条带客户 ID 保存在我的数据库中以供以后付款.一位客户将拥有多张卡,我想用他们现有的卡检查/验证新的客户卡.

I have saved stripe customer id's in my db for later payments. A customer will have multiple cards and I would like to check/validate new customer cards with their existing cards.

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

Suppose the same card details can be stored multiple times as multiple cards.

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

I want to check using the Stripe token whether a newly entered card already exists or not. It will use it if it's already there, if not it will create a new card.

推荐答案

不幸的是,今天在使用 Stripe 时,我注意到它确实允许存储重复的卡片.为了避免这种情况,我做了以下步骤:

Unfortunately while working on Stripe today I noticed that it do allows storing of duplicate cards. To avoid this, I did following steps:

#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,建议将所有卡的指纹保存在本地,用于唯一性检查.将卡片指纹存储在本地是安全的,它可以唯一标识一张卡片.

If you want to make fewer calls to stripe, it is recommended that you store the fingerprints of all the cards locally and use them for checking uniqueness. Storing fingerprints of cards locally is secure and it uniquely identifies a card.

这篇关于在添加新卡之前,我可以检查 Stripe 客户是否已经拥有特定卡吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆