Stripe - Webhook 负载必须以字符串或缓冲区的形式提供

Ada*_*ner 13 javascript node.js express stripe-payments

我正在尝试用条纹编写订阅系统。我的以下代码大部分是从 stripe 文档复制的,但它总是向我显示此错误:

\n
Webhook signature verification failed. Webhook payload must b\ne provided as a string or a Buffer (https://nodejs.org/api/buffer.html) instance representing the _raw_ request body.Payload was provided as a parsed JavaScript object instead.\nSignature verification is impossible without access to the original signed material.\nLearn more about webhook signing and explore webhook integration \nexamples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing\n
Run Code Online (Sandbox Code Playgroud)\n

这是我用 javascript 编写的代码:

\n
app.post(\n  '/webhook',\n  express.raw({ type: 'application/json' }),\n  (request, response) => {\n    let event = request.body;\n\n    const endpointSecret = 'whsec_.....';\n\n    if (endpointSecret) {\n\n      const signature = request.headers['stripe-signature'];\n      try {\n        event = stripe.webhooks.constructEvent(\n          request.body,\n          signature,\n          endpointSecret\n        );\n      } catch (err) {\n        console.log(`\xe2\x9a\xa0\xef\xb8\x8f  Webhook signature verification failed.`, err.message);\n        return response.sendStatus(400);\n      }\n    }\n    let subscription;\n    let status;\n\n    switch (event.type) {\n      case 'customer.subscription.created':\n        subscription = event.data.object;\n        status = subscription.status;\n        console.log(`Subscription status is ${status}.`);\n\n        break;\n      default:\n        console.log(`Unhandled event type ${event.type}.`);\n    }\n    response.send();\n  }\n);\n
Run Code Online (Sandbox Code Playgroud)\n

有谁知道我做错了什么?我已经在互联网上搜索了一个小时,但找不到任何东西。提前致谢!

\n

Zyl*_*com 17

这是因为app.use(express.json());代码。尝试之前移动 webhook 路由app.use(express.json());

\n
// webhook route\napp.post(\n  \'/webhook\',\n  express.raw({ type: \'application/json\' }),\n  (request, response) => {\n    let event = request.body;\n\n    const endpointSecret = \'whsec_.....\';\n\n    if (endpointSecret) {\n\n      const signature = request.headers[\'stripe-signature\'];\n      try {\n        event = stripe.webhooks.constructEvent(\n          request.body,\n          signature,\n          endpointSecret\n        );\n      } catch (err) {\n        console.log(`\xe2\x9a\xa0\xef\xb8\x8f  Webhook signature verification failed.`, err.message);\n        return response.sendStatus(400);\n      }\n    }\n    let subscription;\n    let status;\n\n    switch (event.type) {\n      case \'customer.subscription.created\':\n        subscription = event.data.object;\n        status = subscription.status;\n        console.log(`Subscription status is ${status}.`);\n\n        break;\n      default:\n        console.log(`Unhandled event type ${event.type}.`);\n    }\n    response.send();\n  }\n);\n\napp.use(express.json());\n
Run Code Online (Sandbox Code Playgroud)\n


Jas*_*son 9

引用 Stripe 快速启动 server.js 代码(https://stripe.com/docs/billing/quickstart#provision-access-webhooks),添加以下简单的行,放在之前app.use(express.json());为我解决了问题。

app.use('/webhook', express.raw({ type: 'application/json' }));
Run Code Online (Sandbox Code Playgroud)

这告诉 Express 专门针对“webhook”端点做一些不同的事情。给定的“express.raw...”参数是 Stripe 已经在其路由方法中调用的参数/webhook

您不需要重新安排路线/端点代码或任何内容的顺序。