1

Stripe documentation points out that:

Because properly acknowledging receipt of the webhook notification is so important, your endpoint should return a 2xx HTTP status code prior to any complex logic could cause a timeout.

In the web hook I'm developing with PHP this is exactly the case — there is some complex logic involved after the event is received.

Currently I'm returning the response code with a simple call to http_response_code(). But how can I send the response headers (with the code) before proceeding? Would a flush() call force the headers to be sent?

sidyll
  • 57,726
  • 14
  • 108
  • 151
  • I would think that a flush as well as an ob_flush (after making sure there's a buffer) should do it. If a browser is involved in any way though all bets are off. It also depends on what their timeout value really is. – Dave May 26 '20 at 20:04
  • If you search the internet for some of those phrases from their documentation, they appear on a couple of unrelated sites so I think there's some copy-and-pasting going on. Anyway, you could use `flush` to force the headers through, but then if you had a problem later on you wouldn't be able to send an error header. My recommendation would be to ignore that recommendation (which their sample code does, too). However, if you are going to have any complex logic that might block for a couple of seconds, introduce some form of queueing on your side. – Chris Haas May 26 '20 at 20:06
  • @Dave Thank you. No browser involved, this endpoint is exclusive for communication between servers. – sidyll May 26 '20 at 20:30
  • @ChrisHaas Ah, I see. Thank you. I do perform some checks before determining the return code. So furthers erros wouldn't really be relevant to Stripe and can be handled internally. Anyway, I don't think it will block for 2 seconds. Technically it shouldn't; I'm just trying to follow their documentation. The fact that the sample code doesn't is somewhat annoying. – sidyll May 26 '20 at 20:35
  • If any of you could post this as an answer, I'd be happy to accept it. Thanks. – sidyll May 26 '20 at 20:38
  • view https://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response if you need any more info, haven't tested it – Ethan Mar 29 '22 at 09:18

0 Answers0