Search Unity

[Solved] Unity2D: IAP not working on mobile phone

Discussion in 'Unity IAP' started by wasicool7, Dec 22, 2016.

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

    wasicool7

    Joined:
    Jul 28, 2016
    Posts:
    25
    Hi, I'm having problems with unity IAP services; I followed this tutorial on online to the tea and it works within Unity Editor however it doesn't work on my mobile. I checked through everything and even tested it out on a different mobile, but it still giving the same results. I recently upgraded unity 5.4 to 5.5 and even before I upgraded to 5.5, I was still having the same problem as before; I even tried out the new IAP system that unity 5.5 provide and still having the same problem. Heck, I think that the problem is, is that my IAP script (the one which was also provided my unity) isn't working on mobile, as before mention it does work within unity engine.One more thing to mention is that my IAP script was working before (on mobiles), before it stopped working on mobile phones, however I am not sure how or what triggered my script to not work now on mobile phones.

    My script:

    Code (CSharp):
    1.     public static IAPManager Instance{set; get;}
    2.  
    3.     private static IStoreController m_StoreController;
    4.     private static IExtensionProvider m_StoreExtensionProvider;
    5.  
    6.     public static string PRODUCT_50_GOLD =    "50gold";
    7.     public static string PRODUCT_100_GOLD = "100gold";
    8.     public static string PRODUCT_NO_ADS =  "noad";
    9.  
    10.     private void Awake()
    11.     {
    12.         Instance = this;
    13.     }
    14.     private void Start()
    15.     {
    16.         if (m_StoreController == null)
    17.         {
    18.             InitializePurchasing();
    19.         }
    20.     }
    21.     public void InitializePurchasing()
    22.     {
    23.         if (IsInitialized())
    24.         {
    25.             return;
    26.         }
    27.  
    28.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    29.  
    30.         builder.AddProduct(PRODUCT_50_GOLD, ProductType.Consumable);
    31.         builder.AddProduct(PRODUCT_100_GOLD, ProductType.Consumable);
    32.         builder.AddProduct(PRODUCT_NO_ADS, ProductType.NonConsumable);
    33.  
    34.          
    35.         UnityPurchasing.Initialize(this, builder);
    36.     }
    37.     private bool IsInitialized()
    38.     {
    39.         return m_StoreController != null && m_StoreExtensionProvider != null;
    40.     }
    41.  
    42.     public void Buy50Gold()
    43.     {
    44.         BuyProductID(PRODUCT_50_GOLD);
    45.     }
    46.     public void Buy100Gold()
    47.     {
    48.         BuyProductID(PRODUCT_100_GOLD);
    49.     }
    50.     public void BuyNoAds()
    51.     {
    52.         BuyProductID(PRODUCT_NO_ADS);
    53.     }
    54.  
    55.  
    56.     private void BuyProductID(string productId)
    57.     {
    58.         // If Purchasing has been initialized ...
    59.         if (IsInitialized())
    60.         {
    61.             // ... look up the Product reference with the general product identifier and the Purchasing
    62.             // system's products collection.
    63.             Product product = m_StoreController.products.WithID(productId);
    64.  
    65.             // If the look up found a product for this device's store and that product is ready to be sold ...
    66.             if (product != null && product.availableToPurchase)
    67.             {
    68.                 Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
    69.                 // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
    70.                 // asynchronously.
    71.                 m_StoreController.InitiatePurchase(product);
    72.             }
    73.             // Otherwise ...
    74.             else
    75.             {
    76.                 // ... report the product look-up failure situation
    77.                 Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    78.             }
    79.         }
    80.         // Otherwise ...
    81.         else
    82.         {
    83.             // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
    84.             // retrying initiailization.
    85.             Debug.Log("BuyProductID FAIL. Not initialized.");
    86.         }
    87.     }
    88.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    89.     {
    90.         // Purchasing has succeeded initializing. Collect our Purchasing references.
    91.         Debug.Log("OnInitialized: PASS");
    92.  
    93.         // Overall Purchasing system, configured with products for this application.
    94.         m_StoreController = controller;
    95.         // Store specific subsystem, for accessing device-specific store features.
    96.         m_StoreExtensionProvider = extensions;
    97.     }
    98.     public void OnInitializeFailed(InitializationFailureReason error)
    99.     {
    100.         // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    101.         Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    102.     }
    103.  
    104.  
    105.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    106.     {
    107.         if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_50_GOLD, StringComparison.Ordinal))
    108.         {
    109.             Debug.Log ("Purchase Successfull!");
    110.             ScoreManager.Coin += 50;
    111.         }
    112.         else if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_100_GOLD, StringComparison.Ordinal))
    113.         {
    114.             Debug.Log ("Purchase Successfull!");
    115.             ScoreManager.Coin += 100;
    116.         }
    117.         else if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_NO_ADS, StringComparison.Ordinal))
    118.         {
    119.             Debug.Log ("Purchase Successfull!");
    120.         }
    121.         else
    122.         {
    123.             Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
    124.         }
    125.         return PurchaseProcessingResult.Complete;
    126.     }
    127.  
    128.  
    129.     public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    130.     {
    131.         // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
    132.         // this reason with the user to guide their troubleshooting actions.
    133.         Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    134.     }
    135. }
    The below code is being referenced above

    Code (CSharp):
    1.  
    2.     private void Start() {
    3.  
    4.     }
    5.  
    6.     public void Buy50Gold() {
    7.         IAPManager.Instance.Buy50Gold ();
    8.     }
    9.  
    10.     public void Buy100Gold() {
    11.         IAPManager.Instance.Buy100Gold ();
    12.     }
    13.     public void BuyNoAds() {
    14.         IAPManager.Instance.BuyNoAds ();
    15.     }
     
  2. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
Thread Status:
Not open for further replies.