Search Unity

[Closed] IAP: Checking for previously purchased non-consumables at start.

Discussion in 'Unity IAP' started by t_kaniatobe, Jan 16, 2016.

Thread Status:
Not open for further replies.
  1. t_kaniatobe

    t_kaniatobe

    Joined:
    Nov 19, 2014
    Posts:
    6
    I just set up the new IAP feature with Unity 5.3 . The old system I'm migrating from kept track of previously purchased non-consumables. Is there a built in feature with Unity IAP that manages this(Call RestorePurchases() on Start() for example). I could manage this myself with PlayerPrefs but just wanted to check if this is a feature already available.

    Thanks,
    -Tali
     
  2. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,264
    For Android, Unity IAP returns a list of purchased products after initialization. You can use that to set your PlayerPrefs or product states in your game.

    For iOS there is a RestoreTransactions method. But because Apple requires entering the user password after calling it every time, I wouldn't call it on app launch. The password field would immediately pop up every time.
     
    siddharth3322 likes this.
  3. tnbao91

    tnbao91

    Joined:
    Sep 5, 2013
    Posts:
    46
    What exactly return callback method? OnInitialized right?
     
  4. t_kaniatobe

    t_kaniatobe

    Joined:
    Nov 19, 2014
    Posts:
    6
    Thanks for the info Baroni!
     
  5. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
  6. Guilherme-Otranto

    Guilherme-Otranto

    Joined:
    Mar 12, 2014
    Posts:
    28
    Hi, I have a similar problem, maybe you can help.

    I need to be able to check if my user has a valid subscription...
    I can't just save to player prefs that he bought it because the subscription will cease to be valid after a while.

    Is there a "Does user have a valid subscription to XYZ" function?
    Many thanks!
     
    siddharth3322 likes this.
  7. tnbao91

    tnbao91

    Joined:
    Sep 5, 2013
    Posts:
    46
    @mpinol OnInitialized returns IStoreController with a list of products. How can I know which product (non-consumable) was purchased?
     
  8. chand81

    chand81

    Joined:
    Jun 2, 2011
    Posts:
    25
    >How can I know which product (non-consumable) was purchased?
    Product.hasReceipt (looking at IAPDemo.cs)


    IAPDemo.cs also has the following function:

    private void OnDeferred(Product item)

    From where is this called? - it is not part of IStoreListener...
    Also, does the listener have to be a MonoBehaviour?
     
    WILEz1975 likes this.
  9. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    @Guilherme Otranto Unity IAP will provide the latest receipt. We are shortly to release an update that will include a receipt validation library, which will help you in determining if a receipt is valid and current (ie the user owns the item). In the meantime you will need to use your own validation logic.

    @tnbao91 chand81 is correct. Look at Product.receipt.

    @chand81 This was fixed in the latest store package update, 1.2.1. Please reimport the IAP store package from the IAP cloud service window and take another look.

    Listeners do not have to be monobehaviours, the samples are shown that way for brevity. I would recommend implementing as a plain old c sharp class suitable for unit testing.
     
    tnbao91 likes this.
  10. Guilherme-Otranto

    Guilherme-Otranto

    Joined:
    Mar 12, 2014
    Posts:
    28
    Hey Banderous, thank you for the reply. I managed to get my code working with that in mind, but I've hit a wall:

    On my tests with iOS I purchased a subscription, saved to PlayerPrefs that "user has subscription" and after that I killed the application and started again.
    If the user is offline the Playerprefs kicks in and allows access, all is good.
    However, if he is online, the function OnInitialized is called with a list of products and the subscription comes without a receipt:
    m_StoreController.products.WithID(weekProductID).hasReceipt comes as false.

    I use that receipt to un-validate a subscription (I assume it expired), so this is very bad.
    After this, if I hit "restore transactions", it actually triggers a purchase (i.e, the subscription is still valid), but I cannot depend on the user doing that, since it will require pass and all that..

    I am doing this wrong? Counting on OnInitialize to come with a receit?

    Thanks again for the help!
     
  11. mshunko

    mshunko

    Joined:
    Jul 16, 2015
    Posts:
    4
    Is hasReceipt = false in follow cases for all platform:

    - subscription expired
    - subscription is not owned
    - subscipttion cancelled

    Yes it is?
     
  12. Guilherme-Otranto

    Guilherme-Otranto

    Joined:
    Mar 12, 2014
    Posts:
    28
    Hi mshunko,

    No, what I am seeing here is that:
    hasReceipt = false when called after initialization on subsequent plays after a subscription purchase (regardless of the state of the subscription)

    Meaning:
    I cannot assume that user no longer has a subscription because hasReceipt = false. Control over subscription needs to be achieved somewhere else, it seems (at least for now).
     
  13. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    iOS does not have per-product receipts; you get a single App receipt that encodes details of everything the player owns. Unity IAP can give you the App receipt which you should validate and parse according to Apple's documentation.

    Unity IAP cannot currently parse and validate Apple receipts for you, but this functionality will be available shortly in an upcoming store package update.
     
  14. Guilherme-Otranto

    Guilherme-Otranto

    Joined:
    Mar 12, 2014
    Posts:
    28
    Hi Banderous,

    Sorry, I didn't follow the recommended procedure... Ideally on iOS I should use the app receipt to see what the user owns, right?
    But is that receipt updated automatically? (if, say, a subscription ends)

    I see from the links you provided that I can refresh the receipt, but that would required the user to provide the password again...
    It's great to hear about the update, but for now do we have a recommended/ideal way to remove the subscription from a user that did not renew/canceled his?

    Many thanks!
     
  15. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
  16. farhanblu

    farhanblu

    Joined:
    Dec 29, 2014
    Posts:
    49
    Just confirming, if a user purchased a non-consumable and a subscription, then even when the subscription ends, hasReceipt will be set to true? And if no non-consumable/subscription is purchased, hasReceipt will be false? Previous consumables do not show in receipt?
     
  17. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    How long consumable receipts are available depends on the store provider; eg Apple provides them until receipts are refreshed.

    The general rule is that if you need to process or save consumable receipts you should return Pending from your ProcessPurchase method and only confirm the purchase when you have finished, thus ensuring they aren't lost.
     
    farhanblu likes this.
  18. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    I\ll join this convo as i have similiar problem

    Sometimes when testing offline, OnInitialized does not trigger. Thus i cant get list of available purchases. Is this a known bug? Unity 5.3.4f1.
     
  19. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @_watcher_,

    Because IAP requires a network to get the product information from the store, the method is likely timing out when testing offline. This is expected behavior and I would recommend having the code handle the situation of what to display in the app store when there is no data connection (and therefore no way to buy products anyways).
     
  20. RafaelAlcantara

    RafaelAlcantara

    Joined:
    Mar 30, 2016
    Posts:
    11
    And what about Windows Store? What do I need to retrieve the purchased non-consumables list?
     
  21. JayR

    JayR

    Joined:
    Aug 2, 2016
    Posts:
    84
    For Windows Store there is a recent change (in 1.9.3 and later) to support the case when users are not logged into the Store when starting the game. In that case it will not force a login but will also not be able to restore purchases until the user logs in. There is now a RestoreTransactions() which you can use in Windows Store apps to force a login and restore (see the sample code shipped with the plugin.) The plugin will also eventually restore transactions after the user logs in to make another purchase.

    Prior to 1.9.3 the Windows Store code would always restore purchases during the initialization process.
     
Thread Status:
Not open for further replies.