Search Unity

Question OnTransactionsRestored

Discussion in 'Unity IAP' started by ShinyOpal, Mar 13, 2023.

  1. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    This code was generated inside Visual Studio when implementing the Restore button for the IAP on iOS.

    public void OnTransactionsRestored(bool param1, string param2) {       
    }


    What are the meanings of the param1 and param2 ?

    If param1 is true, does it mean the transaction was restored successfully ?
     
  2. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,264
    ShinyOpal likes this.
  3. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    Apple requires users to be able to restore transactions in case e.g. they replace a device. Users should be able to restore all the transactions on the new device.

    Which would be the code snippet to verify if a specific product was already bought and restore it ? For codeless IAP.
     
  4. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    235
    Hello ShinyOpal,

    For Codeless, all you need to do is add a Restore Button to handle the restore transactions.

    There's currently no way to verify if a specific product was already bought and restore it.
    The restore transactions button will restore transactions for every products.
     
    ShinyOpal likes this.
  5. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    Just to clarify, restore purchases for ONLY non-consumable?
     
  6. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    When a user reinstalls your application they should be granted any Non-Consumable or renewable Subscription products they already own. App stores maintain a permanent record of each user’s Non-Consumable and renewable Subscription products which Unity IAP can retrieve. Non-renewing subscriptions on Apple platforms cannot be restored. If you use non-renewing subscription products on Apple platforms, it is up to you to keep a record of the active subscriptions and sync the subscription between devices.

    https://docs.unity3d.com/2018.1/Documentation/Manual/UnityIAPRestoringTransactions.html
     
    nobluff67 likes this.
  7. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    Was not expecting this behaviour. There should be a way to know which product was restored. This would be needed in order to enable certain features of the app, just as if the user bought the app. Right now this is not possible. In my case I would need it for the non-consumable product. Can't Unity create another OnTransactionsRestored where the list of the restored products is received as input ? That would be the expected solution.
     
  8. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    235
    The restored transactions are treated the same as a regular transaction. These will all make their way to your ProcessPurchase the same way as if the user bought the app.

    Would this be sufficient for your case, where you receive your restored non-consumable during the ProcessPurchase and can then enable the features if it's a valid restored purchase?
    If not, could you provide more details so we can help you better with your case?

    The reason this isn't done is because we lack information about what is part of the restore transaction and we don't know when Apple is done sending restored transactions since these are all received by the same transaction observer. We will investigate this to see if we can find a better solution.
     
  9. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    Are you trying to say to add the OnPurchaseComplete(Product product) on the Restore button instead of the OnTransactionsRestored(bool param1, string param2) ? If this functions properly, that could be the solution.

    It the user did not buy the product and clicks on the restore button, the OnPurchaseComplete will not be invoked, right ?
     
  10. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    235
    That is correct, OnPurchaseComplete will be invoked when restoring the product if it was purchased previously and it is still active.

    For the products that were not bought, these won't invoke the OnPurchaseComplete. Apple won't send anything for those since there was no purchase.
     
    ShinyOpal likes this.
  11. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    Just tried it out. Unfortunately it is not possible to assign the OnPurchaseComplete to the Restore button. Unity does not display it in the list of methods as it does with the Purchase button.
     
  12. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    235
    The OnPurchaseComplete from the IAPButton and IAPListener will be called when the transaction is restored, the same way a regular purchase is made.
     
    ShinyOpal likes this.
  13. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    That means that a solution via the UI is not possible. Attached you can also find that the Unity Editor does not show OnPurchaseComplete in the UI, even though it exists in the script.

    That would mean that a programmatic solution needs to be done.
    I can access the restore button from the script
    Code (CSharp):
    1. public IAPButton restoreButton;
    The restore button has the following property
    Code (CSharp):
    1. [Tooltip ("Event fired after a successful purchase of this product.")]
    2.     public OnPurchaseCompletedEvent? onPurchaseComplete;
    Now the question is how to assign it ? Do you have a code sample ? Should I create a class which inherits from the OnPurchaseCompletedEvent ? How should this class look like ?

    Maybe adding the event listener on the
    Code (CSharp):
    1. restoreButton.onPurchaseComplete.AddListener(OnPurchaseComplete)
    The OnPurchaseComplete is the existing method also used by the Buy button.
     

    Attached Files:

    Last edited: Apr 18, 2023
  14. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    235
    You want to use the On Purchase Completed from either the IAP Buttons or the IAP Listener (see screenshots attached).

    This is the same that is being used for a regular purchase, so there shouldn't be anything else to add for the restore transaction to work.
     

    Attached Files:

    ShinyOpal likes this.