Search Unity

[Closed] IAP failure on first run, success on following runs

Discussion in 'Unity IAP' started by rxmarccall, Dec 27, 2015.

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

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    I'm testing on Android, when I run the app for the first time I get the following error messages:

    When I force close the app and run it again, I get success messages....


    Any ideas about what I'm doing wrong that makes the first initial run of the app fail IAP initialization? I only have 1 scene and my IAP script gets run right when the scene loads, do I need to wait a few frames before the script is run?

    Here is my IAP script:
    Code (CSharp):
    1. public class Purchaser : MonoBehaviour, IStoreListener
    2. {
    3.     private static IStoreController m_StoreController;                                                                  // Reference to the Purchasing system.
    4.     private static IExtensionProvider m_StoreExtensionProvider;                                                         // Reference to store-specific Purchasing subsystems.
    5.  
    6.     // Product identifiers for all products capable of being purchased: "convenience" general identifiers for use with Purchasing, and their store-specific identifier counterparts
    7.     // 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.)
    8.  
    9.  
    10.     private static string kProductIDConsumable =    "consumable";                                                         // General handle for the consumable product.
    11.     private static string kProductIDNonConsumable = "nonconsumable";                                                  // General handle for the non-consumable product.
    12.     private static string kProductIDSubscription =  "subscription";                                                   // General handle for the subscription product.
    13.  
    14.     private List<string> productNames = new List<string>();
    15.  
    16.     void Start()
    17.     {
    18.      
    19.         // If we haven't set up the Unity Purchasing reference
    20.         if (m_StoreController == null)
    21.         {
    22.             // Begin to configure our connection to Purchasing
    23.             InitializePurchasing();
    24.         }
    25.     }
    26.  
    27.     public void InitializePurchasing()
    28.     {
    29.         // If we have already connected to Purchasing ...
    30.         if (IsInitialized())
    31.         {
    32.             // ... we are done here.
    33.             return;
    34.         }
    35.  
    36.         // Create a builder, first passing in a suite of Unity provided stores.
    37.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    38.  
    39.         // Add a product to sell / restore by way of its identifier, associating the general identifier with its store-specific identifiers.
    40.         // Cycle through list of product ID's given to us in Config.json and add products
    41.         foreach (var productID in gamestate.Instance.iapProducts){
    42.             //builder.AddProduct(productID.Value, ProductType.NonConsumable, new IDs(){ {productID.Key, GooglePlay.Name } });
    43.             builder.AddProduct(productID.Key, ProductType.NonConsumable);
    44.             productNames.Add(productID.Key);
    45.  
    46.         }
    47.  
    48.         UnityPurchasing.Initialize(this, builder);
    49.     }
    50.  
    51.  
    52.     private bool IsInitialized()
    53.     {
    54.         // Only say we are initialized if both the Purchasing references are set.
    55.         return m_StoreController != null && m_StoreExtensionProvider != null;
    56.     }
    57.  
    58.  
    59.     public void BuyConsumable()
    60.     {
    61.         // Buy the consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    62.         BuyProductID(kProductIDConsumable);
    63.     }
    64.  
    65.  
    66.     public void BuyNonConsumable()
    67.     {
    68.         // Buy the non-consumable product using its general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    69.         BuyProductID(kProductIDNonConsumable);
    70.     }
    71.  
    72.  
    73.     public void BuySubscription()
    74.     {
    75.         // Buy the subscription product using its the general identifier. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    76.         BuyProductID(kProductIDSubscription);
    77.     }
    78.  
    79.  
    80.     void BuyProductID(string productId)
    81.     {
    82.         // If the stores throw an unexpected exception, use try..catch to protect my logic here.
    83.         try
    84.         {
    85.             // If Purchasing has been initialized ...
    86.             if (IsInitialized())
    87.             {
    88.                 // ... look up the Product reference with the general product identifier and the Purchasing system's products collection.
    89.                 Product product = m_StoreController.products.WithID(productId);
    90.  
    91.                 // If the look up found a product for this device's store and that product is ready to be sold ...
    92.                 if (product != null && product.availableToPurchase)
    93.                 {
    94.                     Debug.Log (string.Format("Purchasing product asychronously: '{0}'", product.definition.id));// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed asynchronously.
    95.                     m_StoreController.InitiatePurchase(product);
    96.                 }
    97.                 // Otherwise ...
    98.                 else
    99.                 {
    100.                     // ... report the product look-up failure situation
    101.                     Debug.Log ("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    102.                 }
    103.             }
    104.             // Otherwise ...
    105.             else
    106.             {
    107.                 // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or retrying initiailization.
    108.                 Debug.Log("BuyProductID FAIL. Not initialized.");
    109.             }
    110.         }
    111.         // Complete the unexpected exception handling ...
    112.         catch (Exception e)
    113.         {
    114.             // ... by reporting any unexpected exception for later diagnosis.
    115.             Debug.Log ("BuyProductID: FAIL. Exception during purchase. " + e);
    116.         }
    117.     }
    118.  
    119.  
    120.     // Restore purchases previously made by this customer. Some platforms automatically restore purchases. Apple currently requires explicit purchase restoration for IAP.
    121.     public void RestorePurchases()
    122.     {
    123.         // If Purchasing has not yet been set up ...
    124.         if (!IsInitialized())
    125.         {
    126.             // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
    127.             Debug.Log("RestorePurchases FAIL. Not initialized.");
    128.             return;
    129.         }
    130.  
    131.         // If we are running on an Apple device ...
    132.         if (Application.platform == RuntimePlatform.IPhonePlayer ||
    133.             Application.platform == RuntimePlatform.OSXPlayer)
    134.         {
    135.             // ... begin restoring purchases
    136.             Debug.Log("RestorePurchases started ...");
    137.  
    138.             // Fetch the Apple store-specific subsystem.
    139.             var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    140.             // 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.
    141.             apple.RestoreTransactions((result) => {
    142.                 // The first phase of restoration. If no more responses are received on ProcessPurchase then no purchases are available to be restored.
    143.                 Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    144.             });
    145.         }
    146.         // Otherwise ...
    147.         else
    148.         {
    149.             // We are not running on an Apple device. No work is necessary to restore purchases.
    150.             Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
    151.         }
    152.     }
    153.  
    154.  
    155.     //
    156.     // --- IStoreListener
    157.     //
    158.  
    159.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    160.     {
    161.         // Purchasing has succeeded initializing. Collect our Purchasing references.
    162.         Debug.Log("OnInitialized: PASS");
    163.  
    164.         // Overall Purchasing system, configured with products for this application.
    165.         m_StoreController = controller;
    166.         // Store specific subsystem, for accessing device-specific store features.
    167.         m_StoreExtensionProvider = extensions;
    168.     }
    169.  
    170.  
    171.     public void OnInitializeFailed(InitializationFailureReason error)
    172.     {
    173.         // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    174.         Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    175.     }
    176.  
    177.  
    178.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    179.     {
    180.         // A consumable product has been purchased by this user.
    181.         if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable, StringComparison.Ordinal))
    182.         {
    183.             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.
    184.             //ScoreManager.score += 100;
    185.         }
    186.  
    187.         // Or ... a non-consumable product has been purchased by this user.
    188.         else if (productNames.Contains(args.purchasedProduct.definition.id))
    189.         {
    190.             Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    191.             int volNum;
    192.             int.TryParse(gamestate.Instance.iapProducts[args.purchasedProduct.definition.id], out volNum);
    193.             gamestate.Instance.purchasedVolumes[volNum] = true;
    194.             gamestate.Instance.SavePurchasedVolumes();
    195.         }// Or ... a subscription product has been purchased by this user.
    196.         else
    197.         {
    198.             Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
    199.         }// 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.
    200.         return PurchaseProcessingResult.Complete;
    201.     }
    202.  
    203.  
    204.     public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    205.     {
    206.         // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing this reason with the user.
    207.         Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}",product.definition.storeSpecificId, failureReason));}
    208.  
     
  2. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    Where does gameState.Instance.iapProducts come from? If it is empty then you'll init without any products so they'll all be unknown to Unity IAP.
     
  3. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    @Banderous gameState is a singleton that holds settings, and iapProducts either downloads a .json file with my product id's or if loads them from disk if there were no updates. Thats a good point though, I bet iapProducts isn't getting populated in time! I think I'll try moving my iapProduct population to a scene before my main scene that has the IAP script to make sure its populated. I'll give that a try and let you know.
     
Thread Status:
Not open for further replies.