Search Unity

How does the "availableToPurchase" property work?

Discussion in 'Unity IAP' started by leynier41, May 24, 2019.

  1. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    Subscription: Should return false if you are not subscribed, either because the subcription was never made or a past subscription was canceled and the expiration time passed.

    NonConsumables: Should return true if the purchase was not made and false otherwise, since this type of products can only be purchased once.

    Consumables: Should always return true since you can always buy products of this type.

    Everything I say is correct? If something is wrong, can you give me the explanation?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    No, availableToPurchase is more general than that. It's not specific to a user. It just means that the products are indeed configured properly. You need to track purchases yourself, such as saving to PlayerPrefs.
     
  3. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    Is the information of the subscription type products specific to a user? And how do I know if the user has the subscription?
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You mean during ProcessPurchase? Then yes. If/when the user makes a purchase, you can write the information to PlayerPrefs.
     
  5. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    What I want to say is that if the methods of the SubscriptionInfo class as isSubscribed provide information about the current user's subscription? And how to know if a user has any purchased subscription?
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, I understand your question. Once they make their first purchase, you could write the information to PlayerPrefs. For example code for SubscriptionInfo, please review the IAPDemo.cs
     
    Last edited: May 28, 2019
  7. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    Thank you. I was reading the documentation and I found the hasReceipt and receipt properties of the Product class, which according to the documentation:

    hasReceipt: Owned Non Consumables and Subscriptions should always have receipts.

    receipt: The purchase receipt for this product, if owned. Otherwise null.

    So, my question is: Can I know if the product was purchased with these properties?
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes you can. Please see the IAPDemo.cs code, it uses the SubscriptionInfo class in OnInitialized. If no receipt, no prior purchase.
     
  9. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    I want to know if a NonConsumable or Subscription type product is active.

    Non-consumable product: if receipt != null then the product is purchased by the user.

    Susbcription product: if receipt != null and subscriptionInfo.isExpired () == Result.False then the product is acquired by the user.

    Code (CSharp):
    1. if (product.receipt == null)
    2.     return false;
    3. if (product.definition.type != ProductType.Subscription)
    4.     return product.definition.type == ProductType.NonConsumable;
    5. var introductory_info_dict = storeManager.appleExtension.GetIntroductoryPriceDictionary();
    6. var intro_json = (introductory_info_dict?.ContainsKey(product.definition.storeSpecificId) ?? false)
    7.     ? introductory_info_dict[product.definition.storeSpecificId] : null;
    8. var info = (new SubscriptionManager(product, intro_json)).getSubscriptionInfo();
    9. return info.isExpired() == Result.False;
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry, did you have a question? Is this working for you?
     
  11. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    In Android it work fine, but in iOS not is correct. Why? What should I do?
     
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is not correct? Please be specific. Have you tested by making a subscription purchase in TestFlight?
     
  13. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    I configure the products in the AppStore, then I compile an application for iOS and upload it to the store in production mode (I do not use anything from TestFlight or another service to test the app).

    Then, in the application if the purchases are made correctly. But when it is going to verify if the user has any acquired subscription (be active and canceled) does not work (it is not detected that the user has made the subscription).
     
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I might suggest that you never release an app to live production without first testing it fully in TestFlight. Your test users are not actually charged against their credit cards.
     
  15. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    Thanks for the suggestion. So, for iOS we have to do a different check apparently. What should we do?
     
  16. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Be sure to test for both expired and cancelled subscriptions, and make note of the receipt properties for each during your testing. Also, you'll want to implement inventory management, such as using PlayerPrefs, to track user purchases, as mentioned previously.