Skip to content

Instantly share code, notes, and snippets.

@alahdal
Last active August 21, 2021 19:26
Show Gist options
  • Save alahdal/e73e286b62da7470faf953ff9d6b5006 to your computer and use it in GitHub Desktop.
Save alahdal/e73e286b62da7470faf953ff9d6b5006 to your computer and use it in GitHub Desktop.
A snippet to get the subscription status from RevenueCat.
# Below snippet to help using RevenueCat API as descibed in below link
# https://docs.revenuecat.com/docs/purchaserinfo
import json
import urllib3
from datetime import datetime
# To call the function, you should pass the app user id, the API Key and the entitlement that you would like to check,.
def user_is_subscribed(app_user_id, API_KEY_AUTH, entitlement):
http = urllib3.PoolManager()
req = http.request(
'GET',
'https://api.revenuecat.com/v1/subscribers/' + app_user_id,
headers={
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + API_KEY_AUTH,
}
)
revenuecat_response = (json.loads(req.data.decode('utf-8')))
try:
user_subscription = revenuecat_response['subscriber']['entitlements'][entitlement]
subscription_expiry = datetime.strptime(user_subscription['expires_date'], '%Y-%m-%dT%H:%M:%SZ')
print(f'expiry date: {subscription_expiry}')
print(f'current time: {datetime.utcnow()}')
if subscription_expiry > datetime.utcnow():
print('Subscribed')
print(revenuecat_response)
return True
else:
print('Subscription Expired!')
return False
except:
print('No Subscription!')
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment