Exemplo n.º 1
0
        /// <summary>
        /// <para>
        /// Fetch Apprien variant IAP id for the given product.
        /// NOTE: Only use this overload for fetching single products, if required by game/store logic.
        /// Use the other overload when fetching multiple products, to save on request volume.
        /// </para>
        /// <para>>
        /// Prices are located in the Apprien -generated IAP id variants. Typically
        /// the actual prices are fetched from the Store (Google or Apple) by the
        /// StoreManager by providing the IAP id (or in this case the variant).
        /// </para>
        /// </summary>
        /// <param name="product">Apprien.Product instance. After the request completes, will contain the Apprien IAP id variant.</param>
        /// <param name="callback">Callback that is called when the request finishes. Takes string argument, containing the resolved IAP id.</param>
        /// <returns>Returns an IEnumerator that can be forwarded manually or passed to StartCoroutine.</returns>
        public IEnumerator FetchApprienPrice(ApprienProduct product, Action callback = null)
        {
            var requestSendTimestamp = DateTime.Now;
            var url = string.Format(ApprienUtility.REST_GET_PRICE_URL, StoreIdentifier, GamePackageName, product.BaseIAPId);

            using (var request = UnityWebRequest.Get(url))
            {
                request.SetRequestHeader("Authorization", "Bearer " + Token);
                request.SetRequestHeader("Session-Id", ApprienIdentifier);
                ApprienUtility.SendWebRequest(request);

                while (!request.isDone)
                {
                    // Timeout the request and return false
                    if ((DateTime.Now - requestSendTimestamp).TotalSeconds > RequestTimeout)
                    {
                        if (callback != null)
                        {
                            callback();
                        }
                        yield break;
                    }

                    // Specify that the request is still in progress
                    yield return(null);
                }

                if (ApprienUtility.IsHttpError(request))
                {
                    // send apprien api info about the error
                    SendError((int)request.responseCode, "Error occured while fetching Apprien prices. HTTP error: " + request.downloadHandler.text);
                }
                else if (ApprienUtility.IsNetworkError(request))
                {
                    // send apprien api info about the error
                    SendError((int)request.responseCode, "Error occured while fetching Apprien prices. Network error");
                }
                else
                {
                    if (request.responseCode == 200)
                    {
                        // Apprien IAP id variant fetched, apply it to the given product and
                        var apprienVariantIAPid = request.downloadHandler.text;
                        product.ApprienVariantIAPId = apprienVariantIAPid;
                    }
                    else
                    {
                        // If Apprien returns a non-200 message code, return base IAP id price
                        SendError((int)request.responseCode, "Error occured while fetching Apprien prices");
                        Debug.Log("Apprien request error: " + request.responseCode + ". " + request.downloadHandler.text);
                    }
                }

                // Regardless of the outcome, execute the callback
                if (callback != null)
                {
                    callback();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates ApprienProduct objects from the products already added to the given builder.
        /// Does not add any products to the builder.
        /// </summary>
        /// <param name="builder">Reference to a builder containing products.</param>
        /// <returns>Returns an array of Apprien Products built from the given ConfigurationBuilder object</returns>
        public static ApprienProduct[] FromConfigurationBuilder(ConfigurationBuilder builder)
        {
            var products = new ApprienProduct[builder.products.Count];
            var i        = 0;

            // HashSet cannot be indexed with [i]
            foreach (var product in builder.products)
            {
                products[i++] = new ApprienProduct(product.id, product.type);
            }

            return(products);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Convert a Unity IAP Product Catalog into ApprienProduct objects ready for fetching Apprien prices.
        /// Does not alter the catalog
        /// </summary>
        /// <param name="catalog"></param>
        /// <returns>Returns an array of Apprien Products built from the given ProductCatalog object</returns>
        public static ApprienProduct[] FromIAPCatalog(ProductCatalog catalog)
        {
            var catalogProducts = catalog.allValidProducts;

            /*
             * // TODO: Get the store-specific products
             * foreach (var product in catalogProducts)
             * {
             *  Debug.Log(product.GetStoreID("GooglePlay"));
             *  Debug.Log(product.GetStoreID("AppleAppStore"));
             * }
             */
            var products = new ApprienProduct[catalogProducts.Count];

            var i = 0;

            // ICollection cannot be indexed with [i], foreach required
            foreach (var catalogProduct in catalogProducts)
            {
                products[i++] = new ApprienProduct(catalogProduct.id, catalogProduct.type);
            }

            return(products);
        }
Exemplo n.º 4
0
        public override void OnInspectorGUI()
        {
            // Display the Inspector properties defined in the ApprienConnection ScriptableObject, i.e. the token
            DrawDefaultInspector();

            if (_apprienConnection.stringValue.Length == 0)
            {
                EditorGUILayout.HelpBox("Provide the authentication token into the field above before testing the connection. You should see a list of all products loaded into Apprien for the game.", MessageType.Info);
                return;
            }

            if (_apprienManager == null)
            {
                EditorGUILayout.HelpBox("The assembly containing this editor script was recompiled. Please deselect and reselect the asset.", MessageType.Error);
                return;
            }

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("API Integration", EditorStyles.boldLabel);

            if (GUILayout.Button("Test Connection"))
            {
                _connectionCheckPressed = false;
                _fetchingStatus         = true;

                // Refresh the token to the manager before testing connection
                var token = _apprienConnection.stringValue;
                _apprienManager.Token = token;

                _initializeFetch = TestConnection((available, valid) =>
                {
                    _fetchingStatus         = false;
                    _connectionCheckPressed = true;
                    _connectionOK           = available;
                    _tokenOK = valid;
                });
            }

            EditorGUILayout.LabelField("Status:");
            if (_fetchingStatus)
            {
                EditorGUILayout.LabelField("  Loading...");
            }

            if (_connectionCheckPressed)
            {
                // Display connection information
                EditorGUILayout.LabelField(_connectionOK ?
                                           "  Apprien API connection is OK." :
                                           "  Apprien API connection failed.");

                EditorGUILayout.LabelField(_tokenOK ?
                                           "  Apprien Token is OK" :
                                           "  Apprien Token is invalid.");
            }

            EditorGUI.BeginDisabledGroup(_connectionOK == false || _tokenOK == false);
            EditorGUILayout.HelpBox("After testing the connection, click below to fetch all Apprien generated IAP variants for products defined in the default IAP Catalog. Make sure the correct package name is set in Player settings, i.e. your com.company.product identifier.", MessageType.Info);

            // Enable after active/inactive products are supported in Apprien
            //_onlyActiveProducts = EditorGUILayout.ToggleLeft("Fetch only active products", _onlyActiveProducts);

            _catalogResourceName = EditorGUILayout.TextField("Catalog resource name", _catalogResourceName);

            if (GUILayout.Button("Test fetching Apprien-generated products"))
            {
                _fetchingProducts = true;
                _fetchedProducts.Clear();

                var catalogFile = Resources.Load <TextAsset>(_catalogResourceName);
                if (catalogFile != null)
                {
                    // catalog file exists
                    var catalog  = ProductCatalog.FromTextAsset(catalogFile);
                    var products = ApprienProduct.FromIAPCatalog(catalog);

                    if (products.Length == 0)
                    {
                        _anyProducts = false;
                    }
                    else
                    {
                        _anyProducts = true;
                        _pricesFetch = _apprienManager.FetchApprienPrices(products, () =>
                        {
                            _fetchingProducts = false;
                            _fetchedProducts  = products.ToList();
                        });
                    }
                }
                else
                {
                    _anyProducts = false;
                }
            }

            if (!_anyProducts)
            {
                // catalog file does not exist
                EditorGUILayout.LabelField("Could not load catalog file: " + _catalogResourceName);
                return;
            }

            EditorGUILayout.LabelField("Products:");

            if (_fetchingProducts)
            {
                EditorGUILayout.LabelField("  Loading...");
            }
            else
            {
                foreach (var product in _fetchedProducts)
                {
                    EditorGUILayout.LabelField("  Base Product ID: " + product.BaseIAPId);
                    EditorGUILayout.LabelField("  Apprien variant ID: " + product.ApprienVariantIAPId);
                    EditorGUILayout.Space();
                }
            }

            EditorGUI.EndDisabledGroup();
        }