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

365 lines
11 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-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-12-30 17:05:46 +08:00
using Gameplay;
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
}
2024-06-06 15:44:06 +08:00
public Dictionary<string, int> ProductDic;
2024-06-17 15:25:04 +08:00
public Dictionary<int, DataCurrencyShopCfg> IapDataDic;
2024-06-06 15:44:06 +08:00
private bool _isInitialized;
2024-11-22 17:37:15 +08:00
2024-05-24 14:24:59 +08:00
private IAPHandle _helper;
2024-11-22 17:37:15 +08:00
2024-06-06 15:44:06 +08:00
private CurrencyShopCfg _currencyShopCfg;
private Dictionary<int, string> _consumableGoods;
private Dictionary<int, string> _nonConsumableGoods;
2024-11-11 15:35:17 +08:00
public PayChannel CurrentPayChannel { get; private set; }
2024-06-06 15:44:06 +08:00
private const string PayChannelKey = "PayChannel";
2024-06-06 15:44:06 +08:00
2024-12-26 17:26:46 +08:00
private bool _clientVerify;
2024-06-06 15:44:06 +08:00
public bool ClientVerify
{
set
{
if (_isInitialized)
{
Debug.Log("当前验证为客户端:" + value);
SetClientVerify(value);
}
_clientVerify = value;
}
get { return _clientVerify; }
}
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;
2024-05-24 14:24:59 +08:00
_helper = new IAPHandle();
2024-05-27 15:28:19 +08:00
ProductDic = 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>
/// 读表获取内购商品
/// </summary>
private void InitProducts()
2024-05-27 15:28:19 +08:00
{
2024-06-06 15:44:06 +08:00
var table = TableManager.Instance.Tables;
_currencyShopCfg = table.CurrencyShopCfg;
_consumableGoods = new Dictionary<int, string>();
_nonConsumableGoods = new Dictionary<int, string>();
2024-06-17 15:25:04 +08:00
IapDataDic = 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
{
2024-06-17 15:25:04 +08:00
IapDataDic.TryAdd(currencyShop.Key, currencyShop.Value);
2024-06-06 15:44:06 +08:00
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-05-27 15:28:19 +08:00
2024-06-06 15:44:06 +08:00
ProductDic.TryAdd(currencyShop.Value.AppleProductID, currencyShop.Key);
}
2024-06-11 14:35:00 +08:00
2024-06-11 15:58:47 +08:00
foreach (var good in ProductDic)
{
2024-06-11 14:35:00 +08:00
var count = Actor.Instance.Purchase.GetPurchaseCount((uint)good.Value);
Debug.Log($"商品{good.Key},购买了{count}次");
}
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)
{
_helper.ConsumableGoods = consumableGoods;
_helper.NonConsumableGoods = nonConsumableGoods;
_helper.OnBreakPurchase = onBreakPurchase;
try
{
2024-12-30 17:33:03 +08:00
#if UNITY_IOS
2024-11-11 15:35:17 +08:00
_helper.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
{
2024-06-06 15:44:06 +08:00
_helper.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>
2024-06-06 15:44:06 +08:00
/// IAP购买商品 通过商品ID
2024-05-24 14:24:59 +08:00
/// </summary>
2024-06-06 15:44:06 +08:00
public void IAPBuyGoods(int id)
2024-05-24 14:24:59 +08:00
{
2024-06-06 15:44:06 +08:00
if (!_consumableGoods.TryGetValue(id, out var good))
{
if (!_nonConsumableGoods.TryGetValue(id, out good))
{
DebugUtil.LogError("检查 IAPProducts 中配置ID为 {0} 的商品", id);
return;
}
}
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,
2024-11-25 18:37:22 +08:00
() => { BuyGoods(id, good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail); },
2024-07-23 16:15:30 +08:00
() => { DebugUtil.LogError("充值上限 无法充值"); });
}
else
2024-11-25 18:36:26 +08:00
BuyGoods(id, good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail);
2024-12-30 17:05:46 +08:00
#else
2024-11-25 16:52:19 +08:00
BuyGoods(id, good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail);
2024-07-23 16:15:30 +08:00
#endif
2024-06-06 15:44:06 +08:00
}
/// <summary>
/// 购买商品
/// </summary>
2024-12-30 17:05:46 +08:00
private void BuyGoods(int currencyID, string goodsName, string actorId, Action<string> onPurchaseSuccess, Action onPurchaseFail)
2024-06-06 15:44:06 +08:00
{
2024-12-26 17:26:46 +08:00
if (!IapDataDic.TryGetValue(currencyID, out var goodData))
return;
2024-12-30 17:05:46 +08:00
2024-11-22 17:37:15 +08:00
switch (CurrentPayChannel)
{
case PayChannel.WeChat:
2024-12-30 17:05:46 +08:00
{
if (WXPayHandle.Instance.CheckWxInit())
WXPayHandle.Instance.BuyGoods(goodData.PhxhProductID, 1);
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(goodsName);
break;
case PayChannel.Apple:
2024-11-22 17:37:15 +08:00
_helper.BuyGoods(goodsName, actorId, onPurchaseSuccess, onPurchaseFail);
break;
2025-01-02 14:18:26 +08:00
case PayChannel.Bilibili:
{
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;
2025-01-02 15:54:22 +08:00
if (11 == goodData.ItemID) // 付费钻石
2025-01-02 14:18:26 +08:00
diamondCount = goodData.ItemCount;
biliSdk.BiliListerner.RegistPayCallback(BilibiliPayHandle.Instance.BiliPaySuccess);
2025-01-03 18:09:41 +08:00
BilibiliPayHandle.Instance.BuyGoods(goodData.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,
2025-01-02 14:18:26 +08:00
CommonUtils.GetLocalizeText(goodData.NameLocal), order, string.Empty, sign);
});
}
2025-01-02 14:21:23 +08:00
#endif
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-11-25 17:47:26 +08:00
2024-05-24 14:24:59 +08:00
/// <summary>
/// 恢复购买 TODO待测试
/// </summary>
public void RestorePurchase(Action<List<string>> restorePurchase)
{
_helper.RestorePurchase(restorePurchase);
}
/// <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)
{
2024-06-11 15:58:47 +08:00
if (ProductDic.TryGetValue(productID, out var id) &&
_nonConsumableGoods.TryGetValue(id, out var nonConsumableGood))
2024-06-06 15:44:06 +08:00
{
return Actor.Instance.Purchase.GetPurchaseCount((uint)id) > 0;
}
return false;
}
2025-01-02 14:21:23 +08:00
#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
{
_helper.RemovePendingOrder(receipt);
}
2024-06-06 15:44:06 +08:00
#endregion
#region 释放
public void Release()
{
_isInitialized = false;
ProductDic = null;
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)
{
_helper.ClientVerify = clientVerify;
}
2024-06-11 15:58:47 +08:00
public void ConfirmPending()
{
_helper.ConfirmPending();
}
2024-06-06 15:44:06 +08:00
#endregion
2024-05-24 14:24:59 +08:00
}