fix:1、修复bug。 2、1.0.8版本提审
This commit is contained in:
+274
-89
@@ -6,6 +6,7 @@
|
||||
// 请将商品 ID 替换为你在 App Store Connect / Google Play Console 中配置的真实 ID。
|
||||
// ============================================================
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using SDK_IAP;
|
||||
@@ -196,7 +197,7 @@ namespace BallKingdomCrush
|
||||
if (result.success)
|
||||
{
|
||||
Debug.Log($"[IAP Google] 订阅成功: {subscriptionName} ({result.productId}) | tid={result.transactionId}");
|
||||
ShowSubscriptionInfo();
|
||||
// ShowSubscriptionInfo();
|
||||
onSuccess?.Invoke();
|
||||
}
|
||||
else
|
||||
@@ -269,20 +270,59 @@ namespace BallKingdomCrush
|
||||
// ──────────────────────────────────────────────────────────
|
||||
// 订阅信息查询
|
||||
// ──────────────────────────────────────────────────────────
|
||||
public void ShowSubscriptionInfo()
|
||||
/// <summary>显示指定商品的订阅信息</summary>
|
||||
/// <param name="productId">商品ID</param>
|
||||
public void ShowSubscriptionInfo(string productId)
|
||||
{
|
||||
var info = IAPManager.GetSubscriptionInfo(PRODUCT_VIP_MONTH);
|
||||
Debug.Log($"[IAP Google] VIP 订阅状态:");
|
||||
var info = IAPManager.GetSubscriptionInfo(productId);
|
||||
Debug.Log($"[IAP Google] VIP 订阅状态 ({productId}):");
|
||||
Debug.Log($" isSubscribed = {info.isSubscribed}");
|
||||
Debug.Log($" isExpired = {info.isExpired}");
|
||||
Debug.Log($" expireDate = {info.expireDate:yyyy-MM-dd HH:mm:ss}");
|
||||
Debug.Log($" expireDate = {info.expireDate}");
|
||||
Debug.Log($" isAutoRenewing= {info.isAutoRenewing}");
|
||||
|
||||
if (info.isSubscribed && !info.isExpired)
|
||||
{
|
||||
int vipLevel = GetVipLevelByProductId(productId);
|
||||
if (vipLevel > 0)
|
||||
{
|
||||
DataMgr.VipLevel.Value = vipLevel;
|
||||
Debug.Log($"[IAP Google] 设置 VIP 等级: {vipLevel}");
|
||||
}
|
||||
|
||||
if (info.expireDate.Year > 1970 && info.expireDate.Year < 10000)
|
||||
{
|
||||
var expireTimestamp = ((DateTimeOffset)info.expireDate).ToUnixTimeSeconds();
|
||||
Debug.Log($"Expire timestamp: {expireTimestamp}");
|
||||
DataMgr.VipExpirationTime.Value = Math.Max(DataMgr.VipExpirationTime.Value, expireTimestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[IAP Google] 无效的到期时间: {info.expireDate}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"[IAP Google] 用户未订阅或订阅已过期");
|
||||
}
|
||||
}
|
||||
/// <summary>根据商品ID获取VIP等级</summary>
|
||||
/// <param name="productId">商品ID</param>
|
||||
/// <returns>VIP等级:周订阅=1,月订阅=2,年订阅=3,其他=0</returns>
|
||||
private int GetVipLevelByProductId(string productId)
|
||||
{
|
||||
if (productId == PRODUCT_VIP_WEEK)
|
||||
return 1;
|
||||
else if (productId == PRODUCT_VIP_MONTH)
|
||||
return 2;
|
||||
else if (productId == PRODUCT_VIP_YEAR)
|
||||
return 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────
|
||||
// 全局发货处理(OnDeliver 事件接收)
|
||||
// ──────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 统一发货处理入口。
|
||||
/// 无论是新购买、补单、还是恢复购买,都会触发此方法。
|
||||
@@ -290,97 +330,242 @@ namespace BallKingdomCrush
|
||||
/// </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;
|
||||
// }
|
||||
if (productId == PRODUCT_VIP_WEEK || productId == PRODUCT_VIP_MONTH || productId == PRODUCT_VIP_YEAR)
|
||||
{
|
||||
// 订阅商品:需要先获取有效的订阅信息,成功后才会分发支付成功消息
|
||||
StartCoroutine(DelayedGetSubscriptionInfo(productId));
|
||||
}
|
||||
else
|
||||
{
|
||||
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);
|
||||
|
||||
// 非订阅商品(消耗品):直接发货
|
||||
Debug.Log($"[IAP Google] 发货通知: {productId}");
|
||||
GameDispatcher.Instance.Dispatch(GameMsg.IAP_PAY_SUCCESS, productId);
|
||||
PurchasingManager.SendEventClickByName(productId, "open");
|
||||
PurchasingManager.SendEventClickByName(productId, "success");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private IEnumerator DelayedGetSubscriptionInfo(string productId)
|
||||
{
|
||||
Debug.Log($"[IAP Google] 开始获取订阅信息: {productId}");
|
||||
|
||||
// 先尝试立即获取一次
|
||||
var immediateInfo = IAPManager.GetSubscriptionInfo(productId);
|
||||
|
||||
// 检查是否是无效数据
|
||||
bool isInvalid = immediateInfo.expireDate == default(DateTime) ||
|
||||
(immediateInfo.expireDate.Year == 1 && !immediateInfo.isSubscribed);
|
||||
|
||||
if (!isInvalid && immediateInfo.isSubscribed)
|
||||
{
|
||||
// 立即获取到了有效数据,直接处理
|
||||
bool success = ProcessSubscriptionInfo(immediateInfo, productId);
|
||||
if (success)
|
||||
{
|
||||
DispatchPaySuccess(productId);
|
||||
}
|
||||
yield break;
|
||||
}
|
||||
|
||||
// 无效数据,开始重试
|
||||
Debug.Log($"[IAP Google] 订阅信息未就绪,开始重试...");
|
||||
|
||||
int maxRetries = 5;
|
||||
float waitTime = 1.5f;
|
||||
for (int i = 0; i < maxRetries; i++)
|
||||
{
|
||||
yield return new WaitForSeconds(waitTime); // 每次等待0.5秒
|
||||
|
||||
var info = IAPManager.GetSubscriptionInfo(productId);
|
||||
|
||||
if (info.isSubscribed && !info.isExpired && info.expireDate.Year > 1970)
|
||||
{
|
||||
Debug.Log($"[IAP Google] 订阅信息获取成功 (重试 {i + 1} 次)");
|
||||
bool success = ProcessSubscriptionInfo(info, productId);
|
||||
if (success)
|
||||
{
|
||||
DispatchPaySuccess(productId);
|
||||
}
|
||||
yield break;
|
||||
}
|
||||
|
||||
Debug.Log($"[IAP Google] 第 {i + 1} 次重试: isSubscribed={info.isSubscribed}, isExpired={info.isExpired}, expireDate={info.expireDate}");
|
||||
}
|
||||
|
||||
// 所有重试都失败了,使用降级方案
|
||||
Debug.LogError($"[IAP Google] 无法获取订阅信息,使用降级方案");
|
||||
bool fallbackSuccess = ProcessFallbackSubscription(productId);
|
||||
if (fallbackSuccess)
|
||||
{
|
||||
DispatchPaySuccess(productId);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 降级也失败了,上报错误,不分发支付成功
|
||||
Debug.LogError($"[IAP Google] 降级方案也失败,支付成功消息将不分发,请检查配置");
|
||||
PurchasingManager.SendEventClickByName(productId, "open");
|
||||
// 可选:向用户显示错误提示
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理订阅信息,设置VIP等级和过期时间
|
||||
/// </summary>
|
||||
/// <returns>是否设置成功</returns>
|
||||
private bool ProcessSubscriptionInfo(SubscriptionInfoLite info, string productId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Debug.Log($"[IAP Google] VIP 订阅状态 ({productId}):");
|
||||
Debug.Log($" isSubscribed = {info.isSubscribed}");
|
||||
Debug.Log($" isExpired = {info.isExpired}");
|
||||
Debug.Log($" expireDate = {info.expireDate}");
|
||||
Debug.Log($" isAutoRenewing= {info.isAutoRenewing}");
|
||||
|
||||
// 获取VIP等级
|
||||
int vipLevel = GetVipLevelByProductId(productId);
|
||||
if (vipLevel <= 0)
|
||||
{
|
||||
Debug.LogError($"[IAP Google] 无法获取VIP等级,商品ID: {productId}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取过期时间戳
|
||||
long expireTimestamp = 0;
|
||||
if (info.expireDate.Year > 1970 && info.expireDate.Year < 10000)
|
||||
{
|
||||
expireTimestamp = ((DateTimeOffset)info.expireDate).ToUnixTimeSeconds();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[IAP Google] 无效的到期时间: {info.expireDate},使用预估时间");
|
||||
expireTimestamp = GetEstimatedExpireTimestamp(productId);
|
||||
}
|
||||
|
||||
if (expireTimestamp <= 0)
|
||||
{
|
||||
Debug.LogError($"[IAP Google] 无法获取有效的过期时间戳");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 保存数据(使用前后对比,确保设置成功)
|
||||
int oldVipLevel = DataMgr.VipLevel.Value;
|
||||
long oldExpireTime = DataMgr.VipExpirationTime.Value;
|
||||
|
||||
DataMgr.VipLevel.Value = Math.Max(DataMgr.VipLevel.Value, vipLevel);
|
||||
DataMgr.VipExpirationTime.Value = Math.Max(DataMgr.VipExpirationTime.Value, expireTimestamp);
|
||||
|
||||
// 验证设置是否成功
|
||||
bool vipLevelSuccess = DataMgr.VipLevel.Value >= vipLevel;
|
||||
bool expireTimeSuccess = DataMgr.VipExpirationTime.Value >= expireTimestamp;
|
||||
|
||||
if (vipLevelSuccess && expireTimeSuccess)
|
||||
{
|
||||
Debug.Log($"[IAP Google] VIP设置成功 - 等级: {vipLevel} (原:{oldVipLevel}), 过期时间: {expireTimestamp} (原:{oldExpireTime})");
|
||||
|
||||
// 可选:触发VIP状态更新事件
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"[IAP Google] VIP设置失败 - 等级设置: {vipLevelSuccess}, 过期时间设置: {expireTimeSuccess}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[IAP Google] 处理订阅信息时发生异常: {e.Message}\n{e.StackTrace}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 降级方案:当无法从商店获取订阅信息时,根据商品类型估算VIP信息
|
||||
/// </summary>
|
||||
private bool ProcessFallbackSubscription(string productId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Debug.LogWarning($"[IAP Google] 执行降级方案: {productId}");
|
||||
|
||||
int vipLevel = GetVipLevelByProductId(productId);
|
||||
if (vipLevel <= 0)
|
||||
{
|
||||
Debug.LogError($"[IAP Google] 降级方案失败 - 无法获取VIP等级");
|
||||
return false;
|
||||
}
|
||||
|
||||
long expireTimestamp = GetEstimatedExpireTimestamp(productId);
|
||||
if (expireTimestamp <= 0)
|
||||
{
|
||||
Debug.LogError($"[IAP Google] 降级方案失败 - 无法获取预估过期时间");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 设置VIP信息
|
||||
DataMgr.VipLevel.Value = Math.Max(DataMgr.VipLevel.Value, vipLevel);
|
||||
DataMgr.VipExpirationTime.Value = Math.Max(DataMgr.VipExpirationTime.Value, expireTimestamp);
|
||||
|
||||
Debug.Log($"[IAP Google] 降级方案成功 - 等级: {vipLevel}, 过期时间: {expireTimestamp}");
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[IAP Google] 降级方案异常: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取预估的过期时间戳
|
||||
/// </summary>
|
||||
private long GetEstimatedExpireTimestamp(string productId)
|
||||
{
|
||||
TimeSpan duration;
|
||||
if (productId == PRODUCT_VIP_WEEK)
|
||||
{
|
||||
duration = TimeSpan.FromDays(7);
|
||||
}
|
||||
else if (productId == PRODUCT_VIP_MONTH)
|
||||
{
|
||||
duration = TimeSpan.FromDays(30);
|
||||
}
|
||||
else if (productId == PRODUCT_VIP_YEAR)
|
||||
{
|
||||
duration = TimeSpan.FromDays(365);
|
||||
}
|
||||
else
|
||||
{
|
||||
duration = TimeSpan.FromDays(30);
|
||||
}
|
||||
|
||||
var expireDate = DateTime.UtcNow.Add(duration);
|
||||
return ((DateTimeOffset)expireDate).ToUnixTimeSeconds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分发支付成功消息(统一出口)
|
||||
/// </summary>
|
||||
private void DispatchPaySuccess(string productId)
|
||||
{
|
||||
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);
|
||||
|
||||
Debug.Log($"[IAP Google] 支付成功并完成发货: {productId}");
|
||||
GameDispatcher.Instance.Dispatch(GameMsg.IAP_PAY_SUCCESS, productId);
|
||||
PurchasingManager.SendEventClickByName(productId, "open");
|
||||
PurchasingManager.SendEventClickByName(productId, "success");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>验证商品ID是否为已定义的有效商品</summary>
|
||||
/// <param name="productId">商品ID</param>
|
||||
/// <returns>是否为有效商品</returns>
|
||||
|
||||
Reference in New Issue
Block a user