291 lines
11 KiB
C#
291 lines
11 KiB
C#
using UnityEngine;
|
|||
|
|
using System;
|
||
|
|
using DG.Tweening;
|
||
|
|
using Newtonsoft.Json;
|
||
|
|
using Newtonsoft.Json.Linq;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Linq;
|
||
|
|
using ScrewsMaster;
|
||
|
|
|
||
|
|
|
||
|
|
public class PurchasingManager
|
||
|
|
{
|
||
|
|
public static readonly PurchasingManager Instance = new PurchasingManager();
|
||
|
|
|
||
|
|
//private static IGooglePlayStoreExtensions googleExtension;
|
||
|
|
|
||
|
|
private Action<string> failedCallback;
|
||
|
|
|
||
|
|
private ApplePayClass applePayData;
|
||
|
|
|
||
|
|
PurchasingManager()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 初始化商品
|
||
|
|
/// 建议在游戏初始化完成的时候就去初始化商品
|
||
|
|
/// </summary>
|
||
|
|
public void InitProduct()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public const string buy_one = "com.twistturn.space.24.99";
|
||
|
|
public const string buy_gold_1 = "com.twistturn.shop.1.99";
|
||
|
|
public const string buy_gold_2 = "com.twistturn.shop.3.99";
|
||
|
|
public const string buy_gold_3 = "com.twistturn.shop.9.99";
|
||
|
|
public const string buy_gold_4 = "com.twistturn.shop.19.99";
|
||
|
|
public const string buy_gold_5 = "com.twistturn.shop.39.99";
|
||
|
|
public const string remove_ad = "com.twistturn.remove.2.99";
|
||
|
|
public const string battle_pass = "com.twistturn.pass.9.99";
|
||
|
|
public const string pack_reward = "com.twistturn.gift.1.99";
|
||
|
|
public const string fail_pack = "com.twistturn.fail.1.99";
|
||
|
|
public const string three_days_gift = "com.twistturn.threeday.3.99";
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 发起内购
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="_productId">要购买的商品ID</param>
|
||
|
|
/// <param name="_successedCallback">购买成功回调</param>
|
||
|
|
/// <param name="_failedCallback">购买失败回调</param>/* */
|
||
|
|
public void Purchase(ApplePayClass data_)
|
||
|
|
{
|
||
|
|
string _productId = data_.sku;
|
||
|
|
Debug.Log($"[barry] Purchase;;;;;;;;;;;;;;;;;;;{_productId}\n data=== {JsonConvert.SerializeObject(data_)}");
|
||
|
|
|
||
|
|
if (Time.time - SaveData.pay_time < 5)
|
||
|
|
{
|
||
|
|
GameHelper.ShowTips("Clicks are too frequent");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
SaveData.pay_time = Time.time;
|
||
|
|
|
||
|
|
if (data_.sku == buy_one)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.buy_one_click, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == remove_ad)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.remove_ad_click, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == pack_reward)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.pack_click, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == fail_pack)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.fail_click, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == three_days_gift)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.three_days_gift_click, 1);
|
||
|
|
}
|
||
|
|
else if (data_.shopName != null && data_.shopName.StartsWith("buy_gold"))
|
||
|
|
{
|
||
|
|
int startIndex = "buy_gold".Length;
|
||
|
|
string suffix = data_.shopName[startIndex..]; // 截取 "gold" 后的所有字符
|
||
|
|
string eventClickName = $"gold_click_{suffix}";
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, eventClickName, 1);
|
||
|
|
|
||
|
|
}
|
||
|
|
else if (data_.sku == battle_pass)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.pass_click, 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
ApplePay(data_);
|
||
|
|
|
||
|
|
|
||
|
|
Debug.Log("[barry] Purchase;;;;;;;;;;;;;;;;;;;");
|
||
|
|
// ApplePay(_productId, data_);
|
||
|
|
// GameDispatcher.Instance.Dispatch(GameMsg.apple_pay_success, data_.sku);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ApplePay(ApplePayClass data_)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//private List<Product> product_list = new List<Product>();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// IOS恢复内购
|
||
|
|
/// 会在删除应用后,第一次安装是自动恢复
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="restoreCallback">恢复回调</param>
|
||
|
|
public void IosRestore(Action<bool, string> restoreCallback)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//======================================分割线=========================================
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public void SaveApplePayData(Dictionary<string, ApplePayClass> payData)
|
||
|
|
{
|
||
|
|
// 保存更新后的数据
|
||
|
|
PlayerPrefs.SetString("apple_pay_data", JsonConvert.SerializeObject(payData));
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public void ApplePaySuccess(string transactionID)
|
||
|
|
{
|
||
|
|
Debug.Log("apple 支付 StartCoroutine------");
|
||
|
|
CrazyAsyKit.StartCoroutine(ProcessPayData(transactionID));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void startPay()
|
||
|
|
{
|
||
|
|
var payDataJson = PlayerPrefs.GetString("apple_pay_data", "{}");
|
||
|
|
var statusDictionary = JsonConvert.DeserializeObject<Dictionary<string, ApplePayClass>>(payDataJson);
|
||
|
|
|
||
|
|
foreach (var entry in statusDictionary.OrderByDescending(pair => pair.Key))
|
||
|
|
{
|
||
|
|
DOVirtual.DelayedCall(1f, () =>
|
||
|
|
{
|
||
|
|
Debug.Log($"max 支付 ProcessPayData0======{entry.Key} status:{entry.Value}");
|
||
|
|
CrazyAsyKit.StartCoroutine(ProcessPayData(entry.Key));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public IEnumerator ProcessPayData(string orderId)
|
||
|
|
{
|
||
|
|
// 发起请求
|
||
|
|
ApplePayRequest(orderId);
|
||
|
|
|
||
|
|
yield return null; // 等待本次请求完成
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ApplePayRequest(string transactionID)
|
||
|
|
{
|
||
|
|
var payDataJson = PlayerPrefs.GetString("apple_pay_data", "{}");
|
||
|
|
var statusDictionary = JsonConvert.DeserializeObject<Dictionary<string, ApplePayClass>>(payDataJson);
|
||
|
|
|
||
|
|
if (statusDictionary.ContainsKey(transactionID))
|
||
|
|
{
|
||
|
|
ApplePayClass data_ = statusDictionary[transactionID];
|
||
|
|
var test = new checkData();
|
||
|
|
test.signedPayload = Base64Kit.Encode(transactionID, true);
|
||
|
|
test.innerOrderId = data_.innerOrderId;
|
||
|
|
NetworkKit.PostWithHeader<orderData>("shop/applePayCheck", test, (isSuccess, obj) =>
|
||
|
|
{
|
||
|
|
Debug.Log("[barry] yanzhengchengleeeee");
|
||
|
|
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);
|
||
|
|
|
||
|
|
if (isSuccess)
|
||
|
|
{
|
||
|
|
Debug.Log("barry] yanzhengchenggonggggg");
|
||
|
|
|
||
|
|
statusDictionary.Remove(transactionID);
|
||
|
|
// 保存更新后的数据
|
||
|
|
SaveApplePayData(statusDictionary);
|
||
|
|
|
||
|
|
SaveingPotClass taskData = SaveData.GetSaveobject().saveingpot_history.Last();
|
||
|
|
Makeup_2 makeupVo = ConfigSystem.GetConfig<MakeupModel_2>().GetData(taskData.tableId);
|
||
|
|
SaveData.GetSaveobject().saveingpot_cash += ((float)data_.amount) / 100 * makeupVo.PayIncrease;
|
||
|
|
GameDispatcher.Instance.Dispatch(GameMsg.RefreshSaveingPot);
|
||
|
|
if (ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1 && GameHelper.IsGiftSwitch())
|
||
|
|
{
|
||
|
|
if ((SaveData.GetSaveobject().saveingpot_cash > taskData.amount) && (!taskData.auto_show) && !UIManager.Instance.IsExistUI(UIConst.H5UI))
|
||
|
|
{
|
||
|
|
|
||
|
|
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SaveingPotUI_Open);
|
||
|
|
taskData.auto_show = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
string sku = data_.sku.Contains("shop") ? data_.shopName : data_.sku;
|
||
|
|
GameDispatcher.Instance.Dispatch(GameMsg.apple_pay_success, sku);
|
||
|
|
if (data_.sku == buy_one)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.buy_one_open, 1);
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.buy_one_success, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == remove_ad)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.remove_ad_open, 1);
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.remove_ad_success, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == pack_reward)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.pack_open, 1);
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.pack_success, 1);
|
||
|
|
}
|
||
|
|
else if (data_.shopName != null && data_.shopName.StartsWith("buy_gold"))
|
||
|
|
{
|
||
|
|
int startIndex = "buy_gold".Length;
|
||
|
|
string suffix = data_.shopName[startIndex..]; // 截取 "gold" 后的所有字符
|
||
|
|
string eventOpenName = $"gold_open_{suffix}";
|
||
|
|
string eventSuccessName = $"gold_success_{suffix}";
|
||
|
|
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, eventOpenName, 1);
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, eventSuccessName, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == battle_pass)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.pass_open, 1);
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.pass_success, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == fail_pack)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.fail_open, 1);
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.fail_buy_success, 1);
|
||
|
|
}
|
||
|
|
else if (data_.sku == three_days_gift)
|
||
|
|
{
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.three_days_gift_open, 1);
|
||
|
|
NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.three_days_gift_buy_success, 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
//验证订单失败
|
||
|
|
// reCreatPur(2);
|
||
|
|
//根据code 判断是否需要重试
|
||
|
|
|
||
|
|
if (!new List<int>() { 1021, 1026, 1027, 1028 }.Contains(obj.code))
|
||
|
|
{
|
||
|
|
ApplePayRequest(transactionID);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
statusDictionary.Remove(transactionID);
|
||
|
|
// 保存更新后的数据
|
||
|
|
SaveApplePayData(statusDictionary);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
private void OnFailedCallback(string _reason)
|
||
|
|
{
|
||
|
|
Debug.Log("[barry 11]OnFailedCallback Reason:" + _reason);
|
||
|
|
if (failedCallback != null)
|
||
|
|
{
|
||
|
|
failedCallback(_reason);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|