Handling Order of 'app_subscription/update' Webhooks for Subscription Changes

Handling Order of 'app_subscription/update' Webhooks for Subscription Changes

umairakram
Shopify Partner
17 1 6

Hi Shopify Community,

 

I hope you're all doing well! I'm currently working on handling subscription changes in my Shopify app and have encountered a scenario where the order of the 'app_subscription/update' webhooks becomes crucial.

Here's the situation:

  1. A user subscribes to a plan, and a recurring application charge is created (status: active).
  2. The user decides to change their plan, leading to the cancellation of the existing charge and the creation of a new one (status: canceled and status: active).
  3. I'm receiving 'app_subscription/update' webhooks for both the cancellation and the new activation.

The challenge I'm facing is that I cannot reliably determine the order in which these webhooks will be triggered. My application logic needs to know whether the cancellation webhook or the new activation webhook is called first.

How can I handle this scenario effectively? Is there a recommended approach or best practice for managing the order of these webhooks?

 

Any insights, experiences, or suggestions would be greatly appreciated. Thank you in advance for your help!

 

Best regards,

Umair Akram

Reply 1 (1)

Liam
Shopify Staff
2873 312 821

Hi Umair,

In most cases, the webhooks are sent in the same order the events. However, due to network latency or other factors, you can't always guarantee the order of delivery.

 

Here's a suggested approach:

  1. Timestamps: Each webhook includes a timestamp of when the event occurred. You can use this to reconstruct the precise order of events, regardless of when your app receives them.

  2. Idempotency: Make sure your app handles webhooks in an idempotent manner. This means that no matter how many times your app receives a webhook, the result should be the same. For instance, if you receive a webhook for a subscription cancellation and then a new activation, your app should end up with the user having an active subscription.

  3. State Checking: When your app receives a webhook, it can also make a GET request to Shopify's API to check the current state of the object. This way, even if you receive webhooks out of order you can still respond based on the current state of the object.

Try out the above to see if these strategies work,

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog