221 lines
7.1 KiB
C#
221 lines
7.1 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 WXPayHandle : SingletonMono<WXPayHandle>
|
||
{
|
||
private static WXPayHandle _instance;
|
||
|
||
private void Awake()
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = this;
|
||
InitWxPay();
|
||
DontDestroyOnLoad(gameObject);
|
||
}
|
||
else
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
private const string PayPost = "orders/wechat";
|
||
|
||
|
||
[Serializable]
|
||
private class PayRequestData
|
||
{
|
||
public string items;
|
||
public string actor_id;
|
||
|
||
public PayRequestData(string items, string actorID)
|
||
{
|
||
this.items = items;
|
||
actor_id = actorID;
|
||
}
|
||
}
|
||
|
||
[Serializable]
|
||
private class Items
|
||
{
|
||
public int id;
|
||
public int count;
|
||
}
|
||
|
||
// 请求结果
|
||
private class WebRequestResult
|
||
{
|
||
public int code;
|
||
public string message;
|
||
public WxData data;
|
||
}
|
||
|
||
private class WxData
|
||
{
|
||
public string noncestr;
|
||
public string package;
|
||
public string partnerid;
|
||
public string prepayid;
|
||
public string sign;
|
||
public string timestamp;
|
||
}
|
||
|
||
private async void WxPayRequest(int currencyID, int count)
|
||
{
|
||
var actorID = Actor.Instance.Base.GetProp(ActorProp.ActorId).ToString();
|
||
var payUrl = AppConfig.Instance.LoginURL + PayPost;
|
||
DebugUtil.Log("发送拉起微信支付请求地址: " + payUrl);
|
||
|
||
var itemsList = new List<Items> { new() { id = currencyID, count = count } };
|
||
var itemJsonData = JsonConvert.SerializeObject(itemsList);
|
||
DebugUtil.Log("items.jsonData:{0}", itemJsonData);
|
||
|
||
PayRequestData sendData = new PayRequestData(itemJsonData, actorID);
|
||
var headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
|
||
{
|
||
{ "items", sendData.items },
|
||
{ "actor_id", sendData.actor_id },
|
||
{ "timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }
|
||
});
|
||
|
||
var jsonData = JsonUtility.ToJson(sendData);
|
||
DebugUtil.Log("WebRequestData.jsonData:{0}", jsonData);
|
||
var bytes = Encoding.UTF8.GetBytes(jsonData);
|
||
|
||
var www = new UnityWebRequest(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);
|
||
}
|
||
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("拉起微信支付SDK成功");
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("拉起微信支付SDK失败");
|
||
}
|
||
}
|
||
}
|
||
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);
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("请求失败!");
|
||
}
|
||
}
|
||
}
|
||
|
||
public void BuyGoods(int currencyID, int count)
|
||
{
|
||
WxPayRequest(currencyID, count);
|
||
}
|
||
|
||
private AndroidJavaObject wxJavaObjcet;
|
||
|
||
private const string AppID = "wxa286fb2f1bcb664c"; // AppID
|
||
private const string PackageName = "com.phxh.dev.nld"; // 包名
|
||
private const string PayMethodName = "WeChatPayReq"; // 调起微信支付的方法名
|
||
|
||
/// <summary>
|
||
/// 初始化WXPay
|
||
/// </summary>
|
||
private void InitWxPay()
|
||
{
|
||
try
|
||
{
|
||
using AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
||
wxJavaObjcet = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
||
DebugUtil.Log("微信支付初始化成功");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.Log("初始化微信支付异常:{0}", e);
|
||
}
|
||
}
|
||
|
||
private void BuyGoods(WxData wxData)
|
||
{
|
||
if (wxData == null)
|
||
{
|
||
DebugUtil.LogError("请求支付数据为空");
|
||
return;
|
||
}
|
||
|
||
// PayMethodName // 支付方法
|
||
// req.appId = APP_ID; // 应用ID
|
||
// req.partnerId = MCH_ID; // 商户号
|
||
// req.prepayId = prepayid; // 预支付交易会话ID
|
||
// req.packageValue = packageValue; // 包名
|
||
// req.nonceStr = noncaestr; // 随机字符串
|
||
// req.timeStamp = timestamp; // 时间戳
|
||
// req.sign = sign; // 签名
|
||
// WXPayEntryActivity.GameObjectName = callBackObjectName; // 支付回调组件
|
||
// WXPayEntryActivity.CallBackFuncName = CallBackFuncName; // 支付回调方法
|
||
wxJavaObjcet.Call(PayMethodName, AppID, wxData.partnerid, wxData.prepayid, wxData.package, wxData.noncestr,
|
||
wxData.timestamp, wxData.sign, "WXPayHandle",
|
||
"WxPayCallback");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 支付回调
|
||
/// </summary>
|
||
public void WxPayCallback(string str)
|
||
{
|
||
// 返回结果 0 成功
|
||
// -1 错误 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常原因等
|
||
// -2 用户取消
|
||
DebugUtil.LogError("位置支付回调,参数:{0}", str);
|
||
}
|
||
|
||
public bool CheckWxInit()
|
||
{
|
||
try
|
||
{
|
||
if (wxJavaObjcet == null) return false;
|
||
return wxJavaObjcet.Call<bool>("IsWechatInstalled");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError("WxPayHandle Error, e:{0} ", e);
|
||
return false;
|
||
}
|
||
}
|
||
} |