295 lines
8.2 KiB
C#
295 lines
8.2 KiB
C#
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<IAPManager>
|
||
{
|
||
public Dictionary<string, int> ProductDic;
|
||
public Dictionary<int, DataCurrencyShopCfg> IapDataDic;
|
||
|
||
private bool _isInitialized;
|
||
private string _curProduct;
|
||
private IAPHandle _helper;
|
||
private CurrencyShopCfg _currencyShopCfg;
|
||
private Dictionary<int, string> _consumableGoods;
|
||
private Dictionary<int, string> _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<string>(EventManager.EventName.IAPServerVerifySuccessful, SeverVerifySuccess);
|
||
_helper = new IAPHandle();
|
||
ProductDic = new Dictionary<string, int>();
|
||
|
||
//创建不销毁的监听器
|
||
GameObject AliListener = new GameObject("AliPayListener");
|
||
AliListener.AddComponent<AliPayConnector>();
|
||
|
||
InitIAP();
|
||
}
|
||
|
||
private void InitIAP()
|
||
{
|
||
if (_isInitialized) return;
|
||
InitProducts();
|
||
OnReceiveReceipt(SendReceipt);
|
||
SetClientVerify(_clientVerify);
|
||
AddGoods(_consumableGoods.Values.ToList(), _nonConsumableGoods.Values.ToList());
|
||
_isInitialized = true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 读表获取内购商品
|
||
/// </summary>
|
||
private void InitProducts()
|
||
{
|
||
var table = TableManager.Instance.Tables;
|
||
|
||
_currencyShopCfg = table.CurrencyShopCfg;
|
||
_consumableGoods = new Dictionary<int, string>();
|
||
_nonConsumableGoods = new Dictionary<int, string>();
|
||
IapDataDic = new Dictionary<int, DataCurrencyShopCfg>();
|
||
|
||
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}次");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加商品并初始化IAP
|
||
/// </summary>
|
||
private void AddGoods(List<string> consumableGoods, List<string> nonConsumableGoods,
|
||
Action<string> 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);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置购买成功后的回调验证
|
||
/// </summary>
|
||
private void OnReceiveReceipt(Action<string, string> onResult)
|
||
{
|
||
_helper.SendReceiptAction = onResult;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 购买
|
||
|
||
public string GetGoodPrice(string productID)
|
||
{
|
||
return _helper?.GetGoodPrice(productID);
|
||
}
|
||
|
||
/// <summary>
|
||
/// IAP购买商品 通过商品ID
|
||
/// </summary>
|
||
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
|
||
}
|
||
|
||
/// <summary>
|
||
/// 购买商品
|
||
/// </summary>
|
||
private void BuyGoods(string goodsName, string actorId, Action<string> onPurchaseSuccess, Action onPurchaseFail)
|
||
{
|
||
_helper.BuyGoods(goodsName, actorId, onPurchaseSuccess, onPurchaseFail);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 恢复购买 TODO:待测试
|
||
/// </summary>
|
||
public void RestorePurchase(Action<List<string>> restorePurchase)
|
||
{
|
||
_helper.RestorePurchase(restorePurchase);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 购买成功服务器验证
|
||
/// </summary>
|
||
/// <param name="recipt"></param>
|
||
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 服务器验证
|
||
|
||
/// <summary>
|
||
/// 服务器验证成功消息事件
|
||
/// </summary>
|
||
private void SeverVerifySuccess(string receipt)
|
||
{
|
||
Debug.Log("服务器验证成功");
|
||
ReceiveServerReceipt(receipt);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 服务器验证成功后操作
|
||
/// </summary>
|
||
private void ReceiveServerReceipt(string receipt)
|
||
{
|
||
_helper.RemovePendingOrder(receipt);
|
||
}
|
||
|
||
#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;
|
||
}
|
||
|
||
public void ConfirmPending()
|
||
{
|
||
_helper.ConfirmPending();
|
||
}
|
||
|
||
#endregion
|
||
} |