356 lines
11 KiB
C#
356 lines
11 KiB
C#
using System.Collections.Generic;
|
|
using SGModule.NetKit;
|
|
using UnityEngine;
|
|
using RedHotRoast;
|
|
using Newtonsoft.Json;
|
|
using SGModule.Net;
|
|
using UnityEngine.Events;
|
|
|
|
// 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);
|
|
// });
|
|
|
|
if (payData.sku.Contains("sub"))
|
|
{
|
|
IAPPayManager.Instance.SubscribeProduct(payData.sku, "",() =>
|
|
{
|
|
|
|
});
|
|
}
|
|
else
|
|
{
|
|
IAPPayManager.Instance.BuyProduct(payData.sku, "",() =>
|
|
{
|
|
|
|
});
|
|
}
|
|
}
|
|
|
|
// 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.IAP_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 typespes = 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;
|
|
// }
|
|
|
|
// 商店商品映射表
|
|
public static readonly Dictionary<string, string> ShopProductMap = new Dictionary<string, string>
|
|
{
|
|
{ IAPPayManager.PRODUCT_SHOP_1, "0" },
|
|
{ IAPPayManager.PRODUCT_SHOP_2, "1" },
|
|
{ IAPPayManager.PRODUCT_SHOP_3, "2" },
|
|
{ IAPPayManager.PRODUCT_SHOP_4, "3" },
|
|
{ IAPPayManager.PRODUCT_SHOP_5, "4" }
|
|
};
|
|
|
|
// 订阅商品映射表
|
|
public static readonly Dictionary<string, string> VipProductMap = new Dictionary<string, string>
|
|
{
|
|
{ IAPPayManager.PRODUCT_VIP_WEEK, "0" },
|
|
{ IAPPayManager.PRODUCT_VIP_MONTH, "1" },
|
|
{ IAPPayManager.PRODUCT_VIP_YEAR, "2" },
|
|
};
|
|
|
|
/// <summary>
|
|
/// 更新map中的value,与配置表的id对应
|
|
/// _shopProductMap
|
|
/// </summary>
|
|
public static void SetShopMapValue()
|
|
{
|
|
var list = ConfigSystem.GetConfig<Paidcoins>();
|
|
if (list is not { Count: > 0 }) return;
|
|
foreach (var paidcoin in list)
|
|
{
|
|
if (string.IsNullOrEmpty(paidcoin.SKU))
|
|
continue;
|
|
|
|
if (ShopProductMap.ContainsKey(paidcoin.SKU))
|
|
{
|
|
ShopProductMap[paidcoin.SKU] = (paidcoin.id - 1).ToString();
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void SendEventClickByName(string name, string type)
|
|
{
|
|
if (name == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string clickTrackKey = null;
|
|
string successTrackKey = null;
|
|
string openTrackKey = null;
|
|
|
|
if (name == IAPPayManager.PRODUCT_FIRST_GIFT)
|
|
{
|
|
clickTrackKey = ADEventTrack.Property.lucky_gift_click;
|
|
successTrackKey = ADEventTrack.Property.lucky_gift_receive;
|
|
openTrackKey = ADEventTrack.Property.first_pack_show;
|
|
}
|
|
else if (name == IAPPayManager.PRODUCT_REMOVE_ADS)
|
|
{
|
|
clickTrackKey = ADEventTrack.Property.remove_ad_click;
|
|
successTrackKey = ADEventTrack.Property.remove_ad_receive;
|
|
openTrackKey = ADEventTrack.Property.gift_show;
|
|
}
|
|
else if (name == IAPPayManager.PRODUCT_PASS_BONUS)
|
|
{
|
|
clickTrackKey = ADEventTrack.Property.master_pass_click;
|
|
successTrackKey = ADEventTrack.Property.master_pass_receive;
|
|
openTrackKey = ADEventTrack.Property.master_pass_show;
|
|
}
|
|
else if (name == IAPPayManager.PRODUCT_SPACE_BONUS)
|
|
{
|
|
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;
|
|
// }
|
|
|
|
else if (ShopProductMap.TryGetValue(name, out var idx))
|
|
{
|
|
clickTrackKey = ADEventTrack.Property.gold_click_ + idx;
|
|
successTrackKey = ADEventTrack.Property.shop_receive_ + idx;
|
|
openTrackKey = ADEventTrack.Property.shop_show;
|
|
}
|
|
|
|
else if (VipProductMap.TryGetValue(name, out var vipIdx))
|
|
{
|
|
clickTrackKey = ADEventTrack.Property.vip_click_ + vipIdx;
|
|
successTrackKey = ADEventTrack.Property.vip_success_ + vipIdx;
|
|
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);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// ios支付创建订单接口,改一下用来上传数据给后台统计支付数据
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <param name="onCompleted"></param>
|
|
public static void ApplePayCreate<T>(ApplePayData data, UnityAction<ResponseData<T>> onCompleted = null)
|
|
{
|
|
NetKit.Instance.Post<T>("shop/applePayCreate", data,
|
|
response => { onCompleted?.Invoke(response); });
|
|
}
|
|
|
|
}
|
|
public class ApplePayData
|
|
{
|
|
[JsonProperty("innerOrderId")]
|
|
public string innerOrderId;
|
|
[JsonProperty("amount")]
|
|
public int amount;
|
|
[JsonProperty("sku")]
|
|
public string sku;
|
|
[JsonProperty("currency")]
|
|
public string currency = "USD";
|
|
[JsonProperty("shopName")]
|
|
public string shopName;
|
|
[JsonProperty("type")]
|
|
public string type;
|
|
[JsonProperty("isCompleted")]
|
|
public bool isCompleted;
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
}
|