Learning and sharing
Learning and sharing

Reputation: 1398

Stripe Webhooks - Can I send response codes other than 200?

I've been checking the Stripe documentation, and it suggests that we respond as soon as possible with a 200 code "It must be before we process it in our database".

Link: https://stripe.com/docs/webhooks#acknowledge-events-immediately.

I have some doubts:

  1. Is it strictly required to always send Stripe response code 200?

  2. In which cases is it necessary to send Stripe another response code? Like these: https://stripe.com/docs/api/errors

  3. What would happen if after sending the 200 code to Stripe, the system for some reason fails to process an event in my database, update an invoice, subscription etc ... I can respond to Stripe with a 402 response so that it will forward me that same event, would it be a good practice?

  4. How can I ask Stripe to send the same event again?

  5. After sending Stripe a 200 response code, can Stripe retry sending the same event?

Upvotes: 2

Views: 1707

Answers (1)

Tommy
Tommy

Reputation: 39827

  1. Yes, well, at least a 2xx success code
  2. When the situation warrants returning a different status code (Server Error, end point not found, etc). This should be only sparingly as the documentation states: "If Stripe doesn’t quickly receive a 2xx response status code for an event, we mark the event as failed and stop trying to send it to your endpoint. After multiple days, we email you about the misconfigured endpoint, and automatically disable it soon after if you haven’t addressed it."
  3. In this case, this is on you to handle the problem**(see more below)
  4. You can't as far as I am aware
  5. No

Your questions, especially the last 3, are all centered around the issue of a failure on your end after Stripe notifies you of an event.

**(The more below section)

From Stripe's perspective, the event has occurred, and it was successful. The webhook is a notification to your system on what happened and most likely has a short timeout for awaiting a response (which is why they ask for the quick 200 response). Stripe will not renotify you of the same event multiple times as they have done so once, multiple times would be redundant from Stripe's perspective.

Given this, you should think of your own application's structure a bit differently.

  1. Stripe send a notification to your webhook
  2. You store/save/persist that notification and respond to Stripe 200 Response, we got the notification - thanks!
  3. At this point, Stripe is out of the loop. They have done their job of processing payment (or whatever) and notified you.
  4. With the saved notification, you can go forth and do what you need to do. If what you need to do fails, you can mark your own stored entry from Step 2 as failed, needs retry, however you want to handle it. Either way, again, Stripe's role in all of this was completed on Step 2 and there is no reason that they need to be involved any longer.

Upvotes: 3

Related Questions