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] Unity IAP is not working on android device

Discussion in 'Unity IAP' started by Cutier73098, Apr 19, 2016.

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

    Cutier73098

    Joined:
    Apr 14, 2016
    Posts:
    7
    Hiiii

    my problem is that, i developed android app
    added unity IAP services to it
    i checked IAP in Unity editor it show working successfully but when i checked it on real device
    i click on buy then nothing happens....

    no google window pop ups
    nothing happening

    i checked product ids , they are same in script as well as in google product

    is i have to add another function?
    or i m missing some thing to do?
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Purchasing;
    5.  
    6. // Placing the Purchaser class in the CompleteProject namespace allows it to interact with ScoreManager, one of the existing Survival Shooter scripts.
    7.  
    8.  
    9.     // Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
    10.     public class Purchaser1 : MonoBehaviour, IStoreListener
    11.     {
    12.         private static IStoreController m_StoreController;                                                                  // Reference to the Purchasing system.
    13.         private static IExtensionProvider m_StoreExtensionProvider;                                                         // Reference to store-specific Purchasing subsystems.
    14.  
    15.         // Product identifiers for all products capable of being purchased: "convenience" general identifiers for use with Purchasing, and their store-specific identifier counterparts
    16.         // for use with and outside of Unity Purchasing. Define store-specific identifiers also on each platform's publisher dashboard (iTunes Connect, Google Play Developer Console, etc.)
    17.  
    18.         private static string kProductIDConsumable =    "consumable";                                                         // General handle for the consumable product.
    19.         private static string kProductIDNonConsumable = "nonconsumable";                                                  // General handle for the non-consumable product.
    20.         private static string kProductIDSubscription =  "subscription";                                                   // General handle for the subscription product.
    21.  
    22.         private static string kProductNameAppleConsumable =    "com.unity3d.test.services.purchasing.consumable";             // Apple App Store identifier for the consumable product.
    23.         private static string kProductNameAppleNonConsumable = "com.unity3d.test.services.purchasing.nonconsumable";      // Apple App Store identifier for the non-consumable product.
    24.         private static string kProductNameAppleSubscription =  "com.unity3d.test.services.purchasing.subscription";       // Apple App Store identifier for the subscription product.
    25.  
    26.         private static string kProductNameGooglePlayConsumable =    "com.unity3d.test.services.purchasing.consumable";        // Google Play Store identifier for the consumable product.
    27.         private static string kProductNameGooglePlayNonConsumable = "com.unity3d.test.services.purchasing.nonconsumable";     // Google Play Store identifier for the non-consumable product.
    28.         private static string kProductNameGooglePlaySubscription =  "com.unity3d.test.services.purchasing.subscription";  // Google Play Store identifier for the subscription product.
    29.  
    30.         void Start()
    31.         {
    32.             // If we haven't set up the Unity Purchasing reference
    33.             if (m_StoreController == null)
    34.             {
    35.                 // Begin to configure our connection to Purchasing
    36.                 InitializePurchasing();
    37.             }
    38.         }
    39.  
    40.         public void InitializePurchasing()
    41.         {
    42.             // If we have already connected to Purchasing ...
    43.             if (IsInitialized())
    44.             {
    45.                 // ... we are done here.
    46.                 return;
    47.             }
    48.  
    49.             // Create a builder, first passing in a suite of Unity provided stores.
    50.             var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    51.  
    52.             // Add a product to sell / restore by way of its identifier, associating the general identifier with its store-specific identifiers.
    53.             builder.AddProduct(kProductIDConsumable, ProductType.Consumable, new IDs(){{ kProductNameAppleConsumable,       AppleAppStore.Name },{ kProductNameGooglePlayConsumable,  GooglePlay.Name },});// Continue adding the non-consumable product.
    54.             builder.AddProduct(kProductIDNonConsumable, ProductType.NonConsumable, new IDs(){{ kProductNameAppleNonConsumable,       AppleAppStore.Name },{ kProductNameGooglePlayNonConsumable,  GooglePlay.Name },});// And finish adding the subscription product.
    55.             builder.AddProduct(kProductIDSubscription, ProductType.Subscription, new IDs(){{ kProductNameAppleSubscription,       AppleAppStore.Name },{ kProductNameGooglePlaySubscription,  GooglePlay.Name },});// Kick off the remainder of the set-up with an asynchrounous call, passing the configuration and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
    56.             UnityPurchasing.Initialize(this, builder);
    57.         }
    58.  
    59.  
    60.         private bool IsInitialized()
    61.         {
    62.             // Only say we are initialized if both the Purchasing references are set.
    63.             return m_StoreController != null && m_StoreExtensionProvider != null;
    64.         }
    65.  
    66.  
    67.         public void BuyConsumable()
    68.         {
    69.             // Buy the consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    70.             BuyProductID(kProductIDConsumable);
    71.         }
    72.  
    73.  
    74.         public void BuyNonConsumable()
    75.         {
    76.             // Buy the non-consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    77.             BuyProductID(kProductIDNonConsumable);
    78.         }
    79.  
    80.  
    81.         public void BuySubscription()
    82.         {
    83.             // Buy the subscription product using its the general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    84.             BuyProductID(kProductIDSubscription);
    85.         }
    86.  
    87.  
    88.         void BuyProductID(string productId)
    89.         {
    90.             // If the stores throw an unexpected exception, use try..catch to protect my logic here.
    91.             try
    92.             {
    93.                 // If Purchasing has been initialized ...
    94.                 if (IsInitialized())
    95.                 {
    96.                     // ... look up the Product reference with the general product identifier and the Purchasing system's products collection.
    97.                     Product product = m_StoreController.products.WithID(productId);
    98.  
    99.                     // If the look up found a product for this device's store and that product is ready to be sold ...
    100.                     if (product != null && product.availableToPurchase)
    101.                     {
    102.                         Debug.Log (string.Format("Purchasing product asychronously: '{0}'", product.definition.id));// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    103.                         m_StoreController.InitiatePurchase(product);
    104.                     }
    105.                     // Otherwise ...
    106.                     else
    107.                     {
    108.                         // ... report the product look-up failure situation
    109.                         Debug.Log ("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    110.                     }
    111.                 }
    112.                 // Otherwise ...
    113.                 else
    114.                 {
    115.                     // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or retrying initiailization.
    116.                     Debug.Log("BuyProductID FAIL. Not initialized.");
    117.                 }
    118.             }
    119.             // Complete the unexpected exception handling ...
    120.             catch (Exception e)
    121.             {
    122.                 // ... by reporting any unexpected exception for later diagnosis.
    123.                 Debug.Log ("BuyProductID: FAIL. Exception during purchase. " + e);
    124.             }
    125.         }
    126.  
    127.  
    128.         // Restore purchases previously made by this customer. Some platforms automatically restore purchases. Apple currently requires explicit purchase restoration for IAP.
    129.         public void RestorePurchases()
    130.         {
    131.             // If Purchasing has not yet been set up ...
    132.             if (!IsInitialized())
    133.             {
    134.                 // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
    135.                 Debug.Log("RestorePurchases FAIL. Not initialized.");
    136.                 return;
    137.             }
    138.  
    139.             // If we are running on an Apple device ...
    140.             if (Application.platform == RuntimePlatform.IPhonePlayer ||
    141.                 Application.platform == RuntimePlatform.OSXPlayer)
    142.             {
    143.                 // ... begin restoring purchases
    144.                 Debug.Log("RestorePurchases started ...");
    145.  
    146.                 // Fetch the Apple store-specific subsystem.
    147.                 var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    148.                 // Begin the asynchronous process of restoring purchases. Expect a confirmation response in the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
    149.                 apple.RestoreTransactions((result) => {
    150.                     // The first phase of restoration. If no more responses are received on ProcessPurchase then no purchases are available to be restored.
    151.                     Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    152.                 });
    153.             }
    154.             // Otherwise ...
    155.             else
    156.             {
    157.                 // We are not running on an Apple device. No work is necessary to restore purchases.
    158.                 Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
    159.             }
    160.         }
    161.  
    162.  
    163.         //
    164.         // --- IStoreListener
    165.         //
    166.  
    167.         public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    168.         {
    169.             // Purchasing has succeeded initializing. Collect our Purchasing references.
    170.             Debug.Log("OnInitialized: PASS");
    171.  
    172.             // Overall Purchasing system, configured with products for this application.
    173.             m_StoreController = controller;
    174.             // Store specific subsystem, for accessing device-specific store features.
    175.             m_StoreExtensionProvider = extensions;
    176.         }
    177.  
    178.  
    179.         public void OnInitializeFailed(InitializationFailureReason error)
    180.         {
    181.             // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    182.             Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    183.         }
    184.  
    185.  
    186.         public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    187.         {
    188.             // A consumable product has been purchased by this user.
    189.             if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable, StringComparison.Ordinal))
    190.             {
    191.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));//If the consumable item has been successfully purchased, add 100 coins to the player's in-game score.
    192.              
    193.             }
    194.  
    195.             // Or ... a non-consumable product has been purchased by this user.
    196.             else if (String.Equals(args.purchasedProduct.definition.id, kProductIDNonConsumable, StringComparison.Ordinal))
    197.             {
    198.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));}// Or ... a subscription product has been purchased by this user.
    199.             else if (String.Equals(args.purchasedProduct.definition.id, kProductIDSubscription, StringComparison.Ordinal))
    200.             {
    201.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));}// Or ... an unknown product has been purchased by this user. Fill in additional products here.
    202.             else
    203.             {
    204.                 Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));}// Return a flag indicating wither this product has completely been received, or if the application needs to be reminded of this purchase at next app launch. Is useful when saving purchased products to the cloud, and when that save is delayed.
    205.             return PurchaseProcessingResult.Complete;
    206.         }
    207.  
    208.  
    209.         public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    210.         {
    211.             // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing this reason with the user.
    212.             Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}",product.definition.storeSpecificId, failureReason));}
    213.     }
    214.  
     
  2. Erik-Hegen

    Erik-Hegen

    Joined:
    Aug 5, 2014
    Posts:
    17
    What does the output log say? (Install Android Studio and build with debug settings)
    I have a similar problem: The Callback method "OnInitializeFailed" is not called when I attempt to initialize the IAP again after loading a new scene. Under iOS, WinStandalone and Editor it works, only Anroid doesn't.
     
  3. Cutier73098

    Cutier73098

    Joined:
    Apr 14, 2016
    Posts:
    7

    i checked and the main thing is in Unity5.3.4 has a serious bug if we use any type of plugin and export it to android studio then it not take plugins completely or not generate export folder properly
    all type of plugin files are missing
     
  4. Cutier73098

    Cutier73098

    Joined:
    Apr 14, 2016
    Posts:
    7

    have any suggestion what to do now?
     
  5. Erik-Hegen

    Erik-Hegen

    Joined:
    Aug 5, 2014
    Posts:
    17
    You don't have to export your project or parts of it into Android Studio. Just connect your device to your computer, install the device' adb driver and start android studio. Create a new project or use an existing. That doesn't matter you only need the logcat output.
    Then run the app and initialize your payment
     
    nicholasr likes this.
  6. Erik-Hegen

    Erik-Hegen

    Joined:
    Aug 5, 2014
    Posts:
    17
    I investigated for awhile and I can say that the IStoreController and IExtensionProvider has to be static as they can not be initialized twice under android. In my implementation they were deleted with the other data of the payment. So I had to initialize the payment again.
    Unfortunatly i hate making things static as they cannot be inherited or overridden. Another workaround may be not to destroy the monobehaviour at scene loading.

    Sorry, Cutier for only fixing my own problem :(
     
  7. nicholasr

    nicholasr

    Joined:
    Aug 15, 2015
    Posts:
    183
  8. Cutier73098

    Cutier73098

    Joined:
    Apr 14, 2016
    Posts:
    7

    Hye hi

    i found whats the problem...

    problem is that unity not exporting project properly...

    in android studio i saw errors that


    Unity: AndroidJavaException: java.lang.ClassNotFoundException: com.unity.purchasing.googleplay.GooglePlayPurchasing
    java.lang.ClassNotFoundException: com.unity.purchasing.googleplay.GooglePlayPurchasing
    etc etc...

    thats happens with every plugin like facebook sdk and soomla Iap errors are according to the pugins...

    i reported bug

    have any idea how i can solve that problem...

    because my game is online and have to add IAP , FB sdk
     
    Last edited: Apr 21, 2016
  9. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    Please see this answer for working around the ClassNotFoundException when exporting an android studio project. This will be fixed in a forthcoming Unity release.
     
  10. manny003

    manny003

    Joined:
    Mar 18, 2014
    Posts:
    69
    I've posted this question in Unity Answers but got zero responses so I'm trying here.

    Original posting is at: http://answers.unity3d.com/questions/1182397/updaterefresh-screen-before-iap-initiatepurchase.html

    I've been working on implementing Unity's IAP into my game for both iOS and Android (currently focusing on Android for this question), and it has been working fine so far.

    In my design, whenever the user taps on a purchase button, I make screen changes, like disabling items, changing colors, showing the spinning graphic image, etc -- to provide visual cues.

    I make all the necessary changes and then invoke the InitiatePurchase method in the IAP Controller. However, the IAP popup appears instantly before any of my screen changes takes effect. I've tried adding a "yield" command with "Wait For End Of Frame"; adding a flag that gets set in the Update function and checking that flag in a yield loop.

    None of these works to allow the screen changes to take effect before InitiatePurchase takes over.

    How can I do this?

    Thanks, Manny
     
  11. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    If you want to delay doing something I suggest you use a coroutine with WaitForSeconds
     
  12. pea

    pea

    Joined:
    Oct 29, 2013
    Posts:
    98
    I keep getting a bunch of PurchaseFailureReason.Unknown errors back from my live Android devices. The purchases work fine when I test them, even with an account that's not set up for testing (i.e. actual live purchase).

    I have remote logging set up and every day I get multiple PurchaseFailureReason.Unknown come back from devices. It's impossible to debug it seems, as there's no added information.

    It'd be excellent if some underlying error would be accessible, rather than a useless PurchaseFailureReason.Unknown. Not sure if this is possible.

    I'm at a loss currently. Been looking at this for several days now.
     
  13. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
Thread Status:
Not open for further replies.