How to get the current subscription status of the user, to validate that the subscription is still valid?

1,020

I've since found a great solution. In the end I just used RevenueCat. It handles everything. You don't even need to use Google Pub/Sub or Realtime Developer Notifications. So much easier! Here's the docs: https://docs.revenuecat.com/docs.

Once setup, which was pretty easy, I just use:

try {
  PurchaserInfo purchaserInfo = await Purchases.getPurchaserInfo();
  if (purchaserInfo.entitlements.all["my_entitlement_identifier"].isActive) {
    // Grant user "pro" access
  }
} catch (e) {
  // Error fetching purchaser info
}

Hope this answer helps someone in future - it saved me so much work and time once I'd figured it out!

Share:
1,020
Jason Lloyd
Author by

Jason Lloyd

Updated on December 19, 2022

Comments

  • Jason Lloyd
    Jason Lloyd over 1 year

    I'm building a flutter app that uses a freemium model.

    I'm using the in_app_purchase flutter package in my app, so users can now upgrade to a paid monthly subscription. After reading through the documentation https://developer.android.com/google/play/billing/billing_subscriptions#Handle-states for adding subscription-specific features, it says that ‘you should also have real-time developer notifications enabled’, which I have now done.

    According to this video (https://www.youtube.com/watch?v=9Ta2chg_Ak4), I will receive a purchase token directly from Cloud Pub/Sub anytime there is an update to an existing subscription. This purchase token doesn't contain the details of the change. So I'll need to send the purchase token from my secure back end to the Google Play Developer API to get the updated information attached to the purchase token. Once verified, I will have the revised information about the subscription, which can be stored on my back end server as well as sent to my Android app if needed to ensure users have access to the appropriate in-app content and services.

    I'm using Cloud Firestore as the backend for the app.

    The question I have is: How do I get the current subscription status of the user, to validate that the subscription is still valid? I believe that i need to use Purchases.subscriptions: get, which checks whether a user's subscription purchase is valid and returns its expiry time (https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get) - but I'm not sure how to actually do this in flutter. How do you implement this in flutter?

    Thanks in advance for your help!

    Jason