245 lines
7.3 KiB
C#
245 lines
7.3 KiB
C#
using NLDPB;
|
||
using System;
|
||
using PhxhSDK;
|
||
using Gameplay;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
using Gameplay.Net;
|
||
using Newtonsoft.Json;
|
||
using UnityEngine.Networking;
|
||
using Cysharp.Threading.Tasks;
|
||
using System.Collections.Generic;
|
||
using System.Text.RegularExpressions;
|
||
|
||
public class BilibiliPayHandle : SingletonMono<BilibiliPayHandle>
|
||
{
|
||
private static BilibiliPayHandle _instance;
|
||
|
||
private const string PayPost = "orders/biliGame";
|
||
/// <summary>
|
||
/// 支付
|
||
/// </summary>
|
||
private Action<string, string, int> payAction;
|
||
|
||
private void Awake()
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = this;
|
||
DontDestroyOnLoad(gameObject);
|
||
}
|
||
else
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
[Serializable]
|
||
private class PayRequestData
|
||
{
|
||
public string items;
|
||
public int actor_id;
|
||
public string game_money;
|
||
|
||
public PayRequestData(string items, int actorID, int moneyCount)
|
||
{
|
||
this.items = items;
|
||
actor_id = actorID;
|
||
game_money = moneyCount.ToString();
|
||
}
|
||
}
|
||
|
||
[Serializable]
|
||
private class Items
|
||
{
|
||
public string product_id;
|
||
public int count;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 请求结果
|
||
/// </summary>
|
||
private class WebRequestResult
|
||
{
|
||
public int code;
|
||
public string message;
|
||
public BiliPayData data;
|
||
}
|
||
|
||
private class BiliPayData
|
||
{
|
||
/// <summary>
|
||
/// 订单号
|
||
/// </summary>
|
||
public string order_sn;
|
||
/// <summary>
|
||
/// 签名
|
||
/// </summary>
|
||
public string order_sign;
|
||
/// <summary>
|
||
/// 价格
|
||
/// </summary>
|
||
public int money;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否有回调结果
|
||
/// </summary>
|
||
private bool isCallBack;
|
||
|
||
/// <summary>
|
||
/// 开始支付
|
||
/// </summary>
|
||
public void BuyGoods(string currencyID, int count, int diamonCount, Action<string, string, int> pay)
|
||
{
|
||
payAction = pay;
|
||
|
||
BiliPayRequest(currencyID, count, diamonCount);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 请求服务器
|
||
/// </summary>
|
||
private async void BiliPayRequest(string currencyID, int count, int diamonCount)
|
||
{
|
||
int actorID = (int)Actor.Instance.Base.GetProp(ActorProp.ActorId);
|
||
string payUrl = AppConfig.Instance.LoginURL + PayPost;
|
||
DebugUtil.Log($"发送拉起Bilibili支付请求地址: {payUrl}");
|
||
|
||
List<Items> itemsList = new() { new() { product_id = currencyID, count = count } };
|
||
string itemJsonData = JsonConvert.SerializeObject(itemsList);
|
||
DebugUtil.Log($"items.jsonData:{itemJsonData}");
|
||
|
||
PayRequestData sendData = new(itemJsonData, actorID, diamonCount);
|
||
Dictionary<string, string> headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
|
||
{
|
||
{ "items", sendData.items },
|
||
{ "actor_id", sendData.actor_id.ToString() },
|
||
{ "game_money", sendData.game_money },
|
||
{ "timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }
|
||
});
|
||
|
||
string jsonData = JsonUtility.ToJson(sendData);
|
||
DebugUtil.Log($"WebRequestData.jsonData:{jsonData}");
|
||
byte[] bytes = Encoding.UTF8.GetBytes(jsonData);
|
||
|
||
UnityWebRequest www = new(payUrl, UnityWebRequest.kHttpVerbPOST)
|
||
{
|
||
uploadHandler = new UploadHandlerRaw(bytes),
|
||
downloadHandler = new DownloadHandlerBuffer(),
|
||
};
|
||
www.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); //为了保证客户端发送的json格式在后端能解析
|
||
www.SetRequestHeader("signature", headData["signature"]);
|
||
www.SetRequestHeader("timestamp", headData["timestamp"]);
|
||
www.SetRequestHeader("accessKey", headData["accessKey"]);
|
||
|
||
try
|
||
{
|
||
await www.SendWebRequest();
|
||
if (www.result != UnityWebRequest.Result.Success)
|
||
{
|
||
CommonUtils.ShowMessageTips("支付失败,请重试");
|
||
DebugUtil.LogError(www.downloadHandler.text);
|
||
|
||
BiliPayData biliPayData = new()
|
||
{
|
||
order_sn = "B2025010283888143362",
|
||
order_sign = "33dbdd69c25cbe58461b6cda556470dd"
|
||
};
|
||
BuyGoods(biliPayData);
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.Log("http消息请求成功!\n返回内容:" + www.downloadHandler.text);
|
||
WebRequestResult result = JsonConvert.DeserializeObject<WebRequestResult>(www.downloadHandler.text);
|
||
if (result.code == 0)
|
||
{
|
||
BuyGoods(result.data);
|
||
DebugUtil.Log("拉起bili支付SDK成功");
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("拉起bili支付SDK失败");
|
||
UITipsManager.ShowTips("支付失败,请重试");
|
||
}
|
||
}
|
||
}
|
||
catch (UnityWebRequestException e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
var pattern = @"\{.*\}";
|
||
var match = Regex.Match(e.Message, pattern);
|
||
if (match.Success)
|
||
{
|
||
var json = match.Value;
|
||
WebRequestResult res = JsonConvert.DeserializeObject<WebRequestResult>(json);
|
||
DebugUtil.LogError("请求失败, 服务器有错误响应: " + res.message);
|
||
UITipsManager.ShowTips("无法购买");
|
||
}
|
||
else
|
||
{
|
||
UITipsManager.ShowTips("无法购买");
|
||
DebugUtil.LogError("请求失败!");
|
||
}
|
||
|
||
BiliPayData biliPayData = new()
|
||
{
|
||
order_sn = "B2025010283888143362",
|
||
order_sign = "33dbdd69c25cbe58461b6cda556470dd"
|
||
};
|
||
BuyGoods(biliPayData);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 支付回调
|
||
/// </summary>
|
||
public void WxPayCallback(string str)
|
||
{
|
||
// 返回结果 0 成功
|
||
// -1 错误 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常原因等
|
||
// -2 用户取消
|
||
try
|
||
{
|
||
isCallBack = true;
|
||
DebugUtil.Log("微信支付回调, 结果码:{0}", str);
|
||
|
||
if (int.TryParse(str, out var code))
|
||
{
|
||
switch (code)
|
||
{
|
||
case -2:
|
||
UITipsManager.ShowTips("取消支付");
|
||
break;
|
||
case -1:
|
||
UITipsManager.ShowTips("支付失败,请重试");
|
||
break;
|
||
}
|
||
}
|
||
|
||
UIManager.Instance.CloseWindow(UINameConst.UI_Pay);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError("WXPayHandle.WxPayCallback error:{0}", e);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 购买商品
|
||
/// </summary>
|
||
/// <param name="biliPayData"></param>
|
||
private void BuyGoods(BiliPayData biliPayData)
|
||
{
|
||
if (null == biliPayData)
|
||
{
|
||
DebugUtil.LogError("请求支付数据为空");
|
||
return;
|
||
}
|
||
|
||
isCallBack = false;
|
||
|
||
DebugUtil.Log("开始调起bili支付");
|
||
payAction?.Invoke(biliPayData.order_sn, biliPayData.order_sign, biliPayData.money);
|
||
}
|
||
} |