Stripe is a very popular payment solution that integrates really well into a Rails application. In this blog, we’ll guide you through how to check whether stripe's source is already exist or not.
Stripe allows storing of duplicate sources if you are using same card. In this case, if customer have provided same card multiple time. Stripe will create same card details storing multiple times as multiple cards.
For overcome from this situation and if you want make less calls to stripe, it is recommended that you store the fingerprints of all the souces locally and use them for checking uniqueness. Storing fingerprints of sources locally is secure and it uniquely identifies a source.
first we will fetch customer details from 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.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
Stripe allows storing of duplicate sources if you are using same card. In this case, if customer have provided same card multiple time. Stripe will create same card details storing multiple times as multiple cards.
For overcome from this situation and if you want make less calls to stripe, it is recommended that you store the fingerprints of all the souces locally and use them for checking uniqueness. Storing fingerprints of sources locally is secure and it uniquely identifies a source.
first we will fetch customer details from 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.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
No comments:
Post a Comment