在Node Express应用程序中为Stripe创建webhook;阅读要求

时间:2019-09-08 23:55:21

标签: node.js express stripe-payments

当端点被命中时,我无法读取来自Stripe的请求,甚至无法记录测试字符串。

Stripe说测试成功,但是我无法对请求采取任何措施。

这是我的app.js文件包含的内容。

var billingWebhook = require('./routes/billingWebhook');
// Additional code here
app.use('/billingWebhook', billingWebhook);

这是我的route / billingWebhook.js文件所包含的内容。

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

router.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
  console.log("We got here");
  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;
      handlePaymentIntentSucceeded(paymentIntent);
      break;
    case 'payment_method.attached':
      const paymentMethod = event.data.object;
      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});
});

module.exports = router;

以下是为Webhook设置的URL: https://xxxx.ngrok.io/billingWebhook/webhook

这是终端显示的内容: enter image description here

以下是Stripe的内容: enter image description here

0 个答案:

没有答案