From ca109136a2175d4fcce87871df9d40b5047260db Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Sat, 8 Jan 2022 23:14:01 +0300 Subject: [PATCH] updated payment pages --- .../EShopOnAbpPaymentConsts.cs | 1 + .../EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs | 5 +++-- .../Pages/PaymentCompleted.cshtml.cs | 15 +++++++++++++-- .../wwwroot/components/payment/payment-widget.js | 8 ++++++-- .../Orders/PaymentType.cs | 6 +++--- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs index c9a75f54..b58e38a9 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs @@ -3,6 +3,7 @@ public static class EShopOnAbpPaymentConsts { public const string Currency = "USD"; + public const string PaymentIdCookie = "selected_payment_id"; // Setted in payment-widget.js public static class DemoAddressTypes { diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs index aac4373c..9ad48597 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs @@ -59,7 +59,7 @@ public class PaymentModel : AbpPageModel var placedOrder = await _orderAppService.CreateAsync(new OrderCreateDto() { - PaymentTypeId = model.SelectedPaymentId, + PaymentTypeId = model.SelectedPaymentId, Address = GetUserAddress(model.SelectedAddressId), Products = productItems }); @@ -75,9 +75,10 @@ public class PaymentModel : AbpPageModel var response = await _paymentRequestAppService.StartAsync(new PaymentRequestStartDto { + PaymentTypeId = model.SelectedPaymentId, PaymentRequestId = paymentRequest.Id, ReturnUrl = _publicWebPaymentOptions.PaymentSuccessfulCallbackUrl, - CancelUrl = _publicWebPaymentOptions.PaymentFailureCallbackUrl, + CancelUrl = _publicWebPaymentOptions.PaymentFailureCallbackUrl }); return Redirect(response.CheckoutLink); diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/PaymentCompleted.cshtml.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/PaymentCompleted.cshtml.cs index e313939d..ffaa090e 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/PaymentCompleted.cshtml.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/PaymentCompleted.cshtml.cs @@ -24,12 +24,23 @@ public class PaymentCompletedModel : AbpPageModel public async Task OnGetAsync() { - PaymentRequest = await _paymentRequestAppService.CompleteAsync(Token); + int selectedPaymentId = 0; + if (HttpContext.Request.Cookies.TryGetValue(EShopOnAbpPaymentConsts.PaymentIdCookie, + out var selectedPaymentIdString)) + { + selectedPaymentId = string.IsNullOrEmpty(selectedPaymentIdString) ? 0 : int.Parse(selectedPaymentIdString); + } + + PaymentRequest = await _paymentRequestAppService.CompleteAsync( + new PaymentRequestCompleteInputDto() {Token = Token, PaymentTypeId = selectedPaymentId}); IsSuccessful = PaymentRequest.State == PaymentRequestState.Completed; + if (IsSuccessful) { - return RedirectToPage("OrderReceived", new { orderNo = PaymentRequest.OrderNo }); + // Remove cookie so that can be set again when default payment type is set + HttpContext.Response.Cookies.Delete(EShopOnAbpPaymentConsts.PaymentIdCookie); + return RedirectToPage("OrderReceived", new {orderNo = PaymentRequest.OrderNo}); } return Page(); diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/payment/payment-widget.js b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/payment/payment-widget.js index 1c541575..0632b47c 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/payment/payment-widget.js +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/wwwroot/components/payment/payment-widget.js @@ -1,4 +1,8 @@ (function () { + // Write selected payment type to cookie anyways + const paymentTypeId = $(".payment-list").find(".is-selected").attr('data-payment-id'); + abp.utils.setCookieValue("selected_payment_id", paymentTypeId); + abp.widgets.PaymentWidget = function ($wrapper) { var widgetManager = $wrapper.data('abp-widget-manager'); @@ -7,7 +11,6 @@ .find('.address-list .card') .click(function () { const $this = $(this); - // const addressId = $this.attr('data-address-id'); $this.parents(".address-list").find('.card').removeClass("is-selected"); $this.addClass("is-selected"); }); @@ -16,7 +19,8 @@ .find('.payment-list .card') .click(el => { const $this = $(el.currentTarget); - // const paymentTypeId = $this.attr('data-payment-id'); + const paymentTypeId = $this.attr('data-payment-id'); + abp.utils.setCookieValue("selected_payment_id", paymentTypeId); $this.parents(".payment-list").find('.card').removeClass("is-selected"); $this.addClass("is-selected"); }); diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/PaymentType.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/PaymentType.cs index 2e556acd..ec3a0e35 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/PaymentType.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/PaymentType.cs @@ -7,16 +7,16 @@ namespace EShopOnAbp.OrderingService.Orders; public class PaymentType : Enumeration { + public static PaymentType Demo = new PaymentType(0, nameof(Demo).ToLowerInvariant()); public static PaymentType Paypal = new PaymentType(1, nameof(Paypal).ToLowerInvariant()); - public static PaymentType Demo = new PaymentType(2, nameof(Paypal).ToLowerInvariant()); public PaymentType(int id, string name) : base(id, name) { } public static IEnumerable List() => - new[] {Paypal, Demo}; - + new[] {Demo, Paypal}; + public static PaymentType FromName(string name) { var state = List()