Reputation: 369
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
Reputation: 2863
price: 1
is not right.
There are two ways to pass a price for a payment in Stripe Checkout:
price_data
: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,
},
],
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