예제 #1
0
        /// <summary>
        /// Make an In App Purchase with this product ID
        /// </summary>
        /// <param name="braincloudProductId"></param>
        /// <param name="in_success"></param>
        /// <param name="in_failure"></param>
        public void BuyProductID(string braincloudProductId, SuccessCallback in_success = null, FailureCallback in_failure = null, bool in_forcedRealPurchase = false)
        {
            IAPProduct product = GetIAPProductByBrainCloudID(braincloudProductId);

#if DEBUG_IAP_ENABLED
            if (!in_forcedRealPurchase)
            {
                GStateManager.InitializeDelegate init = null;
                init = (BaseState state) =>
                {
                    IAPMessageSubState messageSubState = state as IAPMessageSubState;
                    if (messageSubState)
                    {
                        GStateManager.Instance.OnInitializeDelegate -= init;
                        messageSubState.LateInit(product, GPlayerMgr.Instance.OnVirtualCurrencies + in_success, in_failure);
                    }
                };

                GStateManager.Instance.OnInitializeDelegate += init;
                GStateManager.Instance.PushSubState(IAPMessageSubState.STATE_NAME);
            }
            else
#endif
            {
                handlePurchase(product.StoreProductId, GPlayerMgr.Instance.OnVirtualCurrencies + in_success, in_failure);
            }
        }
예제 #2
0
        public void ForceSuccess(IAPProduct in_product, SuccessCallback in_success, FailureCallback in_fail)
        {
            var itemsCount = in_product.CurrencyRewards.Count;

            // handle currecies
            foreach (KeyValuePair <string, object> entry in in_product.CurrencyRewards)
            {
                itemsCount--;
                var isLast = itemsCount == 0;

                Dictionary <string, object> currency = new Dictionary <string, object>();
                currency[BrainCloudConsts.JSON_CURRENCY_TYPE]   = entry.Key;
                currency[BrainCloudConsts.JSON_CURRENCY_AMOUNT] = entry.Value;

                var cbSuccess = isLast ? in_success : null;
                // TODO!! do we actually want to do this.. maybe only allow is Testers to call this
                GCore.Wrapper.Client.ScriptService.RunScript("AwardCurrency", JsonWriter.Serialize(currency), cbSuccess);
            }

            //check if there's a SuccessCallback and that there's no currencies
            if (in_success != null && in_product.CurrencyRewards.Count == 0)
            {
                //just handle rewards... currencies have been handled above
                Dictionary <string, object> jsonData = new Dictionary <string, object>();
                Dictionary <string, object> data     = new Dictionary <string, object>();
                Dictionary <string, object> rewards  = new Dictionary <string, object>();
                jsonData[BrainCloudConsts.JSON_DATA] = data;

                rewards[BrainCloudConsts.JSON_TOURNAMENT_REWARDS] = in_product.Rewards;
                data[BrainCloudConsts.JSON_TOURNAMENT_REWARDS]    = rewards;

                in_success.Invoke(JsonWriter.Serialize(jsonData), null);
            }
        }
예제 #3
0
        /// <summary>
        /// returns a product that has the requested braincloud id.
        /// </summary>
        /// <param name="in_id"></param>
        /// <returns></returns>
        public IAPProduct GetIAPProductByBrainCloudID(string in_id)
        {
            IAPProduct product = null;

            for (int index = 0; index < m_productInventory.Count; index++)
            {
                if (string.Equals(m_productInventory[index].BrainCloudProductID, in_id))
                {
                    product = m_productInventory[index];
                }
            }
            return(product);
        }
예제 #4
0
        /// <summary>
        /// returns a product that has the requested id.
        /// </summary>
        /// <param name="in_id"></param>
        /// <returns></returns>
        public IAPProduct GetIAPProductByStoreId(string in_id)
        {
            IAPProduct product = null;

            for (int index = 0; index < m_productInventory.Count; index++)
            {
                if (string.Equals(m_productInventory[index].StoreProductId, in_id))
                {
                    product = m_productInventory[index];
                }
            }
            return(product);
        }
