Search Unity

Checking Subscription and NonConsumable status

Discussion in 'Unity IAP' started by Mars91, Jan 8, 2021.

  1. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    IAP is working like a charm, I now need to understand how can I check sub and non consumable status.

    For a non consumable my idea is:
    User press a button to make a purchase, if the purchase goes well I save a bool var that says that the purchase has been done.
    If user unistall or delete data he just need to press the button again and the purchase is restored, the var is back on its "purchased" state.

    Subscriptions are different, I would need to check their status in case someone cancel it or if more than a month passed.
    What is the best way to handle this scenario?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Restore happens automatically on Google (Android) devices. If you reinstall the app, you should see ProcessPurchase fired automatically for each such prior purchase during IAP initialization https://docs.unity3d.com/Manual/UnityIAPRestoringTransactions.html (the code they show there is for iOS)

    Many of the larger studios persist the purchase information in the cloud using ChiliConnect or PlayFab. You could use PlayerPrefs but it's not very secure, but would be good for prototyping.

    Also, this might help, a method that I've added to my Sample IAP Test project here. You'll want to examine the receipt fields in your debugger

    Code (CSharp):
    1.  public void ListProducts()
    2.     {
    3.  
    4.         foreach (UnityEngine.Purchasing.Product item in m_StoreController.products.all)
    5.         {
    6.  
    7.             if (item.receipt != null)
    8.             {
    9.                 MyDebug("List Product = " + item.definition.id.ToString() + ", Trans = " + item.transactionID.ToString());
    10.             }
    11.         }
    12.     }
     
  3. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Thank you! Regarding Subscriptions I found this that would solve all of my problem.
    https://docs.unity3d.com/Manual/UnityIAPSubscriptionProducts.html

    Where can I find some example code?
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  5. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    A quick code I wrote, do you think this is the right way to quickly check the state of one of my two subscription product?

    Code (CSharp):
    1.  
    2.     public void ListSubscriptionProducts()
    3.     {
    4.         foreach (UnityEngine.Purchasing.Product item in m_StoreController.products.all)
    5.         {
    6.             if (item.definition.type == ProductType.Subscription)
    7.             {
    8.                 if (item.receipt != null)
    9.                 {
    10.                     SubscriptionManager subscriptionManager = new SubscriptionManager(item, null); // Modify for apple, add intro_json
    11.                     SubscriptionInfo info = subscriptionManager.getSubscriptionInfo();
    12.  
    13.                     if (info.isSubscribed() == Result.True)
    14.                     {
    15.                         if (item.definition.id == monthlySubscriptionProductID)
    16.                         {
    17.                             Debug.Log("Subscribed for MONTHLY");
    18.                         }
    19.                         else if (item.definition.id == yearlySubscriptionProductID)
    20.                         {
    21.                             Debug.Log("Subscribed for YEARLY");
    22.                         }
    23.                     }
    24.                 }
    25.             }
    26.         }
    27.     }
    28.  
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446