250 lines
7.4 KiB
C#
Raw Permalink Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2022-02-10
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "PayProxy" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using F;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace G
{
/// <summary>
/// 支付
/// </summary>
public class PayProxy : F.GameProxy
{
/// <summary>
///
/// </summary>
public class PayInfo
{
public int payId
{
get;
set;
}
public string payName
{
get;
set;
}
public string payType
{
get;
set;
}
public string payKey
{
get { return payType + payId; }
}
/// <summary>
///
/// </summary>
public int rmbPrice
{
get;
set;
}
/// <summary>
///
/// </summary>
public int buyAmount
{
get;
set;
}
public string userId => UserProxy.UserId;
public string platform => KPlatform.Instance.platform;
/// <summary>
/// 0 内购 或1小程序拉起 或2客服
/// </summary>
public int payMethod
{
get;
set;
}
/// <summary>
/// 账单
/// </summary>
public string trade_no
{
get;
set;
}
}
#region Field
#endregion
#region Method
/// <summary>
/// 支付接口
/// </summary>
/// <param name="payInfo"></param>
/// <param name="callback"></param>
public void Pay(PayInfo payInfo, Callback2 callback)
{
#if UNITY_EDITOR
KPlatform.Instance.Pay(payInfo, (payError, payMessage) =>
{
if (payError == 0)
{
if (payInfo.payMethod != 0)
{
GetPayResult(callback);
}
else
{
string key = payInfo.payKey;
var lastCount = ArchiveProxy.Instance.GetInt(key, 0);
ArchiveProxy.Instance.SetInt(key, lastCount + 1);
callback?.Invoke(0, "");
ArchiveProxy.Instance.UploadImmediate();
}
}
else
{
callback?.Invoke(payError, payMessage);
}
});
#else
ServerProxy.Instance.CreateOrder(payInfo.payKey, 1, payInfo.payName, (error, message, data) =>
{
if (error == 0)
{
if (data is IDictionary<string, object> dataT)
{
var order_id = dataT.GetString("order_id");
payInfo.trade_no = order_id;
Debug.Log("创建订单 " + order_id);
}
KPlatform.Instance.Pay(payInfo, (payError, payMessage) =>
{
if (payError == 0)
{
if (payInfo.payMethod != 0)
{
GetPayResult(callback);
}
else
{
string key = payInfo.payKey;
var lastCount = ArchiveProxy.Instance.GetInt(key, 0);
ArchiveProxy.Instance.SetInt(key, lastCount + 1);
GetPayResult(callback);
//callback?.Invoke(0, "");
//ArchiveProxy.Instance.UploadImmediate();
//KStatistics.Instance.ReportEvent_Pay(payInfo.payKey, KPlatform.Instance.platform, "success");
}
}
else
{
callback?.Invoke(payError, payMessage);
}
});
}
else
{
callback?.Invoke(error, message);
}
});
#endif
}
/// <summary>
///
/// </summary>
/// <param name="callback"></param>
private void GetPayResult(Callback2 callback)
{
ServerProxy.Instance.GetOrderResult((resultError, resultMessage, resultData) =>
{
if (resultError == 0)
{
Debug.Log("查询订单成功");
var result = CheckOrder(resultData as IList<object>);
if (result != null && result.Count > 0)
{
RmbShopProxy.Instance.IndirectBuy(result, callback);
ArchiveProxy.Instance.UploadImmediate();
PostNotification(GlobalDefine.EVENT_PAY_SUCCESS);
//KStatistics.Instance.ReportEvent_Pay(payInfo.payKey, KPlatform.Instance.platform, "success");
}
}
else
{
Debug.Log("查询订单失败" + resultError + resultMessage);
}
});
}
/// <summary>
///
/// </summary>
/// <param name="orderList"></param>
/// <returns></returns>
private Dictionary<string, int> CheckOrder(IList<object> orderList)
{
if (orderList == null || orderList.Count == 0)
return null;
var str = "check order ret: ";
var result = new Dictionary<string, int>();
for (int i = 0; i < orderList.Count; i++)
{
var order = orderList.GetDictionary(i);
var key = order.GetString("id");
var count = order.GetInt("count");
str += key + " " + count + " ";
if (count > 0)
{
var lastCount = ArchiveProxy.Instance.GetInt(key, 0);
str += lastCount + ",";
if (count > lastCount)
{
result[key] = count - lastCount;
ArchiveProxy.Instance.SetInt(key, count);
KStatistics.Instance.ReportEvent_Pay(key, KPlatform.Instance.platform, "success");
}
}
}
Debug.Log(str);
return result;
}
#endregion
#region Unity
public static PayProxy Instance => GetInstance<PayProxy>();
#endregion
}
}