Files
tysdk-intergration/sdk-intergration/Packages/tysdk/Runtime/TYSdkFacade_Pay.cs
2026-05-21 00:37:19 +08:00

215 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using UnityEngine;
using System;
using System.Text;
namespace tysdk
{
public partial class TYSdkFacade : MonoBehaviour
{
/*================================================
____ _
| _ \ __ _ _ _ _ __ ___ ___ _ __ | |_
| |_) / _` | | | | '_ ` _ \ / _ \ '_ \| __|
| __/ (_| | |_| | | | | | | __/ | | | |_
|_| \__,_|\__, |_| |_| |_|\___|_| |_|\__|
|___/
=================================================*/
private bool _payFirstNegativeHandled;
//public async Task<PaymentInfo> Pay(string prodId, string prodPrice, string prodName, int count, string pType, string price_amount_micros =null)
public async Task<PaymentInfo> Pay(SKUDetail prod, int count, float usdprice, JObject purchaseInfo)
{
if (!IsLoggedIn) return new PaymentInfo() { code = "-1", msg = "未登录" };
var orderId = System.Guid.NewGuid().ToString();
if(purchaseInfo == null)
purchaseInfo = new JObject();
purchaseInfo["orderId"] = orderId;
string extraInfo = purchaseInfo.ToString(Newtonsoft.Json.Formatting.None);
//to base64
extraInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(extraInfo));
Debug.Log("[TYSdk] extraInfo: " + extraInfo);
var prodId = prod.ProdID;
var prodPrice = usdprice.ToString();
var prodName = prod.title;
#if UNITY_EDITOR
await Task.Yield();
var result = new PaymentInfo()
{
code = "0",
msg = "success",
count = count,
orderId = orderId,
productId = prodId,
price = prodPrice
};
return result;
#elif UNITY_ANDROID
var pType = ChannelConfig.PayType;
var task = TYSDKCallbackManager.Instance.RegisterCallback<PaymentInfo>(TimeSpan.FromSeconds(90));
try
{
UnityBridgeFunc.UnityKnowNew(prodId, prodPrice, prodName, count.ToString(), orderId, extraInfo,pType);
var result = await task;
result.count = count;
result.orderId = orderId;
result.productId = prodId;
result.price = prodPrice;
result.Check(orderId, prodId);
return result;
}
catch (Exception e)
{
Debug.LogWarning($"[TYSdkFacade::Pay] Faild {e.Message}");
return new PaymentInfo(){code = "-1"};
}
finally
{
_payFirstNegativeHandled = false;
}
#elif UNITY_IOS
var task = TYSDKCallbackManager.Instance.RegisterCallback<PaymentInfo>(TimeSpan.FromSeconds(30));
UnityBridgeFunc.UnityKnow(_accountInfo.strUserId, prodId, prodPrice, prodName, count.ToString(), orderId, extraInfo);
try{
var result = await task;
result.count = count;
result.orderId = orderId;
result.productId = prodId;
result.price = prodPrice;
return result;
}
catch(Exception e)
{
Debug.LogWarning($"[TYSdkFacade::Pay] Faild {e.Message}");
return new PaymentInfo(){code = "-1"};
}
finally
{
_payFirstNegativeHandled = false;
}
#endif
}
//支付的回调
public void PayResult(string json)
{
Debug.Log("[TYSdkFacade] pay callback:" + json);
var jresult = JObject.Parse(json);
var result = new PaymentInfo();
result.code = jresult["code"].ToString();
result.msg = jresult["errStr"].ToString();
var purchaseJson = jresult["purchaseJson"]?.ToString();
var signature = jresult["signature"]?.ToString();
var purchaseToken = jresult["purchaseToken"]?.ToString();
result.purchaseJson = purchaseJson;
result.signature = signature;
result.purchaseToken = purchaseToken;
if (!string.IsNullOrEmpty(purchaseJson) || !string.IsNullOrEmpty(purchaseToken))
{
result.Receipt = new PaymentReceipt
{
Provider = PaymentReceiptProvider.Flexion,
PurchaseJson = purchaseJson,
Signature = signature,
PurchaseToken = purchaseToken
};
}
result.storeOrderId = jresult["orderId"]?.ToString();
result.userId = _accountInfo?.strUserId;
if(result.code == "-1")
{
if (!_payFirstNegativeHandled)
{
_payFirstNegativeHandled = true;
TYSDKCallbackManager.Instance.ResetCallbackTimeout<PaymentInfo>(TimeSpan.FromSeconds(5));
Debug.Log("[TYSdkFacade] pay failed (first time): reset timeout to 5s, waiting next pay callback");
}
else
{
Debug.Log("[TYSdkFacade] pay failed: waiting next pay callback");
}
return;
}
TYSDKCallbackManager.Instance.TryTriggerCallback(result);
}
// Get Pay list
public async Task<ProductListInfo> GetSKUList()
{
if (!IsLoggedIn) return new ProductListInfo() { code = -1, msg = "未登录" };
#if UNITY_EDITOR
await Task.Yield();
var result = new ProductListInfo();
return result;
#elif UNITY_ANDROID || UNITY_IOS
var task = TYSDKCallbackManager.Instance.RegisterCallback<ProductListInfo>();
UnityBridgeFunc.GetSKUList();
var result = await task;
return result;
#endif
}
//通过商品ID列表获取商品详情Flexion 渠道使用)
public async Task<ProductListInfo> GetSKUListByIapIds(string productIds)
{
if (!IsLoggedIn) return new ProductListInfo() { code = -1, msg = "未登录" };
#if UNITY_EDITOR
await Task.Yield();
return new ProductListInfo();
#elif UNITY_ANDROID
var task = TYSDKCallbackManager.Instance.RegisterCallback<ProductListInfo>();
UnityBridgeFunc.GetSKUListByIapIds(productIds);
var result = await task;
return result;
#else
return new ProductListInfo() { code = -1, msg = "unsupported platform" };
#endif
}
//商品列表的回调
public void SKUListResult(string json)
{
Debug.Log("[TYSdkFacade] GetSKUList callback");
var jresult = JObject.Parse(json);
int code = int.Parse(jresult["code"].ToString());
var result = new ProductListInfo();
if (code == 0)
{
string productStr = jresult["respObj"].ToString();
result.code = code;
#if UNITY_ANDROID && !UNITY_EDITOR
var jsonObjList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SKUDetail>>(productStr);
result.products = jsonObjList;
#elif UNITY_IOS && !UNITY_EDITOR
result.ReadSKUFromJson(productStr);
#endif
}
TYSDKCallbackManager.Instance.TryTriggerCallback(result);
}
}
}