using System; using PhxhSDK; using Framework; using Gameplay.Net; using cfg.ChargeCfg; using System.Collections.Generic; using System.Linq; using UnityEngine; public class IAPManager : Singlenton { public Dictionary ProductDic; public Dictionary IapDataDic; private bool _isInitialized; private string _curProduct; private IAPHandle _helper; private CurrencyShopCfg _currencyShopCfg; private Dictionary _consumableGoods; private Dictionary _nonConsumableGoods; private const uint RepeatableCode = 40007; public bool ClientVerify { set { if (_isInitialized) { Debug.Log("当前验证为客户端:" + value); SetClientVerify(value); } _clientVerify = value; } get { return _clientVerify; } } private bool _clientVerify; #region 初始化 public void Init() { EventManager.Instance.Register(EventManager.EventName.IAPServerVerifySuccessful, SeverVerifySuccess); _helper = new IAPHandle(); ProductDic = new Dictionary(); //创建不销毁的监听器 GameObject AliListener = new GameObject("AliPayListener"); AliListener.AddComponent(); InitIAP(); } private void InitIAP() { if (_isInitialized) return; InitProducts(); OnReceiveReceipt(SendReceipt); SetClientVerify(_clientVerify); AddGoods(_consumableGoods.Values.ToList(), _nonConsumableGoods.Values.ToList()); _isInitialized = true; } /// /// 读表获取内购商品 /// private void InitProducts() { var table = TableManager.Instance.Tables; _currencyShopCfg = table.CurrencyShopCfg; _consumableGoods = new Dictionary(); _nonConsumableGoods = new Dictionary(); IapDataDic = new Dictionary(); foreach (var currencyShop in _currencyShopCfg.DataMap) { IapDataDic.TryAdd(currencyShop.Key, currencyShop.Value); switch (currencyShop.Value.AppleBuyType) { case AppleBuyType.Consumable: _consumableGoods.Add(currencyShop.Key, currencyShop.Value.AppleProductID); break; case AppleBuyType.NonConsumable: _nonConsumableGoods.Add(currencyShop.Key, currencyShop.Value.AppleProductID); break; } ProductDic.TryAdd(currencyShop.Value.AppleProductID, currencyShop.Key); } foreach (var good in ProductDic) { var count = Actor.Instance.Purchase.GetPurchaseCount((uint)good.Value); Debug.Log($"商品{good.Key},购买了{count}次"); } } /// /// 添加商品并初始化IAP /// private void AddGoods(List consumableGoods, List nonConsumableGoods, Action onBreakPurchase = null) { _helper.ConsumableGoods = consumableGoods; _helper.NonConsumableGoods = nonConsumableGoods; _helper.OnBreakPurchase = onBreakPurchase; try { // _helper.InitIap(); } catch (Exception e) { DebugUtil.LogError("Add Goods Failed, Error: {0}", e.Message); } } /// /// 设置购买成功后的回调验证 /// private void OnReceiveReceipt(Action onResult) { _helper.SendReceiptAction = onResult; } #endregion #region 购买 public string GetGoodPrice(string productID) { return _helper?.GetGoodPrice(productID); } /// /// IAP购买商品 通过商品ID /// public void IAPBuyGoods(int id) { if (!_consumableGoods.TryGetValue(id, out var good)) { if (!_nonConsumableGoods.TryGetValue(id, out good)) { DebugUtil.LogError("检查 IAPProducts 中配置ID为 {0} 的商品", id); return; } } _curProduct = good; try { DebugUtil.Log($"商品{good},价格:{GetGoodPrice(_curProduct)}"); } catch (Exception e) { DebugUtil.LogError(e); } #if SDK_TAPTAP SdkHelper sdkHelper = SDKManager.Instance.GetSdkHelper(SDKManager.SdkName.TapTap); if (sdkHelper is IPayLimitService payLimitService) { payLimitService.CheckPayLimit(5000, () => { BuyGoods(good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail); }, () => { DebugUtil.LogError("充值上限 无法充值"); }); } else BuyGoods(good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail); #else BuyGoods(good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail); #endif } /// /// 购买商品 /// private void BuyGoods(string goodsName, string actorId, Action onPurchaseSuccess, Action onPurchaseFail) { _helper.BuyGoods(goodsName, actorId, onPurchaseSuccess, onPurchaseFail); } /// /// 恢复购买 TODO:待测试 /// public void RestorePurchase(Action> restorePurchase) { _helper.RestorePurchase(restorePurchase); } /// /// 购买成功服务器验证 /// /// private void SendReceipt(string recipt, string product) { if (string.IsNullOrEmpty(recipt) || string.IsNullOrEmpty(product)) { DebugUtil.LogError("IAP交易错误 检查商品id"); return; } Debug.Log($"发送商品:{product},交易ID:{recipt}到服务器验证"); NetHelper.InAppPurchase(recipt, product); } private void PurchaseSuccess(string goodsID) { #if SDK_TAPTAP SdkHelper sdkHelper = SDKManager.Instance.GetSdkHelper(SDKManager.SdkName.TapTap); if (sdkHelper is IPayLimitService payLimitService) payLimitService.SubmitPayResult(5000); #endif Debug.Log($"{goodsID}商品购买成功"); } private void PurchaseFail() { _curProduct = null; Debug.Log("!! Unsuccessful purchase !!"); } public bool OwnedNonConsumableGood(string productID) { if (ProductDic.TryGetValue(productID, out var id) && _nonConsumableGoods.TryGetValue(id, out var nonConsumableGood)) { return Actor.Instance.Purchase.GetPurchaseCount((uint)id) > 0; } return false; } #endregion #region 服务器验证 /// /// 服务器验证成功消息事件 /// private void SeverVerifySuccess(string receipt) { Debug.Log("服务器验证成功"); ReceiveServerReceipt(receipt); } /// /// 服务器验证成功后操作 /// private void ReceiveServerReceipt(string receipt) { _helper.RemovePendingOrder(receipt); } #endregion #region 释放 public void Release() { _isInitialized = false; ProductDic = null; EventManager.Instance.Unregister(EventManager.EventName.IAPServerVerifySuccessful, SeverVerifySuccess); } #endregion #region Debug public void ReLoad() { InitProducts(); SetClientVerify(_clientVerify); AddGoods(_consumableGoods.Values.ToList(), _nonConsumableGoods.Values.ToList()); } /// /// 设为客户端验证凭证 /// private void SetClientVerify(bool clientVerify = false) { _helper.ClientVerify = clientVerify; } public void ConfirmPending() { _helper.ConfirmPending(); } #endregion }