从 Stripe 获取 webhook 异常 [英] Getting exception on webhook from Stripe
问题描述
我正在尝试从 Stripe 设置一个 webhook 来处理 payment_intent.succeeded
事件,但出现异常.这是我在 Node 后端的代码(我已经提取了我希望的所有相关部分.如果还需要其他任何东西,请告诉我):
I'm trying to set up a webhook from Stripe to handle the payment_intent.succeeded
event, but I get an exception. This is my code from the Node backend (I have extracted all the relevant parts I hope. Let me know if anything else is needed):
const stripeWebHookSecret = 'whsec_WA0Rh4vAD3z0rMWy4kv2p6XXXXXXXXXXX';
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 块中获得了结果.错误信息如下:
So far so good.When I fire a test webhook event from the Stripe dashboard, I hit the endpoint, but get the result from the catch block. The error message is as follows:
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"
我返回的签名和您在上面看到的一样带有错误消息,签名如下所示:
I'm returning the signature with the error message as well as you see above, and the signature looks like this:
<代码> T = 1557911017,V1 = bebf499bcb35198b8bfaf22a68b8879574298f9f424e57ef292752e3ce21914d,V0 = 23402bb405bfd6bd2a13c310cfecda7ae1905609923d801fa4e8b872a4f82894"代码>
据我从文档中了解到,获取上述原始请求正文所需的是 bodyParser.raw({type: 'application/json'})
路由器的参数我已经在那里了.
As far as I understand from the documentation, what is needed to get the raw request body as mentioned are the bodyParser.raw({type: 'application/json'})
argument to the router that I already have there.
谁能看到缺失的部分?
推荐答案
这是因为你已经将你的 express 应用设置为使用 bodyParser.json()
中间件,这与 冲突>bodyParser.raw()
您在 webhook 路由中设置的中间件.
It's because you've already set your express app to use the bodyParser.json()
middleware, which clashes with the bodyParser.raw()
middleware you set up in your webhook route.
如果您删除 app.use(bodyParser.json());
行,您的 webhooks 将按预期工作,但是您的其余路由将没有解析的正文,这是不理想.
If you remove the app.use(bodyParser.json());
line your webhooks will work as intended, but then the rest of your routes won't have a parsed body, which isn't ideal.
我建议添加一个自定义中间件来根据路由选择bodyParser
版本.类似的东西:
I suggest adding a custom middleware to choose the bodyParser
version based on route. Something like:
// only use the raw bodyParser for webhooks
app.use((req, res, next) => {
if (req.originalUrl === '/payment/intent/webhook') {
next();
} else {
bodyParser.json()(req, res, next);
}
});
然后在 webhook 路由上显式使用 bodyParser.raw()
中间件.
Then explicitly use the bodyParser.raw()
middleware on the webhook routes.
这篇关于从 Stripe 获取 webhook 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!