【问题标题】:Stripe Webhooks StripeSignatureVerificationErrorStripe Webhooks StripeSignatureVerificationError
【发布时间】:2022-02-13 23:00:13
【问题描述】:

我目前正在为 Checkout 流程测试 Stripe Webhook 端点。

我很困惑,因为 Stripe 在他们的文档中展示了两个不同的 sn-ps 如何设置 Webhook 端点。

Checkout Doc 他们显示这个代码sn-p:

const stripe = require('stripe')('sk_test_...');

// Find your endpoint's secret in your Dashboard's webhook settings
const endpointSecret = 'whsec_...';

// Using Express
const app = require('express')();

// Use body-parser to retrieve the raw body as a buffer
const bodyParser = require('body-parser');

// Match the raw body to content type application/json
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
  const sig = request.headers['stripe-signature'];

  let event;

  try {
    event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
  } catch (err) {
    return response.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Handle the checkout.session.completed event
  if (event.type === 'checkout.session.completed') {
    const session = event.data.object;

    // Fulfill the purchase...
    handleCheckoutSession(session);
  }

  // Return a response to acknowledge receipt of the event
  response.json({received: true});
});

app.listen(8000, () => console.log('Running on port 8000'));

他们在他们的Webhook Docs 中展示了这个sn-p:

const app = require('express')();
// Use body-parser to retrieve the raw body as a buffer
const bodyParser = require('body-parser');

// Match the raw body to content type application/json
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
  let event;

  try {
    event = JSON.parse(request.body);
  } catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Handle the event
  switch (event.type) {
    case 'payment_intent.succeeded':
      const paymentIntent = event.data.object;
      // Then define and call a method to handle the successful payment intent.
      // handlePaymentIntentSucceeded(paymentIntent);
      break;
    case 'payment_method.attached':
      const paymentMethod = event.data.object;
      // Then define and call a method to handle the successful attachment of a PaymentMethod.
      // handlePaymentMethodAttached(paymentMethod);
      break;
    // ... handle other event types
    default:
      // Unexpected event type
      return response.status(400).end();
  }
  // Return a response to acknowledge receipt of the event
  response.json({received: true});
});

app.listen(8000, () => console.log('Running on port 8000'));

我尝试了两种 sn-ps,但似乎没有任何效果。

当我尝试构造事件(...)时,第一个给了我 StripeSignatureVerificationError

第二个告诉我对象 eventundefined

有人知道为什么这两个端点都不适合我吗?

【问题讨论】:

  • 第一个 sn-p 的错误消息是:“没有找到与有效负载的预期签名匹配的签名。您是否传递了从 Stripe 收到的原始请求正文?”
  • 现在使用第一个 sn-p。如果您在调用constructEvent 之前记录request.body,它是否与Stripe 仪表板上显示的事件完全匹配,包括空格?您的 endpointSecret 是否正确 - 它是否与您的仪表板中的秘密完全匹配/由 stripe-cli 发出?

标签: javascript node.js server stripe-payments backend


【解决方案1】:

在使用 JSON Body 解析器之前配置接收 RAW body

app.use(bodyParser.raw({type: "*/*"}))  <-- This line need to be added
app.use(bodyParser.json())

更多讨论https://github.com/stripe/stripe-node/issues/331

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2016-06-11
    • 2017-02-22
    • 2021-09-10
    • 2020-04-19
    • 2014-04-18
    • 2023-03-17
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多