Search Unity

Purchases not restored on Android - Error: You already own this item

Discussion in 'Unity IAP' started by farazk86, Apr 15, 2021.

  1. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Hi,

    I searched before posting and apparently Android is supposed to automatically restore purchases using the IAP listener but its not working in my case.

    So as the title says, I purchased a non-consumable and then uninstalled. The purchase is not restored when I re-install. When I click the buy button I get the error: "You already won this item"

    I am using IAP 3.0.1 and Unity 2019.4.23f1
    • I am populating the catalogue in Editor and set it to automatically initialize.
    image_2021-04-15_134256.png
    • I then created a Unity IAP button and am calling the following function in OnPurchaseComplete()
    Code (CSharp):
    1. public void OnPurchaseComplete(Product product)
    2.     {
    3.         if (product != null)
    4.         {
    5.             switch (product.definition.id)
    6.             {
    7.                 case "weapons_unlock":
    8.                     // YAY!! someone actually spent actual money on this lousy game!
    9.                     if (weaponUnlock != null)
    10.                     {
    11.                         weaponUnlock.UnlockAllWeapons();
    12.                     }
    13.                    
    14.                     //reload the scene to properly repopulate shop
    15.                     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    16.                     break;
    17.  
    18.                 case "disable_ads":
    19.                     PlayerPrefs.SetInt("ShowAds", 0);
    20.                     break;
    21.  
    22.                 default:
    23.                     print("Sorry, this is not a valid product.");
    24.                     break;
    25.             }
    26.         }
    27.     }
    I also adde an IAP Listener in my scene and provided the same function above to the OnPurchaseComplete()

    But the weapons are not being unlocked.

    What am I doing wrong?

    Thanks
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @farazk86 Can you show the Inspector properties for your IAP Button? Ensure to select the Consume checkbox (it is a poorly named checkbox). Also, I would encourage you to Debug.Log (print) your product.definition.id to ensure you are getting the right product. The "Already owned message" is correct for a non-consumable.
     
  3. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Hi Jeff, Thanks for the reply.

    The Consume checkbox is already checked. Here is the screenshot:

    image_2021-04-15_154220.png
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Can you show your code for IAPManager? Attach it here, It looks like you might be mixing Codeless and Scripted IAP.
     
  5. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Here is the full code for IAPManager

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.Purchasing;
    6.  
    7.  
    8. public class IAPManager : MonoBehaviour
    9. {
    10.     private WeaponUnlockSystem weaponUnlock;
    11.  
    12.     private void Start()
    13.     {
    14.         weaponUnlock = FindObjectOfType<WeaponUnlockSystem>();
    15.     }
    16.  
    17.     public void OnPurchaseComplete(Product product)
    18.     {
    19.         if (product != null)
    20.         {
    21.             switch (product.definition.id)
    22.             {
    23.                 case "weapons_unlock":
    24.                     // YAY!! someone actually spent actual money on this lousy game!
    25.                     if (weaponUnlock != null)
    26.                     {
    27.                         weaponUnlock.UnlockAllWeapons();
    28.                     }
    29.                    
    30.                     //reload the scene to properly repopulate shop
    31.                     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    32.                     break;
    33.                    
    34.                 case "disable_ads":
    35.                     PlayServices.instance.UnlockAchievement(GPGSIds.achievement_gracious_runner);
    36.                     PlayerPrefs.SetInt("ShowAds", 0);
    37.                     break;
    38.  
    39.                 default:
    40.                     print("Sorry, this is not a valid product.");
    41.                     break;
    42.             }
    43.         }
    44.     }
    45.  
    46. }
    47.  
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  7. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195

    Here is the logcat for what I press the unlock weapons button:

    Code (CSharp):
    1. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    2. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    3. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    4. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    5. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    6. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    7. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    8. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    9. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    10. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    11. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    12. 04-15 16:41:07.020 10102 10536 V FA-SVC  :         string_value: UnityPlayerActivity
    13. 04-15 16:41:14.455 10103 10155 I Unity   : IAPButton.PurchaseProduct() with product ID: weapons_unlock
    14. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.Purchasing.IAPButton:PurchaseProduct()
    15. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.Events.UnityAction:Invoke()
    16. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.Events.UnityEvent:Invoke()
    17. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
    18. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
    19. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
    20. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
    21. 04-15 16:41:14.455 10103 10155 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:Process()
    22. 04-15 16:41:14.455 10103 10155 I Unity   :
    23. 04-15 16:41:14.455 10103 10155 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 39)
    24. 04-15 16:41:14.455 10103 10155 I Unity   :
    25. 04-15 16:41:14.458 10103 10155 I Unity   : purchase(weapons_unlock)
    26. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.Purchasing.CodelessIAPStoreListener:InitiatePurchase(String)
    27. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.Events.UnityAction:Invoke()
    28. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.Events.UnityEvent:Invoke()
    29. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
    30. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
    31. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
    32. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
    33. 04-15 16:41:14.458 10103 10155 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:Process()
    34. 04-15 16:41:14.458 10103 10155 I Unity   :
    35. 04-15 16:41:14.458 10103 10155 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 39)
    36. 04-15 16:41:14.458 10103 10155 I Unity   :
    37. 04-15 16:41:14.523 10103 10573 D FA      : Logging event (FE): user_engagement(_e), Bundle[{ga_event_origin(_o)=auto, engagement_time_msec(_et)=53336, ga_screen_class(_sc)=UnityPlayerActivity, ga_screen_id(_si)=-855021746340006730}]
    38. 04-15 16:41:14.554 10103 10573 D FA      : Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=UnityPlayerActivity, ga_previous_id(_pi)=-855021746340006730, ga_screen_class(_sc)=ProxyBillingActivity, ga_screen_id(_si)=-855021746340006728}]
    39. 04-15 16:41:14.597 10102 10536 V FA-SVC  : Logging event: origin=auto,name=user_engagement(_e),params=Bundle[{ga_event_origin(_o)=auto, engagement_time_msec(_et)=53336, ga_screen_class(_sc)=UnityPlayerActivity, ga_screen_id(_si)=-855021746340006730}]
    40. 04-15 16:41:14.628 10102 10536 V FA-SVC  : Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=UnityPlayerActivity, ga_previous_id(_pi)=-855021746340006730, ga_screen_class(_sc)=ProxyBillingActivity, ga_screen_id(_si)=-855021746340006728}]
    41. 04-15 16:43:51.980 10103 10103 D ViewRootImpl[UnityPlayerActivity]: msg1 update new insets InsetsState: {mDisplayFrame=Rect(0, 0 - 2400, 1080), mSources= { InsetsSource: {mType=ITYPE_STATUS_BAR, mFrame=[0,0][2400,77], mVisible=false}, InsetsSource: {mType=ITYPE_NAVIGATION_BAR, mFrame=[2284,0][2400,1080], mVisible=false}, InsetsSource: {mType=ITYPE_TOP_GESTURES, mFrame=[0,0][2400,77], mVisible=true}, InsetsSource: {mType=ITYPE_BOTTOM_GESTURES, mFrame=[2284,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_LEFT_GESTURES, mFrame=[0,0][0,1080], mVisible=true}, InsetsSource: {mType=ITYPE_RIGHT_GESTURES, mFrame=[2400,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_TOP_TAPPABLE_ELEMENT, mFrame=[0,0][2400,77], mVisible=true}, InsetsSource: {mType=ITYPE_BOTTOM_TAPPABLE_ELEMENT, mFrame=[2284,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_LEFT_DISPLAY_CUTOUT, mFrame=[0,0][76,1080], mVisible=true}, InsetsSource: {mType=ITYPE_TOP_DISPLAY_CUTOUT, mFrame=[0,0][2400,-2147483648], mVisible=true}, InsetsSource: {mType=ITYPE_RIGHT_DISPLAY_CUTOUT, mFrame=[2147483647,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_BOTTOM_DISPLAY_CUTOUT, mFrame=[0,2147483647][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_IME, mFrame=[0,0][0,0], mVisible=false} }
    42. 04-15 16:43:51.980 10103 10103 D ViewRootImpl[UnityPlayerActivity]: msg1 update new insets InsetsState: {mDisplayFrame=Rect(0, 0 - 2400, 1080), mSources= { InsetsSource: {mType=ITYPE_STATUS_BAR, mFrame=[0,0][2400,77], mVisible=false}, InsetsSource: {mType=ITYPE_NAVIGATION_BAR, mFrame=[2284,0][2400,1080], mVisible=false}, InsetsSource: {mType=ITYPE_TOP_GESTURES, mFrame=[0,0][2400,77], mVisible=true}, InsetsSource: {mType=ITYPE_BOTTOM_GESTURES, mFrame=[2284,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_LEFT_GESTURES, mFrame=[0,0][0,1080], mVisible=true}, InsetsSource: {mType=ITYPE_RIGHT_GESTURES, mFrame=[2400,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_TOP_TAPPABLE_ELEMENT, mFrame=[0,0][2400,77], mVisible=true}, InsetsSource: {mType=ITYPE_BOTTOM_TAPPABLE_ELEMENT, mFrame=[2284,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_LEFT_DISPLAY_CUTOUT, mFrame=[0,0][76,1080], mVisible=true}, InsetsSource: {mType=ITYPE_TOP_DISPLAY_CUTOUT, mFrame=[0,0][2400,-2147483648], mVisible=true}, InsetsSource: {mType=ITYPE_RIGHT_DISPLAY_CUTOUT, mFrame=[2147483647,0][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_BOTTOM_DISPLAY_CUTOUT, mFrame=[0,2147483647][2400,1080], mVisible=true}, InsetsSource: {mType=ITYPE_IME, mFrame=[0,0][0,0], mVisible=false} }
    43. 04-15 16:43:51.989 10103 10659 D FA      : Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=ProxyBillingActivity, ga_previous_id(_pi)=-855021746340006728, ga_screen_class(_sc)=UnityPlayerActivity, ga_screen_id(_si)=-855021746340006730}]
    44. 04-15 16:43:52.037 10103 10155 I Unity   : onPurchaseFailedEvent(productId:weapons_unlock message:)
    45. 04-15 16:43:52.037 10103 10155 I Unity   : UnityEngine.Purchasing.PurchasingManager:OnPurchaseFailed(PurchaseFailureDescription)
    46. 04-15 16:43:52.037 10103 10155 I Unity   : System.Action:Invoke()
    47. 04-15 16:43:52.037 10103 10155 I Unity   : UnityEngine.Purchasing.Extension.UnityUtil:Update()
    48. 04-15 16:43:52.037 10103 10155 I Unity   :
    49. 04-15 16:43:52.037 10103 10155 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 39)
    50. 04-15 16:43:52.037 10103 10155 I Unity   :
    51. 04-15 16:43:52.038 10103 10155 I Unity   : IAPButton.OnPurchaseFailed(Product UnityEngine.Purchasing.Product, PurchaseFailureReason DuplicateTransaction)
    52. 04-15 16:43:52.038 10103 10155 I Unity   : UnityEngine.Purchasing.IAPButton:OnPurchaseFailed(Product, PurchaseFailureReason)
    53. 04-15 16:43:52.038 10103 10155 I Unity   : UnityEngine.Purchasing.CodelessIAPStoreListener:OnPurchaseFailed(Product, PurchaseFailureReason)
    54. 04-15 16:43:52.038 10103 10155 I Unity   : System.Action:Invoke()
    55. 04-15 16:43:52.038 10103 10155 I Unity   : UnityEngine.Purchasing.Extension.UnityUtil:Update()
    56. 04-15 16:43:52.038 10103 10155 I Unity   :
    57. 04-15 16:43:52.038 10103 10155 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 39)
    58. 04-15 16:43:52.038 10103 10155 I Unity   :
    59. 04-15 16:43:52.038 10103 10155 I Unity   : IAPListener.OnPurchaseFailed(Product UnityEngine.Purchasing.Product, PurchaseFailureReason DuplicateTransaction)
    60. 04-15 16:43:52.038 10103 10155 I Unity   : UnityEngine.Purchasing.IAPListener:OnPurchaseFailed(Product, PurchaseFailureReason)
    61. 04-15 16:43:52.038 10103 10155 I Unity   : UnityEngine.Purchasing.CodelessIAPStoreListener:OnPurchaseFailed(Product, PurchaseFailureReason)
    62. 04-15 16:43:52.038 10103 10155 I Unity   : System.Action:Invoke()
    63. 04-15 16:43:52.038 10103 10155 I Unity   : UnityEngine.Purchasing.Extension.UnityUtil:Update()
    64. 04-15 16:43:52.038 10103 10155 I Unity   :
    65. 04-15 16:43:52.038 10103 10155 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 39)
    66. 04-15 16:43:52.038 10103 10155 I Unity   :
    67. 04-15 16:43:52.038 10103 10155 I Unity   :  [Play Games Plugin DLL] 04/15/21 16:43:52 +01:00 DEBUG: Application is pausing, which disconnects the RTMP  client.  Leaving room.
    68. 04-15 16:43:52.038 10103 10155 I Unity   : System.Action:Invoke()
    69. 04-15 16:43:52.038 10103 10155 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
    70. 04-15 16:43:52.038 10103 10155 I Unity   :
    71. 04-15 16:43:52.038 10103 10155 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 39)
    72. 04-15 16:43:52.038 10103 10155 I Unity   :
    73. 04-15 16:43:52.041 10102 10665 V FA-SVC  : Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=ProxyBillingActivity, ga_previous_id(_pi)=-855021746340006728, ga_screen_class(_sc)=UnityPlayerActivity, ga_screen_id(_si)=-855021746340006730}]
    However, my Debug.Log is not showing up in there.. I added the following to my code above:

    Code (CSharp):
    1. public void OnPurchaseComplete(Product product)
    2.     {
    3.         if (product != null)
    4.         {
    5.             Debug.Log("This is the product.definition.id: " + product.definition.id.ToString());
    6.             switch (product.definition.id)
    7.             {
    8.                 case "weapons_unlock":
    9. .
    10. .
    11. .
    12. .
     
  8. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    And yes IAPListener is active in scene.. here is the screenshot for it::

    image_2021-04-15_164832.png
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @farazk86 So that code is not firing which means the Listener is not active or placed into your opening scene. I would highly recommend to use Scripted IAP, and Initialize IAP in Start() or Awake(). Your screenshot only shows the Listener properties, not its placement in any scene.
     
    farazk86 likes this.
  10. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
  11. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195

    So its finally working :)

    You mentioning the listener not being in the opening scene made me try something. I have a Splash Screen where I initialize a few things and then I have the start menu. Where I had the listener and the IAPButton.

    I copied the IAPListener and IAPManager to the very first scene - the splash screen and now my purchases are restored automotically.

    I did not know the IAP stuff has to be in the very first scene in the game.
     
    Last edited: Apr 16, 2021
    JeffDUnity3D likes this.