Home > Net >  How to setup payment method on creating checkout session?
How to setup payment method on creating checkout session?

Time:12-03

I'm using stripe as an payment integration in my app. I'm creating session checkout for the customer with using mode=payment because i want to get payment from the order, but how to save payment method of the customer with this checkout session?

Here is my code:

await stripe.checkout.sessions.create({
      line_items: [
        {
          price_data: {
            unit_amount: 433,
            currency: "usd",
            product_data: {
              name: ""
            }
          },
          quantity: 1,
        }
      ],
      cancel_url: "",
      success_url: "",
      customer: customerID,
      mode: STRIPE_SESSION_MODE.PAYMENT,
      payment_method_types: ["card"],
    });

thanks to this in response i've got an url to the stripe payment form. Is there an option to save information about payment method of the customer for the future using? I know that I can use mode=setup but i want also to get payment from the price_data.

Thanks for any help!

CodePudding user response:

You can opt to save the created payment method to a Customer object for future on-session (or off-session) payments using the setup_future_usage parameter:

await stripe.checkout.sessions.create({
  line_items: [
    {
      price_data: {
        unit_amount: 433,
        currency: "usd",
        product_data: {
          name: ""
        }
      },
      quantity: 1,
    }
  ],
  payment_intent_data: {
    setup_future_usage: "off_session"
  },
  cancel_url: "",
  success_url: "",
  customer: customerID,
  mode: STRIPE_SESSION_MODE.PAYMENT,
  payment_method_types: ["card"],
});
  • Related