Reputation: 972
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.
Upvotes: 5
Views: 1681
Reputation: 1943
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
Upvotes: 1