Question

How to sync subscription status of a user using the REST API

  • 7 March 2022
  • 3 replies
  • 112 views

Badge

Hi,

 

I have a mobile app (iOS/Android) and the backend is built with Django+MySQL. My app offers a monthly/yearly “premium user” subscription and we’re using RevenueCat to handle IAPs. But we also want to keep the subscription status (isPremiumUser or not) of a user in our own database’s user records as well.

 

I read through the documentation for RevenueCat webhooks and came across this excerpt.

Webhooks are commonly used to keep a subscribers status in sync with RevenueCat across multiple systems. To simplify the logic of handling different webhook types, we recommend creating a polling system using the GET /subscribers REST API to sync the subscription status of the customer from RevenueCat to your database. Then, each webhook event can simply be a trigger to call this sync function. This approach has a few benefits and can make your system more robust and scalable.

 

The way I understand is, below are the steps to implement such a system.

1. Register URL to receive webhook events.
  1.1. Send 200 response back to RevenueCat servers when a webhook event is received.

2. Every time a webhook is received, get the 'original_app_user_id' from the response.

3. Call /subscribers/<original_app_user_id> to get the subscriber info.

4. Determine whether the user currently has an active subscription.

5. Update my database's user object 'isPremiumUser' property accordingly.

 

Now my question has a few parts.

  1. Is my approach correct? Am I missing any steps?
  2. How do I actually determine whether the user is subscribed or not? What fields in the JSON should I check?
  3. In the REST API’s docs, it says if I call the /subscribers endpoint with a user id and that user doesn’t exist, it will create one. Wouldn’t that be a problem?

 

I’d really appreciate any help to get these clarified. Thanks in advance.


3 replies

Badge +3
  1. Is my approach correct? Am I missing any steps?

 

That sounds about right to me!

 

  1. How do I actually determine whether the user is subscribed or not? What fields in the JSON should I check?

 

One way is to loop through the entitlements dictionary and check the expires_date of each entitlement object. If the date is in the future, the user still has access to the entitlement. If you’re not using entitlements in RevenueCat, loop through the subscriptions object instead and check the expires_date on each subscription.

 

  1. In the REST API’s docs, it says if I call the /subscribers endpoint with a user id and that user doesn’t exist, it will create one. Wouldn’t that be a problem?

 

You can tell if the user was created by checking the HTTP status code of the response. If it’s 201, that means the user was created as a result of this request. If it’s 200, the user existed before you made this request. So you can check if the status code is 201, and if you didn’t want to create the user, you can delete them.

 

-- Sharif

 

One way is to loop through the entitlements dictionary and check the expires_date of each entitlement object. If the date is in the future, the user still has access to the entitlement. If you’re not using entitlements in RevenueCat, loop through the subscriptions object instead and check the expires_date on each subscription.

 

 @RC Support What are we expected to do if the expires_date is in the future (user has access until end of billing period)? Is there a webhook that triggers again for a user when the expires_date is reached or are we expected to somehow schedule the entitlement to be removed from the user on the expires_date?

 

One way is to loop through the entitlements dictionary and check the expires_date of each entitlement object. If the date is in the future, the user still has access to the entitlement. If you’re not using entitlements in RevenueCat, loop through the subscriptions object instead and check the expires_date on each subscription.

 

 @RC Support What are we expected to do if the expires_date is in the future (user has access until end of billing period)? Is there a webhook that triggers again for a user when the expires_date is reached or are we expected to somehow schedule the entitlement to be removed from the user on the expires_date?

 

For my server side code, in addition to checking the isPremiumUser I use an extra check to make sure that their subscription is still active by checking the expiration date. 

So in your case I think before returning true for isPremiumUser you could perform another check in your database to make sure that the expires_date is in the future.

 

 

Reply