I am trying to make a NonConsumable IAP purchase persist without internet connection.

I have implemented the IAP system from Unity 5.6 through Google Play for a NonConsumable item and it is working. From a previous question I learned how to avoid using Unity's PlayerPrefs to remember purchases in favor of checking the receipt from the Unity storeController that initializes with Unity's API code below.

   public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
            this.controller = controller;
            this.extensions = extensions;

What I have observed is that the OnInitialized will run without internet connection as long as it has recently been initialized. This seems to be cached somewhere within Android because clearing the cache on my app will not force the purchase to be forgotten. The problem is after about half a day without internet connection the controller cannot initialize. Therefore I cannot check the item receipt in the controller with the code below.

Product product = storeController.products.WithID(productId);
 if (product != null && product.hasReceipt)
     // Owned Non Consumables and Subscriptions should always have receipts.
     // So here the Non Consumable product has already been bought.
     itemBought = true;

What strategy or method should I use to persist a NonConsumable purchase during times without internet connection as it is recommended to not use Playerprefs on numerous guides.

  • 1
    \$\begingroup\$ You're running on an Android device? You should have access to write files to a (private) directory on the device. You can "safely" save any data you want there. It's more secure than PlayerPrefs because you can encode the data in binary, encrypt it, or just obfuscate the hell out of it, unlike with PlayerPrefs which stores values in plain text. Ostensibly it's still not 100% secure, but as long as you're attempting to validate with the server first you should be fine. \$\endgroup\$ – Draco18s no longer trusts SE Apr 25 '17 at 14:55
  • \$\begingroup\$ As a note on the research I did, I found one forum that said that the behavior I noted above is a bug in Unity's IAP system, but no Unity moderator confirmed that. \$\endgroup\$ – Tartle Wizard Apr 25 '17 at 14:58
  • \$\begingroup\$ Possible. I haven't messed with it yet myself. \$\endgroup\$ – Draco18s no longer trusts SE Apr 25 '17 at 15:02
  • \$\begingroup\$ How is this approach supposed to work in terms of who owns the purchase? In other words, a purchase is associated with an account, so if you store a file locally to indicate the purchase has been made, then multiple accounts can make use of the purchase even though they didn't actually buy the product.. \$\endgroup\$ – Maria Nov 3 '17 at 10:05
  • \$\begingroup\$ Another concern is refunds. If an account refunds a product, will the hasReceipt become false automatically (as long as the device is not offline) when the refund takes place? \$\endgroup\$ – Maria Nov 3 '17 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.