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.
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.
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!
@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.
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?
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).
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.
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?
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?
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.
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).