279 lines
8.8 KiB
C#
279 lines
8.8 KiB
C#
using Cysharp.Threading.Tasks;
|
||
using Gameplay;
|
||
using Gameplay.Net;
|
||
using Newtonsoft.Json;
|
||
using NLDPB;
|
||
using PhxhSDK;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEngine;
|
||
|
||
public class WXPayHandle : SingletonMono<WXPayHandle>
|
||
{
|
||
private static WXPayHandle _instance;
|
||
|
||
private void Awake()
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = this;
|
||
InitWxPay();
|
||
DontDestroyOnLoad(gameObject);
|
||
}
|
||
else
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
void OnApplicationPause(bool pauseStatus)
|
||
{
|
||
if (starPay && !isCallBack && !pauseStatus)
|
||
{
|
||
DebugUtil.Log("用户直接回到应用");
|
||
starPay = false;
|
||
WaitingForServer.Instance.EventDone(WaitingForServer.EventName.Pay);
|
||
}
|
||
}
|
||
|
||
private const string PayPost = "orders/wechat";
|
||
|
||
[Serializable]
|
||
private class PayRequestData
|
||
{
|
||
public string items;
|
||
public int actor_id;
|
||
|
||
public PayRequestData(string items, int actorID)
|
||
{
|
||
this.items = items;
|
||
actor_id = actorID;
|
||
}
|
||
}
|
||
|
||
[Serializable]
|
||
private class Items
|
||
{
|
||
public string product_id;
|
||
public int count;
|
||
public int param;
|
||
}
|
||
|
||
// 请求结果
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否有回调结果
|
||
/// </summary>
|
||
private bool isCallBack;
|
||
|
||
/// <summary>
|
||
/// 开始支付
|
||
/// </summary>
|
||
public bool starPay;
|
||
|
||
/// <summary>
|
||
/// 开始支付
|
||
/// </summary>
|
||
public void BuyGoods(string currencyID, int count, int param = 0)
|
||
{
|
||
WaitingForServer.Instance.EventWaiting(WaitingForServer.EventName.Pay, false);
|
||
starPay = true;
|
||
WxPayRequest(currencyID, count, param);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 请求服务器
|
||
/// </summary>
|
||
private async void WxPayRequest(string currencyID, int count, int param = 0)
|
||
{
|
||
var actorID = (int)Actor.Instance.Base.GetProp(ActorProp.ActorId);
|
||
var payUrl = PostHeader.PostUrl + PayPost;
|
||
DebugUtil.Log("发送拉起微信支付请求地址: " + payUrl);
|
||
|
||
var itemsList = new List<Items> { new() { product_id = currencyID, count = count, param = param } };
|
||
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.ToString() },
|
||
{ "timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }
|
||
});
|
||
|
||
try
|
||
{
|
||
var res = await HttpHelper.PostAsync(payUrl, sendData, headData);
|
||
if (res.code != 0)
|
||
{
|
||
CommonUtils.ShowMessageTips("支付失败,请重试");
|
||
DebugUtil.LogError(res.message);
|
||
starPay = false;
|
||
WaitingForServer.Instance.EventDone(WaitingForServer.EventName.Pay);
|
||
return;
|
||
}
|
||
|
||
DebugUtil.Log("http消息请求成功!\n返回内容:" + res.message);
|
||
WebRequestResult result = JsonConvert.DeserializeObject<WebRequestResult>(res.message);
|
||
if (result.code == 0)
|
||
{
|
||
BuyGoods(result.data);
|
||
DebugUtil.Log("拉起微信支付SDK成功");
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("拉起微信支付SDK失败");
|
||
UITipsManager.ShowTips("支付失败,请重试");
|
||
starPay = false;
|
||
WaitingForServer.Instance.EventDone(WaitingForServer.EventName.Pay);
|
||
}
|
||
}
|
||
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("请求失败!");
|
||
}
|
||
|
||
starPay = false;
|
||
WaitingForServer.Instance.EventDone(WaitingForServer.EventName.Pay);
|
||
}
|
||
}
|
||
|
||
#region 与微信通信
|
||
|
||
private const string AppID = "wxa286fb2f1bcb664c"; // AppID
|
||
private const string PayMethodName = "WxPayByApp"; // 调起微信支付的方法
|
||
private const string InitMethodName = "WechatInit"; // 调起微信初始化方法
|
||
private const string WxAndroidClassName = "com.phxh.official.nld.wx.MainActivity";
|
||
private AndroidJavaObject utils;
|
||
|
||
/// <summary>
|
||
/// 初始化WXPay
|
||
/// </summary>
|
||
private void InitWxPay()
|
||
{
|
||
try
|
||
{
|
||
DebugUtil.Log("开始初始化微信支付");
|
||
using AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
||
var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
||
utils = new AndroidJavaObject(WxAndroidClassName);
|
||
utils.Call(InitMethodName, currentActivity);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.Log("初始化微信支付异常:{0}", e);
|
||
}
|
||
}
|
||
|
||
public bool CheckWxInit()
|
||
{
|
||
try
|
||
{
|
||
if (utils == null)
|
||
return false;
|
||
var result = utils.Call<bool>("IsWechatInstalled");
|
||
return result;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError("WxPayHandle.CheckWxInit Error, e:{0} ", e);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 支付回调
|
||
/// </summary>
|
||
public async void WxPayCallback(string str)
|
||
{
|
||
// 返回结果 0 成功
|
||
// -1 错误 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常原因等
|
||
// -2 用户取消
|
||
starPay = false;
|
||
isCallBack = true;
|
||
WaitingForServer.Instance.EventDone(WaitingForServer.EventName.Pay);
|
||
try
|
||
{
|
||
DebugUtil.Log("微信支付回调, 结果码:{0}", str);
|
||
|
||
if (int.TryParse(str, out var code))
|
||
{
|
||
switch (code)
|
||
{
|
||
case -2:
|
||
// UITipsManager.ShowTips("取消支付");
|
||
// break;
|
||
case -1:
|
||
// UITipsManager.ShowTips("支付失败,请重试");
|
||
await UI_ChargeShopPurchaseController.Open();
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError("WXPayHandle.WxPayCallback error:{0}", e);
|
||
}
|
||
}
|
||
|
||
private void BuyGoods(WxData wxData)
|
||
{
|
||
if (wxData == null)
|
||
{
|
||
DebugUtil.LogError("请求支付数据为空");
|
||
return;
|
||
}
|
||
|
||
isCallBack = false;
|
||
|
||
// 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; // 支付回调方法
|
||
DebugUtil.Log("开始调起微信支付");
|
||
UIManager.Instance.CloseWindow(UINameConst.UI_Pay);
|
||
|
||
utils.Call(PayMethodName, AppID, wxData.partnerid, wxData.prepayid, wxData.package, wxData.noncestr,
|
||
wxData.timestamp, wxData.sign, "WXPayHandle",
|
||
"WxPayCallback");
|
||
}
|
||
|
||
#endregion
|
||
} |