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

281 lines
8.7 KiB
C#

using System;
using System.Collections.Generic;
using SGModule.ApplePay;
using SGModule.NetKit;
using UnityEngine;
using LoveLegend;
using UnityEngine.Purchasing;
using Newtonsoft.Json;
public enum PayType
{
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,
}
public class PurchasingManager
{
private static readonly List<ApplePay> PayConfig = ConfigSystem.GetConfig<ApplePay>();
private static readonly List<ApplePay2> PayConfig2 = ConfigSystem.GetConfig<ApplePay2>();
public static string GetPaySku(PayType key)
{
string keys = "";
if (PayConfig != null)
{
foreach (var data in PayConfig)
{
if (data.payKey != key.ToString()) continue;
keys = data.sku;
break;
}
}
return keys;
}
public static int GetVipLvFormConfig(string sku)
{
var lv = 1;
if (GetPaySku(PayType.weekly_subscription) == sku)
{
lv = 1;
}
else if (GetPaySku(PayType.monthly_subscription) == sku)
{
lv = 2;
}
else if (GetPaySku(PayType.yearly_subscription) == sku)
{
lv = 3;
}
return lv;
}
public static void Purchase(ApplePayData payData)
{
//本地测试
// TestIOSPay(payData);
// return;
Debug.Log($"[Apple Pay] unity Purchase--0-----: {payData.sku}");
ApplePayManager.Instance.Purchase(payData.sku, ApplePaySuccessCallback(payData), message =>
{
Debug.Log("purchase fail------- reason: " + message);
});
}
public static Action<ApplePayBackType, AppleResponseData> ApplePaySuccessCallback(ApplePayData payData)
{
return (backType, AppleResponseData) =>
{
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;
}
};
}
private static void TestIOSPay(ApplePayData payData)
{
var tranId = "";
var types = ProductType.Consumable;
if (GetPaySku(PayType.battle_pass) == payData.sku)
{
tranId = "2000000983625783";
}
else if (GetPaySku(PayType.weekly_subscription) == payData.sku)
{
tranId = "2000000984643029";
types = ProductType.Subscription;
}
if (tranId == "") return;
ApplePayManager.Instance.ApplePayTest(types, payData.sku, tranId, ApplePaySuccessCallback(payData));
}
private static string SetSku(ApplePayData data)
{
string sku = "";
if (data.type != null && data.type == GetPaySku(PayType.remove_ad))
{
sku = data.type;
}
else if (data.sku.Contains("shop"))
{
sku = data.shopName;
}
else if (data.sku.Contains("sub"))
{
sku = data.shopName;
}
else
{
sku = data.sku;
}
return sku;
}
private static void SendEventClickByName(string name, string type)
{
if (name == null)
{
return;
}
string clickTrackKey = null;
string successTrackKey = null;
string openTrackKey = null;
if (name == GetPaySku(PayType.pack_reward))
{
clickTrackKey = ADEventTrack.Property.lucky_gift_click;
successTrackKey = ADEventTrack.Property.lucky_gift_receive;
openTrackKey = ADEventTrack.Property.first_pack_show;
}
else if (name == GetPaySku(PayType.remove_ad))
{
clickTrackKey = ADEventTrack.Property.remove_ad_click;
successTrackKey = ADEventTrack.Property.remove_ad_receive;
openTrackKey = ADEventTrack.Property.gift_show;
}
else if (name == GetPaySku(PayType.battle_pass))
{
clickTrackKey = ADEventTrack.Property.master_pass_click;
successTrackKey = ADEventTrack.Property.master_pass_receive;
openTrackKey = ADEventTrack.Property.master_pass_show;
}
else if (name == GetPaySku(PayType.buy_one))
{
clickTrackKey = ADEventTrack.Property.buy_one_click;
successTrackKey = ADEventTrack.Property.buy_one_success;
openTrackKey = ADEventTrack.Property.buy_one_show;
}
else if (name == GetPaySku(PayType.buy_one_off))
{
// clickTrackKey = ADEventTrack.Property.BuyOneOffClick;
// successTrackKey = ADEventTrack.Property.BuyOneOffSuccess;
// openTrackKey = ADEventTrack.Property.BuyOneOffShow;
}
else if (name == GetPaySku(PayType.fail_pack))
{
clickTrackKey = ADEventTrack.Property.fail_click;
successTrackKey = ADEventTrack.Property.fail_buy_success;
openTrackKey = ADEventTrack.Property.fail_show;
}
else if (name == GetPaySku(PayType.three_days_gift))
{
clickTrackKey = ADEventTrack.Property.three_days_gift_click;
successTrackKey = ADEventTrack.Property.three_days_gift_buy_success;
openTrackKey = ADEventTrack.Property.three_days_gift_show;
}
if (name.StartsWith("buy_gold"))
{
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;
}
if (name.StartsWith("vip_club"))
{
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;
}
if (type == "success" && successTrackKey != null)
{
TrackKit.SendEvent(ADEventTrack.IOS_Pay_Event, successTrackKey);
}
else if (type == "click" && clickTrackKey != null)
{
TrackKit.SendEvent(ADEventTrack.IOS_Pay_Event, clickTrackKey);
}
else if (type == "open" && openTrackKey != null)
{
// TrackKit.SendEvent(ADEventTrack.IOS_Pay_Event, openTrackKey);
}
}
}
public class SubscriptionList
{
[JsonProperty("renew_time")]
public long renew_time;
[JsonProperty("renew_sku")]
public string renew_sku;
[JsonProperty("sku")]
public string sku;
[JsonProperty("orig_tx_id")]
public string orig_tx_id;
}
public class HistoryObject
{
[JsonProperty("id")]
public string id;
}