Skip to content

Commit 9fe713b

Browse files
committed
feat 新增 IPC 接口
1 parent c523e82 commit 9fe713b

4 files changed

Lines changed: 176 additions & 2 deletions

File tree

ASFEnhance.IPC/Controllers/CartController.cs

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using ASFEnhance.Store;
1212
using Microsoft.AspNetCore.Http;
1313
using Microsoft.AspNetCore.Mvc;
14+
using SteamKit2;
1415
using System.Globalization;
1516
using System.Net;
1617
using WebRequest = ASFEnhance.Cart.WebRequest;
@@ -530,7 +531,13 @@ public async Task<ActionResult<GenericResponse>> Purchase(string botNames, [From
530531
return result;
531532
}
532533

533-
var response2 = await WebRequest.InitTransaction(bot).ConfigureAwait(false);
534+
AddressConfig? address = null;
535+
if (Config.Addresses?.Count > 0)
536+
{
537+
address = Config.Addresses[Random.Shared.Next(0, Config.Addresses.Count)];
538+
}
539+
540+
var response2 = await WebRequest.InitTransaction(bot, "steamaccount", address).ConfigureAwait(false);
534541

535542
if (response2 == null)
536543
{
@@ -599,4 +606,134 @@ public async Task<ActionResult<GenericResponse>> Purchase(string botNames, [From
599606

600607
return Ok(new GenericResponse<IDictionary<string, OnlyPurchaseResponse>>(true, "Ok", response));
601608
}
609+
610+
/// <summary>
611+
/// 使用外部支付结算购物车
612+
/// </summary>
613+
/// <param name="botName"></param>
614+
/// <param name="tranId"></param>
615+
/// <returns></returns>
616+
/// <exception cref="ArgumentNullException"></exception>
617+
[HttpPost("{botName:required}")]
618+
[EndpointDescription("使用外部支付结算购物车")]
619+
[EndpointSummary("购物车下单(外部支付方式)")]
620+
public async Task<ActionResult<GenericResponse>> CancelPayment(string botName, string tranId)
621+
{
622+
623+
if (!Config.EULA)
624+
{
625+
return BadRequest(new GenericResponse(false, Langs.EulaFeatureUnavilable));
626+
}
627+
628+
if (string.IsNullOrEmpty(tranId))
629+
{
630+
return BadRequest(new GenericResponse(false, "TranId 不能为空"));
631+
}
632+
633+
var bot = Bot.GetBot(botName);
634+
if (bot == null)
635+
{
636+
return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botName)));
637+
}
638+
639+
if (!bot.IsConnectedAndLoggedOn)
640+
{
641+
return Ok(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotConnected)));
642+
}
643+
644+
var result = await WebRequest.CancelTransaction(bot, tranId).ConfigureAwait(false);
645+
if (result?.Result == EResult.OK)
646+
{
647+
return Ok(new GenericResponse(true, "Ok"));
648+
}
649+
650+
return Ok(new GenericResponse(false, "取消订单失败"));
651+
}
652+
653+
/// <summary>
654+
/// 使用外部支付结算购物车
655+
/// </summary>
656+
/// <param name="botName"></param>
657+
/// <param name="payment"></param>
658+
/// <returns></returns>
659+
/// <exception cref="ArgumentNullException"></exception>
660+
[HttpPost("{botName:required}")]
661+
[EndpointDescription("使用外部支付结算购物车")]
662+
[EndpointSummary("购物车下单(外部支付方式)")]
663+
public async Task<ActionResult<GenericResponse>> PurchaseExternal(string botName, string payment = "alipay")
664+
{
665+
if (string.IsNullOrEmpty(botName))
666+
{
667+
throw new ArgumentNullException(nameof(botName));
668+
}
669+
670+
if (!Config.EULA)
671+
{
672+
return BadRequest(new GenericResponse(false, Langs.EulaFeatureUnavilable));
673+
}
674+
675+
if (string.IsNullOrEmpty(payment))
676+
{
677+
return BadRequest(new GenericResponse(false, "Payment 不能为空"));
678+
}
679+
680+
var bot = Bot.GetBot(botName);
681+
if (bot == null)
682+
{
683+
return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botName)));
684+
}
685+
686+
//下单
687+
if (!bot.IsConnectedAndLoggedOn)
688+
{
689+
return Ok(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotConnected)));
690+
}
691+
692+
var response1 = await WebRequest.CheckOut(bot).ConfigureAwait(false);
693+
694+
if (response1 == null)
695+
{
696+
return Ok(new GenericResponse(false, "CheckOut 失败"));
697+
}
698+
699+
AddressConfig? address = null;
700+
if (Config.Addresses?.Count > 0)
701+
{
702+
address = Config.Addresses[Random.Shared.Next(0, Config.Addresses.Count)];
703+
}
704+
705+
var response2 = await WebRequest.InitTransaction(bot, payment, address).ConfigureAwait(false);
706+
if (string.IsNullOrEmpty(response2?.TransId))
707+
{
708+
return Ok(new GenericResponse(false, "InitTransaction 失败"));
709+
}
710+
711+
var transId = response2.TransId;
712+
var response3 = await WebRequest.GetFinalPrice(bot, transId).ConfigureAwait(false);
713+
714+
if (string.IsNullOrEmpty(response3?.ExternalUrl) || string.IsNullOrEmpty(response2.TransId))
715+
{
716+
return Ok(new GenericResponse(false, "GetFinalPrice 失败"));
717+
}
718+
719+
var response4 = await WebRequest.TransactionStatusAddFunds(bot, null, transId).ConfigureAwait(false);
720+
if (response4?.Result != EResult.Pending)
721+
{
722+
return Ok(new GenericResponse(false, "TransactionStatus 失败"));
723+
}
724+
725+
var response5 = await WebRequest.CheckoutExternalLinkAddFunds(bot, null, transId).ConfigureAwait(false);
726+
if (response5 == null)
727+
{
728+
return Ok(new GenericResponse(false, "Checkout 失败"));
729+
}
730+
731+
var finalUrl = await WebRequest.GetRealExternalPaymentLink(bot, response5).ConfigureAwait(false);
732+
if (finalUrl == null)
733+
{
734+
return Ok(new GenericResponse(false, "获取支付链接失败"));
735+
}
736+
737+
return Ok(new GenericResponse<ExternalPurchaseResponse>(true, "Ok", new ExternalPurchaseResponse(transId, response3.FormattedTotal, finalUrl.ToString())));
738+
}
602739
}

