Search Unity

Codeless IAP - How to restore purchases on Android?

Discussion in 'Unity IAP' started by atalkingfish, Sep 10, 2019.

  1. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    I have spent quite a while looking through other threads and documentation about codeless IAP, and I guess I'm at a loss as to how to actually restore purchases on Android when the IAP Buttons are not active when the game first launches.

    I understand that the restore button doesn't have functionality on Android (as per the documentation), but I'm not exactly sure what effective ways there are of restoring the purchases at all. Currently, the Android users in my internal testing get no response when trying to re-purchase themes they've already purchased. I would rather not have the buttons active when the game opens and the entire game takes place in once scene.

    I am getting the idea that IAP listeners are the way to go, but the documentation is so sparse on this that I really have not been able to find anything showing how they work, such as how to represent the various purchases needed (I'm working with non-consumables). Any guidances or help is appreciated.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I would suggest that you take a look at the Sample IAP projects here, the second one uses Codeless and uses a Listener. Is there a specific reason you are hesitant to use scripting? It is the preferred approach, Codeless is often used for prototyping and unfortunately is not very flexible. https://forum.unity.com/threads/sample-iap-project.529555/
     
  3. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    I'm not really looking for any sort of flexibility here, I just have some very basic purchases that (on iOS) work like a charm with Codeless IAP. The only issue I've run into so far at all is struggling to figure out how to restore purchases on Android. I would hate to rework the entire system just for that one thing. If restoring on Android is possible with Codeless IAP, I'd rather stick with that for this project.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Understood, then you will want to use a Listener as shown in the sample project. It will handle the Restore process.
     
  5. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    Sorry for the last response, I was very busy last week. I have taken a look at the Codeless IAP project to see how the listener works. When I load the project, the IAP has lost all of its unique attributes (the onPurchase field, etc) and just has a generic error: "The associated script cannot be loaded". This is a similar error to what has happened with me with purchase buttons when something gets messed up and I need redo them. Anyway, this isn't helping me see how to use IAP listeners. I also haven't been able to find any documentation as to how to set them up, what to put for the purchase event (especially when there is more than one purchase available in the game—in my case, I have three). Any help would be appreciated.
     
  6. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    Opening the project in a version closer to the project's version (2018 instead of 2019) fixed the above problem. However, I'm still unsure of how to actually use the listener. I see a game manager script, but I'm not really sure how to identify which specific purchases have been done and how to restore them when the rest of the project uses the buttons (which are de-activated at the game start-up)
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please refer to the sample IAP project on the forum as mentioned. Also please review the IAP Listener documentation here, it describes how to determine which product is being restored in the On Purchase Complete event https://docs.unity3d.com/Manual/UnityIAPCodelessIAP.html
     
  8. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    Sorry for my ignorance, but I must be missing something. What I see going on in the documentation + the sample project is this: By clicking on an IAP button, a function within a script is called, with any variable being sent into the function when the purchase is completed. In the case of the documentation, 500 coins are granted upon purchase, and the "500" int is sent into the GrantTokens function. With the sample project, I'm only seeing one function for a successful purchase and one function for a successful listener, neither of which take in any variables, and neither of which make any distinction between purchases.

    In the case of Listeners, what I'm confused about, and not seeing anything about in the documentation, is how they determine which purchases are fulfilled and which are not. For example, if I have 3 purchases, do I need 3 listeners? Even in the case of the documentation, how would I determine how many coins to give the individual if there are different types of purchases?

    In my case, these are non-consumable purchases, three of them—all of which are successfully executed with the IAP buttons—and I am having a hard time finding where in the documentation where Listeners get integrated, and the sample project only has a blank "Listener Successful" function, which doesn't really explain what I'm supposed to do in that function to actually determine which purchases are successful and which are not (say someone bought one of the three purchases on my end)

    I just must be missing something. Hopefully this post helps explain what I'm not understanding.
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    On Purchase Complete passes a Product object which your method would accept. So instead of GrantCredits() it would be GrantCredits(Product product) or similar.
     
  10. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    Okay, okay, so the IAP listener gets activated (in this case, when the app is first opened on Android), and it returns a Product. I'll have to do some testing to figure out exactly how that works, but if I could ask, if there are 3 purchasable items and the user has previously purchased 2 of them, how many times does this function get called? Once, returning some set of successful purchases; twice, once per successful restore; or three times, once per purchasable item (two successes and one fail)?
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    ProcessPurchase is called once per product, whether it's a first purchase or during restore. In your example, twice.
     
  12. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    Thank you, your help is much appreciated. I will work with this and see how it goes.