Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Codeless IAP not restoring purchase on android

Discussion in 'Unity IAP' started by onurduha, Mar 19, 2019.

  1. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    I'm testing purchases. it's not restoring remove ads. when I click to buy again, I'm not getting any response and it's not restoring purchase. it's in alpha test, is this happening because of alpha. I'm saving to cloud I think it will not be problem but I wanna know why it's not restoring purchase.

    This is the only script I have for purchases

    Code (CSharp):
    1.  public void OnPurchaseCompleted(Product product)
    2.     {
    3.         if(product != null)
    4.         {
    5.             switch (product.definition.id)
    6.             {
    7.                 case "go50.ld":
    8.                     PlayerPrefs.SetInt("Gold", PlayerPrefs.GetInt("Gold") + 50);
    9.                     GoldText.text = PlayerPrefs.GetInt("Gold").ToString();
    10.                     CloudVariables.ImportantValues[3] = PlayerPrefs.GetInt("Gold");
    11.                     PlayGamesController.Instance.SaveCloud();
    12.                     Debug.Log("Completed");
    13.                     break;
    14.                 case "gold.100":
    15.                     PlayerPrefs.SetInt("Gold", PlayerPrefs.GetInt("Gold") + 100);
    16.                     GoldText.text = PlayerPrefs.GetInt("Gold").ToString();
    17.                     CloudVariables.ImportantValues[3] = PlayerPrefs.GetInt("Gold");
    18.                     PlayGamesController.Instance.SaveCloud();
    19.                     Debug.Log("Completed");
    20.                     break;
    21.                 case "gold.250":
    22.                     PlayerPrefs.SetInt("Gold", PlayerPrefs.GetInt("Gold") + 250);
    23.                     GoldText.text = PlayerPrefs.GetInt("Gold").ToString();
    24.                     CloudVariables.ImportantValues[3] = PlayerPrefs.GetInt("Gold");
    25.                     PlayGamesController.Instance.SaveCloud();
    26.                     Debug.Log("Completed");
    27.                     break;
    28.                 case "gold.500":
    29.                     PlayerPrefs.SetInt("Gold", PlayerPrefs.GetInt("Gold") + 500);
    30.                     GoldText.text = PlayerPrefs.GetInt("Gold").ToString();
    31.                     CloudVariables.ImportantValues[3] = PlayerPrefs.GetInt("Gold");
    32.                     PlayGamesController.Instance.SaveCloud();
    33.                     Debug.Log("Completed");
    34.                     break;
    35.                 case "gold.1000":
    36.                     PlayerPrefs.SetInt("Gold", PlayerPrefs.GetInt("Gold") + 1000);
    37.                     GoldText.text = PlayerPrefs.GetInt("Gold").ToString();
    38.                     CloudVariables.ImportantValues[3] = PlayerPrefs.GetInt("Gold");
    39.                     PlayGamesController.Instance.SaveCloud();
    40.                     Debug.Log("Completed");
    41.                     break;
    42.                 case "remove.ads":
    43.                     PlayerPrefs.SetInt("Remove", 1);
    44.                     Destroy(AdManager.Instance.gameObject);
    45.                     CloudVariables.ImportantValues[5] = PlayerPrefs.GetInt("Remove");
    46.                     PlayGamesController.Instance.SaveCloud();
    47.                     break;
    48.                 default:
    49.                     Debug.Log("Failed");
    50.                     break;
    51.             }
    52.         }
    53.     }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    IAP Restore on Android only occurs when you reinstall the game. And you'll need an IAP Listener if your IAP buttons are not on your opening scene. The Sample project here shows how to use an IAP Listener https://forum.unity.com/threads/sample-iap-project.529555/ However keep in mind that this example could be improved and has the Listener and buttons on the same scene, so you might expect to see two transactions for Restore.
     
    onurduha likes this.
  3. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    IAP listener worked, but let's say I bought 50gold it gives me 100gold I think this two transaction problem happened. how can I fix it I just created a IAP listener and I put my purchaser script to OnPurchaseComplete field
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Try to reproduce with additional products. And you mention "let's say", are you assuming this will happen, or actually does? 50Gold is typically not a non-consumable. Only non-consumables are restored.
     
  5. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    It was working fine before I add the IAP listener but restore was not working so you said add IAP listener so I add the IAP listener and now if I buy a consumable it gives me double like I bought 50gold and it gives me 100gold
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, you don't want to have the Listener on the same scene. It would not give you a separate product, but you are likely just seeing the same purchase twice for 50gold. Be sure to use Debug.Log to confirm.
     
  7. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    Ok, Im little confused so Im gonna put some images so that you can see whats going on.

    IAP buttons and IAP listener they are in the same scene

    IAP button and IAP listener are using the same script you can see the script above
    probably it's happening because of both are using the same script but I don't know what im gonna code for IAP listener


    IAPbutton.png



    IAPlistener.png console.png HY.png purchaser.png
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Correct, you don't want the Listener and the IAP Buttons on the same scene, you'll get the callback twice. You could use a separate method for the Listener, and/or set a Boolean flag or similar if you want to keep them on the same scene. However, if you continue to have issues with Codeless, I might recommend the scripting approach. The Sample project also has a scripting example.
     
  9. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    I fixed the problem, I created a function for IAPlistener. IAPlistener and IAP button on the same scene, but it's working. but something wrong when I launched the app IAPlistener is working its calling the function I don't know if that's a problem. If you think this will cause problem, how can I fix it. or is it okay to call the function every time

    This is the code

    Code (CSharp):
    1.     public void IapListenerPurchaseComplete(Product product)
    2.     {
    3.         if (product.hasReceipt)
    4.         {
    5.             switch (product.definition.id)
    6.             {
    7.                 case "remove.ads":
    8.                     PlayerPrefs.SetInt("Remove", 1);
    9.                     Destroy(AdManager.Instance.gameObject);
    10.                     CloudVariables.ImportantValues[5] = PlayerPrefs.GetInt("Remove");
    11.                     Debug.Log("Restore Successful");
    12.                     break;
    13.                 default:
    14.                     Debug.Log("Failed");
    15.                     break;
    16.             }
    17.         }
    18.     }
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    The Listener should not be firing every time you launch. Only if you reinstall, or have a product left in Pending state. But if it's working for you in your testing, then I guess it should be OK. Be sure to test with another user account, and test with other non-consumables.
     
  11. AndrewCooper69

    AndrewCooper69

    Joined:
    Jun 13, 2021
    Posts:
    11
    Hello! I am having a similar problem with this board, and am wondering if you can help.

    My non-consumable purchases are not restoring on reinstall for Android.
    We have 2 non-consumable items - a "Remove Ads" button, and an unlockable level.
    The Codeless IAP buttons are inactive on start, and become active when their UI is activated.
    I have tried adding an IAP listener that is always active, however this doesn't seem to do anything.
    I have been able to get them restored through code, however this prevents the Codeless buttons from initializing properly and then the IAP buttons stop working.
    Right now, I can get the buttons working Codeless but they do not restore content on reinstall, or I can use code to get them to restore but the buttons stop working.

    Any help is appreciated, thank you! For instance, do the IAP buttons need to be active (and perhaps off-screen) at the application launch?
     
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Correct, you don't want to mix Codeless with Scripted IAP, that would result in multiple initializations. I would recommend that you not use an IAP button but instead use Scripted IAP. I actually find it easier to use, and is more flexible. It doesn't require a listener. Also Codeless doesn't support receipt validation which is helpful to reduce fraudulent purchases. We are working on an update to Codeless IAP in a future release next year. Here is a Sample Scripted IAP Project that you can start with. It uses regular UI buttons that you could modify https://forum.unity.com/threads/sample-iap-project.529555/#post-6950270
     
  13. AndrewCooper69

    AndrewCooper69

    Joined:
    Jun 13, 2021
    Posts:
    11
    Thank you so much for your quick response! I really appreciate it! I think you are right, I will need to redo the IAPs as scripted instead of Codeless. Thank you so much!!
     
  14. AndrewCooper69

    AndrewCooper69

    Joined:
    Jun 13, 2021
    Posts:
    11
    Hello again! Sorry to bother, but I am trying to set this up and I feel as though there may be an easier method than switching to scripted IAPs. The Codeless ones populate the price on their own which I would really like to keep.

    I believe the issue I am having is that when the app starts, the IAP buttons are inactive as they are not on the main menu UI, so they aren't being restored properly on Android as it searchers for active IAP buttons and doesn't find any.
    Is it possible to either:
    a) Move the buttons off screen, so they are active at all times offscreen and move into place when their menu is opened?
    b) Get a callback from when the "Restore" (ProcessPurchase) function completes, to deactivate the buttons when the purchases are restored?

    If so, please let me know if you have ideas on how to do it! I am trying to move the buttons offscreen at the moment, but can't seem to get their transform position like I can with images.

    Thank you!!
     
  15. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, don't use IAP buttons. Use UI buttons like in the sample project. ProcessPurchase is a callback, you don't need anything else. Put your button activate logic in ProcessPurchase. But more simply, do you have the Sample IAP Project working, with no changes (except a new ProjectID)? Get the basics working first, then add your customizations.
     
  16. AndrewCooper69

    AndrewCooper69

    Joined:
    Jun 13, 2021
    Posts:
    11
    Hello again,
    What about if I wanted to use Codeless IAPs? If one wants to use Codeless IAPs, is there a way to restore purchases on Android if the buttons are inactive on first launch? i.e., move the buttons offscreen and keep them active, or to time when the ProcessPurchase runs?
    Thank you!
     
  17. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    This was already answered above by using the Codeless Listener. But don't use Codeless, it has some serious issues. To fix them, you would need to write code that is probably more complicated than simply switching to Scripted. You have a working Scripted IAP project that I wrote for you, start there!
     
  18. AndrewCooper69

    AndrewCooper69

    Joined:
    Jun 13, 2021
    Posts:
    11
    Yes thank you for your help, I was just looking for a quick fix as we are about 5 days away from release on a game that's been in development for 6 months. Was hoping for a quick fix, but looks like scripting it is the way to go. Thank you!
     
  19. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    When you move to scripted IAP, remember to remove all your products from the IAP Catalog, and remove all IAP Buttons.
     
  20. GerorgeCotuna

    GerorgeCotuna

    Joined:
    Feb 10, 2020
    Posts:
    2
    Hello Jeff, im having the same issue. I use codeless IAP. What do you mean by : "Yes, don't use IAP buttons. Use UI buttons like in the sample project. ProcessPurchase is a callback, you don't need anything else. Put your button activate logic in ProcessPurchase" ?
    In my game the first scene is a logo scene that will load automatically the main menu scene that have the non active iap button. If i put an IAP button in the logo scene with the same product as the one in the main menu scene that will be active and non interactable and it will be behind the image of the logo so it will not show and also it will have a different method that the one that completes the other purchase in main menu scene(i only need to save a file), will restore the product after i reinstall the game? (if i understood the topic correctly , im sorry for my bad english).
     
  21. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Do not use Codeless IAP or IAP Buttons. Instead just use a regular Unity button and use scripted IAP like in the Sample project. Codeless IAP has issues that we need to address.
     
    GerorgeCotuna likes this.
  22. Covetousrat

    Covetousrat

    Joined:
    Jun 15, 2021
    Posts:
    4
    For those who are using Codeless IAP for (Non Consumable) product, this problem can be solved by adding 'IAP listener' to your IAPManager's inspector. Use the same 'On Purchase Complete' and 'On Purchase Failed' as your IAP button.

    A lot of people are facing problem where Nonconsumable are not being restored when reinstall app as there are no Direct answer around. This happens because the most of the time, the IAP button is not being active at MainScene.

    Hope this helps people in the future.
     
    MieluShaun likes this.