【问题标题】:Stripe retrieve_or_create card条纹检索或创建卡
【发布时间】:2013-12-08 17:47:09
【问题描述】:

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

现在我必须检索所有卡,检查我拥有的卡令牌是否匹配,然后使用该卡发送费用。

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

【问题讨论】:

  • 您的问题不清楚,请尝试让其更容易理解。
  • 这就像在 rails find_or_create - 我想通过传递客户令牌和来自 stripe.js 的卡令牌来检索或创建客户卡。
  • 我正在发送带有旧客户 ID 的新卡或旧卡令牌,如果是新卡,我希望 Stripe 在该客户上创建一张卡,如果是旧卡。相反,如果卡是新的,我会得到这个: Stripe::InvalidRequestError: Customer cus_2zosVXrRwbgGXb does not have card with ID tok_31CJdo1nFfTc7T
  • @ajbraus 你有没有解决这个问题。我正在寻找类似的东西
  • @ajbraus 我能够做到这一点并补充说作为答案,如果有帮助,请 lmk

标签: ruby-on-rails stripe-payments


【解决方案1】:

不幸的是,今天在使用 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的调用,建议你将所有卡的指纹存储在本地,并使用它们来检查唯一性。在本地存储卡的指纹是安全的,它可以唯一地标识一张卡。

有关此的更多详细信息: Can I check whether stripe a card is already existed before going to create new one?

【讨论】:

    猜你喜欢
    • 2021-02-10
    • 2018-03-19
    • 2013-03-06
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多