在创建新卡之前,我是否可以检查卡片是否已经存在?

因为我已经在我的数据库中保存了条带客户ID以便以后付款。 在这里,客户将拥有多张卡片,我希望使用该特定客户的现有卡片检查/validation客户卡。

假设相同的卡详细信息存储多次作为多张卡。

在这个过程中,我想用令牌检查这张卡是否已经存在。 将使用它如果已经存在,如果没有将创建新卡。

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

存储有条纹的卡的指纹始终是唯一的

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

对于2016年阅读此内容的人 :Sahil Dhankhar的答案仍然正确,尽管Stripe显然已经改变了他们的API语法 :

 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 

希望这有助于某人!

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

如果这是正确的,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会为您处理重复项,您无需执行任何操作。

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

 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