Stripe“缺少必需参数:line_items[0][货币]。” 节点js

Mak*_*pov 5 node.js stripe-payments

我正在 Node js 后端创建订阅。它一直工作得很好,但今天我收到了这个错误。我没有对代码进行任何更改 - 它只是开始返回此错误。后端代码:

 app.post('/api/subscription', async (req, res) => {
  const { priceId } = req.body;
  
  try {
    const session = await stripe.checkout.sessions.create({
      mode: "subscription",
      payment_method_types: ["card"],
      line_items: [
        {
          price: priceId,
          // For metered billing, do not pass quantity
          quantity: 1,
        },
      ],
      // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
      // the actual Session ID is returned in the query parameter when your customer
      // is redirected to the success page.

      success_url: 'https://someurl',
      cancel_url: 'https://someurl',
    });

    res.send({
      sessionId: session.id,
    });
  } catch (e) {
    console.log(e)
    res.status(400);
    return res.send({
      error: {
        message: e.message,
      }
    });
  }
})
Run Code Online (Sandbox Code Playgroud)

从我发送的客户端

fetch("http://localhost:8000/api/subscription", {
      method: "POST",
      body: JSON.stringify({ priceId }),
    });
Run Code Online (Sandbox Code Playgroud)

我从这里的官方条纹示例中获取了这段代码https://stripe.com/docs/billing/subscriptions/checkout 正如我所说,它工作正常,现在我已经在两个不同的条纹帐户上测试了它并得到了相同的结果错误。看起来条纹上有一些变化,但在他们的文档中没有变化

Jus*_*ael 14

如果priceIdundefined/null则不会在请求中发送。如果价格不存在,API 会假设您尝试在不使用价格的情​​况下指定有关订单项的信息,并且它执行的第一项检查是检查有效价格currency(您没有价格),这会导致错误 Missing required param: line_items[0][currency].

要解决此问题,您需要弄清楚为什么priceId没有按预期填充,并且您可能还需要添加检查以确保priceId在继续结帐会话创建步骤之前有效。