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

258 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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>();
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 购买
/// <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;
BuyGoods(good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail);
}
/// <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)
{
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
}