예제 #5
0
        /// <summary>
        /// Callback for succesful ProductService.GetSalesInventory
        /// </summary>
        /// <param name="in_jsonString"></param>
        /// <param name="in_object"></param>
        public void OnReadInventory(string in_jsonString, object in_object)
        {
            //Build IAPProducts based on all platform responses
            // ios and google play store now supported [smrj]

            m_productInventory.Clear(); // Clearing to fill with fresh data

            Dictionary <string, object> jsonMessage = (Dictionary <string, object>)JsonReader.Deserialize(in_jsonString);
            Dictionary <string, object> jsonData    = (Dictionary <string, object>)jsonMessage[BrainCloudConsts.JSON_DATA];
            var jsonArray = jsonData[JSON_PRODUCT_INVENTORY] as Array;
            Dictionary <string, object> product;
            Dictionary <string, object> priceData;

#if UNITY_IOS
            Dictionary <string, object> idsObject; //itunes may have an array of ids for iphone, ipad and appletv
#endif
            Dictionary <string, object> currencyRewards;

            IAPProduct iapProduct = null;

#if BUY_CURRENCY_ENABLED && !STEAMWORKS_ENABLED
            var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
#endif
            for (int index = 0; index < jsonArray.Length; index++)
            {
                product   = ((Dictionary <string, object>)jsonArray.GetValue(index));
                priceData = ((Dictionary <string, object>)product[KEY_PRICEDATA]);

#if UNITY_ANDROID
                var storeId = (string)priceData[KEY_ANDROIDID];
#elif UNITY_WEBGL
                var storeId = (string)product[KEY_PRODUCT_FBURL];
#elif UNITY_IOS
                var idsArray = priceData[KEY_IDS] as Array;
                idsObject = ((Dictionary <string, object>)idsArray.GetValue(0));
                var storeId = (string)idsObject[KEY_ITUNESID];
#elif STEAMWORKS_ENABLED
                var storeId = ((int)priceData[KEY_PRODUCTID]).ToString();
#else
                var storeId = "";
                try
                {
                    // TODO add more store integration here [SMRJ]
                    storeId = (string)priceData[KEY_ANDROIDID];
                }
                catch (Exception)
                {
                    continue;
                }
#endif
                var braincloudID   = (string)product[KEY_PRODUCTID];
                var category       = (string)product[KEY_CATEGORY];
                var title          = (string)product[KEY_TITLE];
                var description    = (string)product[KEY_DESCRIPTION];
                var imageUrl       = (string)product[KEY_IMAGEURL];
                var referencePrice = Convert.ToDecimal(priceData[KEY_REFERENCEPRICE]);
                var price          = referencePrice / 100;
                var isPromotion    = (bool)priceData[KEY_ISPROMOTION];

                //get the value of the currency with the same name as the category
                currencyRewards = (Dictionary <string, object>)product[KEY_CURRENCY];

                // merge the peer / parent currencies within this dictionary as well
                if (product.ContainsKey(KEY_PARENT_CURRENCY))
                {
                    foreach (var item in product[KEY_PARENT_CURRENCY] as Dictionary <string, object> )
                    {
                        foreach (var currencyReward in item.Value as Dictionary <string, object> )
                        {
                            currencyRewards.Add(currencyReward.Key, currencyReward.Value);
                        }
                    }
                }

                if (product.ContainsKey(KEY_PEER_CURRENCY))
                {
                    foreach (var item in product[KEY_PEER_CURRENCY] as Dictionary <string, object> )
                    {
                        foreach (var currencyReward in item.Value as Dictionary <string, object> )
                        {
                            currencyRewards.Add(currencyReward.Key, currencyReward.Value);
                        }
                    }
                }

                object currencyVal;
                currencyRewards.TryGetValue(category, out currencyVal);

                var currencyValue = Convert.ToInt32(currencyVal);
                var type          = (string)product[KEY_TYPE];

#if BUY_CURRENCY_ENABLED
                var productType = ProductType.Consumable;

                switch (type.ToLower())
                {
                case PRODUCT_TYPE_SUBSCRIPTION:
                    productType = ProductType.Subscription;
                    break;

                case PRODUCT_TYPE_NON_CONSUMABLE:
                    productType = ProductType.NonConsumable;
                    break;

                case PRODUCT_TYPE_CONSUMABLE:
                default:
                    //productType = ProductType.Consumable; -> Already set as default value
                    break;
                }
#endif

                Dictionary <string, object> packRewards = (Dictionary <string, object>)product[BrainCloudConsts.JSON_DATA];

#if BUY_CURRENCY_ENABLED
                iapProduct = new IAPProduct(braincloudID, storeId, productType, category, title, description, imageUrl, referencePrice, price, isPromotion, currencyValue, currencyRewards, packRewards);
#else
                iapProduct = new IAPProduct(braincloudID, storeId, category, title, description, imageUrl, referencePrice, price, isPromotion, currencyValue, currencyRewards, packRewards);
#endif

                iapProduct.PriceString = String.Format("{0:c}", referencePrice / 100);

                //update the regular price on sale items
                if (isPromotion && product.ContainsKey(KEY_DEFAULTPRICEDATA))
                {
                    var defaultPriceData = ((Dictionary <string, object>)product[KEY_DEFAULTPRICEDATA]);
                    var refPrice         = Convert.ToDecimal(defaultPriceData[KEY_REFERENCEPRICE]);
#if UNITY_ANDROID
                    var regularId = (string)defaultPriceData[KEY_ANDROIDID];
#elif UNITY_WEBGL
                    var regularId = (string)product[KEY_PRODUCT_FBURL];
#elif STEAMWORKS_ENABLED
                    var regularId = ((int)defaultPriceData[KEY_PRODUCTID]).ToString();
#else
                    var ids       = defaultPriceData[KEY_IDS] as Array;
                    var regIdVal  = ((Dictionary <string, object>)ids.GetValue(0));
                    var regularId = (string)regIdVal[KEY_ITUNESID];
#endif

                    iapProduct.RegularPriceID     = regularId;
                    iapProduct.RegularPrice       = refPrice / 100;
                    iapProduct.RegularPriceString = String.Format("{0:c}", refPrice / 100);
#if BUY_CURRENCY_ENABLED && !STEAMWORKS_ENABLED                 // unity does not support steam works iap
                    builder.AddProduct(regularId, productType); //add this as a reference to the regular priced item
#endif
                }
#if BUY_CURRENCY_ENABLED && !STEAMWORKS_ENABLED                             // unity does not support steam works iap
                builder.AddProduct(iapProduct.StoreProductId, productType); //add to ConfigurationBuilder
#endif
                m_productInventory.Add(iapProduct);                         //add to List of products
            }
#if BUY_CURRENCY_ENABLED && !STEAMWORKS_ENABLED                             // unity does not support steam works iap
            UnityPurchasing.Initialize(this, builder);
#else
            GEventManager.TriggerEvent(GEventManager.ON_IAP_PRODUCTS_UPDATED);
#endif
        }
