public void FetchAdditionalProducts()
        {
            var additionalProductsToFetch = new HashSet <ProductDefinition>
            {
                new ProductDefinition(diamondProductId, diamondType)
            };

            Debug.Log($"Fetching additional products in progress");

            m_StoreController.FetchAdditionalProducts(additionalProductsToFetch,
                                                      () =>
            {
                //Additional products fetched, they can now be purchased.
                Debug.Log($"Successfully fetched additional products");

                //We active the UI associated with the fetched product.
                additionalProductsPanel.SetActive(true);

                fetchAdditionalProductsButton.interactable = false;
            },
                                                      reason =>
            {
                Debug.Log($"Fetching additional products failed: {reason.ToString()}");
            });
        }
Example #2
0
        /// <summary>
        /// ストア商品リストを更新.
        /// </summary>
        /// <param name="productDefinitions"></param>
        private void UpdatePurchasing(ProductDefinition[] productDefinitions)
        {
            Action successCallback = () =>
            {
                StoreProducts = storeController.products.all
                                .Where(x => !string.IsNullOrEmpty(x.metadata.localizedTitle))
                                .Where(x => !string.IsNullOrEmpty(x.metadata.localizedPriceString))
                                .Where(x => productDefinitions.Any(y => y.id == x.definition.id && y.storeSpecificId == x.definition.storeSpecificId))
                                .ToArray();

                if (onStoreProductsUpdate != null)
                {
                    onStoreProductsUpdate.OnNext(StoreProducts);
                }
            };

            Action <InitializationFailureReason> failCallback = reason =>
            {
                var message = string.Format("UpdatePurchasing Error.({0})", reason);

                UnityConsole.Event(ConsoleEventName, ConsoleEventColor, message, LogType.Error);
            };

            storeController.FetchAdditionalProducts(productDefinitions.ToHashSet(), successCallback, failCallback);
        }
        /// <summary>
        /// Fetch additional products from the controlled store.
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        async UniTask <Product[]> FetchSubscriptionPurchase(string productId)
        {
            if (!IsInit)
            {
                Debug.LogWarning("Cannot fetch subscription. IAPManager not successfully initialized!");
                return(null);
            }

            HashSet <ProductDefinition> additionalProducts = new HashSet <ProductDefinition>()
            {
                new ProductDefinition(productId, productId, ProductType.Subscription)
            };

            Product[] result    = null;
            bool      isWaiting = true;

            DebugLog($"---FetchAdditionalProducts start!! productId = {productId}");

            storeController.FetchAdditionalProducts(additionalProducts, successCallback: () => {
                DebugLog($"---FetchAdditionalProducts success!! productId = {productId}");
                result = storeController.products.all.Where(p => p.definition.id == productId).ToArray();
#if DEBUG_IAP
                StringBuilder sb = new StringBuilder();
                foreach (var p in result)
                {
                    sb.Append("\n\n----------------\n");
                    foreach (PropertyInfo property in p.GetType().GetProperties())
                    {
                        sb.Append(property.Name).Append("=").Append(property.GetValue(p)).Append("\n");
                    }
                }

                DebugLog(sb);
#endif
                isWaiting = false;
            },
                                                    (InitializationFailureReason reason) => {
                DebugLog($"---FetchAdditionalProducts fail!! productId = {productId}");
                DebugLog(reason);
                isWaiting = false;
            });

            await UniTask.WaitWhile(() => isWaiting);

            return(result);
        }
        /// <summary>
        /// ストア商品リストを更新.
        /// </summary>
        /// <param name="productDefinitions"></param>
        private void UpdatePurchasing(ProductDefinition[] productDefinitions)
        {
            storeController.FetchAdditionalProducts(
                productDefinitions.ToHashSet(),
                () =>
            {
                StoreProducts = storeController.products.all
                                .Where(x => !string.IsNullOrEmpty(x.metadata.localizedTitle))
                                .Where(x => !string.IsNullOrEmpty(x.metadata.localizedPriceString))
                                .Where(x => productDefinitions.Any(y => y.id == x.definition.id && y.storeSpecificId == x.definition.storeSpecificId))
                                .ToArray();

                if (onStoreProductsUpdate != null)
                {
                    onStoreProductsUpdate.OnNext(StoreProducts);
                }
            },
                x => { Debug.LogErrorFormat("[PurchaseManager] UpdatePurchasing Error.({0})", x); }
                );
        }
Example #5
0
        public async Task FetchAsync()
        {
            ThrowIfDisposed();

            if (_storeController == null)
            {
                await InitializeAsync();
            }
            else if (_fetchOpCs != null)
            {
                await _fetchOpCs.Task;
            }
            else if (Application.isMobilePlatform || Application.isEditor)
            {
                _console.TraceEvent(TraceEventType.Start, _traceEventFetch, "Fetch");

                try
                {
                    _fetchOpCs = new TaskCompletionSource <object>();

                    // 1) Get store configuration. Should be provided by the service user.
                    var storeConfig = await _delegate.GetStoreConfigAsync();

                    var productsToFetch = new HashSet <ProductDefinition>();

                    // 2) Initialize the store content.
                    foreach (var product in storeConfig.Products)
                    {
                        var productDefinition = product.Definition;

                        if (_products.ContainsKey(productDefinition.id))
                        {
                            _products[productDefinition.id] = product;
                        }
                        else
                        {
                            _products.Add(productDefinition.id, product);
                        }

                        productsToFetch.Add(productDefinition);
                    }

                    // 3) Request the store data. This connects to real store and retrieves information on products specified in the previous step.
                    _storeController.FetchAdditionalProducts(productsToFetch, OnFetch, OnFetchFailed);
                    await _fetchOpCs.Task;

                    // 4) Trigger user-defined events.
                    InvokeInitializeCompleted(_traceEventFetch);
                }
                catch (StoreInitializeException e)
                {
                    InvokeInitializeFailed(_traceEventFetch, GetInitializeError(e.Reason), e);
                    throw;
                }
                catch (Exception e)
                {
                    _console.TraceData(TraceEventType.Error, _traceEventFetch, e);
                    InvokeInitializeFailed(_traceEventFetch, StoreInitializeError.Unknown, e);
                    throw;
                }
                finally
                {
                    _fetchOpCs = null;
                }
            }
        }