Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Better SubscriptionManager definitions

Discussion in 'Unity IAP' started by Livealot, May 18, 2019.

  1. Livealot

    Livealot

    Joined:
    Sep 2, 2013
    Posts:
    228
    Day 1 of implementing UnityIAP and I'm super impressed at how much better everything is than a couple of years ago, and how fast it's evolving.

    Downside of progress is the inevitable backlog of documenting it all. In particular I'm trying to learn about the SubscriptionManager class. There are some outdated threads on the topic, so I'm going to try to ask the questions fresh with the basic goal of knowing whether a player has an active subscription or not, which never seemed to get answered.

    I think it's some combination of:
    • Product item.hasReceipt
    • SubscriptionInfo info.isSubscribed()
    • SubscriptionInfo info.isExpired()
    Knowing the up-to-date answer would be great.

    Areas of confusion include:
    • How do those 3 relate? Are isSubscribed and isExpired opposites?
    • What are default values for info.getRemainingTime() and info.getExpireDate() when isExpired = true or isSubscribed = false. Should either of the timechecks be used in the calculation of whether a player is actively subscribed?
    The basic solution here would make a great demo script for how to check a player's status.
     
  2. Livealot

    Livealot

    Joined:
    Sep 2, 2013
    Posts:
    228
    Since I had bad timing to originally post this about the same time as all of the API update problems, I'm hoping the dust has settled enough to bump and get a response to how to know when a player has an active subscription or not.

    Thanks!
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  4. Livealot

    Livealot

    Joined:
    Sep 2, 2013
    Posts:
    228
    After completing a bunch of pre-checks, I'm currently using SubscriptionInfo info.isSubscribed to determine whether the player is an active subscriber or not. Seemed the obvious choice, but then seeing isExpired in the docs made me wonder if there are scenarios where both isSubscribed and isExpired are both true. Because if that happens, I'm not sure how to define whether the player is an active subscriber or not.

    The common scenario I'm trying to solve for is where there are several different subscription lengths to get the same content (weekly, monthly, annual pass etc.). So I need to check to see if any of them are current. Here's what I cobbled together from reading prior posts.

    Code (CSharp):
    1.     private bool IsSubscriberActive() {
    2.  
    3.         //is the store initialized and available
    4.         if (IsInitialized()) {
    5.  
    6.             //get subscription info from store
    7.             foreach (var item in m_StoreController.products.all) {
    8.                 if (item.availableToPurchase) {
    9.  
    10.                     #if SUBSCRIPTION_MANAGER
    11.                     // this is the usage of SubscriptionManager class
    12.                     if (item.receipt != null) {
    13.                         if (item.definition.type == ProductType.Subscription) {
    14.  
    15.                             if (checkIfProductIsAvailableForSubscriptionManager(item.receipt)) {
    16.                                 Dictionary<string, string> introductory_info_dict = m_AppleExtensions.GetIntroductoryPriceDictionary();
    17.                                 string intro_json = (introductory_info_dict == null || !introductory_info_dict.ContainsKey(item.definition.storeSpecificId)) ? null : introductory_info_dict[item.definition.storeSpecificId];
    18.                                 SubscriptionManager p = new SubscriptionManager(item, intro_json);
    19.                                 SubscriptionInfo info = p.getSubscriptionInfo();
    20.  
    21.                                 if (info.isSubscribed() == Result.True) {
    22.                                     Debug.Log("The player has an active subscription, unlock the premium benefits");
    23.                                     return true;
    24.                                 }
    25.                             }
    26.                             else {
    27.                                 Debug.Log("This product is not available for SubscriptionManager class, only products that are purchase by 1.19+ SDK can use this class.");
    28.                             }
    29.                         }
    30.                         else {
    31.                             Debug.Log("the product is not a subscription product");
    32.                         }
    33.                     }
    34.                     else {
    35.                         Debug.Log(item.definition.id + "does not have a valid receipt");
    36.                     }
    37.                     #endif
    38.                 }
    39.             }
    40.         }
    41.         else {
    42.             Debug.Log("WARNING: Trying to check membership before store has initialized");
    43.         }
    44.  
    45.         //if get here, the player is not an active subscriber
    46.         return false;
    47.     }
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  6. Livealot

    Livealot

    Joined:
    Sep 2, 2013
    Posts:
    228
    All my subscriptions are auto-renewing, so I wasn't focused on Expired. To me, I only care whether the sub is active or not. For the docs, it would be helpful to see the flow for how a single user moves from one state to another through their lifecycle (e.g., trial, subscribed, canceled, expired, etc.) and I think there's something about a grace period that might throw curveballs to consider as well, especially for auto-renew where the credit card has expired.
     
    JeffDUnity3D likes this.