Can I check whether a Stripe customer already has a specific card before adding a new one?

后端 未结 5 1942
清歌不尽
清歌不尽 2020-12-13 20:34

I have saved stripe customer id\'s in my db for later payments. A customer will have multiple cards and I would like to check/validate new customer cards with their existing

5条回答
  •  情歌与酒
    2020-12-13 21:13

    It sounds like you're caching the card data locally to be able to display it to the customer.

    If that is correct, Stripe provides a fingerprint for each card/token which you can begin storing in the card records (if you're not already). Each fingerprint is unique to a card, so before storing additional cards for a customer, you can simply search the user's cards by fingerprint.

    As a simple example, assuming a 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
    

    If you're not caching the card data locally, Stripe handles duplicates for you and you don't need to do anything.

提交回复
热议问题