ASFEnhance.IPC/Controllers/PurchaseController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,13 @@ public async Task<ActionResult<GenericResponse>> Purchase(string botNames, [From
534534
return result;
535535
}
536536

537-
var response2 = await WebRequest.InitTransaction(bot).ConfigureAwait(false);
537+
AddressConfig? address = null;
538+
if (Config.Addresses?.Count > 0)
539+
{
540+
address = Config.Addresses[Random.Shared.Next(0, Config.Addresses.Count)];
541+
}
542+
543+
var response2 = await WebRequest.InitTransaction(bot, "steamaccount", address).ConfigureAwait(false);
538544

539545
if (response2 == null)
540546
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace ASFEnhance.IPC.Data.Requests;
2+
3+
/// <summary>
4+
/// 下单请求
5+
/// </summary>
6+
public sealed record PurchaseRegionRequest
7+
{
8+
/// <summary>
9+
/// SubID列表
10+
/// </summary>
11+
public HashSet<uint>? SubIds { get; init; }
12+
13+
/// <summary>
14+
/// BundleID列表
15+
/// </summary>
16+
public HashSet<uint>? BundleIds { get; init; }
17+
18+
/// <summary>
19+
/// 跳过已拥有
20+
/// </summary>
21+
public bool SkipOwned { get; init; } = true;
22+
23+
public string? CountryCode { get; init; }
24+
public string? Payment { get; init; }
25+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ASFEnhance.IPC.Data.Responses;
2+
3+
/// <summary>
4+
/// 购物结果响应
5+
/// </summary>
6+
public sealed record ExternalPurchaseResponse(string? TransId, string? TotalCoast, string PaymentUrl);

0 commit comments

Comments
 (0)