bili支付测试
parent
020cc80bed
commit
d36f663cbd
|
|
@ -0,0 +1,222 @@
|
|||
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> 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 int count;
|
||||
|
||||
public PayRequestData(string items, int actorID, int count)
|
||||
{
|
||||
this.items = items;
|
||||
actor_id = actorID;
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
|
||||
[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>
|
||||
private bool isCallBack;
|
||||
|
||||
/// <summary>
|
||||
/// 开始支付
|
||||
/// </summary>
|
||||
public void BuyGoods(string currencyID, int count, int diamonCount, Action<string, string> 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, count);
|
||||
Dictionary<string, string> headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
|
||||
{
|
||||
{ "items", sendData.items },
|
||||
{ "actor_id", sendData.actor_id.ToString() },
|
||||
{ "game_money", diamonCount.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);
|
||||
}
|
||||
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("请求失败!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
|
||||
private void BuyGoods(BiliPayData biliPayData)
|
||||
{
|
||||
if (null == biliPayData)
|
||||
{
|
||||
DebugUtil.LogError("请求支付数据为空");
|
||||
return;
|
||||
}
|
||||
|
||||
isCallBack = false;
|
||||
|
||||
DebugUtil.Log("开始调起bili支付");
|
||||
payAction?.Invoke(biliPayData.order_sn, biliPayData.order_sign);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f364d81f5cb205d4288567d066646445
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -6,6 +6,8 @@ using UnityEngine;
|
|||
using Gameplay.Net;
|
||||
using cfg.ChargeCfg;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
#if SDK_BILIBILI
|
||||
using PhxhSDK.AOT.Bilibili;
|
||||
#endif
|
||||
|
|
@ -20,6 +22,7 @@ public class IAPManager : Singlenton<IAPManager>
|
|||
WeChat = 2,
|
||||
Huabei = 3,
|
||||
Apple = 4,
|
||||
Bilibili = 5,
|
||||
}
|
||||
|
||||
public Dictionary<string, int> ProductDic;
|
||||
|
|
@ -189,19 +192,6 @@ public class IAPManager : Singlenton<IAPManager>
|
|||
}
|
||||
else
|
||||
BuyGoods(id, good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail);
|
||||
#elif SDK_BILIBILI
|
||||
SdkHelper sdkHelper = SDKManager.Instance.GetSdkHelper(SDKManager.SdkName.Bilibili);
|
||||
if (sdkHelper is BilibiliSdkHelper biliSdk)
|
||||
{
|
||||
if (!IapDataDic.TryGetValue(id, out DataCurrencyShopCfg cfg))
|
||||
return;
|
||||
|
||||
if (TableManager.Instance.Tables.GlobalConfig.DiamondItemId == cfg.ItemID)
|
||||
biliSdk.Pay(Actor.Instance.Base.Nickname, cfg.NowPriceRMB * 100, cfg.ItemCount,
|
||||
CommonUtils.GetLocalizeText(cfg.NameLocal), "111", string.Empty);
|
||||
}
|
||||
else
|
||||
BuyGoods(id, good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail);
|
||||
#else
|
||||
BuyGoods(id, good, Actor.Instance.Base.AccountID, PurchaseSuccess, PurchaseFail);
|
||||
#endif
|
||||
|
|
@ -231,6 +221,24 @@ public class IAPManager : Singlenton<IAPManager>
|
|||
case PayChannel.Apple:
|
||||
_helper.BuyGoods(goodsName, actorId, onPurchaseSuccess, onPurchaseFail);
|
||||
break;
|
||||
case PayChannel.Bilibili:
|
||||
{
|
||||
SdkHelper sdkHelper = SDKManager.Instance.GetSdkHelper(SDKManager.SdkName.Bilibili);
|
||||
if (sdkHelper is BilibiliSdkHelper biliSdk)
|
||||
{
|
||||
int diamondCount = 0;
|
||||
if (TableManager.Instance.Tables.GlobalConfig.DiamondItemId == goodData.ItemID)
|
||||
diamondCount = goodData.ItemCount;
|
||||
|
||||
BilibiliPayHandle.Instance.BuyGoods(goodData.PhxhProductID, 1, diamondCount,
|
||||
(string order, string sign) =>
|
||||
{
|
||||
biliSdk.Pay(Actor.Instance.Base.Nickname, goodData.NowPriceRMB * 100, diamondCount,
|
||||
CommonUtils.GetLocalizeText(goodData.NameLocal), order, string.Empty, sign);
|
||||
});
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
UITipsManager.ShowTips($"没有{CurrentPayChannel}支付方式");
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -56,6 +56,9 @@ public class UI_BuyDiamondController : UIWindow
|
|||
IAPManager.Instance.SetCurrentPayChannel(IAPManager.PayChannel.Apple);
|
||||
IAPManager.Instance.IAPBuyGoods(cfg.Id);
|
||||
return;
|
||||
#elif SDK_BILIBILI
|
||||
IAPManager.Instance.SetCurrentPayChannel(IAPManager.PayChannel.Bilibili);
|
||||
IAPManager.Instance.IAPBuyGoods(cfg.Id);
|
||||
#endif
|
||||
await UI_PayWindowController.Open(cfg);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,12 +25,13 @@ namespace PhxhSDK
|
|||
/// <param name="goodName">商品名</param>
|
||||
/// <param name="orderNum">订单号</param>
|
||||
/// <param name="goodDescription">商品描述</param>
|
||||
public void Pay(string role, int totalFee, int gameMoney, string goodName,string orderNum, string goodDescription)
|
||||
public void Pay(string role, int totalFee, int gameMoney, string goodName,string orderNum, string goodDescription, string sign)
|
||||
{
|
||||
if (long.TryParse(UID, out long uid))
|
||||
{
|
||||
DebugUtil.Log("bilibili支付");
|
||||
GSCSdkInterface.androidPay(uid, UserName, role, totalFee, gameMoney, goodName, orderNum, goodDescription,
|
||||
"Test", "https://dev_openapi.kedrgame.com/api/v1/biliGame/notify", "签名");
|
||||
"Test", "https://dev_openapi.kedrgame.com/api/v1/biliGame/notify", sign);
|
||||
}
|
||||
else
|
||||
Debug.LogError($"UID错误:{UID}");
|
||||
|
|
|
|||
Loading…
Reference in New Issue