Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] You already own this item...

Discussion in 'Unity IAP' started by Lukasz-Wardak, Jan 16, 2017.

Thread Status:
Not open for further replies.
  1. Lukasz-Wardak

    Lukasz-Wardak

    Joined:
    Jan 4, 2016
    Posts:
    1
    Hello,
    I'm trying to add to my game micropayments using the in-App Purchasing Services.
    I implemented the following code:

    Code (CSharp):
    1. public class Purchaser : MonoBehaviour, IStoreListener
    2. {
    3.     [Tooltip("Consumable Product")]
    4.     public string[] COIN_PRODUCTS;
    5.  
    6.     private static IStoreController m_StoreController;          // The Unity Purchasing system.
    7.     private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
    8.  
    9.     public int currentProductIndex;
    10.  
    11.     public static event OnSuccessBuyGoldEgg OnPurchaseGoldEgg;
    12.     public delegate void OnSuccessBuyGoldEgg(PurchaseEventArgs args);
    13.  
    14.     private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
    15.  
    16.     void Start()
    17.     {
    18.         if (m_StoreController == null)
    19.         {
    20.             InitializePurchasing();
    21.         }
    22.     }
    23.  
    24.     public void OnPurchaseComplete(Product produt)
    25.     {
    26.         Debug.Log(produt.metadata);
    27.     }
    28.  
    29.     public void InitializePurchasing()
    30.     {
    31.         if (IsInitialized())
    32.         {
    33.             return;
    34.         }
    35.  
    36.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    37.  
    38.         foreach (string s in COIN_PRODUCTS)
    39.             builder.AddProduct(s, ProductType.Consumable);
    40.  
    41.         UnityPurchasing.Initialize(this, builder);
    42.     }
    43.  
    44.  
    45.     private bool IsInitialized()
    46.     {
    47.         return m_StoreController != null && m_StoreExtensionProvider != null;
    48.     }
    49.  
    50.  
    51.     public void BuyConsumable(int index)
    52.     {
    53.         currentProductIndex = index;
    54.         BuyProductID(COIN_PRODUCTS[index]);
    55.     }
    56.  
    57.     void BuyProductID(string productId)
    58.     {
    59.         if (IsInitialized())
    60.         {
    61.             Product product = m_StoreController.products.WithID(productId);
    62.  
    63.             if (product != null && product.availableToPurchase)
    64.             {
    65.                 m_StoreController.InitiatePurchase(product);
    66.             }
    67.             else
    68.             {
    69.                 Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    70.             }
    71.         }
    72.         else
    73.         {
    74.             Debug.Log("BuyProductID FAIL. Not initialized.");
    75.         }
    76.     }
    77.  
    78.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    79.     {
    80.         Debug.Log("OnInitialized: PASS");
    81.         m_StoreController = controller;
    82.         m_StoreExtensionProvider = extensions;
    83.     }
    84.  
    85.  
    86.     public void OnInitializeFailed(InitializationFailureReason error)
    87.     {
    88.         Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    89.     }
    90.  
    91.  
    92.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    93.     {
    94.         m_StoreController.ConfirmPendingPurchase(args.purchasedProduct);
    95.  
    96.         if (COIN_PRODUCTS.Length > 0 && String.Equals(args.purchasedProduct.definition.id, COIN_PRODUCTS[currentProductIndex], StringComparison.Ordinal))
    97.         {
    98.             OnSuccessC(args);
    99.         }
    100.         else {
    101.             Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product '{0}'", args.purchasedProduct.definition.id));
    102.         }
    103.         return PurchaseProcessingResult.Complete;
    104.     }
    105.  
    106.     private void OnSuccessC(PurchaseEventArgs args)
    107.     {
    108.         if (OnPurchaseGoldEgg != null) OnPurchaseGoldEgg(args);
    109.         Debug.Log(COIN_PRODUCTS[currentProductIndex] + " Buyed!");
    110.     }
    111.  
    112.     public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    113.     {
    114.         Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    115.     }
    116.  
    117.     public void ExitToMainMenu()
    118.     {
    119.         SceneManager.UnloadScene(3);
    120.  
    121.         GameObject cameras = GameObject.Find("Main Camera");
    122.         cameras.GetComponent<SaveGameManager>().LoadState(cameras);
    123.         cameras.GetComponent<MainMenu_GUI>().mainMenuShow = true;
    124.     }
    and:

    Code (CSharp):
    1.     private GameObject gameBaseObject;
    2.  
    3.     private CoinProduct[] coinProducts = new CoinProduct[]
    4.     {
    5.         new CoinProduct("eggs_pack_20.000", "Egg's Pack 20.000", 20000, "Buyng 20 000 Gold Egg!"),
    6.         new CoinProduct("eggs_pack_50.000", "Egg's Pack 50.000", 50000, "Buyng 50 000 Gold Egg!"),
    7.         new CoinProduct("eggs_pack_100.000", "Egg's Pack 100.000", 100000, "Buyng 100 000 Gold Egg!"),
    8.         new CoinProduct("eggs_pack_500.000", "Egg's Pack 500.000", 500000, "Buyng 500 000 Gold Egg!")
    9.     };
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         gameBaseObject = GameObject.Find("Main Camera");
    14.         Purchaser.OnPurchaseGoldEgg += Purchaser_OnPurchaseGoldEgg;
    15.     }
    16.  
    17.     private void Purchaser_OnPurchaseGoldEgg(UnityEngine.Purchasing.PurchaseEventArgs args)
    18.     {
    19.         for (int index = 0; index < coinProducts.Length; index++)
    20.         {
    21.             if (args.purchasedProduct.definition.id == coinProducts[index].ID)
    22.             {
    23.                 gameBaseObject.GetComponent<Game>().goldEgg += coinProducts[index].AddingCoin;
    24.                 gameBaseObject.GetComponent<SaveGameManager>().SaveState(gameBaseObject);
    25.                 Debug.Log(coinProducts[index].DebugLog);
    26.             }
    27.         }
    28.     }
    What is wrong? Why are you after purchase does not receive the money? Why google play returns this message: "You already own this item"

     
  2. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @Lukasz-Wardak

    In your ProcessPurchase function, you are calling ConfirmPendingPurchase. This is not the intended use of of ConfirmPendingPurchase and will result in unpredictable behavior.

    In your example, you could just remove line 94 and your code should still work.

    The most common use case for ConfirmPendingPurchase is when you are confirming your transactions on a server. The expected flow would be to return PurchaseProcessingResult.Pending in ProcessPurchase and then after your server confirms your purchase, call ConfirmPendingPurchase to finish that transaction.
     
Thread Status:
Not open for further replies.