【发布时间】:2022-01-18 07:54:53
【问题描述】:
我一直在关注有关通过 Stripe CLI 设置 webhook 和本地测试的 Stripes 文档。我可以成功创建事件,但收到 400 POST 响应。
2021-12-14 23:18:53 --> payment_intent.created [evt_3K6ox6LqfpEEJtVG0yhTBKDf]
2021-12-14 23:18:53 <-- [400] POST http://localhost:4242/webhook [evt_3K6ox6LqfpEEJtVG0yhTBKDf]
无论我做什么,我总是收到“未找到与有效负载的预期签名匹配的签名。您是否传递了从 Stripe 收到的原始请求正文?”错误。回顾与此相关的先前问题,问题围绕传递原始数据。
我已经仔细检查了我的 webhook 密码是否与 CLI 提供的密码相匹配。
这是最新的尝试:
// Use JSON parser for all non-webhook routes
app.use((req, res, next) => {
if (req.originalUrl === '/webhook') {
next();
} else {
express.json()(req, res, next);
}
});
// Stripe requires the raw body to construct the event
app.post('/webhook', express.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];
const rawBody = req.body
let event;
try {
event = stripe.webhooks.constructEvent( rawBody, 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});
});
【问题讨论】:
-
我认为你可以调试请求对象,尝试确保端点密码正确
-
所以我找到了搞砸的代码部分。进一步我的代码: app.use(express.json({ limit: "30 mb"})) app.use(express.urlencoded({ limit: "30 mb"})) app.use(cors() ) 如果我将这些注释掉,我的 webhook 就可以工作了! ...但是以用户身份登录该站点的能力失败了...
-
我认为你需要传递 corOption,有时 cors 配置对我来说是黑魔法。 app.use(cors({origin: true}))
标签: node.js express next.js stripe-payments body-parser