287 lines
11 KiB
C#
287 lines
11 KiB
C#
/*
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using asap.core;
|
|
using Newtonsoft.Json.Linq;
|
|
using tysdk;
|
|
using UnityEngine;
|
|
using UniRx;
|
|
using System.Threading;
|
|
|
|
namespace game
|
|
{
|
|
public class AggIAPRTService : IIAPService
|
|
{
|
|
[Inject] public IUserService userService { get; set; }
|
|
[Inject] public IConfig config { get; set; }
|
|
[Inject] public IRTService rts { get; set; }
|
|
[Inject] public ICustomServerMgr svr { get; set; }
|
|
|
|
private TaskCompletionSource<SC_OrderConfirmEvt> orderConfirmTask;
|
|
private IDisposable orderCompletedDisposable;
|
|
|
|
public void Init()
|
|
{
|
|
Release();
|
|
orderCompletedDisposable = rts.OnEvent<SC_OrderConfirmEvt>(OnOrderCompleted);
|
|
Debug.Log("[AggIAPService] inited SC_OrderConfirmEvt subscription");
|
|
}
|
|
|
|
public void Release()
|
|
{
|
|
orderCompletedDisposable?.Dispose();
|
|
orderCompletedDisposable = null;
|
|
orderConfirmTask = null;
|
|
}
|
|
|
|
private void OnOrderCompleted(SC_OrderConfirmEvt evt)
|
|
{
|
|
Debug.Log($"[AggIAPService] OnOrderCompleted \r\n {evt}");
|
|
if (orderConfirmTask != null)
|
|
{
|
|
orderConfirmTask.SetResult(evt);
|
|
orderConfirmTask = null;
|
|
}
|
|
}
|
|
|
|
|
|
#if UNITY_ANDROID
|
|
private const string payment = "google";
|
|
#else
|
|
private const string payment = "apple";
|
|
#endif
|
|
|
|
public async Task<IList<SKUDetail>> GetSKUs()
|
|
{
|
|
Debug.Log("[AggIAPService] GetSKUList fetch");
|
|
var prodList = await TYSdkFacade.Instance.GetSKUList();
|
|
if (prodList.code != 0)
|
|
{
|
|
Debug.LogWarning("[AggIAPService] GetSKUList Failed");
|
|
return null;
|
|
}
|
|
Debug.Log($"[AggIAPService] GetSKUList Success prod Num {prodList.products.Count}");
|
|
return prodList.products;
|
|
}
|
|
|
|
public async Task<PaymentInfo> Pay(Action<string> OnSuccessFromSdk,string replenishmentDataStr, SKUDetail sku, int count, float usdPrice, JObject extraPurcheInfo)
|
|
{
|
|
if (!rts.IsConnected)
|
|
{
|
|
GameCore.UIManager.ShowWaitingBlock("AggIAPService::Pay");
|
|
try
|
|
{
|
|
await rts.Connect();
|
|
}
|
|
catch {
|
|
|
|
GameCore.UIManager.HideWaitingBlock("AggIAPService::Pay");
|
|
}
|
|
|
|
using (var e = GEvent.TackEvent("recharge_state"))
|
|
{
|
|
e.AddContent(AFInAppEvents.CONTENT_ID, sku.ProdID)
|
|
.AddContent(AFInAppEvents.PRICE, usdPrice)
|
|
.AddContent(AFInAppEvents.CURRENCY, sku.price_currency_code)
|
|
.AddContent("payment", payment)
|
|
.AddContent("payment_state", "disconnected")
|
|
.AddContent(AFInAppEvents.QUANTITY, count);
|
|
if (null != extraPurcheInfo)
|
|
{
|
|
foreach (var item in extraPurcheInfo)
|
|
{
|
|
e.AddContent(item.Key, item.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
await Awaiters.Seconds(3);
|
|
GameCore.UIManager.HideWaitingBlock("AggIAPService::Pay");
|
|
return new PaymentInfo() { code = "-1", msg = "连接失败" };
|
|
}
|
|
|
|
using (var e = GEvent.TackEvent("recharge_state"))
|
|
{
|
|
e.AddContent(AFInAppEvents.CONTENT_ID, sku.ProdID)
|
|
.AddContent(AFInAppEvents.PRICE, usdPrice)
|
|
.AddContent(AFInAppEvents.CURRENCY, sku.price_currency_code)
|
|
.AddContent("payment", payment)
|
|
.AddContent("payment_state", "start")
|
|
.AddContent(AFInAppEvents.QUANTITY, count);
|
|
if (null != extraPurcheInfo)
|
|
{
|
|
foreach (var item in extraPurcheInfo)
|
|
{
|
|
e.AddContent(item.Key, item.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
var purchaseInfo = new JObject();
|
|
purchaseInfo["ver"] = 1;
|
|
purchaseInfo["pfid"] = userService.UserId;
|
|
purchaseInfo["sum"] = usdPrice * count;
|
|
purchaseInfo["customData"] = replenishmentDataStr;
|
|
|
|
orderConfirmTask = new TaskCompletionSource<SC_OrderConfirmEvt>();
|
|
|
|
PaymentInfo paymentInfo = await TYSdkFacade.Instance.Pay(sku, count, usdPrice, purchaseInfo);
|
|
|
|
if (!paymentInfo.isSuccessFromSdk)
|
|
{
|
|
orderConfirmTask = null;
|
|
using (var e = GEvent.TackEvent("recharge_state"))
|
|
{
|
|
e.AddContent(AFInAppEvents.CONTENT_ID, sku.ProdID)
|
|
.AddContent(AFInAppEvents.PRICE, usdPrice)
|
|
.AddContent(AFInAppEvents.CURRENCY, sku.price_currency_code)
|
|
.AddContent("payment", payment)
|
|
.AddContent("payment_state", "fail")
|
|
.AddContent(AFInAppEvents.QUANTITY, count);
|
|
if (null != extraPurcheInfo)
|
|
{
|
|
foreach (var item in extraPurcheInfo)
|
|
{
|
|
e.AddContent(item.Key, item.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
GameCore.UIManager.ShowWaitingBlock("AggIAPService::Pay");
|
|
Debug.Log("[AggIAPService] Pay sdk Success ");
|
|
using (var cts = new CancellationTokenSource())
|
|
{
|
|
cts.CancelAfter(TimeSpan.FromSeconds(30));
|
|
cts.Token.Register(() => orderConfirmTask.SetCanceled());
|
|
try
|
|
{
|
|
await Awaiters.NextFrame;
|
|
|
|
UnityEngine.Assertions.Assert.IsTrue(rts.IsConnected, "[AggIAPService] RTService is not connected");
|
|
var evt = await orderConfirmTask.Task;
|
|
|
|
if (evt.isSuccess && paymentInfo.Check(evt.orderId, evt.productId))
|
|
{
|
|
try
|
|
{
|
|
|
|
string confirmResult = await rts.Publish("CS_OrderConfirmMessage", userService.CustomId, evt.orderId);
|
|
if (confirmResult == null || !bool.Parse(confirmResult))
|
|
{
|
|
Debug.LogWarning("[AggIAPService] CS_OrderConfirmMessage Failed");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"[AggIAPService] Publish CS_OrderConfirmMessage Error {e}");
|
|
}
|
|
|
|
using (var e = GEvent.RevenueEvent())
|
|
{
|
|
e.AddContent(AFInAppEvents.CONTENT_ID, sku.ProdID)
|
|
.AddContent(AFInAppEvents.PRICE, usdPrice)
|
|
.AddContent(AFInAppEvents.CURRENCY, sku.price_currency_code)
|
|
.AddContent("payment", payment)
|
|
.AddContent("restore", false)
|
|
.AddContent(AFInAppEvents.QUANTITY, 1)
|
|
.AddContent("order_id", paymentInfo.orderId)
|
|
.AddContent("payment_state", "finish")
|
|
.AddContent("af_revenue", sku.ProdPrice * count);
|
|
|
|
if (null != extraPurcheInfo)
|
|
{
|
|
foreach (var item in extraPurcheInfo)
|
|
{
|
|
e.AddContent(item.Key, item.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
Debug.LogWarning("[AggIAPService] Pay timeout");
|
|
if (PlayerPrefs.HasKey(IIAPService.UnConfirmedOrder_key))
|
|
{
|
|
var unConfirmedOrders = JArray.Parse(PlayerPrefs.GetString(IIAPService.UnConfirmedOrder_key));
|
|
unConfirmedOrders.Add(paymentInfo.orderId);
|
|
PlayerPrefs.SetString(IIAPService.UnConfirmedOrder_key, unConfirmedOrders.ToString());
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
using (var e = GEvent.TackEvent("recharge_state"))
|
|
{
|
|
e.AddContent(AFInAppEvents.CONTENT_ID, sku.ProdID)
|
|
.AddContent(AFInAppEvents.PRICE, usdPrice)
|
|
.AddContent(AFInAppEvents.CURRENCY, sku.price_currency_code)
|
|
.AddContent("payment", payment)
|
|
.AddContent("payment_state", "timeout")
|
|
.AddContent("orderId", paymentInfo.orderId)
|
|
.AddContent(AFInAppEvents.QUANTITY, count);
|
|
if (null != extraPurcheInfo)
|
|
{
|
|
foreach (var item in extraPurcheInfo)
|
|
{
|
|
e.AddContent(item.Key, item.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
GameCore.UIManager.HideWaitingBlock("AggIAPService::Pay");
|
|
return paymentInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取未领奖的订单号列表 时间戳
|
|
/// </summary>
|
|
/// <param name="day">时间戳 最早订单时间 -1m</param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, string>> GetOrderUnreceived()
|
|
{
|
|
var orderUnreceived = new CS_OrderUnreceived()
|
|
{
|
|
sdkUserId = userService.CustomId,
|
|
customData = true
|
|
}.ToJson();
|
|
|
|
string result = await svr.CustomServerPost("order/unreceive", orderUnreceived);
|
|
|
|
if (string.IsNullOrEmpty(result) || result == "[]")
|
|
{
|
|
return new Dictionary<string, string>();
|
|
}
|
|
else
|
|
{
|
|
SC_OrderUnreceived response = Newtonsoft.Json.JsonConvert.DeserializeObject<SC_OrderUnreceived>(result);
|
|
return response?.orders;
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<bool> ConfirmOrders(List<string> orderIDs)
|
|
{
|
|
if (orderIDs == null || orderIDs.Count == 0) return false;
|
|
|
|
var confirmEvt = new CS_OrdersConfirmEvt()
|
|
{
|
|
orderIds = orderIDs,
|
|
sdkUserId = userService.CustomId
|
|
}.ToJson();
|
|
|
|
string result = await svr.CustomServerPost("order/receiveorders", confirmEvt);
|
|
if (string.IsNullOrEmpty(result) || result == "[]" || !bool.TryParse(result, out bool re))
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
*/
|