using System; using PhxhSDK; using Framework; using System.Linq; using UnityEngine; using Gameplay.Net; using cfg.ChargeCfg; using System.Collections.Generic; #if SDK_BILIBILI using PhxhSDK.AOT.Bilibili; #endif public class IAPManager : Singlenton { public enum PayChannel { None = 0, Alipay = 1, WeChat = 2, Huabei = 3, Apple = 4, Bilibili = 5, } public Dictionary IapShopItem => iapShopItem; public Dictionary AppleProductDic => appleProductDic; public bool ClientVerify { set { if (_isInitialized) { Debug.Log("当前验证为客户端:" + value); SetClientVerify(value); } _clientVerify = value; } get => _clientVerify; } public PayChannel CurrentPayChannel { get; private set; } /// /// 跳过真实支付 /// public bool SkipRealPay = false; private const string PayChannelKey = "PayChannel"; /// /// 苹果商品ID, 商品配置表ID /// private Dictionary appleProductDic; /// /// 现金商店商品 /// private Dictionary iapShopItem; /// /// 所有内购商品 /// private Dictionary allItemDic; /// /// 可消耗商品列表 /// private Dictionary _consumableGoods; /// /// 不可消耗商品列表 /// private Dictionary _nonConsumableGoods; /// /// 客户端验证 测试使用 /// private bool _clientVerify; private bool _isInitialized; private IAPHandle iapHelper; public void SetCurrentPayChannel(PayChannel payChannel) { CurrentPayChannel = payChannel; PlayerPrefs.SetInt("PayChannel", (int)payChannel); } #region 初始化 public void Init() { EventManager.Instance.Register(EventManager.EventName.IAPServerVerifySuccessful, SeverVerifySuccess); // 缓存支付方法 var payChannelValue = PlayerPrefs.GetInt(PayChannelKey, (int)PayChannel.Alipay); CurrentPayChannel = Enum.IsDefined(typeof(PayChannel), payChannelValue) ? (PayChannel)payChannelValue : PayChannel.Alipay; iapHelper = new IAPHandle(); appleProductDic = new Dictionary(); // 创建支付宝的监听器 var aliListener = new GameObject("AliPayHandle"); aliListener.AddComponent(); // 创建微信回调监听 var wxPayHandle = new GameObject("WXPayHandle"); wxPayHandle.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 currencyShopCfg = TableManager.Instance.Tables.CurrencyShopCfg; _consumableGoods = new Dictionary(); _nonConsumableGoods = new Dictionary(); allItemDic = new Dictionary(); iapShopItem = new Dictionary(); foreach (var currencyShop in currencyShopCfg.DataMap) { // 所有商品 allItemDic.TryAdd(currencyShop.Key, currencyShop.Value); // 现金商店展示商品 if (currencyShop.Value.ShowInShop) iapShopItem.TryAdd(currencyShop.Key, currencyShop.Value); // 苹果内购 if (!string.IsNullOrEmpty(currencyShop.Value.AppleProductID)) { appleProductDic.TryAdd(currencyShop.Value.AppleProductID, currencyShop.Key); 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; } } /*var count = Actor.Instance.Purchase.GetPurchaseCount((uint)currencyShop.Key); Debug.Log($"商品{(uint)currencyShop.Key},购买了{count}次");*/ } } /// /// 添加商品并初始化IAP /// private void AddGoods(List consumableGoods, List nonConsumableGoods, Action onBreakPurchase = null) { iapHelper.ConsumableGoods = consumableGoods; iapHelper.NonConsumableGoods = nonConsumableGoods; iapHelper.OnBreakPurchase = onBreakPurchase; try { #if UNITY_IOS iapHelper.InitIap(); #endif } catch (Exception e) { DebugUtil.LogError("Add Goods Failed, Error: {0}", e.Message); } } /// /// 设置购买成功后的回调验证 /// private void OnReceiveReceipt(Action onResult) { iapHelper.SendReceiptAction = onResult; } #endregion #region 购买 /// /// 通过商品 ID购买商品 /// public void CheckAndBuyGoods(int id, int param = 0) { if (!allItemDic.TryGetValue(id, out var good)) { DebugUtil.LogError("检查 CurrencyShop 中配置ID为 {0} 的商品", id); return; } #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, param); #endif } /// /// 购买商品 /// private void BuyGoods(DataCurrencyShopCfg data, string actorId, Action onPurchaseSuccess, Action onPurchaseFail, int param = 0) { if (SkipRealPay) { NetHelper.InAppPurchaseTest((uint)data.Id); return; } switch (CurrentPayChannel) { case PayChannel.WeChat: { if (WXPayHandle.Instance.CheckWxInit()) { if (!WXPayHandle.Instance.starPay) WXPayHandle.Instance.BuyGoods(data.PhxhProductID, 1, param); } else UITipsManager.ShowTips("未安装微信应用"); } break; case PayChannel.Alipay: AliPayHandle.Instance.BuyGoods(data.AppleProductID); break; case PayChannel.Apple: iapHelper.BuyGoods(data.AppleProductID, actorId, onPurchaseSuccess, onPurchaseFail); break; case PayChannel.Bilibili: { #if SDK_BILIBILI SdkHelper sdkHelper = SDKManager.Instance.GetSdkHelper(SDKManager.SdkName.Bilibili); if (sdkHelper is BilibiliSdkHelper biliSdk) { int diamondCount = 0; if (11 == data.ItemID) // 付费钻石 diamondCount = data.ItemCount; biliSdk.BiliListerner.RegistPayCallback(BilibiliPayHandle.Instance.BiliPaySuccess); BilibiliPayHandle.Instance.BuyGoods(data.PhxhProductID, 1, diamondCount, biliSdk.UID, (string order, string sign, int money) => { biliSdk.Pay(Actor.Instance.Base.Nickname, money, diamondCount, CommonUtils.GetLocalizeText(data.NameLocal), order, string.Empty, sign); }); } #endif } break; default: UITipsManager.ShowTips($"没有{CurrentPayChannel}支付方式"); break; } } /// /// 恢复购买 TODO:待测试 /// public void RestorePurchase(Action> restorePurchase) { iapHelper.RestorePurchase(restorePurchase); } /// /// 购买成功服务器验证 /// private void SendReceipt(string receipt, string product) { if (string.IsNullOrEmpty(receipt) || string.IsNullOrEmpty(product)) { DebugUtil.LogError("IAP交易错误 检查商品id"); return; } Debug.Log($"发送商品:{product},交易ID:{receipt}到服务器验证"); NetHelper.InAppPurchase(receipt, 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 WaitingForServer.Instance.EventDone(WaitingForServer.EventName.Pay); Debug.Log($"{goodsID}商品购买成功"); } private void PurchaseFail() { WaitingForServer.Instance.EventDone(WaitingForServer.EventName.Pay); Debug.Log("!! Unsuccessful purchase !!"); } /// /// 是否为不可消耗产品 /// public bool OwnedNonConsumableGood(string productID) { if (appleProductDic.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) { iapHelper.RemovePendingOrder(receipt); } #endregion #region 释放 public void Release() { _isInitialized = false; appleProductDic = 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) { iapHelper.ClientVerify = clientVerify; } public void ConfirmPending() { iapHelper.ConfirmPending(); } #endregion }