How should you handle the fact that events received via webhooks can be received in random order ?
For instance, given the following ordered event:
- A: invoiceitem.created (with quantity of 1)
- B: invoiceitem.updated (with quantity going from 1 to 3)
- C: invoiceitem.updated (with quantity going from 3 to 2)
How do you make sure receiving C-A-B does not result in corrupted data (ie with a quantity of 2 instead of 3)?
You could reject the webhook if the previous_attributes in Event#data do not correspond to the current state, but then you are stuck if your local model was updated already, as you will never find yourself in the state expected by the webhook.
Or you can just use treat any webhook as a hint to retrieve and update an object. You just disregard the data sent by the webhook and always retrieve it. Even if you receive events ordered as update/delete/create it should work, as update would in fact create the object, delete would delete it, and create would fail to retrieve the object and do nothing. But it feels like a waste of resources to retrieve data each time when the webhook offers it as event data.
This question was asked before but the answers don't cover the above solutions.
Thanks
created
common field to all Stripe webhook events. You could implement a small holding area for incoming requests and process them in correct date order. – Mycetozoan