简体   繁体   中英

Stripe: webhook events order

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 CAB 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

If your application is sensitive to changes like this that can occur close in time, you really should just use the event as a signal to retrieve the object, as @koopajah noted in their comment. That's the only way to ensure you have the latest state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM