Prevent Stripe API from saving duplicate cards on customer

Viewed 518

I allow users to add new cards during the checkout. What happens is that the paymentIntent uses the 'setup_future_usage' option, which automatically attaches the payment method to the customer.

await stripe.paymentIntents.create({
        customer: customer_id,
        setup_future_usage: 'off_session',
        amount: calculateOrderAmount(items),
        currency: items.currency.toLowerCase()
      })

The problem with this is that it means that the user can enter the same card multiple times, and stripe will attach them all as individual cards to the user. I tried to take off 'setup_future_usage', but it gave me an error when trying to confirm payments saying that the payment wasn't attached to the customer. Does anyone know of a way within the Javascript SDK of making it so Stripe won't save duplicate cards on a per customer basis?

1 Answers

I chose a non-sophisticated approach to solve this - make an API call to "detach" all payment methods except the new one that was just setup.

https://stripe.com/docs/api/payment_methods/detach

I don't know that you could effectively pre-match the card data prior to creating the card in Stripe with the Elements API. It may be possible but this basic approach worked for me.

Related