// // ©2015 - 2025 Candy Smith // // All rights reserved // // Redistribution of this software is strictly not allowed. // // Copy of this software can be obtained from unity asset store only. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE // // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // // THE SOFTWARE. using System.Linq; using UnityEngine; using UnityEngine.Serialization; using WordsToolkit.Scripts.Audio; using WordsToolkit.Scripts.Data; using WordsToolkit.Scripts.Enums; using WordsToolkit.Scripts.GUI; using WordsToolkit.Scripts.GUI.Labels; using WordsToolkit.Scripts.Services; using WordsToolkit.Scripts.Services.IAP; using WordsToolkit.Scripts.Settings; using WordsToolkit.Scripts.System; namespace WordsToolkit.Scripts.Popups { public class CoinsShop : PopupWithCurrencyLabel { public ItemPurchase[] packs; private CoinsShopSettings shopSettings; public ProductID noAdsProduct; [SerializeField] private AudioClip coinsSound; private void OnEnable() { shopSettings = Resources.Load("Settings/CoinsShopSettings"); foreach (var itemPurchase in packs) { if (shopSettings.coinsProducts.Count > 0) { var productID = itemPurchase.productID; if (shopSettings.coinsProducts.TryToGetPair(kvp => kvp.Key == productID, out var settingsShopItem)) { itemPurchase.Init(settingsShopItem, iapManager); } } } EventManager.GetEvent(EGameEvent.PurchaseSucceeded).Subscribe(PurchaseSucceded); EventManager.GetEvent<(string, string)>(EGameEvent.PurchaseFailed).Subscribe(PurchaseFailed); } private void OnDisable() { EventManager.GetEvent(EGameEvent.PurchaseSucceeded).Unsubscribe(PurchaseSucceded); } private void PurchaseSucceded(string id) { var shopItem = packs.First(i => i.productID.ID == id); var count = shopItem.settingsShopItem.Value; shopItem.resource.AddAnimated(count, shopItem.BuyItemButton.transform.position, animationSourceObject: null, callback: () => { GetComponentInParent().CloseDelay(); }); // If the item is non-consumable, mark it as purchased if (shopItem.productID.productType == ProductTypeWrapper.ProductType.NonConsumable) { PlayerPrefs.SetInt("Purchased_" + id, 1); PlayerPrefs.Save(); // Disable the button for this item var pack = shopItem; if (pack.BuyItemButton != null) { pack.BuyItemButton.interactable = false; } } if (id == noAdsProduct.ID) { adsManager.RemoveAds(); Close(); } } private void PurchaseFailed((string, string) info) { var productId = info.Item1; var errorMessage = info.Item2; var errorType = IAPErrorHelper.ParseError(errorMessage); Debug.LogWarning($"Purchase failed for product {productId}: {errorMessage}, Error Type: {errorType}"); switch (errorType) { case IAPErrorType.InvalidProductID: Debug.LogError($"Invalid product ID: {productId}"); ShowErrorMessage("Invalid product ID. Please try again later."); break; case IAPErrorType.UserCancelled: Debug.LogWarning("Purchase cancelled by user."); break; case IAPErrorType.DuplicateTransaction: Debug.LogWarning($"Duplicate transaction for product {productId}. This usually means the purchase was already completed."); break; case IAPErrorType.IAPInitFailed: Debug.LogError("IAP initialization failed. Please check your IAP settings."); ShowErrorMessage("IAP initialization failed. Please try again later."); break; default: ShowErrorMessage($"Payment failed: {errorMessage}"); break; } } private void ShowErrorMessage(string message) { var popup = menuManager.ShowPopup(); if (popup != null) { popup.SetMessage(message); } } public void BuyCoins(string id) { // StopInteration(); #if UNITY_WEBGL gameManager.PurchaseSucceeded(id); #else iapManager.BuyProduct(id); #endif } } }