Stripe Webhooks StripeSignatureVerificationError

问题描述 投票:0回答:2

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

我很困惑,因为 Stripe 在其文档中显示了两个不同的片段如何设置 Webhook 端点。

Checkout Doc 中,他们显示了以下代码片段:

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 文档 中,他们显示了这个片段:

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'));

我尝试了两个片段,但似乎对我不起作用。

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

第二个告诉我对象事件未定义

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

javascript node.js server stripe-payments backend
2个回答
4
投票

在使用 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


0
投票
After changing const sig = req.headers["stripe-signature"]; to const sig = req.headers.get("stripe-signature");, it works."
© www.soinside.com 2019 - 2024. All rights reserved.