Score:-1

~90% error rate with Stripe webhooks, unpredictable response. Server issue?

dz flag

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.

paladin avatar
id flag
100 of test requests? In which time frame? TCP connections have a latency, you need to be aware of that.
Joseph avatar
dz flag
@paladin, I've made 100s of requests over the past 24 hours. I normally send 10 in quick succession. Sometimes 5 could succeed, sometimes 0 succeed. Currently at 84% error rate.
paladin avatar
id flag
What does quick mean? If you write a loop, which fires 10 requests without delay, it's no wonder why you are getting such a high error rate.
Joseph avatar
dz flag
I've mostly been manually triggering requests with curl, a few seconds apart, but I've also been triggering them by doing proper test sales through Stripe checkout, and get the same errors. I have also tried this by just sending one request, but get an error rate all the same. So, I don't think that's the issue I have, but could you please explain why sending multiple requests may cause an error?
paladin avatar
id flag
In example, you have a client and a server. The server is able to handle 2 client requests per second at the same time, no more. Now you have a client which does 2 requests within 1 second, the server is totally fine with it. Now the same client tries to do 3 requests within 1 second. 2 of 3 requests can be answered in time by the server, the 3th requests times out.
paladin avatar
id flag
Same applies to IP packages, there cannot be unlimited IP packages on the line, only 1 (ONE) IP package can be on the line, if another IP packages arrives too, one of those packages "dies", which one is random.
Joseph avatar
dz flag
Ah, understood, thanks for explaining. I think I've just fixed it, after a day+ of trying various things! I found this: https://www.youtube.com/watch?v=S1uExj7mMgM&ab_channel=Twilio, and set up ngrok to forward to localhost:4000. I just send a load of requests and they were all successful. I'm not sure quite how this works / why I needed it, but I'm very happy to have it working. I'll answer the question now as well.
Score:-1
dz flag

I seem to have it working.

I installed ngrok via apt (https://ngrok.com/download)

Then followed this: youtube.com/watch?v=S1uExj7mMgM&ab_channel=Twilio, and set up ngrok to forward to localhost:4000:

ngrok http 4000

I just send a load of requests and they were all successful. I'm not sure quite how this works / why I needed it, but I'm very happy to have it working. If anyone can explain why it wasn't working without this, please let me know!

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.