I'm setting up Stripe webhooks for the first time. I've made hundreds of test requests and I've had an error rate of around 90%. It's unpredictable as to why the failures occur.
The failure responses on the Stripe dashboard are either:
Timed out connecting to remote host
or
Failed to connect to remote host
My webhook (I've simplified it for testing). Roughly 10% of the time, I get a 200 response with {received: true}
:
expressRouter.route('/hooks').post( async (req, res) => {
const event = req.body;
console.log("Event:");
console.log(event);
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
// Then define and call a method to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent);
break;
case 'payment_method.attached':
const paymentMethod = event.data.object;
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached(paymentMethod);
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
// Return a response to acknowledge receipt of the event
res.json({received: true});
})
I have tried these two ways of defining the hook, and also with and without async:
expressRouter.route('/hooks').post( async (req, res) => {
...
})
// and
app.post("/hooks", async (req, res) => {
...
})
I've been in contact with Stripe support and have tried all their suggestions.
They left me with the following possibilities:
It possible there is a slow network involved or some other issue with routing.
The host provider may need to allow Stripe's delivery IP address, too, FYI
they may be getting blocked before reaching your server
I have added Stripe's IPs to iptables, eg:
iptables -I INPUT -p tcp -s 3.18.12.63 -j ACCEPT
I'm running my Hostinger VPS server with Caddy on Ubuntu 18.04. Could this be an issue with my server set up? Any advice is greatly appreciated.