Search Unity

How do I manually initialize the IAP but use the products from the IAP Catalog?

Discussion in 'Unity IAP' started by JustAnotherDude, May 7, 2019.

  1. JustAnotherDude

    JustAnotherDude

    Joined:
    Oct 28, 2013
    Posts:
    279
    When I create a product on the IAP Catalog I can uncheck the "Automatically initialize UnityPurchasing".

    All the examples on the documentation only show the manual initialization of the UnityPurchasing while also adding the products, none show how to do it using the IAPs on the catalog.

    https://docs.unity3d.com/Manual/UnityIAPDefiningProducts.html

    and

    https://docs.unity3d.com/Manual/UnityIAPInitialization.html

    I tried to not add a product manually during initialization but it failed saying I have no products defined.
     
    tjbarrott likes this.
  2. JustAnotherDude

    JustAnotherDude

    Joined:
    Oct 28, 2013
    Posts:
    279
    Thanks, that's what I needed :


    Code (CSharp):
    1.         ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    2.         IAPConfigurationHelper.PopulateConfigurationBuilder(ref builder, ProductCatalog.LoadDefaultCatalog());
    3.         UnityPurchasing.Initialize(this, builder);
     
    gdbjohnson3, Oxy949, JoWenk and 13 others like this.
  3. Raviraj_Uppal

    Raviraj_Uppal

    Joined:
    Apr 26, 2020
    Posts:
    2
    Thanks mate, I had the same problem
     
  4. Raviraj_Uppal

    Raviraj_Uppal

    Joined:
    Apr 26, 2020
    Posts:
    2
    But now in the editor the fake store (that panel with buy and cancel option) is not popping up and OnpurchaseComplete() is executing directly. Is it normal if you initialize manually?
     
  5. John_Corbett

    John_Corbett

    Joined:
    May 17, 2019
    Posts:
    151
    Hi @Raviraj_Uppal

    I don't believe that's normal. Can you please show us your initializaion code?

    Also what do you mean by "OnpurchaseComplete"? Normally that function is for Codeless IAP only. Are you using IAPButton and IAPListener?
     
  6. xxluky

    xxluky

    Joined:
    Dec 4, 2014
    Posts:
    19
    I have the same problem. I initialize manually and I get no Fake Store Panel. I face one more problem: The price in the editor is always zero. Is it normal, too?
    EDIT: Price is not showing on Android device as well. I think there is a problem with the initialization.

    I am using v 4.4.1. Here is my init script:

    Code (CSharp):
    1. public class MyIAPInit : MonoBehaviour, IStoreListener
    2. {
    3.     public MyIAPManager myIAPManager;
    4.  
    5.     private IStoreController m_StoreController;             // The Unity Purchasing system.
    6.     private IExtensionProvider m_StoreExtensionProvider;    // The store-specific Purchasing subsystems.
    7.  
    8.     private void Start()
    9.     {
    10.         InitializePurchasing();
    11.     }
    12.  
    13.     public void InitializePurchasing()
    14.     {
    15.         // If we have already connected to Purchasing ...
    16.         if (IsInitialized())
    17.         {
    18.             // ... we are done here.
    19.             return;
    20.         }
    21.  
    22.         // Create a builder, first passing in a suite of Unity provided stores.
    23.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    24.  
    25.         // Add products to sell / restore by way of its identifier, associating the general identifier
    26.         // with its store-specific identifiers.
    27.         builder.AddProduct("molly_key1", ProductType.Consumable);
    28.         builder.AddProduct("molly_key2", ProductType.Consumable);
    29.         builder.AddProduct("molly_key3", ProductType.Consumable);
    30.         builder.AddProduct("molly_key4", ProductType.Consumable);
    31.         builder.AddProduct("molly_egypt", ProductType.Consumable);
    32.         builder.AddProduct("molly_russia", ProductType.Consumable);
    33.  
    34.         // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
    35.         // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
    36.         UnityPurchasing.Initialize(this, builder);
    37.     }
    38.  
    39.     // OUR CODE: Called in MyIAPManager.
    40.     public string GetPrice(string productID)
    41.     {
    42.         if (m_StoreController == null)
    43.             InitializePurchasing();
    44.  
    45.         if (m_StoreController != null && m_StoreController.products != null)
    46.             return m_StoreController.products.WithID(productID).metadata.localizedPriceString;
    47.         else
    48.             return "Offline";
    49.     }
    50.  
    51.     private bool IsInitialized()
    52.     {
    53.         // Only say we are initialized if both the Purchasing references are set.
    54.         return m_StoreController != null && m_StoreExtensionProvider != null;
    55.     }
    56.  
    57.     public void BuyProductID(string productId)
    58.     {
    59.         // If Purchasing has been initialized ...
    60.         if (IsInitialized())
    61.         {
    62.             // ... look up the Product reference with the general product identifier and the Purchasing
    63.             // system's products collection.
    64.             Product product = m_StoreController.products.WithID(productId);
    65.  
    66.             // If the look up found a product for this device's store and that product is ready to be sold ...
    67.             if (product != null && product.availableToPurchase)
    68.             {
    69.                 // Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
    70.  
    71.                 // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
    72.                 // asynchronously.
    73.                 m_StoreController.InitiatePurchase(product);
    74.             }
    75.             // Otherwise ...
    76.             else
    77.             {
    78.                 // ... report the product look-up failure situation
    79.                 Debug.LogError("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase " + productId);
    80.             }
    81.         }
    82.         // Otherwise ...
    83.         else
    84.         {
    85.             // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
    86.             // retrying initiailization.
    87.             Debug.Log("BuyProductID FAIL. Not initialized.");
    88.         }
    89.     }
    90.  
    91.     // Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
    92.     // Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
    93.     public void RestorePurchases()
    94.     {
    95.         // If Purchasing has not yet been set up ...
    96.         if (!IsInitialized())
    97.         {
    98.             // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
    99.             Debug.Log("RestorePurchases FAIL. Not initialized.");
    100.             return;
    101.         }
    102.  
    103.         // If we are running on an Apple device ...
    104.         if (Application.platform == RuntimePlatform.IPhonePlayer ||
    105.             Application.platform == RuntimePlatform.OSXPlayer)
    106.         {
    107.             // ... begin restoring purchases
    108.             // Debug.Log("RestorePurchases started ...");
    109.  
    110.             // Fetch the Apple store-specific subsystem.
    111.             var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    112.             // Begin the asynchronous process of restoring purchases. Expect a confirmation response in
    113.             // the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
    114.             apple.RestoreTransactions((result) => {
    115.                 // The first phase of restoration. If no more responses are received on ProcessPurchase then
    116.                 // no purchases are available to be restored.
    117.                 // Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    118.             });
    119.         }
    120.         // Otherwise ...
    121.         else
    122.         {
    123.             // We are not running on an Apple device. No work is necessary to restore purchases.
    124.             Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
    125.         }
    126.     }
    127.  
    128.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    129.     {
    130.         // Purchasing has succeeded initializing. Collect our Purchasing references.
    131.         // Debug.Log("OnInitialized: PASS");
    132.  
    133.         // Overall Purchasing system, configured with products for this application.
    134.         m_StoreController = controller;
    135.         // Store specific subsystem, for accessing device-specific store features.
    136.         m_StoreExtensionProvider = extensions;
    137.     }
    138.  
    139.     public void OnInitializeFailed(InitializationFailureReason error)
    140.     {
    141.         // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    142.         Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    143.     }
    144.  
    145.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    146.     {
    147.         // A consumable product has been purchased by this user.
    148.         switch (args.purchasedProduct.definition.id)
    149.         {
    150.             case "molly_key1":
    151.                 PlayerPrefs.SetInt("ProcessPurchase", 1);
    152.                 break;
    153.             case "molly_key2":
    154.                 PlayerPrefs.SetInt("ProcessPurchase", 2);
    155.                 break;
    156.             case "molly_key3":
    157.                 PlayerPrefs.SetInt("ProcessPurchase", 3);
    158.                 break;
    159.             case "molly_key4":
    160.                 PlayerPrefs.SetInt("ProcessPurchase", 4);
    161.                 break;
    162.             case "molly_egypt":
    163.                 PlayerPrefs.SetInt("ProcessPurchase", 5);
    164.                 break;
    165.             case "molly_russia":
    166.                 PlayerPrefs.SetInt("ProcessPurchase", 6);
    167.                 break;
    168.             default:
    169.                 Debug.LogException(new Exception("qBqZMfMU3w" + " - " + "ProcessPurchase: FAIL. Unrecognized product: " + args.purchasedProduct.definition.id));
    170.                 break;
    171.         }
    172.  
    173.         myIAPManager.OnPurchaseSuccess();
    174.  
    175.  
    176.         // Return a flag indicating whether this product has completely been received, or if the application needs
    177.         // to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
    178.         // saving purchased products to the cloud, and when that save is delayed.
    179.         return PurchaseProcessingResult.Complete;
    180.     }
    181.  
    182.     public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    183.     {
    184.         // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
    185.         // this reason with the user to guide their troubleshooting actions.
    186.         Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    187.     }
    188. }
    and here is my manager script where I call myIAPInit.GetPrice

    Code (CSharp):
    1. public class MyIAPManager : MonoBehaviour
    2. {
    3.     public LanguageManager languageManager;
    4.     public AdMobController adMobController;
    5.     public MyIAPInit myIAPInit;
    6.     public CoinManagerScript coinManagerScript;
    7.     public ShopManagerScript shopManagerScript;
    8.     public PaidLevelsControlScript paidLevelsControlScript;
    9.     public GameObject paidLevelBuyPanel;
    10.  
    11.     public Text[] paidLevelsPriceTexts;
    12.     public Text[] shopPriceTexts;
    13.     public Text[] shopValueTexts;
    14.     public int[] amount;
    15.  
    16.     // Coin Increment Animation variables.
    17.     public float duration = 2f;
    18.     private int score = 0;
    19.  
    20.     void Start()
    21.     {
    22.         // Show product price for the Shop Panel
    23.         for (int i = 0; i < shopPriceTexts.Length; i++)
    24.         {
    25.             shopPriceTexts[i].text = myIAPInit.GetPrice("molly_key" + (i + 1).ToString());
    26.         }
    27.  
    28.         for (int i = 0; i < shopValueTexts.Length; i++)
    29.         {
    30.             shopValueTexts[i].text = amount[i].ToString("n0") + " " + languageManager.getString("shopCoins");
    31.         }
    32.  
    33.         paidLevelsPriceTexts[0].text = myIAPInit.GetPrice("molly_egypt");
    34.         paidLevelsPriceTexts[1].text = myIAPInit.GetPrice("molly_russia");
    35.     }
    36.  
    37.     public void BuyConsumable(string productID)     // Called on Product Buttons !!!
    38.     {
    39.         // Buy the consumable product using its general identifier. Expect a response either
    40.         // through ProcessPurchase or OnPurchaseFailed asynchronously.
    41.         myIAPInit.BuyProductID(productID);
    42.     }
    43.  
    44.     public void OnPurchaseSuccess()     // Called in MyIAPInit
    45.     {
    46.         PlayerPrefs.SetInt("MadeAnyPurchase", 1);   // Prevents to show Banner Ad in the future.
    47.         adMobController.DestroyBannerAd();          // Remove Banner Ad.
    48.  
    49.         switch (PlayerPrefs.GetInt("ProcessPurchase", 0))
    50.         {
    51.             case 1:
    52.                 shopManagerScript.CloseShop();
    53.                 StartCoroutine(CoinIncrementAnimation(amount[0]));
    54.                 break;
    55.             case 2:
    56.                 shopManagerScript.CloseShop();
    57.                 StartCoroutine(CoinIncrementAnimation(amount[1]));
    58.                 break;
    59.             case 3:
    60.                 shopManagerScript.CloseShop();
    61.                 StartCoroutine(CoinIncrementAnimation(amount[2]));
    62.                 break;
    63.             case 4:
    64.                 shopManagerScript.CloseShop();
    65.                 StartCoroutine(CoinIncrementAnimation(amount[3]));
    66.                 break;
    67.             case 5:
    68.                 PlayerPrefs.SetInt("PaidLevel0Purchased", 1);
    69.                 paidLevelBuyPanel.SetActive(false);
    70.                 paidLevelsControlScript.RefreshPaidButtons();
    71.                 break;
    72.             case 6:
    73.                 PlayerPrefs.SetInt("PaidLevel1Purchased", 1);
    74.                 paidLevelBuyPanel.SetActive(false);
    75.                 paidLevelsControlScript.RefreshPaidButtons();
    76.                 break;
    77.             default:
    78.                 Debug.LogException(new Exception("qBqZMfMU3w" + " - " + "OnPurchaseSuccess: Unknown Product ID! Purchase Failed."));
    79.                 break;
    80.         }
    81.     }
    82.  
    83.     IEnumerator CoinIncrementAnimation(int target)
    84.     {
    85.         int start = score;
    86.         for (float timer = 0; timer < duration; timer += Time.deltaTime)
    87.         {
    88.             float progress = timer / duration;
    89.             score = (int)Mathf.Lerp(start, target, progress);
    90.             coinManagerScript.coinScoreText.text = (coinManagerScript.coinScore + score).ToString();
    91.             yield return null;
    92.         }
    93.         coinManagerScript.IncreaseScore(target, true);
    94.     }
    95. }
    Edit: Ok, I found solution finaly. UnityPurchasing.Initialize() is an asynchrounous method. In MyIAPManager I was trying to Get Price in Start but at this moment IAP hasn't been initialized yet.
    I moved my code from start into a public function and call it in IAP Init script in OnInitialized.
     
    Last edited: Mar 15, 2023