diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbp.PublicWeb.csproj b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbp.PublicWeb.csproj index a66cf000..e0c8c3ab 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbp.PublicWeb.csproj +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbp.PublicWeb.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,6 +6,7 @@ + @@ -26,6 +27,8 @@ + + diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs new file mode 100644 index 00000000..52d87fb4 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPaymentConsts.cs @@ -0,0 +1,7 @@ +namespace EShopOnAbp.PublicWeb +{ + public static class EShopOnAbpPaymentConsts + { + public const string Currency = "USD"; + } +} diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicAutoMapperProfile.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicAutoMapperProfile.cs new file mode 100644 index 00000000..8b8ada44 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicAutoMapperProfile.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using EShopOnAbp.BasketService; +using EShopOnAbp.PaymentService; + +namespace EShopOnAbp.PublicWeb +{ + public class EShopOnAbpPublicAutoMapperProfile : Profile + { + public EShopOnAbpPublicAutoMapperProfile() + { + CreateMap() + .ForMember(p => p.ReferenceId, opts => opts.MapFrom(p => p.ProductId.ToString())) + .ForMember(p => p.Name, opts => opts.MapFrom(p => p.ProductName)) + .ForMember(p => p.UnitPrice, opts => opts.MapFrom(p => p.TotalPrice / p.Count)) + .ForMember(p => p.Quantity, opts => opts.MapFrom(p => p.Count)); + } + } +} diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs index d912d267..8c45cab2 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs @@ -22,6 +22,7 @@ using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars; using Volo.Abp.AspNetCore.SignalR; +using Volo.Abp.AutoMapper; using Volo.Abp.Caching; using Volo.Abp.Caching.StackExchangeRedis; using Volo.Abp.EventBus.RabbitMq; @@ -68,6 +69,12 @@ namespace EShopOnAbp.PublicWeb var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); + context.Services.AddAutoMapperObjectMapper(); + Configure(options => + { + options.AddMaps(validate: true); + }); + Configure(options => { options.IsEnabled = true; @@ -83,6 +90,11 @@ namespace EShopOnAbp.PublicWeb options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; }); + Configure(options => + { + options.PaymentSuccessfulCallbackUrl = configuration["App:SelfUrl"].EnsureEndsWith('/') + "PaymentCompleted"; + }); + context.Services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebPaymentOptions.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebPaymentOptions.cs new file mode 100644 index 00000000..dd4d1cc0 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebPaymentOptions.cs @@ -0,0 +1,8 @@ +namespace EShopOnAbp.PublicWeb +{ + public class EShopOnAbpPublicWebPaymentOptions + { + public string PaymentSuccessfulCallbackUrl { get; set; } + public string PaymentFailureCallbackUrl { get; set; } + } +} 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 10491959..52783d02 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/Payment.cshtml.cs @@ -1,30 +1,59 @@ -using System.Threading.Tasks; -using EShopOnAbp.BasketService; +using EShopOnAbp.BasketService; +using EShopOnAbp.PaymentService; +using EShopOnAbp.PaymentService.PaymentRequests; +using EShopOnAbp.PublicWeb.Basket; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System.Collections.Generic; +using System.Threading.Tasks; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; +using Volo.Abp.Users; namespace EShopOnAbp.PublicWeb.Pages; [Authorize] public class PaymentModel : AbpPageModel { - private readonly IBasketAppService _basketAppService; + private readonly IPaymentRequestAppService _paymentRequestAppService; + private readonly UserBasketProvider _userBasketProvider; + private readonly EShopOnAbpPublicWebPaymentOptions _publicWebPaymentOptions; - public PaymentModel(IBasketAppService basketAppService) + public PaymentModel( + IPaymentRequestAppService paymentRequestAppService, + UserBasketProvider userBasketProvider, + IOptions publicWebPaymentOptions) { - _basketAppService = basketAppService; + _paymentRequestAppService = paymentRequestAppService; + _userBasketProvider = userBasketProvider; + _publicWebPaymentOptions = publicWebPaymentOptions.Value; } public void OnGet() { } + public async Task OnPostAsync() { Logger.LogInformation("Payment Proceeded..."); - await _basketAppService.PurchaseAsync(); - return RedirectToPage("PaymentCompleted"); + var basket = await _userBasketProvider.GetBasketAsync(); + + var paymentRequest = await _paymentRequestAppService.CreateAsync(new PaymentRequestCreationDto + { + BuyerId = CurrentUser.GetId().ToString(), + Currency = EShopOnAbpPaymentConsts.Currency, + Products = ObjectMapper.Map, List>(basket.Items) + }); + + var response = await _paymentRequestAppService.StartAsync(new PaymentRequestStartDto + { + ReturnUrl = _publicWebPaymentOptions.PaymentSuccessfulCallbackUrl, + PaymentRequestId = paymentRequest.Id, + CancelUrl = _publicWebPaymentOptions.PaymentFailureCallbackUrl, + }); + + return Redirect(response.CheckoutLink); } } \ No newline at end of file