1

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')
  }
}
1
  • What kind of price are you passing in when creating the Checkout Session? If it's a recurring price then you need to change your code to use mode: 'subscription' Commented Sep 14, 2021 at 18:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.