Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Catch which purchase had been restored

Discussion in 'Unity IAP' started by iboshido, Mar 24, 2021.

  1. iboshido

    iboshido

    Joined:
    Mar 4, 2017
    Posts:
    33
    Hey guys,

    i have one non consumable item in my game which can be purchased, the no Ads purchase. I am using Codeless IAP and with that an IAP Button. My code looks like this:

    Code (CSharp):
    1. public void OnPurchaseComplete(Product product)
    2.     {
    3.         if (product.definition.id == noAdsID)
    4.         {
    5.             PlayerPrefs.SetInt("isForcedAdsBought", 1);
    6.         else
    7.         {
    8.             Debug.Log("No product with ID: " + product.definition.id + " found");
    9.         }
    10.     }
    11.  
    12.     public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
    13.     {
    14.         Debug.Log("Product with ID " + product.definition.id + "threw following error" + reason);
    15.  
    16.     }
    In the inspector of my IAP Button i made references to the code above.

    Everything works fine, but i dont know how to handle a reinstall. Cuz all the PlayerPfrefs will be gone but google Playstore automatically restores the purchases. And Apple app store restores after the restore button had been pressed.

    My question, how do i know if any purchases had been restored? I would like to be able to catch those informations and store them in the PlayerPrefs again.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    @iboshido A Restore is just like a Purchase. ProcessPurchase is called for each product, write to PlayerPrefs at that time. Your code above should work. Please describe how your users are impacted during reinstall.
     
  3. iboshido

    iboshido

    Joined:
    Mar 4, 2017
    Posts:
    33
    Ohh ok now i understand. It works now. I removed the references of the functions above out of the inspector of IAP Button. I created an IAP Listener and referenced it in the inspector to the code above. Now every time a purchase is been made or the playstore automatically restores data, the Listener will catch those calls. Thank you for your help :D

    I haven't tested it on iOS yet, but when is a iOS user allowed to push the restore button? And how often can he push this button? Will my IAP Listener work with iOS restore button?
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, the Listener will work with the Restore button. Note that Restore is one of the IAP Button types. Good question on when you should show the Restore button, I did find this https://developer.apple.com/forums/thread/100554