web-dev-qa-db-ja.com

新しいカードを追加する前に、Stripeの顧客がすでに特定のカードを持っているかどうかを確認できますか?

後で支払うために、ストライプの顧客IDをデータベースに保存しました。顧客は複数のカードを持っているので、新しい顧客カードを既存のカードで確認/検証したいと思います。

同じカードの詳細を複数のカードとして複数回保存できるとします。

ストライプトークンを使用して、新しく入力したカードがすでに存在するかどうかを確認したいと思います。すでに存在する場合はそれを使用し、存在しない場合は新しいカードを作成します。

15
Raju akula

残念ながら、今日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 

ストライプで保存されたカードの指紋は常に一意です

ストライプへの呼び出しを減らしたい場合は、すべてのカードのフィンガープリントをローカルに保存し、それらを使用して一意性を確認することをお勧めします。カードの指紋をローカルに保存することは安全であり、カードを一意に識別します。

24
Sahil Dhankhar

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 

これが誰かを助けることを願っています!

14
Edu Wass

カードデータをローカルにキャッシュして、顧客に表示できるようにしているようです。

それが正しければ、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は重複を処理するため、何もする必要はありません。

6
colinm

カード指紋カード番号と一致するの場合にのみ役立ちます。また、有効期限も変更されていないことを確認してくださいを確認する必要があります。顧客のカード番号が同じで、有効期限が更新されている場合

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
5

fingerprintは、重複するカードをチェックする方法です。カードオブジェクトまたはトークンオブジェクトでfingerprintを確認できます。これに従ってください: 既存のカードのストライプAPIチェック

0
Manoj Kumar