简体   繁体   English

Node 中的 Stripe Webhook 上的 404 错误

[英]404 Error on Stripe Webhook in Node

I have the following code setup for a webhook:我为 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;

When I test this with localhost and Postman it works, however when it is live it responds with a 404 error:当我用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>

My only guess is it has something to do with my HTTPS , since the Stripe Docs say this:我唯一的猜测是它与我的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.如果您为 webhook 端点使用 HTTPS URL,Stripe 将在发送 webhook 数据之前验证与服务器的连接是否安全。 For this to work, your server must be correctly configured to support HTTPS with a valid server certificate.为此,您的服务器必须正确配置为使用有效的服务器证书支持 HTTPS。

My SSL certificate is setup with Let's Encrypt on a DO droplet, and it seems to be working fine, so I'm not sure why this would be the issue though.我的 SSL 证书是在 DO droplet 上使用 Let's Encrypt 设置的,它似乎工作正常,所以我不确定为什么会出现这个问题。

Note : I purposefully made event_json a string, since I am trying to remove as many possible confounding variables as possible.注意:我故意将event_json字符串,因为我试图删除尽可能多的混淆变量。 Once this works as a string I will uncomment body-parser and switch event_json over to a variable again.一旦它作为字符串工作,我将取消注释body-parser并将event_json再次切换到变量。

You need to tunnel your localhost to public.您需要将本地主机隧道连接到公共。 Please use ngrok for this.请为此使用ngrok It will generate a random public url for your localhost application every time you run ngrok每次运行ngrok时,它都会为您的 localhost 应用程序生成一个随机公共 url

Suppose your localhost URL is http://localhost:3000/webhook/all , the ngrok url will be like https://somerandomurl/webhook/all .假设您的 localhost URL 是http://localhost:3000/webhook/all ,ngrok url 将类似于https://somerandomurl/webhook/all

Then you can this public url to receive all the webhook events on localhost.然后你可以通过这个公共 url 接收本地主机上的所有 webhook 事件。

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. PS:当您在 ngrok 上按下 CTRL+C 时,它将停止隧道,您的应用程序将不再能够接收任何 webhook 事件。 Also you will get higher failure rates on the stripe您还会在条带上获得更高的故障率

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM