nevoni3008
nevoni3008

Reputation: 447

Stripe create payment and subscribe to one of the products

So I'm working with stripe and I'm kinda new, here is my React code to create a payment, but I want to create a subscription to a specific product that stripe with auto charge that price every month on React.

Here is the code:

const payload = await stripe.createPaymentMethod({
      type: "card",
      metadata: {
        "working": true
      },
      card: elements.getElement(CardNumberElement),

    });

But I don't know how to specify that this a subscription rather than a payment one time?

Upvotes: 0

Views: 565

Answers (1)

soma
soma

Reputation: 2219

The code you shared is to create a PaymentMethod, no to make an actual payment. Instead I recommend doing this:

First, create a Subscription on the backend as mentioned here. In Node.js it would look something like this:

const subscription = await stripe.subscriptions.create({
  customer: 'cus_xxx',
  items: [{ price: 'price_xxx' }],
  payment_behavior: 'default_incomplete',
  payment_settings: { save_default_payment_method: 'on_subscription' },
  expand: ['latest_invoice.payment_intent'],
});

Second, retrieve the subscription's client secret that's in subscription.latest_invoice.payment_intent.client_secret, and send it to your React frontend.

Third, follow this official guide on how to use the client_secret to accept payments in React.

Upvotes: 0

Related Questions