Search Unity

Restore Transactions via Codeless IAP

Discussion in 'Unity IAP' started by Kurius, Jun 23, 2017.

Thread Status:
Not open for further replies.
  1. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    This is an informational post for people who are using Codeless IAP and you want to restore purchases of "non-consumable" and "subscription" items with minimal coding involved on Android, iOS, and Facebook WebGL with "Simple Hosting" and "Payments Lite".
    1. With Codeless IAP, Unity IAP does not get initialized until an "IAP Button" is displayed in a scene. Therefore transactions won't get restored until an IAP Button is displayed in a scene.
    2. In the first scene of your game, hide an IAP Button off screen, but keep it active. Set it's Y value to like negative 5000 to hide it.
    3. In the Inspector, configure that hidden IAP Button such that:
      1. Product ID is <None>
      2. Button Type is <Purchase>
      3. OnPurchaseComplete is assigned your usual item provisioning custom code
    4. Modify the "IAPButton.cs" script found at Assets -> Plugins -> UnityPurchasing -> script
      1. Replace the "ProcessPurchase" function found at line 263 with the following instead...
      2. Code (CSharp):
        1. public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e)
        2. {
        3.    var lastButton = activeButtons[activeButtons.Count-1];
        4.    foreach (var button in activeButtons) {
        5.        if (button.productId == e.purchasedProduct.definition.id) {
        6.            return button.ProcessPurchase(e);
        7.        }else if (button.Equals(lastButton)){
        8.            return lastButton.ProcessPurchase(e);
        9.        }
        10.     }
        11.     return PurchaseProcessingResult.Complete;
        12. }
      3. The new variable "lastButton" represents our hidden IAP Button and calls its OnPurchaseComplete code that you specified in the Inspector.
    5. When your game is launched on an Android device, or in a browser for a Facebook WebGL game, Unity IAP is automatically initialized and automatically scans for "non-consumable" and "subscription" purchases. It then calls the OnPurchaseComplete code in your hidden IAP Button repeatedly for each purchased item it finds. Your OnPurchaseComplete code provisions each item to the user.
    6. Alternatively when your game is launched on an iPhone device, Unity IAP is automatically initialized because of your hidden IAP Button, however it does not automatically scan for purchases. Instead you need to present the user with another Unity IAP button whose Button Type is <Restore>.
      1. Upon the user clicking the Restore button, the Restore() method on line 97 is called in the "IAPButton.cs" script.
      2. This in turn calls the "ProcessPurchase" function on line 263.
      3. This in turn calls the OnPurchaseComplete code in your hidden IAP Button repeatedly for each purchased item it finds. Your OnPurchaseComplete code provisions each item to the user.
    That's it, enjoy!
     
    Last edited: Jun 23, 2017
  2. vinnieg

    vinnieg

    Joined:
    Jan 20, 2013
    Posts:
    4
    Thank You!
     
  3. beta4attack

    beta4attack

    Joined:
    Jun 3, 2017
    Posts:
    3
    Could you please provide an item provisioning custom code example? I would really appreciate it, hope I'm not bothering you :)
     
  4. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    @beta4attack regarding item provisioning custom code, create a new C# script containing the following...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Purchasing;
    3.  
    4. public class myCodelessIAPscript : MonoBehaviour
    5. {
    6.     public void myOnPurchaseCompleteHandler (Product purchasedProduct){
    7.         Debug.Log("RESTORING ITEM: "+purchasedProduct.definition.id);
    8.         if (purchasedProduct.definition.id.CompareTo ("myItem1") == 0) {
    9.             PlayerPrefs.SetInt ("has_myItem1", 1); //true
    10.         } else if (purchasedProduct.definition.id.CompareTo ("myItem2") == 0) {
    11.                 PlayerPrefs.SetInt ("has_myItem2", 1); //true
    12.         }
    13.         PlayerPrefs.Save ();
    14.     }
    15.  
    16.     public void myOnPurchaseFailedHandler (Product failedProduct, PurchaseFailureReason failureReason){
    17.         Debug.Log("RESTORE FAILED FOR ITEM: "+failedProduct.definition.id+" BECAUSE: "+failureReason);
    18.     }
    19.      
    20. }
    21.  
     
    tarnumius and beta4attack like this.
  5. beta4attack

    beta4attack

    Joined:
    Jun 3, 2017
    Posts:
    3
    Thank you very much! I really appreciate it! :D
     
  6. vinnieg

    vinnieg

    Joined:
    Jan 20, 2013
    Posts:
    4
    I'm getting "Already recorded transaction". It's not calling my item provisioning code.
     
  7. zinnercircus

    zinnercircus

    Joined:
    Jul 13, 2013
    Posts:
    8
    How does this exactly tie into the IAP button? I'm a bit confused...? :) any help would be appreciated :)

     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    This is an older post. Codeless IAP now has a Restore button, and also provides an IAP Listener script that you can use for early initialization, located in the Unity Editor under Window/Unity IAP/Create IAP Listener.
     
  9. bariscigal

    bariscigal

    Joined:
    Oct 26, 2009
    Posts:
    46
    This may be old, but still relevant post though.

    On Android platform the restore button doesn't work (with a build warning that it doesn't work "on Android" You have to build it to see this warning)

    Instead as it is shown at the original post, when the button just instanced, it checks the previous purchases . (Like a auto restore at start)

    By the way i have been getting double purchase error because of this design. And luckily found this post and changed my code for android.

    So again this Post is old and it is "still relevant and working" (2018.2.3f1)
     
    Kurius likes this.
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Correct, the Restore button is for iOS only.
     
  11. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    Hello everyone,
    I have a problem. There is no "OnPurchaseComplete" method in the IAPButton script, there is only a "onPurchaseComplete" OnPurchaseCompletedEvent in the script. Where should I find this method, or how should I implement this?

    Edit: Never mind, OnPurchaseComplete of IAP Button can be seen in the Inspector.
     
    Last edited: Jun 13, 2019
  12. lyravr

    lyravr

    Joined:
    Dec 2, 2016
    Posts:
    2
    In case it's useful to anyone else, I was struggling to get purchases to restore automatically on Android. I had read that the service would check automatically if there are purchases to restore on startup, so I had a IAP Listener script in my main scene, where I assumed the service was being initialized. Turns out the service was actually being initialized right at the start in my loading scene - adding an IAP Listener to the loading scene solved it for me.
     
    DmSpurlock, TheFellhuhn and sama-van like this.
  13. TSR_Rajput

    TSR_Rajput

    Joined:
    Jul 1, 2018
    Posts:
    10
    Thank you for the IAP listener suggestion. I was not able to find out the issue, why IAP is not restoring in Android.

    But now I got the solution that to restore in codeless IAP, either the IAP buttons should be in the first Scene, or the IAP listener should be there in the first Scene.
     
    DmSpurlock likes this.
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I might suggest that you avoid Codeless for now, and use Scripted IAP instead. Codeless needs some work and does not support receipt validation for example, or deferred purchases. I would recommend instead that you start with this Sample IAP Project v3 https://forum.unity.com/threads/sample-iap-project.529555/#post-7922275
     
  15. TSR_Rajput

    TSR_Rajput

    Joined:
    Jul 1, 2018
    Posts:
    10
    Yeah, I am working on Scripted IAP. But as the release was near, I thought first to enable it with IAP codeless, and then finish Scripted IAP. Thanks for the sample.
     
  16. helloroy

    helloroy

    Joined:
    Jun 23, 2016
    Posts:
    42
    Seems the IAP 4.1.3 codeless IAP Button and IAP Listener haven't work with auto restore purchasing? I can only use custom script with UnityPurchasing.Initialize to get restore work.
     
  17. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    As was already mentioned https://forum.unity.com/threads/restore-transactions-via-codeless-iap.479251/#post-7957914 Keep in mind that restore only works with non-consumables and subscriptions, but not consumables. I will go ahead and lock this thread now as it has been fully answered.
     
Thread Status:
Not open for further replies.