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

321 lines
8.7 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;
using Gameplay.Net;
using cfg.ChargeCfg;
using System.Collections.Generic;
2024-06-06 15:44:06 +08:00
using System.Linq;
using UnityEngine;
2024-05-24 14:24:59 +08:00
public class IAPManager : Singlenton<IAPManager>
{
2024-11-11 15:35:17 +08:00
public enum PayChannel
{
Alipay,
WeChat,
Huabei,
Apple,
None,
}
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;
private string _curProduct;
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-11-26 12:29:47 +08:00
// private WXPayHandle _wxPayHandle;
2024-11-25 16:52:19 +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
2024-05-27 15:28:19 +08:00
private const uint RepeatableCode = 40007;
2024-06-06 15:44:06 +08:00
public bool ClientVerify
{
set
{
if (_isInitialized)
{
Debug.Log("当前验证为客户端:" + value);
SetClientVerify(value);
}
_clientVerify = value;
}
get { return _clientVerify; }
}
private bool _clientVerify;
2024-11-11 15:35:17 +08:00
public void SetCurrentPayChannel(PayChannel payChannel)
{
CurrentPayChannel = payChannel;
}
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-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
//创建不销毁的监听器
GameObject AliListener = new GameObject("AliPayListener");
AliListener.AddComponent<AliPayConnector>();
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-11-11 15:35:17 +08:00
_helper.InitIap();
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-11-26 12:29:47 +08:00
private string GetGoodPrice(string productID)
2024-07-23 15:09:46 +08:00
{
return _helper?.GetGoodPrice(productID);
}
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;
}
}
_curProduct = good;
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-07-23 16:15:30 +08:00
#else
2024-11-22 17:37:15 +08:00
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-11-25 16:52:19 +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-11-22 17:37:15 +08:00
switch (CurrentPayChannel)
{
case PayChannel.WeChat:
{
2024-11-26 18:23:52 +08:00
WXPayHandle.Instance.BuyGoods(currencyID, 1);
2024-11-22 17:37:15 +08:00
break;
}
default:
_helper.BuyGoods(goodsName, actorId, onPurchaseSuccess, onPurchaseFail);
break;
}
}
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-06-06 15:44:06 +08:00
/// <param name="recipt"></param>
2024-06-11 14:35:00 +08:00
private void SendReceipt(string recipt, string product)
2024-05-24 14:24:59 +08:00
{
2024-06-11 14:35:00 +08:00
if (string.IsNullOrEmpty(recipt) || 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-06-11 14:35:00 +08:00
Debug.Log($"发送商品:{product},交易ID:{recipt}到服务器验证");
2024-06-06 15:44:06 +08:00
2024-06-11 14:35:00 +08:00
NetHelper.InAppPurchase(recipt, 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-06-06 15:44:06 +08:00
Debug.Log($"{goodsID}商品购买成功");
}
private void PurchaseFail()
{
_curProduct = null;
Debug.Log("!! Unsuccessful purchase !!");
}
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;
}
#endregion
#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
}