Search Unity

[Solved] Handling Restore Purchases button click when user did not purchase

Discussion in 'Unity IAP' started by e_v, Nov 21, 2017.

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

    e_v

    Joined:
    Jul 14, 2016
    Posts:
    28
    My game has a single non-consumable. I’ve used the script included here https://unity3d.com/learn/tutorials/topics/ads-analytics/integrating-unity-iap-your-game as a starting point. I have two buttons: [Restore Purchase] and [Activate] (purchase). I want both buttons disabled when Restore Purchase is clicked to prevent more than one call while IAP is doing its thing. If the user has clicked it even though she never purchased the non-consumable, I want to leave it disabled and re-enable the Activate button. My understanding is that calling RestorePurchases() will eventually result in ProcessPurchase(PurchaseEventArgs args) being called if there were purchases to restore. But at what point do I know that there weren’t purchases to restore so I can respond accordingly?
     
  2. dayjur

    dayjur

    Joined:
    Sep 6, 2014
    Posts:
    128
    Are you asking to detect if on restore whether the user had purchased products to restore ?
     
  3. e_v

    e_v

    Joined:
    Jul 14, 2016
    Posts:
    28
    If the user had purchased products to restore, my code should run as though the user had just successfully purchased them: the buttons will go away and the purchased feature will be introduced. But if the user clicks Restore Purchase and there were no previously purchased products, is there a way for me to know that? If so, how and when? Do I want to check the value of result (below in the last view lines) somehow and respond to that?

    Code (CSharp):
    1. public void RestorePurchases()
    2.         {
    3.             // If Purchasing has not yet been set up ...
    4.             if (!IsInitialized())
    5.             {
    6.                 // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
    7.                 Debug.Log("RestorePurchases FAIL. Not initialized.");
    8.                 return;
    9.             }
    10.          
    11.             // If we are running on an Apple device ...
    12.             if (Application.platform == RuntimePlatform.IPhonePlayer ||
    13.                 Application.platform == RuntimePlatform.OSXPlayer)
    14.             {
    15.                 // ... begin restoring purchases
    16.                 Debug.Log("RestorePurchases started ...");
    17.              
    18.                 // Fetch the Apple store-specific subsystem.
    19.                 var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    20.                 // Begin the asynchronous process of restoring purchases. Expect a confirmation response in
    21.                 // the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
    22.                 apple.RestoreTransactions((result) => {
    23.                     // The first phase of restoration. If no more responses are received on ProcessPurchase then
    24.                     // no purchases are available to be restored.
    25.                     Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    26.                 });
     
  4. dayjur

    dayjur

    Joined:
    Sep 6, 2014
    Posts:
    128
    ok so when a user clicks restore. ProcessPurchase will be called on every nonconsumable product that had been purchased, as I understand it restoretransactions callback is on completion of restoration process and returns bool which indicates the 'restoration process was successful' that does not mean that there were products to restore it just indicates that the process was successful,
    so in restoretransaction((result) => if result true and processpurchase was not called then there were no products to restore
     
    codebaby2 and Bazz_boyy like this.
  5. e_v

    e_v

    Joined:
    Jul 14, 2016
    Posts:
    28
    Thanks, @dayjur . So I could have a member bool like "processPurchaseCalled", set it to false at the top of RestorePurchases, set it to true in ProcessPurchase, and check it like this?
    Code (CSharp):
    1. apple.RestoreTransactions((result) => {
    2.     if (result)
    3.     {
    4.         if (!processPurchaseCalled)
    5.         {
    6.             // No previous purchases to restore, so disable Restore button and re-enable Purchase button
    7.         }
    8.     }
    9.     else
    10.     {
    11.         // Don't know what to do here. Something went wrong with RestoreTransactions?
    12.     }
    13. });
    Sorry, I'm just typing this up and have not tested it.
    Any further guidance or insight is appreciated.
     
    Jbs_GameZone likes this.
  6. dayjur

    dayjur

    Joined:
    Sep 6, 2014
    Posts:
    128
    Yep you got it !
     
    e_v likes this.
  7. e_v

    e_v

    Joined:
    Jul 14, 2016
    Posts:
    28
    Thanks again for your help, @dayjur .
     
  8. dayjur

    dayjur

    Joined:
    Sep 6, 2014
    Posts:
    128
    No probs, I tested this other day on IOS and works fine
     
    e_v likes this.
Thread Status:
Not open for further replies.