[iOS and Android] Check if subscription is active (purchased with receipt)

Hi there,

Apologies if its a duplicate,

How do i check on both platforms that a subscription is active? I read in one forum post that ProcessPurchase event will automatically gets called next time you run the app on iOS with all the product info, (assuming not on android)

So what;d be the right way to approach this? What i want to do is everytime user logins the app i check if user has a subscription active (this is where i need help), and then also verify it on backend (assuming ill always have a receipt of purchased product) and then toggle premium features.

Please advise.

You will want to check the receipt in your product controller, perhaps in OnInitialized. ProcessPurchase will not be called each time, so you don’t want to use that. You can also use SubscriptionManager https://docs.unity3d.com/Manual/UnityIAPSubscriptionProducts.html . You can look at the Sample IAP Project v2 as an example https://discussions.unity.com/t/700293/3

Dictionary<string, string> dict = m_AppleExtensions.GetIntroductoryPriceDictionary();

        foreach (UnityEngine.Purchasing.Product item in controller.products.all)
        {

            if (item.receipt != null)
            {
                //MyDebug("INIT Product=" + item.definition.id.ToString());

                string intro_json = (dict == null || !dict.ContainsKey(item.definition.storeSpecificId)) ? null : dict[item.definition.storeSpecificId];

                if (item.definition.type == ProductType.Subscription)
                {
                   
                    SubscriptionManager p = new SubscriptionManager(item, intro_json);
                    SubscriptionInfo info = p.getSubscriptionInfo();
                    MyDebug("SubInfo: " + info.getProductId().ToString());
                    //Debug.Log(info.getPurchaseDate());
                    MyDebug("getExpireDate: " + info.getExpireDate().ToString());
                    MyDebug("isSubscribed: " + info.isSubscribed().ToString());
                    //Debug.Log(info.isExpired());
                    //Debug.Log(info.isCancelled());
                    //Debug.Log(info.isFreeTrial());
                    //Debug.Log(info.isAutoRenewing());
                    //Debug.Log(info.getRemainingTime());
                    //Debug.Log(info.isIntroductoryPricePeriod());
                    //Debug.Log(info.getIntroductoryPrice());
                    //Debug.Log(info.getIntroductoryPricePeriod());
                    //Debug.Log(info.getIntroductoryPricePeriodCycles());
                }

            }
        }
1 Like

Thanks, this is iOS only
I want to use single/shared code for both platforms.

So if i guess, i can use OnInitialized and check isAvailableToPurchase = false && hasRecept

You will want to confirm with actual test purchases. The code I shared is for both platforms.

Im actually refering to this “appleExtensions” part which seems non android to me.

I have already seen all the samples provided in packages, but if IAP Project v2 is better than those samples then ill review it tonight as well

Yeah, if you follow that code, the “dict” variable remains null. So you can avoid the Apple mention altogether on Google/Android. I would suggest the package Samples first, then the Sample IAP Project (they are newer).

But what will be the code for Andorid? Previously (in 3.0) we used GetProductJSONDictionary.

 if (Application.platform == RuntimePlatform.IPhonePlayer)
    dict = _AppleExtensions.GetIntroductoryPriceDictionary();
 else if (Application.platform == RuntimePlatform.Android)
    dict = _GoogleExtensions.GetProductJSONDictionary();

Is it correct?

string intro_json = null;

if (Application.platform == RuntimePlatform.IPhonePlayer)
{
    Dictionary<string, string> dict = _AppleExtensions.GetIntroductoryPriceDictionary();
    intro_json = (dict == null || !dict.ContainsKey(item.definition.storeSpecificId)) ? null : dict[item.definition.storeSpecificId];
}
else if (Application.platform == RuntimePlatform.Android)
{
    intro_json = item.metadata?.GetGoogleProductMetadata()?.originalJson;
}

@Suvitruf_1 I am not familiar with the methods you are using, but if it’s working in your testing, then it looks appropriate to me.

Would be nice to see complete demo IAP project from Unity team)

A “complete demo” of course could never exist, someone will always want another feature. But we do have this updated Sample IAP Project, and the great Samples that are listed in Package Manager for In App Purchasing https://discussions.unity.com/t/700293/4

I saw this project. But there is no code sample for Android subscriptions handling. That’s why I’ve asked.

Yes, it’s in the project and was posted in this thread previously, it’s the second post.

1 Like

Sorry to necro, but i can’t seem to get SubscriptionInfo working, i even tried this,

        SubscriptionInfo info = new SubscriptionInfo("mysub");
        Result result = info.isSubscribed();
        print(result);

on the IAP purchasing example project you made on the other thread, it always comes back true, even when i tried deleting the project and running it without subscribing. Am i doing it wrong? Am i supposed to run through a list of the products or something? Even in my project, the result is always returning true even when the subscription has not been purchased…

Hello Mashimaro7,

You don’t want to use the ```SubscriptionInfo(string productId)


You should go through the SubscriptionManager to obtain the SubscriptionInfo.
He's an extract of it from our sample 02 BuyingSubscription:

```csharp
        bool IsSubscribedTo(Product subscription)
        {
            // If the product doesn't have a receipt, then it wasn't purchased and the user is therefore not subscribed.
            if (subscription.receipt == null)
            {
                return false;
            }

            //The intro_json parameter is optional and is only used for the App Store to get introductory information.
            var subscriptionManager = new SubscriptionManager(subscription, null);

            // The SubscriptionInfo contains all of the information about the subscription.
            // Find out more: https://docs.unity3d.com/Packages/com.unity.purchasing@3.1/manual/UnityIAPSubscriptionProducts.html
            var info = subscriptionManager.getSubscriptionInfo();

            return info.isSubscribed() == Result.True;
        }
1 Like

Oh, i see. Thank you :slight_smile: