JSON 中的意外标记用于条带支付意图

Unexpected token ' in JSON for stripe payment intents

您好,我正在使用 stripe 创建支付意向方法,语言是 node.js,但我遇到了这个错误

unmatched close brace/bracket in URL position 13: currency:usd}'

在我执行此命令时的代码中:

curl -X POST localhost:4242/create-payment-intent -H "Content-Type: application/json" -d '{"paymentMethodType":"card", "currency":"usd"}'

这是我的代码

app.post('/create-payment-intent', async (req, res) => {
const {paymentMethodType, currency} = req.body;
  try {
    const paymentIntent = await stripe.paymentIntents.create({
      amount: 1999,
      currency: currency,
      payment_method_types: [paymentMethodType],
    });
    res.json({ clientSecret: paymentIntent.client_secret });
  } catch (e) {
    res.status(400).json({ error: { message: e.message } });
  }
}); 

请任何人提供帮助。

您忘记在命令末尾添加 '

curl -X POST localhost:4242/create-payment-intent -H "Content-Type: application/json" -d '{"paymentMethodType":"card", "currency":"usd"}'

如果您的请求具有 JSON 负载,您需要确保您的服务器正在解析 JSON。我测试了下面的代码,它对我有效。

CURL 命令(无变化)

curl -X POST localhost:4242/create-payment-intent -H "Content-Type: application/json" -d '{"paymentMethodType":"card", "currency":"usd"}'

Node.js 服务器(添加了一个中间件函数来解析 JSON 有效负载)

const json = express.json({ type: "application/json" });

app.post("/create-payment-intent", json, async (req, res) => {
  const { paymentMethodType, currency } = req.body;
  try {
    const paymentIntent = await stripe.paymentIntents.create({
      amount: 1999,
      currency: currency,
      payment_method_types: [paymentMethodType],
    });
    res.json({ clientSecret: paymentIntent.client_secret });
  } catch (e) {
    res.status(400).json({ error: { message: e.message } });
  }
});

如果您正在使用 Window

curl -X POST localhost:4242/create-payment-intent -H "Content-Type:application/json" -d "{\"paymentMethodType\":\"card\", \"currency\":\"usd\"}"