예제 #6
0
        private void handleProcessPurchase(string in_storeId, string in_receipt = null)
        {
            GStateManager.Instance.EnableLoadingSpinner(false);
            IsPurchasing = false;

            IAPProduct product = GetIAPProductByStoreId(in_storeId);

            if (product != null)
            {
                if (m_enableDebug)
                {
                    GDebug.Log(string.Format("PurchaseProcessingResult with id: {0}", product.StoreProductId));
                }

                //TODO: ADD MORE PLATFORMS[SMRJ]
#if UNITY_IOS
                Dictionary <string, object> jsonMessage = (Dictionary <string, object>)JsonReader.Deserialize(in_receipt);// args.purchasedProduct.receipt);
                // itunes
                Dictionary <string, object> receipt = new Dictionary <string, object>();
                receipt["receipt"] = (string)jsonMessage["Payload"];
                GCore.Wrapper.Client.AppStoreService.VerifyPurchase("itunes", JsonWriter.Serialize(receipt), m_successCallback, m_failureCallback);
#elif UNITY_ANDROID
                Dictionary <string, object> jsonMessage = (Dictionary <string, object>)JsonReader.Deserialize(in_receipt);// args.purchasedProduct.receipt);
                // ANDROID
                Dictionary <string, object> payload = (Dictionary <string, object>)JsonReader.Deserialize((string)jsonMessage["Payload"]);
                Dictionary <string, object> json    = (Dictionary <string, object>)JsonReader.Deserialize((string)payload["json"]);

                Dictionary <string, object> receipt = new Dictionary <string, object>();
                receipt["productId"] = json["productId"];
                receipt["orderId"]   = json["orderId"];
                receipt["token"]     = json["purchaseToken"];
                //receipt["developerPayload"] = json["developerPayload"];

                GCore.Wrapper.Client.AppStoreService.VerifyPurchase("googlePlay", JsonWriter.Serialize(receipt), m_successCallback, m_failureCallback);
#elif UNITY_WEBGL
                // TODO: need to confirm integration
                Dictionary <string, object> jsonMessage = (Dictionary <string, object>)JsonReader.Deserialize(in_receipt);//args.purchasedProduct.receipt);
                Dictionary <string, object> receipt     = new Dictionary <string, object>();
                receipt["signedRequest"] = (string)jsonMessage["Payload"];
                GCore.Wrapper.Client.AppStoreService.VerifyPurchase("facebook", JsonWriter.Serialize(receipt), m_successCallback, m_failureCallback);
#elif STEAMWORKS_ENABLED
                // STEAM
                Dictionary <string, object> purchaseData = new Dictionary <string, object>();
                purchaseData[BrainCloudConsts.JSON_LANGUAGE] = "en"; // TODO get proper language
                purchaseData[BrainCloudConsts.JSON_ITEM_ID]  = product.StoreProductId;

                // steam is a two step process, where you start a purchase, and then finalize it, but we keep these callbacks for later,
                // after processing the start purhcase succesfully
                GCore.Wrapper.Client.AppStoreService.StartPurchase("steam", JsonWriter.Serialize(purchaseData), onSteamStartPurchaseSuccess, m_failureCallback);
#endif
            }
            else
            {
                if (m_enableDebug)
                {
                    GDebug.LogError(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", in_storeId));
                }

                if (m_failureCallback != null)
                {
                    m_failureCallback.Invoke(-1, -1, "{'reason':'ProcessPurchase: FAIL. Unrecognized product' }", null);
                }
            }
        }
예제 #7
0
 public void LateInit(IAPProduct in_product, SuccessCallback in_success = null, FailureCallback in_fail = null)
 {
     m_product = in_product;
     m_success = in_success;
     m_fail    = in_fail;
 }