|
11 | 11 | using ASFEnhance.Store; |
12 | 12 | using Microsoft.AspNetCore.Http; |
13 | 13 | using Microsoft.AspNetCore.Mvc; |
| 14 | +using SteamKit2; |
14 | 15 | using System.Globalization; |
15 | 16 | using System.Net; |
16 | 17 | using WebRequest = ASFEnhance.Cart.WebRequest; |
@@ -530,7 +531,13 @@ public async Task<ActionResult<GenericResponse>> Purchase(string botNames, [From |
530 | 531 | return result; |
531 | 532 | } |
532 | 533 |
|
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); |
534 | 541 |
|
535 | 542 | if (response2 == null) |
536 | 543 | { |
@@ -599,4 +606,134 @@ public async Task<ActionResult<GenericResponse>> Purchase(string botNames, [From |
599 | 606 |
|
600 | 607 | return Ok(new GenericResponse<IDictionary<string, OnlyPurchaseResponse>>(true, "Ok", response)); |
601 | 608 | } |
| 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 | + } |
602 | 739 | } |
0 commit comments