简体   繁体   中英

404 Error on Stripe Webhook in Node

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

<!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:

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.

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.

Note : I purposefully made event_json a string, since I am trying to remove as many possible confounding variables as possible. Once this works as a string I will uncomment body-parser and switch event_json over to a variable again.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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