GCP:Stripe webhook错误:找不到与负载的预期签名匹配的签名



条纹版本:"8.107.0";

每当我在GCP上运行webhook时,我都会收到Stripe webhook验证错误。正如下面的代码片段所提到的,我已经尝试在签名中使用原始主体,以及其他StackOverflow答案所提到的传递req.rawBody的方法。

奇怪的是,这个错误似乎是在我部署到GCP时抛出的,而不是在本地运行时抛出的。我尝试手动创建签名(https://stripe.com/docs/webhooks/signatures#verify-手动(,结果相同:在本地签名匹配,在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});
});

任何帮助都将不胜感激,谢谢。

问题出在我们的一个安装文件上,其中基本上有一个空格或字符被添加到我们的webhookSecret 中

我们在这里遇到了同样的问题,通过寻找开发webhook秘密(因为我们对开发环境有不同的url-这是不同的webhook机密-当你遇到这个问题时,考虑一下(解决了这个问题。

最新更新