Example #1
0
        public UnityTask <PurchaseResult> ShowStoreItem(bool automaticallyPerformPurchase, string storeId, StoreItem storeItem, Sprite icon, string title, string description, Action insufficientFundsStore = null)
        {
            this.automaticallyPerformPurchase = automaticallyPerformPurchase;

            return(UnityTask <PurchaseResult> .Run(Coroutine()));

            IEnumerator <PurchaseResult> Coroutine()
            {
                // resetting the result, and caching the items
                this.result    = PurchaseResult.Cancel;
                this.storeId   = storeId;
                this.storeItem = storeItem;

                // Figuring out which currecy this item costs
                string virtualCurrencyId = null;

                foreach (var virtualCurrencyPrice in storeItem.VirtualCurrencyPrices)
                {
                    if (virtualCurrencyPrice.Value > 0)
                    {
                        virtualCurrencyId = virtualCurrencyPrice.Key;
                    }
                }

                if (virtualCurrencyId == null)
                {
                    Debug.LogErrorFormat("StoreItem {0} has unknown currency.", storeItem.ItemId);
                    yield return(PurchaseResult.Cancel);

                    yield break;
                }

                bool isIapItem = virtualCurrencyId == "RM";

                // Turning on the correct button
                this.iapBuyButton.gameObject.SafeSetActive(isIapItem);
                this.virtualCurrencyBuyButton.gameObject.SafeSetActive(!isIapItem);

                if (isIapItem)
                {
                    string purchasePrice = string.Empty;

                    #if USING_UNITY_PURCHASING && !UNITY_XBOXONE
                    purchasePrice = IAP.UnityPurchasingManager.Instance.GetLocalizedPrice(storeItem.ItemId);
                    #else
                    purchasePrice = string.Format("${0}.{1:D2}", currencyPrice / 100, currencyPrice % 100);
                    #endif

                    this.iapBuyButtonText.text = purchasePrice;
                }
                else
                {
                    int  virtualCurrencyPrice = storeItem.GetVirtualCurrenyPrice(virtualCurrencyId);
                    bool hasSufficientFunds   = PF.VC[virtualCurrencyId] >= virtualCurrencyPrice;

                    if (hasSufficientFunds == false && insufficientFundsStore != null)
                    {
                        var insufficientFundsMessage = PlayFabMessages.ShowInsufficientCurrency();

                        while (insufficientFundsMessage.IsDone == false)
                        {
                            yield return(default(PurchaseResult));
                        }

                        if (insufficientFundsMessage.Value == YesNoResult.Yes)
                        {
                            insufficientFundsStore.Invoke();
                        }
                        else
                        {
                            yield return(PurchaseResult.Cancel);

                            yield break;
                        }
                    }

                    this.virtualCurrencyBuyButton.interactable = hasSufficientFunds;
                    this.virtualCurrencyBuyButtonIcon.sprite   = this.GetSprite(virtualCurrencyId);
                    this.virtualCurrencyBuyButtonText.text     = virtualCurrencyPrice.ToString();
                }

                // Setting the item image/texts
                this.storeItemIcon.sprite      = icon;
                this.storeItemTitle.text       = title;
                this.storeItemDescription.text = description;

                this.Dialog.Show();

                // waiting for it to start showing
                while (this.Dialog.IsShowing == false)
                {
                    yield return(default(PurchaseResult));
                }

                // waiting for it to return to the hidden state
                while (this.Dialog.IsHidden == false)
                {
                    yield return(default(PurchaseResult));
                }

                yield return(this.result);
            }
        }
        private IEnumerator <bool> PurchaseStoreItemInternal(string storeId, StoreItem storeItem, Action showStore)
        {
            bool isIapItem = storeItem.GetVirtualCurrenyPrice("RM") > 0;

            if (isIapItem)
            {
                #if USING_UNITY_PURCHASING && !UNITY_XBOXONE
                // Making sure we're properly initialized
                if (this.IsInitialized == false)
                {
                    var initialize = this.Initialize();

                    while (initialize.IsDone == false)
                    {
                        yield return(default(bool));
                    }

                    // Early out if initialization failed
                    if (initialize.HasError || this.IsInitialized == false)
                    {
                        if (initialize.HasError)
                        {
                            PlayFabMessages.HandleError(initialize.Exception);
                        }

                        yield return(false);

                        yield break;
                    }
                }

                var iapPurchaseItem = Lost.IAP.UnityPurchasingManager.Instance.PurchaseProduct(storeItem.ItemId);

                while (iapPurchaseItem.IsDone == false)
                {
                    yield return(default(bool));
                }

                if (iapPurchaseItem.HasError)
                {
                    PlayFabMessages.HandleError(iapPurchaseItem.Exception);
                }
                else
                {
                    if (Debug.isDebugBuild || Application.isEditor)
                    {
                        var purchase = UnityTask <bool> .Run(this.DebugPurchaseStoreItem(iapPurchaseItem.Value));

                        while (purchase.IsDone == false)
                        {
                            yield return(default(bool));
                        }

                        yield return(purchase.Value);
                    }
                    else
                    {
                        var purchase = UnityTask <bool> .Run(this.ProcessPurchaseCoroutine(iapPurchaseItem.Value, storeId));

                        while (purchase.IsDone == false)
                        {
                            yield return(default(bool));
                        }

                        yield return(purchase.Value);
                    }
                }
                #else
                throw new NotImplementedException("Trying to buy IAP Store Item when USING_UNITY_PURCHASING is not defined!");
                #endif
            }
            else
            {
                string virtualCurrency = storeItem.GetVirtualCurrencyId();
                int    storeItemCost   = storeItem.GetCost(virtualCurrency);

                bool hasSufficientFunds = PF.VC[virtualCurrency] >= storeItemCost;

                if (hasSufficientFunds == false)
                {
                    var messageBoxDialog = PlayFabMessages.ShowInsufficientCurrency();

                    while (messageBoxDialog.IsDone == false)
                    {
                        yield return(default(bool));
                    }

                    if (messageBoxDialog.Value == YesNoResult.Yes)
                    {
                        showStore?.Invoke();
                    }

                    yield break;
                }

                var coroutine = this.Do(new PurchaseItemRequest
                {
                    ItemId          = storeItem.ItemId,
                    Price           = storeItemCost,
                    VirtualCurrency = virtualCurrency,
                    StoreId         = storeId,
                });

                while (coroutine.keepWaiting)
                {
                    yield return(default(bool));
                }

                bool isSuccessful = coroutine.Value != null;

                if (isSuccessful)
                {
                    PF.Inventory.InvalidateUserInventory();
                }

                yield return(isSuccessful);
            }
        }