4

我为 webhook 设置了以下代码:

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var middleware = require('../middleware');
var stripe = require("stripe")(process.env.SECRET_KEY);

// router.use(require("body-parser").raw({type: "*/*"}));

router.post("/webhook/all", function(req, res) {
  // Retrieve the request's body and parse it as JSON
  // var event_json = JSON.parse(req.body);
  middleware.sendEmail('Webhook Test', 'event_json');
  res.sendStatus(200);
});

module.exports = router;

当我使用localhostPostman 对其进行测试时,它可以工作,但是当它处于活动状态时,它会响应 404 错误:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /webhook/all</pre>
</body>
</html>

我唯一的猜测是它与我HTTPS有关,因为Stripe Docs是这样说的:

如果您为您的 webhook 端点使用 HTTPS URL,Stripe 将在发送您的 webhook 数据之前验证与您的服务器的连接是否安全。为此,您的服务器必须正确配置为支持具有有效服务器证书的 HTTPS。

我的 SSL 证书是在 DO 液滴上使用 Let's Encrypt 设置的,它似乎工作正常,所以我不确定为什么会出现这个问题。

注意:我故意制作event_json了一个字符串,因为我试图删除尽可能多的混淆变量。一旦它作为一个字符串工作,我将取消注释body-parser并再次切换event_json到一个变量。

4

1 回答 1

1

You need to tunnel your localhost to public. Please use ngrok for this. It will generate a random public url for your localhost application every time you run ngrok

Suppose your localhost URL is http://localhost:3000/webhook/all, the ngrok url will be like https://somerandomurl/webhook/all.

Then you can this public url to receive all the webhook events on localhost.

PS: When you will hit CTRL+C on ngrok it will stop the tunneling and your application will no longer be able to receive any webhook event. Also you will get higher failure rates on the stripe

于 2020-10-01T20:55:17.270 回答