首页 > 解决方案 > Stripe Webhook - 无法读取未定义的属性“类型”

问题描述

我刚刚在 node.js 中集成了条带 webhook。

我能够实现简单的 webhook 端点,它工作得非常好。

但是,如果我尝试“检查 webhook 签名”,则会收到错误消息:“无法读取未定义的属性‘类型’”

我的代码看起来就像条带提供的一样:

app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
  event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
  // event = request.body;
} catch (err) {
  response.status(400).send(`Webhook Error: ${err.message}`);
}

// Handle the event
switch (event.type) {
 case 'payment_intent.succeeded':
  const paymentIntent = event.data.object;
  console.log('PaymentIntent was successful!');
  break;
 default:
  console.log(`Unhandled event type ${event.type}`);
  // Unexpected event type
  return response.status(400).end();
}

  // Return a 200 response to acknowledge receipt of the event
  response.json({ received: true });
});

我在“事件”中没有得到任何东西

请帮我!!!

标签: stripe-payments

解决方案


尝试使用

bodyParser.raw({type: '*/*'})

https://github.com/stripe/stripe-node/issues/331

我的问题是我正在使用 NextJS 和

我正在使用 Stripe 为 Next.js 应用程序的支付网关供电,并开始验证 webhook 签名密钥。起初我遇到了一些问题,因为 Stripe 需要原始请求正文,而 Next.js 在将请求正文传递给处理程序之前对其进行解析。

https://maxkarlsson.dev/blog/2020/12/verify-stripe-webhook-signature-in-next-js-api-routes/


推荐阅读