Search Unity

Restoring a purchase on Android

Discussion in 'Unity IAP' started by emergki, Jun 1, 2018.

  1. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Hi,

    I've added the Unity IAP on my project and, I'm selling goodies on my game, anyway, how can I check a purchased product (non-consumable) and make it "enabled" for the user after he closes and open the game again?
    (I mean for example, how can I check the purchased products when the game is launched?)
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You will want to track purchases separately, such as storing them in PlayerPrefs. Be sure to test the uninstall/reinstall scenario.
     
  3. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Hi Jeff,

    Yes, I need to check it, anyway, I'm trying this to check if the product is already purchased every time the scene is loaded, please check this code:

    Code (CSharp):
    1.  
    2. public static string prod01 = "prod01";
    3.     public static string prod02 = "prod02"; //Is the same string as in GooglePlay (I'm not showing the real name here on the Forum)
    4. private IEnumerator checkProduct(string prodId)
    5.     {
    6.  
    7.         yield return new WaitForSeconds(1);
    8.  
    9.         Product cProduct = m_StoreController.products.WithID(prodId);
    10.         if(cProduct != null && cProduct.hasReceipt)
    11.         {
    12.             //Already purchased?
    13.             if(prodId == prod01)
    14.             {
    15.                 PlayerPrefs.SetInt("Prod01",1);
    16.              
    17.             }
    18.             else if(prodId == prod02)
    19.             {
    20.                  PlayerPrefs.SetInt("Prod02",1);
    21.              }
    22.         }
    23.     }
    And, after it, I run a verification for that PlayerPrefs to check wich produtcs is "1" then I release it to be choosen by the user.

    I really don't know how can I check the products already purchased in the "automatic" check by Unity ADS. Can you point me in the right direction?
     
    Last edited: Jun 1, 2018
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    During the original purchase, ProcessPurchase is called. At this point, you will want to persist locally the purchase information. We only return the receipt on the original purchase. This way, you don't need to check each time with the store controller like you have above, just check PlayerPrefs.
     
  5. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Yes Jeff, the problem I'm having is because some users uninstall the game and now install the game again, so, in this case, the "PlayerPrefs" is clean, so, what can I do for these users? I passed the last 3 hours making refunds for the users that can't restore their purchases.

    In what method I can check about the purchased products?
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  7. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Yes, I know that I can do it, I already save the info on PlayerPrefs... My question now is:

    How can I know who already purchased the product and who don't ?, so I can save the information for the customers that had purchased the products.
     
    Last edited: Jun 1, 2018
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    For privacy and legal reasons, you can't go back and find out who purchased what products afterwards. You would need to save that information when a user makes the initial purchase. Those articles describe the way you can persist purchases even if the user reinstalls (which is preferred to storing in PlayerPrefs, as you've found out). We don't currently offer inventory management, but is a feature we are discussing.
     
  9. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Take a look

    https://docs.unity3d.com/Manual/UnityIAPRestoringTransactions.html

    On platforms that support it (e.g. Google Play and Universal Windows Applications) Unity IAP automatically restores any products the user owns during the first initialization following reinstallation; the ProcessPurchase method of your IStoreListener will be called for each owned item.

    This is what I want! restores all products... Don't want to know exactly "who" is the person that had bought the things, just want to release the product for the ones that had purchased the products 2 days ago, for example.
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Are you not getting the ProcessPurchase callback for each owned product upon first launch after reinstallation? Can you post the code of what you have for ProcessPurchase?
     
    emergki likes this.
  11. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Code (CSharp):
    1. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    2.     {
    3.         if (String.Equals(args.purchasedProduct.definition.id, prod01, StringComparison.Ordinal))
    4.         {
    5.             PlayerPrefs.SetInt("Prod01",1);
    6.         }
    7.         else if (String.Equals(args.purchasedProduct.definition.id, prod02, StringComparison.Ordinal))
    8.         {
    9.             PlayerPrefs.SetInt("Prod02",1);
    10.         }
    11.         else
    12.         {
    13.             Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
    14.         }
    15.         return PurchaseProcessingResult.Complete;
    16.     }
     
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446