【问题标题】:Stripe Webhook 400 error. Raw request body issues条纹 Webhook 400 错误。原始请求正文问题
【发布时间】: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


【解决方案1】:

这是一个常见问题,可能取决于您的设置和框架。如果您使用的是 Node,我建议您通过 this Github issue 看看那里的解决方法是否适合您。

最后,您可以写信给 Stripe Support,他们的开发人员可以帮助找出问题所在。

【讨论】:

  • 所以我找到了搞砸的代码部分。进一步我的代码: app.use(express.json({ limit: "30 mb"})) app.use(express.urlencoded({ limit: "30 mb"})) app.use(cors() ) 如果我将这些注释掉,我的 webhook 就可以工作了! ...但是以用户身份登录该站点的能力失败了...
猜你喜欢
  • 2015-10-19
  • 2021-04-07
  • 2013-12-02
  • 2017-07-23
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
  • 2014-07-29
相关资源
最近更新 更多