Search Unity

Is subscription expired- when app starts- codeless IAP

Discussion in 'Unity IAP' started by swifter14, Jul 23, 2019.

  1. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    I use codeless IAP.
    I would like to check a subscription is still valid when my app starts.
    In IAPDemo.cs file they reference the SUBSCRIPTION_MANAGER functions in OnInitialized(IStoreController controller, IExtensionProvider extensions) function.

    Where do I need to copy this code in my app? The only OnInitialized function I have is inside CodelessIAPStoreListener (which only works when the user open the store popup
    How do I call this function manually when my app starts?

    Code in IAPDemo-
    Code (CSharp):
    1.   public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    2.     {
    3.         m_Controller = controller;
    4.         m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
    5.         m_SamsungExtensions = extensions.GetExtension<ISamsungAppsExtensions>();
    6.         m_MoolahExtensions = extensions.GetExtension<IMoolahExtension>();
    7.         m_MicrosoftExtensions = extensions.GetExtension<IMicrosoftExtensions>();
    8.         m_UnityChannelExtensions = extensions.GetExtension<IUnityChannelExtensions>();
    9.         m_TransactionHistoryExtensions = extensions.GetExtension<ITransactionHistoryExtensions>();
    10.         m_GooglePlayStoreExtensions = extensions.GetExtension<IGooglePlayStoreExtensions>();
    11.         // Sample code for expose product sku details for google play store
    12.         // Key is product Id (Sku), value is the skuDetails json string
    13.         //Dictionary<string, string> google_play_store_product_SKUDetails_json = m_GooglePlayStoreExtensions.GetProductJSONDictionary();
    14.         // Sample code for manually finish a transaction (consume a product on GooglePlay store)
    15.         //m_GooglePlayStoreExtensions.FinishAdditionalTransaction(productId, transactionId);
    16.  
    17.         InitUI(controller.products.all);
    18.  
    19.         // On Apple platforms we need to handle deferred purchases caused by Apple's Ask to Buy feature.
    20.         // On non-Apple platforms this will have no effect; OnDeferred will never be called.
    21.         m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
    22.  
    23. #if SUBSCRIPTION_MANAGER
    24.         Dictionary<string, string> introductory_info_dict = m_AppleExtensions.GetIntroductoryPriceDictionary();
    25. #endif
    26.         // Sample code for expose product sku details for apple store
    27.         //Dictionary<string, string> product_details = m_AppleExtensions.GetProductDetails();
    28.  
    29.  
    30.         Debug.Log("Available items:");
    31.         foreach (var item in controller.products.all)
    32.         {
    33.             if (item.availableToPurchase)
    34.             {
    35.                 Debug.Log(string.Join(" - ",
    36.                     new[]
    37.                     {
    38.                         item.metadata.localizedTitle,
    39.                         item.metadata.localizedDescription,
    40.                         item.metadata.isoCurrencyCode,
    41.                         item.metadata.localizedPrice.ToString(),
    42.                         item.metadata.localizedPriceString,
    43.                         item.transactionID,
    44.                         item.receipt
    45.                     }));
    46. #if INTERCEPT_PROMOTIONAL_PURCHASES
    47.                 // Set all these products to be visible in the user's App Store according to Apple's Promotional IAP feature
    48.                 // https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StoreKitGuide/PromotingIn-AppPurchases/PromotingIn-AppPurchases.html
    49.                 m_AppleExtensions.SetStorePromotionVisibility(item, AppleStorePromotionVisibility.Show);
    50. #endif
    51.  
    52. #if SUBSCRIPTION_MANAGER
    53.                 // this is the usage of SubscriptionManager class
    54.                 if (item.receipt != null) {
    55.                     if (item.definition.type == ProductType.Subscription) {
    56.                         if (checkIfProductIsAvailableForSubscriptionManager(item.receipt)) {
    57.                             string intro_json = (introductory_info_dict == null || !introductory_info_dict.ContainsKey(item.definition.storeSpecificId)) ? null : introductory_info_dict[item.definition.storeSpecificId];
    58.                             SubscriptionManager p = new SubscriptionManager(item, intro_json);
    59.                             SubscriptionInfo info = p.getSubscriptionInfo();
    60.                             Debug.Log("product id is: " + info.getProductId());
    61.                             Debug.Log("purchase date is: " + info.getPurchaseDate());
    62.                             Debug.Log("subscription next billing date is: " + info.getExpireDate());
    63.                             Debug.Log("is subscribed? " + info.isSubscribed().ToString());
    64.                             Debug.Log("is expired? " + info.isExpired().ToString());
    65.                             Debug.Log("is cancelled? " + info.isCancelled());
    66.                             Debug.Log("product is in free trial peroid? " + info.isFreeTrial());
    67.                             Debug.Log("product is auto renewing? " + info.isAutoRenewing());
    68.                             Debug.Log("subscription remaining valid time until next billing date is: " + info.getRemainingTime());
    69.                             Debug.Log("is this product in introductory price period? " + info.isIntroductoryPricePeriod());
    70.                             Debug.Log("the product introductory localized price is: " + info.getIntroductoryPrice());
    71.                             Debug.Log("the product introductory price period is: " + info.getIntroductoryPricePeriod());
    72.                             Debug.Log("the number of product introductory price period cycles is: " + info.getIntroductoryPricePeriodCycles());
    73.                         } else {
    74.                             Debug.Log("This product is not available for SubscriptionManager class, only products that are purchase by 1.19+ SDK can use this class.");
    75.                         }
    76.                     } else {
    77.                         Debug.Log("the product is not a subscription product");
    78.                     }
    79.                 } else {
    80.                     Debug.Log("the product should have a valid receipt");
    81.                 }
    82. #endif
    83.             }
    84.         }
    85.  
    86.         // Populate the product menu now that we have Products
    87.         AddProductUIs(m_Controller.products.all);
    88.  
    89.         LogProductDefinitions();
    90.     }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I believe you would need to modify IAPButton.cs, but I would not recommend this for you. Instead, I would suggest to not use Codeless and go the scripted approach.
     
  3. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    That doesn't answer my question at all. What kind of answer is that?
     
    Last edited: Jul 23, 2019
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You don't want to modify the listener, you would want to modify IAPButton.cs instead. You could edit the listener, but you would need to make sure it loads on app start. But I would not recommend editing any codeless files and not something we would directly support, and the next IAP update would overwrite your changes. Instead, you will want to use IAP scripting, not codeless. Codeless offers basic implementation for small projects that don't require any customization, as you are requesting. For customization, you would be advised to use scripting. There is a sample project here to help you get started. The first project is scripting, the second is codeless. https://forum.unity.com/threads/sample-iap-project.529555/ I hope this clears it up!