Following the Stripe documentation I come to create a webhook endpoint on my app to catch the customer.subscription.created event, which I use as a signal to create a new account in my database.
I'm using the stripe checkouts to send the user to make the payment process and then when comes back to my webpage I look to and endpoint in my database to fetch the account data.
The problem is, sometimes the Stripe checkout page gets closed and my webpage gets loaded back more fast than stripe actually doing the webhook's request with the event.
So which is the proper way to handle this discrepancy on the timings?
Don't tell me is putting a setTimeout before my fetching process. It cannot be that, right?
What Stripe recommend in their fulfillment documentation page is to handle fulfillment in both your webhook handler code (what you already do) and in the redirect. That way, if the redirect happens first, you don't have to wait for the Event to be sent and then properly processed by your webhook handler, and you can immediately check the status/details about the Checkout Session and handle fulfillment immediately.
You'll want to avoid race conditions and duplicate fulfillments, so if you are in the middle of processing the Event, you can add some logic to "lock" your database/table insert for example so that the fulfillment step in the redirect just waits, and vice versa.