Ruby on rails 条带检索或创建卡片

Ruby on rails 条带检索或创建卡片,ruby-on-rails,stripe-payments,Ruby On Rails,Stripe Payments,我想发送一个客户和卡令牌,以条带化和检索该卡,或者在客户上创建一张新卡(如果该卡尚不存在) 现在,我必须取回所有卡,检查我的卡令牌是否与任何卡匹配,然后用该卡发送费用 是否有“customer.retrieve_或_create(card_token)”?还是更好的解决方案?不幸的是,今天在处理Stripe时,我注意到它确实允许存储重复的卡。为了避免这种情况,我执行了以下步骤: #fetch the customer customer = Stripe::Customer.retrieve(s

我想发送一个客户和卡令牌,以条带化和检索该卡,或者在客户上创建一张新卡(如果该卡尚不存在)

现在,我必须取回所有卡,检查我的卡令牌是否与任何卡匹配,然后用该卡发送费用


是否有“customer.retrieve_或_create(card_token)”?还是更好的解决方案?

不幸的是,今天在处理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 
使用条纹存储的卡的指纹始终是唯一的

如果要减少对stripe的调用,建议将所有卡的指纹存储在本地,并使用它们检查唯一性。在本地存储卡的指纹是安全的,并且可以唯一地识别卡

有关此操作的更多详细信息,请访问:

你的问题不清楚,请尽量让它更容易理解。这就像在rails中查找或创建一样-我想通过从stripe.js传递客户令牌和卡令牌来检索客户的卡。我正在发送一个带有旧客户id的新卡令牌或旧卡令牌,如果这是一张新卡,我希望stripe在该客户上创建一张卡并对该卡收费如果是一张旧卡。相反,如果卡是新的,我会得到这个:Stripe::InvalidRequestError:Customer cuszosvxrrwbggxb没有ID为tok的卡_31CJdo1nFfTc7T@ajbraus你找到解决办法了吗。我在找什么东西similar@ajbraus我能够做到这一点,并补充说,作为一个答案,lmk,如果这是有帮助的