我正在尝试从 Stripe 设置一个 webhook 来处理payment_intent.succeeded
事件,但我得到一个例外。这是我来自 Node 后端的代码(我已经提取了我希望的所有相关部分。如果需要其他任何内容,请告诉我):
const stripeWebHookSecret = 'whsec_WA0Rh4vAD3z0rMWy4kv2p6XXXXXXXXXX';
import express from 'express';
const app = express();
app.use(bodyParser.urlencoded({ extended:true }));
app.use(bodyParser.json());
app.use(session({ <some params here> }));
const openRouter = express.Router();
registerOpenPaymentRoutes(openRouter);
app.use('/open', openRouter);
并实施registerOpenPaymentRoutes
看起来像这样:
export const registerOpenPaymentRoutes = (router) => {
router.post('/payment/intent/webhook', bodyParser.raw({type: 'application/json'}), (req, res) => {
let signature = req.headers['stripe-signature'];
try {
let event = stripe.webhooks.constructEvent(req.body, signature, stripeWebHookSecret);
switch(event.type){
case 'payment_intent.succeeded':
let intent = event.data.object;
res.json({ message: 'Everything went smooth!', intent });
default:
res.status(400).json({ error: 'Event type not supported' });
}
}
catch (error){
res.status(400).json({ message: `Wrong signature`, signature, body: req.body, error });
}
});
}
到目前为止一切顺利。当我从 Stripe 仪表板触发测试 Webhook 事件时,我到达了端点,但从 catch 块中获取了结果。错误信息如下:
No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing"
我将返回带有错误消息的签名,正如您在上面看到的那样,签名如下所示:
"t=1557911017,v1=bebf499bcb35198b8bfaf22a68b8879574298f9f424e57ef292752e3ce21914d,v0=23402bb405bfd6bd2a13c310cfecda7ae1905609923d801fa4e8b872a4f82894"
据我从文档中了解到,获取提到的原始请求正文需要的是bodyParser.raw({type: 'application/json'})
我已经在那里的路由器的参数。
谁能看到缺失的部分吗?