2013-11-23 28 views
3

我想發送客戶和卡令牌來分條和檢索卡,或者在客戶尚未存在時創建新卡。Stripe retrieve_or_create卡

現在,我必須檢索所有卡片,檢查卡片令牌是否與任何卡片匹配,然後使用該卡片發送費用。

有沒有「customer.retrieve_or_create(card_token)」?或者更好的解決方案?

+0

你的問題是不明確的,請嘗試使它更容易理解。 –

+0

這就像在rails find_or_create - 我想通過傳遞客戶令牌和stripe.js中的卡片令牌來檢索或創建客戶卡。 – ajbraus

+0

我正在發送帶有舊客戶ID的新卡或舊卡令牌,並且我希望Stripe爲該客戶創建一張卡(如果它是新卡),並在該卡是舊卡時對該卡進行收費。相反,如果卡是新的,我會得到: Stripe :: InvalidRequestError:客戶cus_2zosVXrRwbgGXb沒有ID卡tok_31CJdo1nFfTc7T – ajbraus

回答

3

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

存儲與磁條卡的指紋始終是唯一的

如果你想以條紋不太來電,建議您存儲的指紋的所有卡片在本地,並用它們來檢查唯一性。在本地存儲卡的指紋是安全的,並且它唯一地識別卡。

更多關於這個細節: Can I check whether stripe a card is already existed before going to create new one?