From cfe009974608b81782e33f30c7f573831148314c Mon Sep 17 00:00:00 2001 From: enisn Date: Mon, 17 Jan 2022 15:49:52 +0300 Subject: [PATCH] Make public website use payment-type instead of id --- .../Components/Payment/Default.cshtml | 2 +- .../EShopOnAbpPaymentConsts.cs | 2 +- .../Pages/Payment.cshtml.cs | 20 +++++++++---------- .../Pages/PaymentCompleted.cshtml.cs | 18 +++++++++-------- .../src/EShopOnAbp.PublicWeb/Pages/payment.js | 4 ++-- .../ServiceProviders/PaymentTypeProvider.cs | 6 +++--- .../components/payment/payment-widget.js | 8 ++++---- 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Payment/Default.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Payment/Default.cshtml index 7813eb18..34740bc5 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Payment/Default.cshtml +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/Payment/Default.cshtml @@ -35,7 +35,7 @@ { string isSelectedClass = paymentType.IsDefault ? "is-selected" : ""; - +

@paymentType.Name diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs index b58e38a9..ac1619f4 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs @@ -3,7 +3,7 @@ public static class EShopOnAbpPaymentConsts { public const string Currency = "USD"; - public const string PaymentIdCookie = "selected_payment_id"; // Setted in payment-widget.js + public const string PaymentTypeCookie = "selected_payment_type"; // 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 9ad48597..39f08826 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs @@ -46,7 +46,7 @@ public class PaymentModel : AbpPageModel { Logger.LogInformation("Payment Proceeded..."); Logger.LogInformation($"AddressId: {model.SelectedAddressId}"); - Logger.LogInformation($"PaymentId: {model.SelectedPaymentId}"); + Logger.LogInformation($"PaymentType: {model.SelectedPaymentType}"); Logger.LogInformation($"Total Discount: {model.TotalDiscountPercentage}"); var basket = await _userBasketProvider.GetBasketAsync(); @@ -59,7 +59,6 @@ public class PaymentModel : AbpPageModel var placedOrder = await _orderAppService.CreateAsync(new OrderCreateDto() { - PaymentTypeId = model.SelectedPaymentId, Address = GetUserAddress(model.SelectedAddressId), Products = productItems }); @@ -73,13 +72,14 @@ public class PaymentModel : AbpPageModel Products = ObjectMapper.Map, List>(basket.Items) }); - var response = await _paymentRequestAppService.StartAsync(new PaymentRequestStartDto - { - PaymentTypeId = model.SelectedPaymentId, - PaymentRequestId = paymentRequest.Id, - ReturnUrl = _publicWebPaymentOptions.PaymentSuccessfulCallbackUrl, - CancelUrl = _publicWebPaymentOptions.PaymentFailureCallbackUrl - }); + var response = await _paymentRequestAppService.StartAsync( + model.SelectedPaymentType, + new PaymentRequestStartDto + { + PaymentRequestId = paymentRequest.Id, + ReturnUrl = _publicWebPaymentOptions.PaymentSuccessfulCallbackUrl, + CancelUrl = _publicWebPaymentOptions.PaymentFailureCallbackUrl + }); return Redirect(response.CheckoutLink); } @@ -87,7 +87,7 @@ public class PaymentModel : AbpPageModel public class PaymentPageViewModel { public int SelectedAddressId { get; set; } - public int SelectedPaymentId { get; set; } + public string SelectedPaymentType { get; set; } public decimal TotalDiscountPercentage { get; set; } } 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 ffaa090e..84c37136 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/PaymentCompleted.cshtml.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/PaymentCompleted.cshtml.cs @@ -1,6 +1,7 @@ using EShopOnAbp.PaymentService.PaymentRequests; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System; using System.Threading.Tasks; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; @@ -24,23 +25,24 @@ public class PaymentCompletedModel : AbpPageModel public async Task OnGetAsync() { - int selectedPaymentId = 0; - if (HttpContext.Request.Cookies.TryGetValue(EShopOnAbpPaymentConsts.PaymentIdCookie, - out var selectedPaymentIdString)) + if (!HttpContext.Request.Cookies.TryGetValue(EShopOnAbpPaymentConsts.PaymentTypeCookie, + out var selectedPaymentType)) { - selectedPaymentId = string.IsNullOrEmpty(selectedPaymentIdString) ? 0 : int.Parse(selectedPaymentIdString); + throw new InvalidOperationException("A payment type must be selected!"); } PaymentRequest = await _paymentRequestAppService.CompleteAsync( - new PaymentRequestCompleteInputDto() {Token = Token, PaymentTypeId = selectedPaymentId}); + // TODO: Use string name + selectedPaymentType, + new PaymentRequestCompleteInputDto() { Token = Token }); IsSuccessful = PaymentRequest.State == PaymentRequestState.Completed; - + if (IsSuccessful) { // 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}); + HttpContext.Response.Cookies.Delete(EShopOnAbpPaymentConsts.PaymentTypeCookie); + return RedirectToPage("OrderReceived", new { orderNo = PaymentRequest.OrderNo }); } return Page(); diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/payment.js b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/payment.js index fd41fe8f..e6ea6a3e 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/payment.js +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/payment.js @@ -9,8 +9,8 @@ form.appendChild(addressInput); let paymentInput = document.createElement('input'); - paymentInput.setAttribute('name', "model.SelectedPaymentId"); - paymentInput.setAttribute('value', document.querySelector(".payment-list .card.is-selected").getAttribute("data-payment-id")); + paymentInput.setAttribute('name', "model.SelectedPaymentType"); + paymentInput.setAttribute('value', document.querySelector(".payment-list .card.is-selected").getAttribute("data-payment-type")); paymentInput.setAttribute('type', "hidden"); form.appendChild(paymentInput); }; diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/ServiceProviders/PaymentTypeProvider.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/ServiceProviders/PaymentTypeProvider.cs index a2b9d755..20f61887 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/ServiceProviders/PaymentTypeProvider.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/ServiceProviders/PaymentTypeProvider.cs @@ -9,15 +9,15 @@ public class PaymentTypeProvider : ITransientDependency { return new List { - new() {Id = 0, Name = "Demo", IconCss = "fa-credit-card demo", IsDefault = true}, - new() {Id = 1, Name = "Paypal", IconCss = "fa-cc-paypal paypal"} + new() { Type = "demo", Name = "Demo", IconCss = "fa-credit-card demo", IsDefault = true}, + new() { Type = "paypal", Name = "Paypal", IconCss = "fa-cc-paypal paypal"} }; } } public class PaymentType { - public int Id { get; set; } + public string Type { get; set; } public string Name { get; set; } public string IconCss { get; set; } public bool IsDefault { get; set; } = false; 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 0632b47c..d02eccb3 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,7 +1,7 @@ (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); + const paymentType = $(".payment-list").find(".is-selected").attr('data-payment-type'); + abp.utils.setCookieValue("selected_payment_type", paymentType); abp.widgets.PaymentWidget = function ($wrapper) { var widgetManager = $wrapper.data('abp-widget-manager'); @@ -19,8 +19,8 @@ .find('.payment-list .card') .click(el => { const $this = $(el.currentTarget); - const paymentTypeId = $this.attr('data-payment-id'); - abp.utils.setCookieValue("selected_payment_id", paymentTypeId); + const paymentTypeId = $this.attr('data-payment-type'); + abp.utils.setCookieValue("selected_payment_type", paymentTypeId); $this.parents(".payment-list").find('.card').removeClass("is-selected"); $this.addClass("is-selected"); });