405 lines
20 KiB
C#
405 lines
20 KiB
C#
// ============================================================
|
||
// IAPPayManager.cs
|
||
// IAP 接入使用(MonoBehaviour)
|
||
//
|
||
// 将此脚本挂到场景中的任意 GameObject 即可运行示例。
|
||
// 请将商品 ID 替换为你在 App Store Connect / Google Play Console 中配置的真实 ID。
|
||
// ============================================================
|
||
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using SDK_IAP;
|
||
using UnityEngine;
|
||
using UnityEngine.Purchasing;
|
||
|
||
namespace BallKingdomCrush
|
||
{
|
||
public class IAPPayManager : MonoBehaviour
|
||
{
|
||
// ──────────────────────────────────────────────────────────
|
||
// 单例实例
|
||
// ──────────────────────────────────────────────────────────
|
||
private static IAPPayManager _instance;
|
||
public static IAPPayManager Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = FindObjectOfType<IAPPayManager>();
|
||
}
|
||
return _instance;
|
||
}
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 商品 ID 常量(替换为真实 ID)
|
||
// ──────────────────────────────────────────────────────────
|
||
|
||
public static string PRODUCT_FIRST_GIFT = "comwackyllamagamegift199"; //首充 消耗品
|
||
public static string PRODUCT_REMOVE_ADS = "comwackyllamagameremove299"; //移除广告 消耗品
|
||
public static string PRODUCT_PASS_BONUS = "comwackyllamagamepass999"; //通行证礼包 消耗品
|
||
public static string PRODUCT_SPACE_BONUS = "comwackyllamagamespace2499"; //加一格礼包 非消耗品
|
||
public static string PRODUCT_SHOP_1 = "comwackyllamagameshop199"; //商店档位1 消耗品
|
||
public static string PRODUCT_SHOP_2 = "comwackyllamagameshop399"; //商店档位2 消耗品
|
||
public static string PRODUCT_SHOP_3= "comwackyllamagameshop999"; //商店档位3 消耗品
|
||
public static string PRODUCT_SHOP_4 = "comwackyllamagameshop1999"; //商店档位4 消耗品
|
||
public static string PRODUCT_SHOP_5 = "comwackyllamagameshop3999"; //商店档位5 消耗品
|
||
public static string PRODUCT_THREE_DAY = "comwackyllamagamethreeday399"; //三天礼包 消耗品
|
||
|
||
public static string PRODUCT_VIP_WEEK = "comwackyllamagamesub999"; // 周订阅 Subscription
|
||
public static string PRODUCT_VIP_MONTH = "comwackyllamagamesub2999"; // 月订阅 Subscription
|
||
public static string PRODUCT_VIP_YEAR = "comwackyllamagamesub9999"; // 年订阅 Subscription
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 生命周期
|
||
// ──────────────────────────────────────────────────────────
|
||
|
||
private void Awake()
|
||
{
|
||
// 确保单例唯一性
|
||
if (_instance != null && _instance != this)
|
||
{
|
||
Debug.LogWarning("[IAP Google] 检测到多个 IAPGoogleManager 实例,销毁重复对象");
|
||
Destroy(gameObject);
|
||
return;
|
||
}
|
||
_instance = this;
|
||
|
||
// 订阅发货事件(持续监听,全局处理发货逻辑)
|
||
IAPManager.OnDeliver += HandleDeliver;
|
||
IAPManager.OnInitialized += HandleInitialized;
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
InitIAP();
|
||
//StartCoroutine(Test());
|
||
}
|
||
|
||
//private IEnumerator Test()
|
||
//{
|
||
// int index = 0;
|
||
// while (true)
|
||
// {
|
||
// yield return new WaitForSeconds(1f);
|
||
|
||
// Analytics.OnTrackEvent?.Invoke($"test{index}", new Dictionary<string, string>());
|
||
// index++;
|
||
// }
|
||
//}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
IAPManager.OnDeliver -= HandleDeliver;
|
||
IAPManager.OnInitialized -= HandleInitialized;
|
||
IAPManager.Dispose();
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 初始化
|
||
// ──────────────────────────────────────────────────────────
|
||
|
||
private void InitIAP()
|
||
{
|
||
var products = new List<ProductDefine>
|
||
{
|
||
// 消耗品
|
||
ProductDefine.Simple(PRODUCT_FIRST_GIFT, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_REMOVE_ADS, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_PASS_BONUS, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_SHOP_1, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_SHOP_2, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_SHOP_3, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_SHOP_4, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_SHOP_5, ProductType.Consumable),
|
||
ProductDefine.Simple(PRODUCT_THREE_DAY, ProductType.Consumable),
|
||
|
||
// 非消耗品
|
||
ProductDefine.Simple(PRODUCT_SPACE_BONUS, ProductType.NonConsumable),
|
||
|
||
// 订阅
|
||
ProductDefine.Simple(PRODUCT_VIP_WEEK, ProductType.Subscription),
|
||
ProductDefine.Simple(PRODUCT_VIP_MONTH, ProductType.Subscription),
|
||
ProductDefine.Simple(PRODUCT_VIP_YEAR, ProductType.Subscription),
|
||
};
|
||
|
||
Debug.Log("[IAP Google] 开始初始化...");
|
||
IAPManager.Init(products, OnInitCallback);
|
||
}
|
||
|
||
// Init 完成回调(与 OnInitialized 事件等价,二选一即可)
|
||
private void OnInitCallback(bool success)
|
||
{
|
||
if (success)
|
||
{
|
||
Debug.Log("[IAP Google] 初始化成功!");
|
||
ShowProductPrices();
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("[IAP Google] 初始化失败,请检查网络或 App Store / Google Play 配置。");
|
||
}
|
||
}
|
||
|
||
// 也可以通过静态事件监听(适合多个 Manager 分散处理)
|
||
private void HandleInitialized(bool success)
|
||
{
|
||
// 此处可做更新 UI 等操作
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 显示商品价格(初始化成功后调用)
|
||
// ──────────────────────────────────────────────────────────
|
||
|
||
public void ShowProductPrices()
|
||
{
|
||
var products = IAPManager.GetProducts();
|
||
foreach (var p in products)
|
||
{
|
||
Debug.Log($"[IAP Google] 商品: {p.definition.id} | 价格: {p.metadata.localizedPriceString}");
|
||
}
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 购买
|
||
// ──────────────────────────────────────────────────────────
|
||
/// <summary>通用购买方法 - 适用于消耗品和非消耗品</summary>
|
||
/// <param name="productId">商品ID</param>
|
||
/// <param name="productName">商品名称(用于日志)</param>
|
||
/// <param name="onSuccess">购买成功后的回调</param>
|
||
public void BuyProduct(string productId, string productName, System.Action onSuccess = null)
|
||
{
|
||
IAPManager.Buy(productId, result =>
|
||
{
|
||
if (result.success)
|
||
{
|
||
Debug.Log($"[IAP Google] 购买成功: {productName} ({result.productId}) | tid={result.transactionId}");
|
||
onSuccess?.Invoke();
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning($"[IAP Google] 购买失败: {productName} ({result.productId}) - {result.error}");
|
||
PurchasingManager.SendEventClickByName(productId, "open");
|
||
}
|
||
});
|
||
}
|
||
|
||
/// <summary>通用订阅方法 - 适用于订阅类型商品</summary>
|
||
/// <param name="productId">商品ID</param>
|
||
/// <param name="subscriptionName">订阅名称(用于日志)</param>
|
||
/// <param name="onSuccess">订阅成功后的回调</param>
|
||
public void SubscribeProduct(string productId, string subscriptionName, System.Action onSuccess = null)
|
||
{
|
||
IAPManager.Buy(productId, result =>
|
||
{
|
||
if (result.success)
|
||
{
|
||
Debug.Log($"[IAP Google] 订阅成功: {subscriptionName} ({result.productId}) | tid={result.transactionId}");
|
||
ShowSubscriptionInfo();
|
||
onSuccess?.Invoke();
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning($"[IAP Google] 订阅失败: {subscriptionName} ({result.productId}) - {result.error}");
|
||
PurchasingManager.SendEventClickByName(productId, "open");
|
||
}
|
||
});
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 恢复购买(iOS 界面上必须提供此按钮)
|
||
// ──────────────────────────────────────────────────────────
|
||
public void OnRestoreButtonClicked()
|
||
{
|
||
// UI 控制:仅在需要时显示此按钮
|
||
if (!IAPManager.NeedShowRestoreButton()) return;
|
||
|
||
IAPManager.Restore(result =>
|
||
{
|
||
if (result.success)
|
||
Debug.Log("[IAP Google] 恢复购买流程完成(实际恢复内容通过 OnDeliver 发放)");
|
||
else
|
||
Debug.LogWarning($"[IAP Google] 恢复购买失败: {result.error}");
|
||
});
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 权益检查
|
||
// ──────────────────────────────────────────────────────────
|
||
/// <summary>通用权益检查方法 - 适用于非消耗品和订阅</summary>
|
||
/// <param name="productId">商品ID</param>
|
||
/// <param name="productName">商品名称(用于日志)</param>
|
||
/// <param name="onResult">检查结果回调</param>
|
||
public void CheckEntitlement(string productId, string productName, System.Action<EntitlementStatus> onResult = null)
|
||
{
|
||
IAPManager.CheckEntitlement(productId, status =>
|
||
{
|
||
Debug.Log($"[IAP Google] {productName} 权益状态: {status}");
|
||
onResult?.Invoke(status);
|
||
});
|
||
}
|
||
|
||
/// <summary>检查加一格礼包权益</summary>
|
||
public void CheckSpaceBonusEntitlement()
|
||
{
|
||
CheckEntitlement(PRODUCT_SPACE_BONUS, "加一格礼包", state =>
|
||
{
|
||
if (state == EntitlementStatus.FullyEntitled)
|
||
{
|
||
// 激活加一格权限
|
||
}
|
||
});
|
||
}
|
||
|
||
/// <summary>检查VIP订阅权益</summary>
|
||
public void CheckVipEntitlement()
|
||
{
|
||
CheckEntitlement(PRODUCT_VIP_MONTH, "VIP月卡", status =>
|
||
{
|
||
if (status == EntitlementStatus.FullyEntitled)
|
||
{
|
||
// 激活VIP权限
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 订阅信息查询
|
||
// ──────────────────────────────────────────────────────────
|
||
public void ShowSubscriptionInfo()
|
||
{
|
||
var info = IAPManager.GetSubscriptionInfo(PRODUCT_VIP_MONTH);
|
||
Debug.Log($"[IAP Google] VIP 订阅状态:");
|
||
Debug.Log($" isSubscribed = {info.isSubscribed}");
|
||
Debug.Log($" isExpired = {info.isExpired}");
|
||
Debug.Log($" expireDate = {info.expireDate:yyyy-MM-dd HH:mm:ss}");
|
||
Debug.Log($" isAutoRenewing= {info.isAutoRenewing}");
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────
|
||
// 全局发货处理(OnDeliver 事件接收)
|
||
// ──────────────────────────────────────────────────────────
|
||
|
||
/// <summary>
|
||
/// 统一发货处理入口。
|
||
/// 无论是新购买、补单、还是恢复购买,都会触发此方法。
|
||
/// ⚠️ 幂等保护已由 DeliverGuardLite 处理,此处无需再判重。
|
||
/// </summary>
|
||
private void HandleDeliver(string productId)
|
||
{
|
||
|
||
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);
|
||
|
||
if (!IsValidProduct(productId))
|
||
{
|
||
Debug.LogWarning($"[IAP Google] 非法商品ID,拒绝发货: {productId}");
|
||
return;
|
||
}
|
||
Debug.Log($"[IAP Google] 发货通知: {productId}");
|
||
|
||
GameDispatcher.Instance.Dispatch(GameMsg.IAP_PAY_SUCCESS, productId);
|
||
|
||
PurchasingManager.SendEventClickByName(productId,"open");
|
||
PurchasingManager.SendEventClickByName(productId,"success");
|
||
|
||
// switch (productId)
|
||
// {
|
||
// // 消耗品
|
||
// case PRODUCT_FIRST_GIFT:
|
||
// Debug.Log("[IAP Google] 发放首充礼包 - 100金币");
|
||
// // GoldManager.Add(100);
|
||
// break;
|
||
//
|
||
// case PRODUCT_REMOVE_ADS:
|
||
// Debug.Log("[IAP Google] 发放移除广告权益");
|
||
// // AdsManager.Disable();
|
||
// break;
|
||
//
|
||
// case PRODUCT_PASS_BONUS:
|
||
// Debug.Log("[IAP Google] 发放通行证礼包");
|
||
// // PassBonusManager.Grant();
|
||
// break;
|
||
//
|
||
// case PRODUCT_SHOP_1:
|
||
// Debug.Log("[IAP Google] 发放商店档位1奖励");
|
||
// // ShopManager.GrantReward(1);
|
||
// break;
|
||
//
|
||
// case PRODUCT_SHOP_2:
|
||
// Debug.Log("[IAP Google] 发放商店档位2奖励");
|
||
// // ShopManager.GrantReward(2);
|
||
// break;
|
||
//
|
||
// case PRODUCT_SHOP_3:
|
||
// Debug.Log("[IAP Google] 发放商店档位3奖励");
|
||
// // ShopManager.GrantReward(3);
|
||
// break;
|
||
//
|
||
// case PRODUCT_SHOP_4:
|
||
// Debug.Log("[IAP Google] 发放商店档位4奖励");
|
||
// // ShopManager.GrantReward(4);
|
||
// break;
|
||
//
|
||
// case PRODUCT_SHOP_5:
|
||
// Debug.Log("[IAP Google] 发放商店档位5奖励");
|
||
// // ShopManager.GrantReward(5);
|
||
// break;
|
||
//
|
||
// case PRODUCT_THREE_DAY:
|
||
// Debug.Log("[IAP Google] 发放三天礼包");
|
||
// // ThreeDayManager.Grant();
|
||
// break;
|
||
//
|
||
// // 非消耗品
|
||
// case PRODUCT_SPACE_BONUS:
|
||
// Debug.Log("[IAP Google] 增加背包空间一格");
|
||
// // InventoryManager.AddSpace(1);
|
||
// break;
|
||
//
|
||
// // 订阅
|
||
// case PRODUCT_VIP_WEEK:
|
||
// Debug.Log("[IAP Google] 激活VIP周卡");
|
||
// // VipManager.Activate(7);
|
||
// break;
|
||
//
|
||
// case PRODUCT_VIP_MONTH:
|
||
// Debug.Log("[IAP Google] 激活VIP月卡");
|
||
// // VipManager.Activate(30);
|
||
// break;
|
||
//
|
||
// case PRODUCT_VIP_YEAR:
|
||
// Debug.Log("[IAP Google] 激活VIP年卡");
|
||
// // VipManager.Activate(365);
|
||
// break;
|
||
//
|
||
// default:
|
||
// Debug.LogWarning($"[IAP Google] 未知商品 ID: {productId}");
|
||
// break;
|
||
// }
|
||
}
|
||
|
||
/// <summary>验证商品ID是否为已定义的有效商品</summary>
|
||
/// <param name="productId">商品ID</param>
|
||
/// <returns>是否为有效商品</returns>
|
||
private bool IsValidProduct(string productId)
|
||
{
|
||
return productId == PRODUCT_FIRST_GIFT ||
|
||
productId == PRODUCT_REMOVE_ADS ||
|
||
productId == PRODUCT_PASS_BONUS ||
|
||
productId == PRODUCT_SPACE_BONUS ||
|
||
productId == PRODUCT_SHOP_1 ||
|
||
productId == PRODUCT_SHOP_2 ||
|
||
productId == PRODUCT_SHOP_3 ||
|
||
productId == PRODUCT_SHOP_4 ||
|
||
productId == PRODUCT_SHOP_5 ||
|
||
productId == PRODUCT_THREE_DAY ||
|
||
productId == PRODUCT_VIP_WEEK ||
|
||
productId == PRODUCT_VIP_MONTH ||
|
||
productId == PRODUCT_VIP_YEAR;
|
||
}
|
||
}
|
||
}
|