I’m building my shopify app and I have monthly subscriptions with usage limits. I started with a managed subscription but I had some thinking and figured out I don’t know when to reset the limits of each store when they enter a new month of payment.
What is the best practice to do this within my Shopify app? Is this possible or in such cases I need to do the entire subscription logic on my end?
figured out I don’t know when to reset the limits of each store when they enter a new month of payment.
Sorry, what do you mean by this exactly?
Shopify automatically resets the usage level at the end of the billing period. So the only part you need to manage is notifying the customer when they are approaching the usage limit and giving them a UX to accept an adjustment to increase it (or decrease it) according to their current months usage.
There’s no mutation for “resetting” the usage for the last period, Shopify handles that for you.
I have a limit of the number of API calls each plan can do. Shopify can’t track that, so I made my own table in the database to track the current usage.
If shop A started subscription on 10.10 and shop B started subscription on 20.10 then for shop A subscription usage should become 0 on 10.11, respectively 20.11 for shop B.
I didn’t found shopify to send a webhook when the current subscription period ends and a new one starts.
So my question is what is best practice to make sure I reset the usage of each shop on their billing date, only if they continue to have an active subscription
I didn’t realize you were building your own internal usage metrics, not directly on the usage charges on the app subscription.
The closest thing you can do is use the billingPeriodEndAt timestamp to roughly understand when you should reset your own internally managed usage counter.
A word of caution though based on my own past experiences, don’t expect this to be an exact timestamp. I’ve seen cases when the billing period doesn’t update for minutes or even hours after the timestamp has changed.
Ok, but for this to actually be implemented correctly, shouldn’t I get any kind of webhook from shopify to tell me: “we started a new cycle of billing”?
Because if I’m doing my own rough estimation, I will reset it multiple times. Or can I make a cron job inside the app? Is this possible?
Would it be correct to store the currentPeriodEnd in my database get the current subscription at each API call. If the end data is not the same, and the current subscription is active → update subscription in my db
Shopify doesn’t have a billing period end/begin webhook as far as I know.
Yes you’ll have to set a cron job within your own app to monitor this, and write your own logic to detect when the period has changed.
Or you can attempt to schedule background jobs that fire at the time of the billing period change itself. Many background job services offer a delay type of functionality to delay a job from running until a specific time.
The solution I found is to make a webhook on the appSubscription. This doesn’t provide me the currentPeriodEnd, so whenever I get this hook, I search in GraphQL for the activeSubscription and retrieve the currentPeriodEnd and check what changed from the latest state of the subscription → thus I understand what I need to do (e.g. currentPeriodEnd changed and status remained ACTIVE → I need to reset usage counter)
yes, I store the latest known currentPeriodEnd in my database, retrieve it and update my entry with new values. Afterwards I will figure out if a new billing cycle entered and reset usage separately.