Search Unity

Purchasing Unavailable

Discussion in 'Unity IAP' started by leynier41, May 29, 2019.

  1. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    The Unity IAP does not initialize. The Purchasing Unavailable error is returned.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Purchasing;
    4. using static CodeStrange.LogNotification;
    5.  
    6. public class StoreManager : MonoBehaviour, IStoreListener
    7. {
    8.     private static StoreManager instance;
    9.     private static IStoreController storeController;
    10.     private static IExtensionProvider extensionProvider;
    11.     private static IAppleExtensions appleExtension;
    12.     private static IGooglePlayStoreExtensions googleExtension;
    13.    
    14.     public static StoreManager storeManager
    15.     {
    16.         get
    17.         {
    18.             if (instance == null)
    19.                 instance = new GameObject("Store Manager").AddComponent<StoreManager>();
    20.             return instance;
    21.         }
    22.     }
    23.    
    24.     private void Awake()
    25.     {
    26.         if (instance && instance.GetInstanceID() != GetInstanceID())
    27.             DestroyImmediate(gameObject);
    28.         else
    29.         {
    30.             instance = this;
    31.             DontDestroyOnLoad(gameObject);
    32.         }
    33.     }
    34.  
    35.     private void Start()
    36.     {
    37.         if (storeController == null)
    38.             StartCoroutine(InitializePurchasing());
    39.     }
    40.    
    41.     private IEnumerator InitializePurchasing()
    42.     {
    43.         if (IsInitialized())
    44.             yield break;
    45.  
    46.         Log("Inicializando la agregacion de los productos a la tienda.");
    47.  
    48.         var products = new[]
    49.         {
    50.             ("va_preplife_premium_1_month", ProductType.Subscription),
    51.             ("va_preplife_premium_6_months", ProductType.Subscription),
    52.             ("va_preplife_premium_1_year", ProductType.Subscription),
    53.             ("va_preplife_premium_forever", ProductType.NonConsumable)
    54.         };
    55.  
    56.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    57.         foreach (var product in products)
    58.         {
    59.             builder.AddProduct(product.Item1, product.Item2);
    60.             Log($"Product Key: {product.Item1}");
    61.         }
    62.  
    63.         Log("Termino la agregacion de los productos a la tienda.");
    64.  
    65.         UnityPurchasing.Initialize(this, builder);
    66.        
    67.         Log("Inicializando la tienda.");
    68.     }
    69.    
    70.     private bool IsInitialized()
    71.     {
    72.         return storeController != null && extensionProvider != null;
    73.     }
    74.  
    75.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    76.     {
    77.         storeController = controller;
    78.         extensionProvider = extensions;
    79.         appleExtension = extensions.GetExtension<IAppleExtensions>();
    80.         googleExtension = extensions.GetExtension<IGooglePlayStoreExtensions>();
    81.        
    82.         Log("Ejecutandose el callback OnInitialized.");
    83.     }
    84.  
    85.     public void OnInitializeFailed(InitializationFailureReason error)
    86.     {
    87.         Log($"Ejecutandose el callback OnInitializeFailed. Error: {error} ");
    88.     }
    89.  
    90.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
    91.     {
    92.         Log($"Compra del producto {e.purchasedProduct.metadata.localizedTitle} completada.");
    93.         return PurchaseProcessingResult.Complete;
    94.     }
    95.  
    96.     public void OnPurchaseFailed(Product p, PurchaseFailureReason e)
    97.     {
    98.         Log($"Compra del producto {p.metadata.localizedTitle} fallida por {e}.");
    99.     }
    100.    
    101.     public void BuyProduct(string id)
    102.     {
    103.         Log($"Inicializando compra del producto con id: {id}");
    104.        
    105.         if (!IsInitialized())
    106.         {
    107.             Log("BuyProduct FAIL. Not initialized.");
    108.             return;
    109.         }
    110.  
    111.         var product = storeController.products.WithID(id);
    112.        
    113.         if (product == null || !product.availableToPurchase)
    114.         {
    115.             Log("BuyProduct FAIL. Either is not found or is not available for purchase");
    116.             return;
    117.         }
    118.        
    119.         Log($"Inicializando compra del producto {product.metadata.localizedTitle} en la tienda.");
    120.         storeController.InitiatePurchase(product);
    121.     }
    122. }
    123.  
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    That would be expected for any number of reasons. Please provide additional information and the device logs.
     
  3. leynier41

    leynier41

    Joined:
    Oct 30, 2018
    Posts:
    11
    @JeffDUnity3D the problem was that the one who was testing the application store was in a country blocked by the United States. Thanks.