gpt4 book ai didi

ruby-on-rails - Stripe retrieve_or_create 卡

转载 作者:行者123 更新时间:2023-12-04 02:39:41 26 4
gpt4 key购买 nike

我想发送一个客户和卡 token 来 strip 化和检索卡,或者在客户上创建一张新卡(如果它尚不存在)。

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

是否有“customer.retrieve_or_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 存储的卡的指纹始终是唯一的

如果想减少对stripe的调用,建议将所有卡片的指纹保存在本地,用于校验唯一性。在本地存储卡的指纹是安全的,它可以唯一标识一张卡。

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

关于ruby-on-rails - Stripe retrieve_or_create 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20168710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com