Katherine Pacheco
Katherine Pacheco

Reputation: 369

NextJS, React , Stripe checkout recurring price error

I have tried implementing a Stripe checkout to my NextJS app. I copied the code almost exactly like the sample application but I get this error below, not sure what im doing wrong:

You specified payment mode but passed a recurring price. Either switch to subscription mode or use only one-time prices.

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)

export default async function handler(req, res) {
  if (req.method === 'POST') {
    try {
      // Create Checkout Sessions from body params.
      const session = await stripe.checkout.sessions.create({
        line_items: [
          {
            // TODO: replace this with the `price` of the product you want to sell
            price: 1,
            quantity: 1,
          },
        ],
        payment_method_types: ['card'],
        mode: 'payment',
        success_url: `${req.headers.origin}/?success=true&session_id={CHECKOUT_SESSION_ID}`,
        cancel_url: `${req.headers.origin}/?canceled=true`,
      })

      res.redirect(303, session.url)
    } catch (err) {
      res.status(err.statusCode || 500).json(err.message)
    }
  } else {
    res.setHeader('Allow', 'POST')
    res.status(405).end('Method Not Allowed')
  }
}

Upvotes: 1

Views: 1294

Answers (1)

LW001
LW001

Reputation: 2863

price: 1 is not right.

There are two ways to pass a price for a payment in Stripe Checkout:

line_items: [
    {
        price_data: {
            currency: 'usd',
            product_data: { // Same as with price_data, we're creating a Product inline here, alternatively pass the ID of an existing Product using line_items.price_data.product
                name: 'Shoes'
            },
            unit_amount: 1000 // 10 US$
        },
        quantity: 1,
    },
],
  • Using an existing Price object. You will pass: price: price_1JZF8B2eZvKYlo2CmPWKn7RI, the price_XXX part being the ID of a previously created Prices object.

You used to be able to set line_items.amount as well but that is deprecated now, so don't do it.

You can read more about creating a Checkout Session in the Documentation.

Upvotes: 2

Related Questions