#if !UNITY_EDITOR && UNITY_IOS using UnityEngine.Purchasing; using UnityEngine; using ET; namespace GFGGame { public class QDAppStoreManager { public static void Init() { IAPManager.Instance.InitializePurchasing(); //#if !UNITY_EDITOR // ReYunSDKManager.Instance.Init(); //#endif } public static void Pay(int buyID, int count, string orderID, long Price) { IAPManager.Instance.Pay(IAPManager.Instance.GetIosProductId(buyID), count, orderID, Price); } public static void OnCreateRole() { //#if !UNITY_EDITOR // var accountInfoComponent = GameGlobal.zoneScene.GetComponent(); // ReYunSDKManager.Instance.Register(accountInfoComponent.Account); //#endif } public static void OnEnterGame() { IAPManager.Instance.CheckOrderToDo(); //#if !UNITY_EDITOR // var accountInfoComponent = GameGlobal.zoneScene.GetComponent(); // ReYunSDKManager.Instance.Login(accountInfoComponent.Account); //#endif } } public class IAPManager : SingletonBase, IStoreListener { IStoreController m_StoreController; // The Unity Purchasing system. private IAppleExtensions m_AppleExtensions; private bool canMakePayments; private string OrderId; private string OrderIdLocal { get { return PlayerPrefs.GetString(OrderIdLocalkey, null); } set { PlayerPrefs.SetString(OrderIdLocalkey, value); } } private string TransactionId { get { return PlayerPrefs.GetString(TransactionIdLocalkey, null); } set { PlayerPrefs.SetString(TransactionIdLocalkey, value); } } private string OrderIdLocalkey { get { return RoleDataManager.roleId + "OrderId"; } } private string TransactionIdLocalkey { get { return RoleDataManager.roleId + "transactionID"; } } public void CheckOrderToDo() { if (!string.IsNullOrEmpty(this.OrderIdLocal) && !string.IsNullOrEmpty(this.TransactionId)) { IOSRechargeSProxy.IosVerifyOrder(OrderIdLocal, this.TransactionId).Coroutine(); } } public void Pay(string buyID, int count, string orderID, long Price) { Debug.Log($"Pay {buyID}"); if(!canMakePayments) { PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。"); return; } if(!string.IsNullOrEmpty(this.OrderIdLocal)) { PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!"); CheckOrderToDo(); return; } if(!string.IsNullOrEmpty(this.OrderId)) { PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!"); return; } ViewManager.Show(""); this.OrderId = orderID; m_StoreController.InitiatePurchase(buyID); } public void OnServerSuccess(string OrderId, string TransactionId) { this.OrderId = null; this.OrderIdLocal = null; this.TransactionId = null; PlayerPrefs.DeleteKey(OrderIdLocalkey); PlayerPrefs.DeleteKey(TransactionIdLocalkey); } public void InitializePurchasing() { Debug.Log("InitializePurchasing"); var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); canMakePayments = builder.Configure().canMakePayments; builder.Configure().SetApplePromotionalPurchaseInterceptorCallback(OnPromotionalPurchase); var dataArray = ShopCfgArray.Instance.dataArray; foreach(var shopCfg in dataArray) { if(shopCfg.costType == CostType.RMB) { builder.AddProduct(GetIosProductId(shopCfg.id), ProductType.Consumable); } } UnityPurchasing.Initialize(this, builder); } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { Debug.Log("In-App Purchasing successfully initialized"); m_StoreController = controller; m_AppleExtensions = extensions.GetExtension(); // 在 Apple 平台上,我们需要处理由 Apple 的"购买前先询问"功能导致的延期购买。 //在非 Apple 平台上,这将不起作用;永远不会调用 OnDeferred。 m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred); } public void OnInitializeFailed(InitializationFailureReason error) { Debug.Log($"In-App Purchasing initialize failed: {error}"); } public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason) { Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}"); this.OrderId = null; ViewManager.Hide(); switch (failureReason) { case PurchaseFailureReason.PurchasingUnavailable: PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。"); break; case PurchaseFailureReason.ExistingPurchasePending: PromptController.Instance.ShowFloatTextPrompt("现购待定,请稍后再试。"); break; case PurchaseFailureReason.ProductUnavailable: PromptController.Instance.ShowFloatTextPrompt("该商品不可购买,请稍后再试。"); break; case PurchaseFailureReason.SignatureInvalid: PromptController.Instance.ShowFloatTextPrompt("验证失败,请稍后再试。"); break; case PurchaseFailureReason.UserCancelled: break; case PurchaseFailureReason.PaymentDeclined: PromptController.Instance.ShowFloatTextPrompt("此次购买被拒绝,请稍后再试。"); break; case PurchaseFailureReason.DuplicateTransaction: PromptController.Instance.ShowFloatTextPrompt("重复的交易,请稍后再试。"); break; default: PromptController.Instance.ShowFloatTextPrompt("购买失败,请稍后再试!"); break; } } public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) { ViewManager.Hide(); //Retrieve the purchased product var product = args.purchasedProduct; Debug.Log($"Purchase Complete - Product: {product.definition.id}"); this.OrderIdLocal = this.OrderId; this.TransactionId = product.transactionID; GameGlobal.maxShowOrderTime = 300000; IOSRechargeSProxy.IosVerifyOrder(OrderIdLocal, product.transactionID).Coroutine(); //We return Complete, informing IAP that the processing on our side is done and the transaction can be closed. return PurchaseProcessingResult.Complete; } /// <summary> /// 特定于 iOS。 /// 未成年人要求购买并提交给家长批准时, /// 此函数将作为 Apple 的"购买前先询问"功能的一部分 /// 接受调用。 /// /// 购买被批准或拒绝时,将触发正常的购买 /// 事件。 /// </summary> /// <param name="item">Item.</param> private void OnDeferred(Product item) { Debug.Log("Purchase deferred: " + item.definition.id); PromptController.Instance.ShowFloatTextPrompt("未成年人购买需要经过家长批准才能继续!"); } private void OnPromotionalPurchase(Product item) { Debug.Log("Attempted promotional purchase: " + item.definition.id); // 已检测到推荐性购买。 // 通过呈现家长控制门等方式处理此事件。 //此处,仅出于演示目的,我们将等待五秒钟 // 再继续购买。 //StartCoroutine(ContinuePromotionalPurchases()); } public string GetIosProductId(int cfgId) { return "wsj" + cfgId; } } } #endif