Search Unity

Purchases not processing on Android

Discussion in 'Scripting' started by Brandon-Keeler, Dec 18, 2015.

  1. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I created a purchasing script using Unity's new IAP system (see below), to handle all purchase related events. In the editor when I test purchases everything works perfectly but when I try to purchase on android nothing happens. I recently launched my game on the app store and integrated all of the items in the google play developer console, but it does not seem to be working.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.Purchasing;
    7.  
    8. public class Purchaser : MonoBehaviour, IStoreListener
    9. {
    10.     private static IStoreController m_StoreController; // Reference to the Purchasing system.
    11.     private static IExtensionProvider m_StoreExtensionProvider; // Reference to store-specific Purchasing subsystems.
    12.  
    13.     private static string kProductIDConsumable1 =    "stack_of_coins"; // General handle for the consumable product.
    14.     private static string kProductIDConsumable2 =    "pile_of_coins";
    15.     private static string kProductIDConsumable3 =    "bag_of_coins";
    16.     private static string kProductIDConsumable4 =    "chest_of_coins";
    17.     private static string kProductIDConsumable5 =    "vault_of_coins";
    18.  
    19.     private static string kProductNameAppleConsumable =    "com.bkgamesstudio.robotrun"; // Apple App Store identifier for the consumable product.
    20.     private static string kProductNameGooglePlayConsumable =    "com.bkgamesstudio.robotrun"; // Google Play Store identifier for the consumable product.
    21.  
    22.     public Text coinsText;
    23.     public Text gemText;
    24.  
    25.     public void Awake()
    26.     {
    27.         coinsText.text = GameControl.control.coins.ToString ();
    28.         gemText.text = GameControl.control.gems.ToString ();
    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.         // Add products to sell / restore by way of its identifier, associating the general identifier with its store-specific identifiers.
    52.         builder.AddProduct(kProductIDConsumable1, ProductType.Consumable, new IDs(){{ kProductNameAppleConsumable,       AppleAppStore.Name },{ kProductNameGooglePlayConsumable,  GooglePlay.Name },});
    53.         builder.AddProduct(kProductIDConsumable2, ProductType.Consumable, new IDs(){{ kProductNameAppleConsumable,       AppleAppStore.Name },{ kProductNameGooglePlayConsumable,  GooglePlay.Name },});
    54.         builder.AddProduct(kProductIDConsumable3, ProductType.Consumable, new IDs(){{ kProductNameAppleConsumable,       AppleAppStore.Name },{ kProductNameGooglePlayConsumable,  GooglePlay.Name },});
    55.         builder.AddProduct(kProductIDConsumable4, ProductType.Consumable, new IDs(){{ kProductNameAppleConsumable,       AppleAppStore.Name },{ kProductNameGooglePlayConsumable,  GooglePlay.Name },});
    56.         builder.AddProduct(kProductIDConsumable5, ProductType.Consumable, new IDs(){{ kProductNameAppleConsumable,       AppleAppStore.Name },{ kProductNameGooglePlayConsumable,  GooglePlay.Name },});
    57.         // 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.
    58.         UnityPurchasing.Initialize(this, builder);
    59.     }
    60.  
    61.     private bool IsInitialized()
    62.     {
    63.         // Only say we are initialized if both the Purchasing references are set.
    64.         return m_StoreController != null && m_StoreExtensionProvider != null;
    65.     }
    66.  
    67.     public void BuyStackOfCoins()
    68.     {
    69.         // Buy the consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    70.         BuyProductID(kProductIDConsumable1);
    71.         GameControl.control.Save ();
    72.         coinsText.text = GameControl.control.coins.ToString ();
    73.     }
    74.  
    75.     public void BuyPileOfCoins()
    76.     {
    77.         // Buy the consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    78.         BuyProductID(kProductIDConsumable2);
    79.         GameControl.control.Save ();
    80.         coinsText.text = GameControl.control.coins.ToString ();
    81.     }
    82.  
    83.     public void BuyBagOfCoins()
    84.     {
    85.         // Buy the consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    86.         BuyProductID(kProductIDConsumable3);
    87.         GameControl.control.Save ();
    88.         coinsText.text = GameControl.control.coins.ToString ();
    89.     }
    90.  
    91.     public void BuyChestOfCoins()
    92.     {
    93.         // Buy the consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    94.         BuyProductID(kProductIDConsumable4);
    95.         GameControl.control.Save ();
    96.         coinsText.text = GameControl.control.coins.ToString ();
    97.     }
    98.  
    99.     public void BuyVaultOfCoins()
    100.     {
    101.         // Buy the consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    102.         BuyProductID(kProductIDConsumable5);
    103.         GameControl.control.Save ();
    104.         coinsText.text = GameControl.control.coins.ToString ();
    105.     }
    106.  
    107.  
    108.     void BuyProductID(string productId)
    109.     {
    110.         // If the stores throw an unexpected exception, use try..catch to protect my logic here.
    111.         try
    112.         {
    113.             // If Purchasing has been initialized ...
    114.             if (IsInitialized())
    115.             {
    116.                 // ... look up the Product reference with the general product identifier and the Purchasing system's products collection.
    117.                 Product product = m_StoreController.products.WithID(productId);
    118.                 // If the look up found a product for this device's store and that product is ready to be sold ...
    119.                 if (product != null && product.availableToPurchase)
    120.                 {
    121.                     Debug.Log (string.Format("Purchasing product asychronously: '{0}'", product.definition.id));// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    122.                     m_StoreController.InitiatePurchase(product);
    123.                 }
    124.                 // Otherwise ...
    125.                 else
    126.                 {
    127.                     // ... report the product look-up failure situation
    128.                     Debug.Log ("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    129.                 }
    130.             }
    131.             // Otherwise ...
    132.             else
    133.             {
    134.                 // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or retrying initiailization.
    135.                 Debug.Log("BuyProductID FAIL. Not initialized.");
    136.             }
    137.         }
    138.         // Complete the unexpected exception handling ...
    139.         catch (Exception e)
    140.         {
    141.             // ... by reporting any unexpected exception for later diagnosis.
    142.             Debug.Log ("BuyProductID: FAIL. Exception during purchase. " + e);
    143.         }
    144.     }
    145.    
    146.     // Restore purchases previously made by this customer. Some platforms automatically restore purchases. Apple currently requires explicit purchase restoration for IAP.
    147.     public void RestorePurchases()
    148.     {
    149.         // If Purchasing has not yet been set up ...
    150.         if (!IsInitialized())
    151.         {
    152.             // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
    153.             Debug.Log("RestorePurchases FAIL. Not initialized.");
    154.             return;
    155.         }
    156.  
    157.         // If we are running on an Apple device ...
    158.         if (Application.platform == RuntimePlatform.IPhonePlayer ||
    159.             Application.platform == RuntimePlatform.OSXPlayer)
    160.         {
    161.             // ... begin restoring purchases
    162.             Debug.Log("RestorePurchases started ...");
    163.             // Fetch the Apple store-specific subsystem.
    164.             var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    165.             // 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.
    166.             apple.RestoreTransactions((result) => {
    167.             // The first phase of restoration. If no more responses are received on ProcessPurchase then no purchases are available to be restored.
    168.             Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    169.             });
    170.         }
    171.         // Otherwise ...
    172.         else
    173.         {
    174.             // We are not running on an Apple device. No work is necessary to restore purchases.
    175.             Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
    176.         }
    177.     }
    178.  
    179.     //
    180.     // --- IStoreListener
    181.     //
    182.  
    183.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    184.     {
    185.         // Purchasing has succeeded initializing. Collect our Purchasing references.
    186.         Debug.Log("OnInitialized: PASS");
    187.  
    188.         // Overall Purchasing system, configured with products for this application.
    189.         m_StoreController = controller;
    190.         // Store specific subsystem, for accessing device-specific store features.
    191.         m_StoreExtensionProvider = extensions;
    192.     }
    193.  
    194.  
    195.     public void OnInitializeFailed(InitializationFailureReason error)
    196.     {
    197.         // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    198.         Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    199.     }
    200.    
    201.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    202.     {
    203.         // A consumable product has been purchased by this user.
    204.         if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable1, StringComparison.Ordinal))
    205.         {
    206.             Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id)); //If the consumable item has been successfully purchased, add 1 coins.
    207.             GameControl.control.coins += 1000;
    208.         }
    209.         else if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable2, StringComparison.Ordinal))
    210.         {
    211.             Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id)); //If the consumable item has been successfully purchased, add 2 coins.
    212.             GameControl.control.coins += 2500;
    213.         }
    214.         else if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable3, StringComparison.Ordinal))
    215.         {
    216.             Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id)); //If the consumable item has been successfully purchased, add 1 coins.
    217.             GameControl.control.coins += 7500;
    218.         }
    219.         else if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable4, StringComparison.Ordinal))
    220.         {
    221.             Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id)); //If the consumable item has been successfully purchased, add 2 coins.
    222.             GameControl.control.coins += 12500;
    223.         }
    224.         else if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable5, StringComparison.Ordinal))
    225.         {
    226.             Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id)); //If the consumable item has been successfully purchased, add 2 coins.
    227.             GameControl.control.coins += 25000;
    228.         }
    229.         else
    230.         {
    231.             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.
    232.         }
    233.         return PurchaseProcessingResult.Complete;
    234.     }
    235.     public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    236.     {
    237.             // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing this reason with the user.
    238.             Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    239.     }
    240. }

     
    Last edited: Dec 18, 2015
  2. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    If anybody could help, that would be great, thanks!
     
  3. Phraxus

    Phraxus

    Joined:
    Dec 10, 2015
    Posts:
    7
    Having the same problem here with very similar code, works fine in editor but on a testers Android phone nothing. Having a nightmare trying to work it out.
     
  4. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    I went through this very recently and have the IAPs working. I don't see anything wrong with your code, and I'm using the exact same basis. So the problem is likely somewhere in Google Play configuration. I enabled Open Alpha testing, created a new gmail account, signed in to Google Play with that and used the Opt-In link from the alpha testing to download the game to get it working.
     
  5. ClaytonOne

    ClaytonOne

    Joined:
    Sep 5, 2015
    Posts:
    89
    Have you added your google api key?

    Code (CSharp):
    1. builder.Configure<IGooglePlayConfiguration>().SetPublicKey("myKey");
     
  6. Phraxus

    Phraxus

    Joined:
    Dec 10, 2015
    Posts:
    7
    So I've done a bit of playing around, mainly piping the debug info through to so UI text so I can see whats going on.

    My script is failing to initialise on startup on Android devices. (any advice on fixing this?)

    Possibly worth using the same technique to debug your script?

    P.S. yes I have the key added in my script :)
     
  7. ClaytonOne

    ClaytonOne

    Joined:
    Sep 5, 2015
    Posts:
    89
    Does OnInitializeFailed report a reason?
     
  8. Phraxus

    Phraxus

    Joined:
    Dec 10, 2015
    Posts:
    7
    Doesn't seem to be returning anything, possibly it's still initializing. going to do some further tests.
     
  9. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    @Phraxus Did you manage to get the IAP system working?
     
  10. Phraxus

    Phraxus

    Joined:
    Dec 10, 2015
    Posts:
    7
    I did, my problem was I had products being added code side that weren't set up on Google Play, meaning the purchaser never fully initialized