// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2022-02-10 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using F; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace G { /// /// 支付 /// public class PayProxy : F.GameProxy { /// /// /// 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; } } /// /// /// public int rmbPrice { get; set; } /// /// /// public int buyAmount { get; set; } public string userId => UserProxy.UserId; public string platform => KPlatform.Instance.platform; /// /// 0 内购 或1小程序拉起 或2客服 /// public int payMethod { get; set; } /// /// 账单 /// public string trade_no { get; set; } } #region Field #endregion #region Method /// /// 支付接口 /// /// /// 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 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 } /// /// /// /// private void GetPayResult(Callback2 callback) { ServerProxy.Instance.GetOrderResult((resultError, resultMessage, resultData) => { if (resultError == 0) { Debug.Log("查询订单成功"); var result = CheckOrder(resultData as IList); 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); } }); } /// /// /// /// /// private Dictionary CheckOrder(IList orderList) { if (orderList == null || orderList.Count == 0) return null; var str = "check order ret: "; var result = new Dictionary(); 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(); #endregion } }