NextJS、React、Stripe 结帐重复出现价格错误

NextJS, React , Stripe checkout recurring price error

我已经尝试对我的 NextJS 应用实施 Stripe 结帐。我复制的代码几乎与示例应用程序完全一样,但我在下面收到此错误,不确定我做错了什么:

您指定了 payment 模式但传递了循环价格。要么切换到 subscription 模式,要么只使用一次性价格。

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')
  }
}

price: 1不对。

有两种方式可以在 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,
    },
],
  • 使用现有的 Price 对象。您将传递:price: price_1JZF8B2eZvKYlo2CmPWKn7RI,price_XXX 部分是先前创建的价格对象的 ID。

您过去也可以设置 line_items.amount,但现在已弃用,所以不要这样做。

您可以在 Documentation 中阅读有关创建结帐会话的更多信息。