Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question IAP Issue, Android Side

Discussion in 'Scripting' started by Leuki, Apr 11, 2023.

  1. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    So I've seen multiple forum posts regarding this issue, and i have applied the fixes described in these posts, but I have yet to figure out the root cause. Within my IAPManager.cs i call initialize. I do this on Start().

    Code (CSharp):
    1.  
    2.  private void InitializePurchasing()
    3.     {
    4.         if (IsInitialized())
    5.         {
    6.             messageText = "Already initialized";
    7.             Debug.Log("Already initialized");
    8.             OnInitComplete(IAPOperationStatus.Success, "Already initialized", null);
    9.             return;
    10.         }
    11.         messageText = "initializing";
    12.         Debug.Log("initializing");
    13.         builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    14.  
    15.         for (int i = 0; i < shopProducts.Count; i++)
    16.         {
    17.             builder.AddProduct(shopProducts[i].GetStoreID(), shopProducts[i].GetProductType());
    18.         }
    19.  
    20.         if (debug)
    21.         {
    22.             Debug.Log("InitializePurchasing");
    23.             ScreenWriter.Write("InitializePurchasing");
    24.         }
    25.  
    26.         UnityPurchasing.Initialize(this, builder);
    27.     }
    28.  
    This calls just fine in Editor. And my OnInitialized is called in editor as well.

    Code (CSharp):
    1. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    2.     {
    3.         if (debug)
    4.         {
    5.             Debug.Log("OnInitialized");
    6.             ScreenWriter.Write("OnInitialized");
    7.         }
    8.         messageText = "initialized";
    9.         Debug.Log("initialized");
    10.         m_StoreController = controller;
    11.         m_StoreExtensionProvider = extensions;
    12.         for (int i = 0; i < shopProducts.Count; i++)
    13.         {
    14.             Product product = m_StoreController.products.WithID(shopProducts[i].GetStoreID());
    15.  
    16.             if (debug)
    17.             {
    18.                 Debug.Log(this + product.metadata.localizedTitle + " is available " + product.availableToPurchase);
    19.                 ScreenWriter.Write(this + product.metadata.localizedTitle + " is available " + product.availableToPurchase);
    20.             }
    21.  
    22.             if (shopProducts[i].productType == ProductType.Subscription)
    23.             {
    24.                 if (product != null && product.hasReceipt)
    25.                 {
    26.                     IAPSecurityException exception;
    27.                     if (ReceiptIsValid(shopProducts[i].productName, product.receipt, out exception))
    28.                     {
    29.                         shopProducts[i].active = true;
    30.                         string introJson=null;
    31.                         SubscriptionManager p = new SubscriptionManager(product, introJson);
    32.                         shopProducts[i].subscriptionInfo = p.getSubscriptionInfo();
    33.                     }
    34.                 }
    35.             }
    36.  
    37.             if (shopProducts[i].productType == ProductType.NonConsumable)
    38.             {
    39.                 if (product != null && product.hasReceipt)
    40.                 {
    41.                     IAPSecurityException exception;
    42.                     if (ReceiptIsValid(shopProducts[i].productName, product.receipt, out exception))
    43.                     {
    44.                         shopProducts[i].active = true;
    45.                     }
    46.                 }
    47.             }
    48.  
    49.             if (product != null && product.availableToPurchase)
    50.             {
    51.                 shopProducts[i].localizedPriceString = product.metadata.localizedPriceString;
    52.                 shopProducts[i].price = System.Decimal.ToInt32(product.metadata.localizedPrice);
    53.                 shopProducts[i].isoCurrencyCode = product.metadata.isoCurrencyCode;
    54.                 shopProducts[i].localizedDescription = product.metadata.localizedDescription;
    55.                 shopProducts[i].localizedTitle = product.metadata.localizedTitle;
    56.             }
    57.         }
    58.         OnInitComplete(IAPOperationStatus.Success, "Success", shopProducts);
    59.     }
    Then when I press the button to purchase the product i run this.

    Code (CSharp):
    1. public void BuyProduct(ShopProductNames productName, UnityAction<IAPOperationStatus, string, StoreProduct> OnCompleteMethod)
    2.     {
    3.         if (debug)
    4.         {
    5.             Debug.Log(this + "Buy Process Started for " + productName);
    6.             ScreenWriter.Write(this + "Buy Process Started for " + productName);
    7.         }
    8.  
    9.         this.OnCompleteMethod = OnCompleteMethod;
    10.  
    11.         for (int i = 0; i < shopProducts.Count; i++)
    12.         {
    13.             if (shopProducts[i].productName == productName.ToString())
    14.             {
    15.                 BuyProductID(shopProducts[i].GetStoreID());
    16.             }
    17.         }
    18.     }
    Which in return calls

    Code (CSharp):
    1.  
    2. private void BuyProductID(string productId)
    3.     {
    4.         if (debug)
    5.         {
    6.             Debug.Log(this + "Buy product with id: " + productId);
    7.             ScreenWriter.Write(this + "Buy product with id: " + productId);
    8.         }
    9.  
    10.         if (IsInitialized())
    11.         {
    12.             Debug.Log("Buying product");
    13.             Product product = m_StoreController.products.WithID(productId);
    14.             if (product != null && product.availableToPurchase)
    15.             {
    16.                 messageText = "available product";
    17.                 Debug.Log("available product");
    18.                 m_StoreController.InitiatePurchase(product);
    19.             }
    20.             else
    21.             {
    22.                 if (debug)
    23.                 {
    24.                     Debug.Log(this + "BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    25.                     ScreenWriter.Write(this + "BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    26.                 }
    27.                 messageText = "BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase";
    28.                 if (OnCompleteMethod != null)
    29.                 {
    30.                     OnCompleteMethod(IAPOperationStatus.Fail, "Not purchasing product, either is not found or is not available for purchase", null);
    31.                 }
    32.             }
    33.         }
    34.         else
    35.         {
    36.             if (debug)
    37.             {
    38.                 Debug.Log(this + "BuyProductID FAIL. Store not initialized.");
    39.                 ScreenWriter.Write(this + "BuyProductID FAIL. Store not initialized.");
    40.             }
    41.             messageText = "BuyProductID FAIL. Store not initialized.";
    42.             if (OnCompleteMethod != null)
    43.             {
    44.                 OnCompleteMethod(IAPOperationStatus.Fail, "Store not initialized.", null);
    45.             }
    46.         }
    47.     }
    In editor, this all runs fine, of course the purchase window never opens but the callbacks are showing that its being initiated and when i press the buy button, my debug text prints "available product". But when i run it on Android the first debug text shows
    messageText = "initializing";
    so this tells me its stuck in the initialize state. And of course when i hit the buy button i get, not initialized. My product ID does match my dev dashboard ID for my subscription im trying to set. I also set up receipt obfuscator and put in my key. The only issue im seeing it could be is when i go to unity settings for IAP it doesn't allow me to set my Key in there, instead i had to go to analytics and manually put it in on my dashboard in unity. Other than that im not seeing why it isn't fully initializing. And yes i have set up internal testing and the .aab is uploaded and running on the device.
     
  2. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    I'd also like to note I upgraded to pay as you go in analytics. Read somewhere that IAP won't initiate on free plan. Is this true?