Search Unity

Sandbox test on iPhone are not working

Discussion in 'Unity IAP' started by tonOnWu, Mar 1, 2020.

  1. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    83
    Hi, guys.

    I test my IAP functionality on Unity and everything is working perfectly (get data about my items, I can purchase and restore), but when I try it on my iPhone with Sandbox, I don't even get the information about the item.

    I'm using UNITY 2019.3.06, iPhone X, iOS 10, and the IAP package is 1.23.1.

    This is my current code:
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Purchasing;
    6.  
    7. // Placing the Purchaser class in the CompleteProject namespace allows it to interact with ScoreManager,
    8. // one of the existing Survival Shooter scripts.
    9. //namespace CompleteProject
    10. //{
    11.     // Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
    12.     public class IAPManager : Singleton<IAPManager>, IStoreListener
    13.     {
    14.         private static IStoreController m_StoreController;          // The Unity Purchasing system.
    15.         private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
    16.  
    17.         public string NO_ADS = "NO_ADS";
    18.  
    19.         void Start()
    20.         {
    21.             // If we haven't set up the Unity Purchasing reference
    22.             if (m_StoreController == null)
    23.             {
    24.                 // Begin to configure our connection to Purchasing
    25.                 InitializePurchasing();
    26.             }
    27.         }
    28.  
    29.         public void InitializePurchasing()
    30.         {
    31.             // If we have already connected to Purchasing ...
    32.             if (IsInitialized())
    33.             {
    34.                 // ... we are done here.
    35.                 return;
    36.             }
    37.  
    38.             // Create a builder, first passing in a suite of Unity provided stores.
    39.             var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    40.  
    41.             // Continue adding the non-consumable product.
    42.             builder.AddProduct(NO_ADS, ProductType.NonConsumable);
    43.  
    44.             // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
    45.             // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
    46.             UnityPurchasing.Initialize(this, builder);
    47.         }
    48.  
    49.         public bool IsInitialized()
    50.         {
    51.             // Only say we are initialized if both the Purchasing references are set.
    52.             return m_StoreController != null && m_StoreExtensionProvider != null;
    53.         }
    54.  
    55.         public void BuyNoAds()
    56.         {
    57.             // Buy the non-consumable product using its general identifier. Expect a response either
    58.             // through ProcessPurchase or OnPurchaseFailed asynchronously.
    59.             BuyProductID(NO_ADS);
    60.         }
    61.  
    62.         public string GetProductPriceFromStore(string id)
    63.         {
    64.             if (m_StoreController != null && m_StoreController.products != null)
    65.                 return m_StoreController.products.WithID(id).metadata.localizedPriceString;
    66.             else
    67.             {
    68.                 Debug.Log("ERROR: Can't find the price of the purchasing product");
    69.                 return "";
    70.             }
    71.         }
    72.  
    73.         void BuyProductID(string productId)
    74.         {
    75.             // If Purchasing has been initialized ...
    76.             if (IsInitialized())
    77.             {
    78.                 // ... look up the Product reference with the general product identifier and the Purchasing
    79.                 // system's products collection.
    80.                 Product product = m_StoreController.products.WithID(productId);
    81.  
    82.                 // If the look up found a product for this device's store and that product is ready to be sold ...
    83.                 if (product != null && product.availableToPurchase)
    84.                 {
    85.                     Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
    86.                     // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
    87.                     // asynchronously.
    88.                     m_StoreController.InitiatePurchase(product);
    89.                 }
    90.                 // Otherwise ...
    91.                 else
    92.                 {
    93.                     // ... report the product look-up failure situation
    94.                     Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    95.                 }
    96.             }
    97.             // Otherwise ...
    98.             else
    99.             {
    100.                 // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
    101.                 // retrying initiailization.
    102.                 Debug.Log("BuyProductID FAIL. Not initialized.");
    103.             }
    104.         }
    105.  
    106.  
    107.         // Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
    108.         // Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
    109.         public void RestorePurchases()
    110.         {
    111.             // If Purchasing has not yet been set up ...
    112.             if (!IsInitialized())
    113.             {
    114.                 // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
    115.                 Debug.Log("RestorePurchases FAIL. Not initialized.");
    116.                 return;
    117.             }
    118.  
    119.             // If we are running on an Apple device ...
    120.             if (Application.platform == RuntimePlatform.IPhonePlayer ||
    121.                 Application.platform == RuntimePlatform.OSXPlayer)
    122.             {
    123.                 // ... begin restoring purchases
    124.                 Debug.Log("RestorePurchases started ...");
    125.  
    126.                 // Fetch the Apple store-specific subsystem.
    127.                 var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    128.                 // Begin the asynchronous process of restoring purchases. Expect a confirmation response in
    129.                 // the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
    130.                 apple.RestoreTransactions((result) => {
    131.                     // The first phase of restoration. If no more responses are received on ProcessPurchase then
    132.                     // no purchases are available to be restored.
    133.                     Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    134.                 });
    135.             }
    136.             // Otherwise ...
    137.             else
    138.             {
    139.                 // We are not running on an Apple device. No work is necessary to restore purchases.
    140.                 Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
    141.             }
    142.         }
    143.  
    144.  
    145.         //
    146.         // --- IStoreListener
    147.         //
    148.  
    149.         public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    150.         {
    151.             // Purchasing has succeeded initializing. Collect our Purchasing references.
    152.             Debug.Log("OnInitialized: PASS");
    153.  
    154.             // Overall Purchasing system, configured with products for this application.
    155.             m_StoreController = controller;
    156.             // Store specific subsystem, for accessing device-specific store features.
    157.             m_StoreExtensionProvider = extensions;
    158.         }
    159.  
    160.  
    161.         public void OnInitializeFailed(InitializationFailureReason error)
    162.         {
    163.             // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    164.             Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    165.         }
    166.  
    167.  
    168.         public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    169.         {
    170.             // A non-consumable NO_ADS product has been purchased by this user.
    171.             if (String.Equals(args.purchasedProduct.definition.id, NO_ADS, StringComparison.Ordinal))
    172.             {
    173.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    174.                 IAPDataManager.Instance.RemoveAds();
    175.             }
    176.             else
    177.             {
    178.                 Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
    179.             }
    180.  
    181.             // Return a flag indicating whether this product has completely been received, or if the application needs
    182.             // to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
    183.             // saving purchased products to the cloud, and when that save is delayed.
    184.             return PurchaseProcessingResult.Complete;
    185.         }
    186.  
    187.  
    188.         public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    189.         {
    190.             // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
    191.             // this reason with the user to guide their troubleshooting actions.
    192.             Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    193.         }
    194.     }
    195. //}
     
  2. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
  3. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    83
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Glad you got it working