NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/IAP/WXPayHandle.cs

99 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using UnityEngine;
public class WXPayHandle
{
private AndroidJavaObject wxJavaObjcet;
private const string AppID = "wxa286fb2f1bcb664c"; // AppID
private const string PartnerID = "1679354050"; // 商户号
private const string PackageName = "com.phxh.dev.nld"; // 包名
private const string PayMethodName = "WeChatPayReq"; // 调起微信支付的方法名
public WXPayHandle()
{
try
{
InitWxPay();
}
catch (Exception exception)
{
DebugUtil.LogError("初始化WXPay异常:" + exception);
}
}
/// <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);
}
}
public void BuyGoods()
{
var prepayID = GetPrepayId();
var nonceStr = PhxhSDK.Utils.GenerateRandomString(32);
var timeStamp = PhxhSDK.Utils.GetTimeStamp().ToString();
var sign = GetSign();
// 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, PartnerID, prepayID, PackageName, nonceStr, timeStamp, sign, "Test",
"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;
}
}
private string GetPrepayId()
{
// TODO 向服务器请求会话ID
return "";
}
private string GetSign()
{
// TODO 获取or计算签名
return "";
}
}