Search Unity

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

Discussion in 'Unity IAP' started by jGate99, Oct 4, 2021.

  1. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    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.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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://forum.unity.com/threads/sample-iap-project.529555/#post-6950270

    Code (CSharp):
    1. Dictionary<string, string> dict = m_AppleExtensions.GetIntroductoryPriceDictionary();
    2.  
    3.         foreach (UnityEngine.Purchasing.Product item in controller.products.all)
    4.         {
    5.  
    6.             if (item.receipt != null)
    7.             {
    8.                 //MyDebug("INIT Product=" + item.definition.id.ToString());
    9.  
    10.                 string intro_json = (dict == null || !dict.ContainsKey(item.definition.storeSpecificId)) ? null : dict[item.definition.storeSpecificId];
    11.  
    12.                 if (item.definition.type == ProductType.Subscription)
    13.                 {
    14.                    
    15.                     SubscriptionManager p = new SubscriptionManager(item, intro_json);
    16.                     SubscriptionInfo info = p.getSubscriptionInfo();
    17.                     MyDebug("SubInfo: " + info.getProductId().ToString());
    18.                     //Debug.Log(info.getPurchaseDate());
    19.                     MyDebug("getExpireDate: " + info.getExpireDate().ToString());
    20.                     MyDebug("isSubscribed: " + info.isSubscribed().ToString());
    21.                     //Debug.Log(info.isExpired());
    22.                     //Debug.Log(info.isCancelled());
    23.                     //Debug.Log(info.isFreeTrial());
    24.                     //Debug.Log(info.isAutoRenewing());
    25.                     //Debug.Log(info.getRemainingTime());
    26.                     //Debug.Log(info.isIntroductoryPricePeriod());
    27.                     //Debug.Log(info.getIntroductoryPrice());
    28.                     //Debug.Log(info.getIntroductoryPricePeriod());
    29.                     //Debug.Log(info.getIntroductoryPricePeriodCycles());
    30.                 }
    31.  
    32.             }
    33.         }
     
    jGate99 likes this.
  3. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    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
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You will want to confirm with actual test purchases. The code I shared is for both platforms.
     
  5. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    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
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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).
     
  7. Suvitruf

    Suvitruf

    Joined:
    Sep 3, 2014
    Posts:
    39
    But what will be the code for Andorid? Previously (in 3.0) we used GetProductJSONDictionary.

    Code (CSharp):
    1.            
    2.  if (Application.platform == RuntimePlatform.IPhonePlayer)
    3.     dict = _AppleExtensions.GetIntroductoryPriceDictionary();
    4.  else if (Application.platform == RuntimePlatform.Android)
    5.     dict = _GoogleExtensions.GetProductJSONDictionary();
    Is it correct?

    Code (CSharp):
    1.  
    2. string intro_json = null;
    3.  
    4. if (Application.platform == RuntimePlatform.IPhonePlayer)
    5. {
    6.     Dictionary<string, string> dict = _AppleExtensions.GetIntroductoryPriceDictionary();
    7.     intro_json = (dict == null || !dict.ContainsKey(item.definition.storeSpecificId)) ? null : dict[item.definition.storeSpecificId];
    8. }
    9. else if (Application.platform == RuntimePlatform.Android)
    10. {
    11.     intro_json = item.metadata?.GetGoogleProductMetadata()?.originalJson;
    12. }
    13.  
    14.  
     
    Last edited: Mar 14, 2022
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @Suvitruf I am not familiar with the methods you are using, but if it's working in your testing, then it looks appropriate to me.
     
  9. Suvitruf

    Suvitruf

    Joined:
    Sep 3, 2014
    Posts:
    39
    Would be nice to see complete demo IAP project from Unity team)
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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://forum.unity.com/threads/sample-iap-project.529555/#post-7922275
     
  11. Suvitruf

    Suvitruf

    Joined:
    Sep 3, 2014
    Posts:
    39
    I saw this project. But there is no code sample for Android subscriptions handling. That's why I've asked.
     
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, it's in the project and was posted in this thread previously, it's the second post.
     
    Suvitruf likes this.
  13. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry to necro, but i can't seem to get SubscriptionInfo working, i even tried this,
    Code (CSharp):
    1.         SubscriptionInfo info = new SubscriptionInfo("mysub");
    2.         Result result = info.isSubscribed();
    3.         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...
     
  14. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    235
    Hello Mashimaro7,

    You don't want to use the
    SubscriptionInfo(string productId)
    as it's meant to be called internally only (this one always returns true for isSubscribed)

    You should go through the SubscriptionManager to obtain the SubscriptionInfo.
    He's an extract of it from our sample 02 BuyingSubscription:
    Code (CSharp):
    1.         bool IsSubscribedTo(Product subscription)
    2.         {
    3.             // If the product doesn't have a receipt, then it wasn't purchased and the user is therefore not subscribed.
    4.             if (subscription.receipt == null)
    5.             {
    6.                 return false;
    7.             }
    8.  
    9.             //The intro_json parameter is optional and is only used for the App Store to get introductory information.
    10.             var subscriptionManager = new SubscriptionManager(subscription, null);
    11.  
    12.             // The SubscriptionInfo contains all of the information about the subscription.
    13.             // Find out more: https://docs.unity3d.com/Packages/com.unity.purchasing@3.1/manual/UnityIAPSubscriptionProducts.html
    14.             var info = subscriptionManager.getSubscriptionInfo();
    15.  
    16.             return info.isSubscribed() == Result.True;
    17.         }
     
    Mashimaro7 likes this.
  15. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Oh, i see. Thank you :)