gpt4 book ai didi

node.js - Node 中 Stripe Webhook 上的 404 错误

转载 作者:行者123 更新时间:2023-12-04 13:45:41 24 4
gpt4 key购买 nike

我为 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;

当我用 localhost 测试时和 postman 它可以工作,但是当它上线时,它会以 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说这个:

If you use an HTTPS URL for your webhook endpoint, Stripe will validate that the connection to your server is secure before sending your webhook data. For this to work, your server must be correctly configured to support HTTPS with a valid server certificate.



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

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

最佳答案

您需要将本地主机隧道传输到公共(public)。请使用 ngrok为了这。它将 为您的 localhost 应用程序生成一个随机公共(public) URL 每次运行 ngrok假设您的本地主机 URL 是 http://localhost:3000/webhook/all ,ngrok 网址将类似于 https://somerandomurl/webhook/all .
然后你可以通过这个公共(public) url 接收本地主机上的所有 webhook 事件。
PS:当您在 ngrok 上按下 CTRL+C 时,它将停止隧道,您的应用程序将不再能够接收任何 webhook 事件。您还会在 strip 上获得更高的故障率

关于node.js - Node 中 Stripe Webhook 上的 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48929514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com