Files
RedHotRoast-ios/Assets/Scripts/PurchasingManager.cs
T

281 lines
8.7 KiB
C#
Raw Normal View History

2026-04-22 09:52:55 +08:00
using System;
using System.Collections.Generic;
using SGModule.ApplePay;
using SGModule.NetKit;
using UnityEngine;
using RedHotRoast;
using UnityEngine.Purchasing;
using Newtonsoft.Json;
2026-04-22 09:52:55 +08:00
public enum PayType
2026-04-22 09:52:55 +08:00
{
buy_one,
buy_one_off,
buy_gold_1,
buy_gold_2,
buy_gold_3,
buy_gold_4,
buy_gold_5,
remove_ad,
battle_pass,
pack_reward,
fail_pack,
three_days_gift,
weekly_subscription,
monthly_subscription,
yearly_subscription,
}
2026-04-22 09:52:55 +08:00
public class PurchasingManager
{
2026-04-22 09:52:55 +08:00
private static readonly List<ApplePay> PayConfig = ConfigSystem.GetConfig<ApplePay>();
private static readonly List<ApplePay2> PayConfig2 = ConfigSystem.GetConfig<ApplePay2>();
2026-04-22 09:52:55 +08:00
public static string GetPaySku(PayType key)
2026-04-22 09:52:55 +08:00
{
string keys = "";
2026-04-22 09:52:55 +08:00
if (PayConfig != null)
{
foreach (var data in PayConfig)
{
if (data.payKey != key.ToString()) continue;
keys = data.sku;
break;
}
}
2026-04-22 09:52:55 +08:00
return keys;
2026-04-22 09:52:55 +08:00
}
public static int GetVipLvFormConfig(string sku)
2026-04-22 09:52:55 +08:00
{
var lv = 1;
2026-04-22 09:52:55 +08:00
if (GetPaySku(PayType.weekly_subscription) == sku)
2026-04-22 09:52:55 +08:00
{
lv = 1;
2026-04-22 09:52:55 +08:00
}
else if (GetPaySku(PayType.monthly_subscription) == sku)
2026-04-22 09:52:55 +08:00
{
lv = 2;
2026-04-22 09:52:55 +08:00
}
else if (GetPaySku(PayType.yearly_subscription) == sku)
2026-04-22 09:52:55 +08:00
{
lv = 3;
2026-04-22 09:52:55 +08:00
}
return lv;
}
2026-04-22 09:52:55 +08:00
public static void Purchase(ApplePayData payData)
{
//本地测试
// TestIOSPay(payData);
// return;
2026-04-22 09:52:55 +08:00
Debug.Log($"[Apple Pay] unity Purchase--0-----: {payData.sku}");
ApplePayManager.Instance.Purchase(payData.sku, ApplePaySuccessCallback(payData), message =>
{
Debug.Log("purchase fail------- reason: " + message);
});
2026-04-22 09:52:55 +08:00
}
public static Action<ApplePayBackType, AppleResponseData> ApplePaySuccessCallback(ApplePayData payData)
2026-04-22 09:52:55 +08:00
{
return (backType, AppleResponseData) =>
2026-04-22 09:52:55 +08:00
{
Debug.Log($"[Apple Pay] unity Purchase--1-----: {backType.ToString()}");
switch (backType)
{
case ApplePayBackType.Create:
Debug.Log("[Apple Pay] Create");
var sku0 = SetSku(payData);
Debug.Log($"[Apple Pay] Create---11: {sku0}");
SendEventClickByName(sku0, "click");
break;
case ApplePayBackType.Check:
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);
if (AppleResponseData != null && AppleResponseData.sku.Contains("sub"))
{
DataMgr.VipExpirationTime.Value = Math.Max(DataMgr.VipExpirationTime.Value, AppleResponseData.expires_time);
var level = GetVipLvFormConfig(AppleResponseData.sku);
DataMgr.VipLevel.Value = level;
payData.sku = AppleResponseData.sku;
payData.shopName = "vip_club" + (level - 1);
}
var sku = SetSku(payData);
Debug.Log($"[Apple Pay] Check sku===2===== {sku}");
GameDispatcher.Instance.Dispatch(GameMsg.apple_pay_success, sku);
Debug.Log($"[Apple Pay] Check sku===3===== {sku}");
SendEventClickByName(sku, "open");
SendEventClickByName(sku, "success");
break;
case ApplePayBackType.Cancel:
Debug.Log("[Apple Pay] Cancel");
var sku1 = SetSku(payData);
SendEventClickByName(sku1, "open");
BIManager.Instance.TrackPurchase(payData.amount, payData.currency, "0", payData.sku, "paid_err");
break;
}
};
}
2026-04-22 09:52:55 +08:00
private static void TestIOSPay(ApplePayData payData)
{
var tranId = "";
var types = ProductType.Consumable;
if (GetPaySku(PayType.battle_pass) == payData.sku)
2026-04-22 09:52:55 +08:00
{
tranId = "2000000983625783";
2026-04-22 09:52:55 +08:00
}
else if (GetPaySku(PayType.weekly_subscription) == payData.sku)
2026-04-22 09:52:55 +08:00
{
tranId = "2000000984643029";
types = ProductType.Subscription;
2026-04-22 09:52:55 +08:00
}
if (tranId == "") return;
2026-04-22 09:52:55 +08:00
ApplePayManager.Instance.ApplePayTest(types, payData.sku, tranId, ApplePaySuccessCallback(payData));
2026-04-22 09:52:55 +08:00
}
private static string SetSku(ApplePayData data)
2026-04-22 09:52:55 +08:00
{
string sku = "";
if (data.type != null && data.type == GetPaySku(PayType.remove_ad))
2026-04-22 09:52:55 +08:00
{
sku = data.type;
}
else if (data.sku.Contains("shop"))
{
sku = data.shopName;
}
else if (data.sku.Contains("sub"))
{
sku = data.shopName;
2026-04-22 09:52:55 +08:00
}
else
{
sku = data.sku;
2026-04-22 09:52:55 +08:00
}
return sku;
2026-04-22 09:52:55 +08:00
}
private static void SendEventClickByName(string name, string type)
2026-04-22 09:52:55 +08:00
{
if (name == null)
{
return;
}
2026-04-22 09:52:55 +08:00
string clickTrackKey = null;
string successTrackKey = null;
string openTrackKey = null;
2026-04-22 09:52:55 +08:00
if (name == GetPaySku(PayType.pack_reward))
2026-04-22 09:52:55 +08:00
{
clickTrackKey = ADEventTrack.Property.lucky_gift_click;
successTrackKey = ADEventTrack.Property.lucky_gift_receive;
openTrackKey = ADEventTrack.Property.first_pack_show;
2026-04-22 09:52:55 +08:00
}
else if (name == GetPaySku(PayType.remove_ad))
2026-04-22 09:52:55 +08:00
{
clickTrackKey = ADEventTrack.Property.remove_ad_click;
successTrackKey = ADEventTrack.Property.remove_ad_receive;
openTrackKey = ADEventTrack.Property.gift_show;
2026-04-22 09:52:55 +08:00
}
else if (name == GetPaySku(PayType.battle_pass))
2026-04-22 09:52:55 +08:00
{
clickTrackKey = ADEventTrack.Property.master_pass_click;
successTrackKey = ADEventTrack.Property.master_pass_receive;
openTrackKey = ADEventTrack.Property.master_pass_show;
2026-04-22 09:52:55 +08:00
}
else if (name == GetPaySku(PayType.buy_one))
2026-04-22 09:52:55 +08:00
{
clickTrackKey = ADEventTrack.Property.buy_one_click;
successTrackKey = ADEventTrack.Property.buy_one_success;
openTrackKey = ADEventTrack.Property.buy_one_show;
2026-04-22 09:52:55 +08:00
}
else if (name == GetPaySku(PayType.buy_one_off))
2026-04-22 09:52:55 +08:00
{
// clickTrackKey = ADEventTrack.Property.BuyOneOffClick;
// successTrackKey = ADEventTrack.Property.BuyOneOffSuccess;
// openTrackKey = ADEventTrack.Property.BuyOneOffShow;
2026-04-22 09:52:55 +08:00
}
else if (name == GetPaySku(PayType.fail_pack))
2026-04-22 09:52:55 +08:00
{
clickTrackKey = ADEventTrack.Property.fail_click;
successTrackKey = ADEventTrack.Property.fail_buy_success;
openTrackKey = ADEventTrack.Property.fail_show;
2026-04-22 09:52:55 +08:00
}
else if (name == GetPaySku(PayType.three_days_gift))
2026-04-22 09:52:55 +08:00
{
clickTrackKey = ADEventTrack.Property.three_days_gift_click;
successTrackKey = ADEventTrack.Property.three_days_gift_buy_success;
openTrackKey = ADEventTrack.Property.three_days_gift_show;
2026-04-22 09:52:55 +08:00
}
if (name.StartsWith("buy_gold"))
2026-04-22 09:52:55 +08:00
{
int startIndex = "buy_gold".Length;
string suffix = name[startIndex..]; // 截取 "gold" 后的所有字符
clickTrackKey = ADEventTrack.Property.gold_click_ + suffix;
successTrackKey = ADEventTrack.Property.shop_receive_ + suffix;
openTrackKey = ADEventTrack.Property.shop_show;
2026-04-22 09:52:55 +08:00
}
if (name.StartsWith("vip_club"))
2026-04-22 09:52:55 +08:00
{
int startIndex = "vip_club".Length;
string suffix = name[startIndex..]; // 截取 "gold" 后的所有字符
clickTrackKey = ADEventTrack.Property.vip_click_ + suffix;
successTrackKey = ADEventTrack.Property.vip_success_ + suffix;
openTrackKey = ADEventTrack.Property.buy_one_show;
}
2026-04-22 09:52:55 +08:00
if (type == "success" && successTrackKey != null)
2026-04-22 09:52:55 +08:00
{
TrackKit.SendEvent(ADEventTrack.IOS_Pay_Event, successTrackKey);
2026-04-22 09:52:55 +08:00
}
else if (type == "click" && clickTrackKey != null)
2026-04-22 09:52:55 +08:00
{
TrackKit.SendEvent(ADEventTrack.IOS_Pay_Event, clickTrackKey);
2026-04-22 09:52:55 +08:00
}
else if (type == "open" && openTrackKey != null)
2026-04-22 09:52:55 +08:00
{
// TrackKit.SendEvent(ADEventTrack.IOS_Pay_Event, openTrackKey);
2026-04-22 09:52:55 +08:00
}
}
}
public class SubscriptionList
2026-04-22 09:52:55 +08:00
{
[JsonProperty("renew_time")]
public long renew_time;
[JsonProperty("renew_sku")]
public string renew_sku;
[JsonProperty("sku")]
2026-04-22 09:52:55 +08:00
public string sku;
[JsonProperty("orig_tx_id")]
public string orig_tx_id;
2026-04-22 09:52:55 +08:00
}
public class HistoryObject
2026-04-22 09:52:55 +08:00
{
[JsonProperty("id")]
public string id;
2026-04-22 09:52:55 +08:00
}