Home > OS >  How to charge with Stripe by customer id
How to charge with Stripe by customer id

Time:11-20

I have flow when the user can be charged by card detail, and I`m using await stripe.createPaymentMethod for that. When payment is successful I can later get details of that user purchases with card and payment info, as well as customer id "cus__idvalue".

 const { error, paymentMethod } = await stripe.createPaymentMethod({
      type: "card",
      card: elements.getElement(CardNumberElement),
      card: elements.getElement(CardExpiryElement),
      card: elements.getElement(CardCvcElement),
    });

My question is how can I charge the same user by previously used card, without giving him the option to type his card data again. I have read Stripe docs on that case, but haven`t got a clear understanding and what methods could be helpful. Appreciate any help in advance!

CodePudding user response:

The functionality of what you can do is limited by what stripe API provides, i doubt if the API allows one to pre enter card details based on previous transactions, except if you store card details on your site DB, which is a security flaw.

Some card processors can allow that though, so confirm from stripe if that is allowed, in which case you can then allow users store card details in their user account on your website, so it is only when a user logs in that the pre-stored card details can be used for transactions.

CodePudding user response:

PaymentMethods are only reusable when attached to a customer. To retrieve all PaymentMethods for a given customer you can use the listPaymentMethods endpoint with a customer id parameter, and then use the PaymentMethod ID to create a new PaymentIntent like this:

const paymentIntent = await stripe.paymentIntents.create({
  amount: 2000,
  currency: "usd",
  payment_method: “REPLACE_WITH_PAYMENT_METHOD_ID”,
  confirm: true,
});
  • Related