NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/IAP/IAPManager.cs

402 lines
12 KiB
C#
Raw Normal View History

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