【发布时间】:2021-02-19 07:40:19
【问题描述】:
条带版本:“8.107.0”
每当我在 GCP 上运行我的 webhook 时,我都会收到 Stripe webhook 验证错误。我尝试在签名中使用原始正文,如下面的代码 sn-p 所述,以及其他 StackOverflow 答案提到的传递 req.rawBody 的其他方法。
奇怪的是,这个错误似乎是在我部署到 GCP 时抛出的,而不是当我在本地运行时抛出的。我尝试手动创建签名 (https://stripe.com/docs/webhooks/signatures#verify-manually),结果相同:本地签名匹配,在 GCP 上不匹配。
我们的服务器托管在 GCP GKE 上,我们通过 Nginx 反向代理向我们的服务器提供请求。其他堆栈溢出解决方案提到了 Google Cloud Functions 和 Lambda。据我所知,我们不解析 GCP 上的请求
我确实使用 bodyParser.json(),但这是在此端点之后设置的。这些是我尝试创建/使用 rawBody 的方式:
app.use(express.json({verify: (req,res,buf) => { req.rawBody = buf }}));
bodyParser.json({
verify: (req: any, res, buf) => {
req.rawBody = buf.toString();
},
}),
event = stripe.webhooks.constructEvent(req.rawBody, sig, webhookSecret);
我的代码基于此处找到的条带示例:https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/node-express/express.js
// Stripe requires the raw body to construct the event
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
// On error, log and return the error message
console.log(`❌ Error message: ${err.message}`);
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Successfully constructed event
console.log('✅ Success:', event.id);
// Return a response to acknowledge receipt of the event
res.json({received: true});
});
任何帮助将不胜感激,谢谢。
【问题讨论】:
标签: javascript node.js express google-cloud-platform stripe-payments