Search Unity

Unity IAP not Working on Device - SOLVED

Discussion in 'Scripting' started by downhilldan, Jun 26, 2017.

  1. downhilldan

    downhilldan

    Joined:
    Nov 16, 2016
    Posts:
    90
    Hi All,

    I'm having troubles getting my IAP to work on both IOS and Android. When I "purchase" something in the editor, the purchase succeeds. When I'm on a device, I click my button to make my purchase (no ads), but the purchase popup does not appear. My android app is in beta and the IOS app is running through Test Flight (aka on a device). I have other apps that have IAP in them and it works perfectly, so there shouldn't be any errors in my developer accounts.

    Quick note, Im only using the "androidNoAdsProductID" and "iosNoAdsProductID" variable ID's at the moment, could the problem be that the other strings are empty?

    Code (CSharp):
    1. private static IStoreController m_StoreController;              // The Unity Purchasing system.
    2.     private static IExtensionProvider m_StoreExtensionProvider;     // The store-specific Purchasing subsystems.
    3.  
    4.  
    5.     #region Variables
    6.  
    7.     //---IAP ID's--//
    8.     [Header("---Android---")]
    9.     public string androidNoAdsProductID = "noads";
    10.     public string androidAProductID = "PRODUCT A ID";
    11.     public string androidBProductID = "PRODUCT B ID";
    12.     public string androidCProductID = "PRODUCT C ID";
    13.     public string androidDProductID = "PRODUCT D ID";
    14.     public string androidEProductID = "PRODUCT E ID";
    15.  
    16.     [Header("---IOS---")]
    17.     public string iosNoAdsProductID = "NO ADS ID";
    18.     public string iosAProductID = "PRODUCT A ID";
    19.     public string iosBProductID = "PRODUCT B ID";
    20.     public string iosCProductID = "PRODUCT C ID";
    21.     public string iosDProductID = "PRODUCT D ID";
    22.     public string iosEProductID = "PRODUCT E ID";
    23.  
    24.     [Header("---Product Types---")]
    25.     [Tooltip("What type of product is product A?")]
    26.     public ProductType productA;
    27.     [Tooltip("What type of product is product B?")]
    28.     public ProductType productB;
    29.     [Tooltip("What type of product is product C?")]
    30.     public ProductType productC;
    31.     [Tooltip("What type of product is product D?")]
    32.     public ProductType productD;
    33.     [Tooltip("What type of product is product E?")]
    34.     public ProductType productE;
    35.     [Space(35)]
    36.  
    37.     private static string noAdsProductID;
    38.     private static string aProductID;
    39.     private static string bProductID;
    40.     private static string cProductID;
    41.     private static string dProductID;
    42.     private static string eProductID;
    43.  
    44.     [Header("---Purchase Reports---")]
    45.     [Tooltip("Enable debugging (reports) for BandzIAPControl")]
    46.     public bool iapReport;
    47.  
    48.  
    49.     #endregion
    50.  
    51.  
    52.     //---Initialize all Products---//
    53.     #region Initialize
    54.  
    55.     private void Awake()
    56.     {
    57.         if (m_StoreController == null)
    58.         {
    59.             InitializePurchasing();
    60.         }
    61.     }
    62.  
    63.     public void InitializePurchasing()
    64.     {
    65.         if (IsInitialized())
    66.         {
    67.             return;
    68.         }
    69. #if UNITY_ANDROID
    70.         noAdsProductID = androidNoAdsProductID;
    71.         aProductID = androidAProductID;
    72.         bProductID = androidBProductID;
    73.         cProductID = androidCProductID;
    74.         dProductID = androidDProductID;
    75.         eProductID = androidEProductID;
    76. #elif UNITY_IPHONE
    77.         noAdsProductID = iosNoAdsProductID;
    78.         aProductID = iosAProductID;
    79.         bProductID = iosBProductID;
    80.         cProductID = iosCProductID;
    81.         dProductID = iosDProductID;
    82.         eProductID = iosEProductID;
    83. #endif
    84.  
    85.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    86.         builder.AddProduct(noAdsProductID, ProductType.NonConsumable);      //Disabling ads requires a "NonConsumable" product type
    87.         builder.AddProduct(aProductID, productA);
    88.         builder.AddProduct(bProductID, productB);
    89.         builder.AddProduct(cProductID, productC);
    90.         builder.AddProduct(cProductID, productD);
    91.         builder.AddProduct(cProductID, productE);
    92.         Debug.Log(noAdsProductID);
    93.  
    94.         UnityPurchasing.Initialize(this, builder);
    95.     }
    96.  
    97.     private bool IsInitialized()
    98.     {
    99.         return m_StoreController != null && m_StoreExtensionProvider != null;
    100.     }
    101.  
    102.     //---Check Initialization---//
    103.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    104.     {
    105.         if (iapReport == true)
    106.         {
    107.             Debug.Log("Unity IAP Initialized");
    108.         }
    109.         m_StoreController = controller;
    110.         m_StoreExtensionProvider = extensions;
    111.     }
    112.     public void OnInitializeFailed(InitializationFailureReason error)
    113.     {
    114.         if (iapReport == true)
    115.         {
    116.             Debug.Log("Unity IAP Failed: " + error);
    117.         }
    118.     }
    119.  
    120.     #endregion
    121.     //-----------------------------//
    122.  
    123.  
    124.  
    125.     //---Buy Products---//
    126.     #region BuyProducts
    127.  
    128.     public void BuyNoAds()
    129.     {
    130.         BuyProductID(noAdsProductID);
    131.         if (iapReport == true)
    132.         {
    133.             Debug.Log("Purchased: No Ads");
    134.         }
    135.     }
    136.  
    137.     public void BuyProductA()
    138.     {
    139.         BuyProductID(aProductID);
    140.         if (iapReport == true)
    141.         {
    142.             Debug.Log("Purchased: Product A");
    143.         }
    144.     }
    145.  
    146.     public void BuyProductB()
    147.     {
    148.         BuyProductID(bProductID);
    149.         if (iapReport == true)
    150.         {
    151.             Debug.Log("Purchased: Product B");
    152.         }
    153.     }
    154.  
    155.     public void BuyProductC()
    156.     {
    157.         BuyProductID(cProductID);
    158.         if (iapReport == true)
    159.         {
    160.             Debug.Log("Purchased: Product C");
    161.         }
    162.     }
    163.  
    164.     public void BuyProductD()
    165.     {
    166.         BuyProductID(dProductID);
    167.         if (iapReport == true)
    168.         {
    169.             Debug.Log("Purchased: Product C");
    170.         }
    171.     }
    172.  
    173.     public void BuyProductE()
    174.     {
    175.         BuyProductID(eProductID);
    176.         if (iapReport == true)
    177.         {
    178.             Debug.Log("Purchased: Product C");
    179.         }
    180.     }
    181.  
    182.     void BuyProductID(string productId)
    183.     {
    184.         if (IsInitialized())
    185.         {
    186.             Product product = m_StoreController.products.WithID(productId);
    187.  
    188.             if (product != null && product.availableToPurchase)
    189.             {
    190.                 Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
    191.                 m_StoreController.InitiatePurchase(product);
    192.             }
    193.             else
    194.             {
    195.                 if (iapReport == true)
    196.                 {
    197.                     Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    198.                 }
    199.             }
    200.         }
    201.         else
    202.         {
    203.             if (iapReport == true)
    204.             {
    205.                 Debug.Log("BuyProductID FAIL. Not initialized.");
    206.             }
    207.         }
    208.     }
    209.  
    210.     #endregion
    211.     //------------------//
    212.  
    213.  
    214.  
    215.     //---Restore Purchases--//
    216.     public void RestorePurchases()
    217.     {
    218.         if (!IsInitialized())
    219.         {
    220.             if (iapReport == true)
    221.             {
    222.                 Debug.Log("RestorePurchases FAIL. Not initialized.");
    223.             }
    224.             return;
    225.         }
    226.  
    227.         // If we are running on an Apple device ...
    228.         if (Application.platform == RuntimePlatform.IPhonePlayer ||
    229.             Application.platform == RuntimePlatform.OSXPlayer)
    230.         {
    231.             if (iapReport == true)
    232.             {
    233.                 Debug.Log("RestorePurchases started ...");
    234.             }
    235.  
    236.             var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    237.             apple.RestoreTransactions((result) =>
    238.             {
    239.                 if (iapReport == true)
    240.                 {
    241.                     Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    242.                 }
    243.             });
    244.         }
    245.  
    246.         else
    247.         {
    248.             // We are not running on an Apple device. No work is necessary to restore purchases.
    249.             if (iapReport == true)
    250.             {
    251.                 Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
    252.             }
    253.         }
    254.     }
    255.  
    256.  
    257.  
    258.     //---Purchase Resualts---//     Place procucts in here
    259.     #region Purchase Resualts
    260.  
    261.     //---PLACE PRODUCTS IN HERE---//
    262.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    263.     {
    264.         //---No Ads---//
    265.         if (string.Equals(args.purchasedProduct.definition.id, noAdsProductID, StringComparison.Ordinal))
    266.         {
    267.             BandzAdControl.instance.adsEnabled = false;
    268.             BandzAdControl.instance.userPurchasedNoAds = true;
    269.             BandzAdControl.instance.HideBanner();
    270.             BandzAdControl.instance.SaveAdData();
    271.  
    272.             if (iapReport == true)
    273.             {
    274.                 Debug.Log("No Ads Purchased");
    275.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));        //THIS IS WHERE YOU PURCHASE STUFF
    276.             }
    277.  
    278. //Other stuff
    279.         }
    Thanks a TON in advance! Im sure I just need a second set of eyes to catch something that I'm not seeing here ;)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I believe the editor always succeeds, so sadly, that's not something you can use to check for success.

    You can try using log viewer and see what sort of messages you get. Great way to see the console after you build to a device. https://www.assetstore.unity3d.com/en/#!/content/12047
     
    downhilldan likes this.
  3. downhilldan

    downhilldan

    Joined:
    Nov 16, 2016
    Posts:
    90
    Ive always wondered if there was something like that out there. It helped solved my problem! The problem was that my "Product A", "Product B", and so on had a space in the variable which broke the entire initialization of the IAP's even though my "noads" ID was correct. I fixed it by just having random ID names without spaces for those products.

    Thanks for your help as always!! :)
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Glad you were able to fix it! And yes, that is a great asset to have for mobile testing.
     
  5. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    i did initialization properly but button is not working in build apk
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I suggest you start your own post, show some code (with code tags) and explain a little of how it's set up and what is going wrong. You should also use the log viewer asset that I had linked above to help you view the console on Android and see what might be wrong. If a button isn't working, make sure you have it hooked in correctly, but use that asset to figure out what is going on.
     
  7. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    i used that asset but it does nothing
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Did you actually draw a circle with your finger to bring the overlay up?
    If you get the overlay, but don't see any messages, then you have nothing going on, which means your button isn't working. Make sure you have debug messages in place so you can get some feedback.
     
  9. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    yup asset is working and i got an error purchase failed because purchase was not initialized correctly