From 4acb4b69955f4dc3febef9f5bd7ba09dde59a4b7 Mon Sep 17 00:00:00 2001 From: enisn Date: Mon, 3 Jan 2022 11:50:14 +0300 Subject: [PATCH 1/2] Add Payment Provider bypassing --- .../PaymentServiceConsts.cs | 7 ++++ .../PaymentRequestAppService.cs | 12 ++++--- .../PaymentRequestByPassAppService.cs | 34 +++++++++++++++++++ .../PaymentServiceApplicationModule.cs | 13 +++++++ .../PaymentServiceHttpApiHostModule.cs | 4 +++ 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 services/payment/src/EShopOnAbp.PaymentService.Application.Contracts/PaymentServiceConsts.cs create mode 100644 services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestByPassAppService.cs diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application.Contracts/PaymentServiceConsts.cs b/services/payment/src/EShopOnAbp.PaymentService.Application.Contracts/PaymentServiceConsts.cs new file mode 100644 index 00000000..1b805927 --- /dev/null +++ b/services/payment/src/EShopOnAbp.PaymentService.Application.Contracts/PaymentServiceConsts.cs @@ -0,0 +1,7 @@ +namespace EShopOnAbp.PaymentService +{ + public static class PaymentServiceConsts + { + public static bool ByPassPaymentProvider { get; set; } + } +} diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs index 34f632ca..2530827b 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs @@ -7,9 +7,11 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; namespace EShopOnAbp.PaymentService.PaymentRequests { + [ExposeServices(typeof(PaymentRequestAppService))] public class PaymentRequestAppService : PaymentServiceAppService, IPaymentRequestAppService { protected IPaymentRequestRepository PaymentRequestRepository { get; } @@ -23,7 +25,7 @@ namespace EShopOnAbp.PaymentService.PaymentRequests PayPalHttpClient = payPalHttpClient; } - public async Task CreateAsync(PaymentRequestCreationDto input) + public virtual async Task CreateAsync(PaymentRequestCreationDto input) { var paymentRequest = new PaymentRequest(GuidGenerator.Create(), input.Currency, input.BuyerId); @@ -45,7 +47,7 @@ namespace EShopOnAbp.PaymentService.PaymentRequests return ObjectMapper.Map(paymentRequest); } - public async Task StartAsync(PaymentRequestStartDto input) + public virtual async Task StartAsync(PaymentRequestStartDto input) { var paymentRequest = await PaymentRequestRepository.GetAsync(input.PaymentRequestId, includeDetails: true); @@ -103,7 +105,7 @@ namespace EShopOnAbp.PaymentService.PaymentRequests }; } - public async Task CompleteAsync(string token) + public virtual async Task CompleteAsync(string token) { var request = new OrdersCaptureRequest(token); request.RequestBody(new OrderActionRequest()); @@ -115,7 +117,7 @@ namespace EShopOnAbp.PaymentService.PaymentRequests return ObjectMapper.Map(paymentRequest); } - public async Task HandleWebhookAsync(string payload) + public virtual async Task HandleWebhookAsync(string payload) { var jObject = JObject.Parse(payload); @@ -134,7 +136,7 @@ namespace EShopOnAbp.PaymentService.PaymentRequests return true; } - private async Task UpdatePaymentRequestStateAsync(Order order) + protected async Task UpdatePaymentRequestStateAsync(Order order) { var paymentRequestId = Guid.Parse(order.PurchaseUnits.First().ReferenceId); diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestByPassAppService.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestByPassAppService.cs new file mode 100644 index 00000000..cc9488cc --- /dev/null +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestByPassAppService.cs @@ -0,0 +1,34 @@ +using PayPalCheckoutSdk.Core; +using System; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace EShopOnAbp.PaymentService.PaymentRequests +{ + [ExposeServices(typeof(PaymentRequestByPassAppService))] + public class PaymentRequestByPassAppService : PaymentRequestAppService + { + public PaymentRequestByPassAppService(IPaymentRequestRepository paymentRequestRepository, PayPalHttpClient payPalHttpClient) : base(paymentRequestRepository, payPalHttpClient) + { + } + + public override Task StartAsync(PaymentRequestStartDto input) + { + return Task.FromResult(new PaymentRequestStartResultDto + { + CheckoutLink = input.ReturnUrl + "?token=" + input.PaymentRequestId + }); + } + + public override async Task CompleteAsync(string token) + { + var paymentRequest = await PaymentRequestRepository.GetAsync(Guid.Parse(token)); + + paymentRequest.SetAsCompleted(); + + await PaymentRequestRepository.UpdateAsync(paymentRequest); + + return ObjectMapper.Map(paymentRequest); + } + } +} diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServiceApplicationModule.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServiceApplicationModule.cs index 864f0c1d..158e6fb1 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServiceApplicationModule.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServiceApplicationModule.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using EShopOnAbp.PaymentService.PayPal; using PayPalCheckoutSdk.Core; using System; +using EShopOnAbp.PaymentService.PaymentRequests; namespace EShopOnAbp.PaymentService { @@ -38,6 +39,18 @@ namespace EShopOnAbp.PaymentService return new PayPalHttpClient(new LiveEnvironment(options.ClientId, options.Secret)); }); + + context.Services.AddTransient(provider => + { + if (PaymentServiceConsts.ByPassPaymentProvider) + { + return provider.GetRequiredService(); + } + else + { + return provider.GetRequiredService(); + } + }); } } } diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs index 5512eb0d..71bbe114 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs @@ -31,6 +31,10 @@ namespace EShopOnAbp.PaymentService var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); + /// Enable bypassing payment provider via uncommenting code line below. + /// If bypassing is enabled, all payment will be completed immediately. + // PaymentServiceConsts.ByPassPaymentProvider = true; + JwtBearerConfigurationHelper.Configure(context, "PaymentService"); // SwaggerConfigurationHelper.Configure(context, "Payment Service API"); From 2c27e29e7b62620c1fe1931959153b128fe98c15 Mon Sep 17 00:00:00 2001 From: enisn Date: Mon, 3 Jan 2022 11:52:42 +0300 Subject: [PATCH 2/2] Fix typo at PaymentServiceHttpApiHostModule --- .../PaymentServiceHttpApiHostModule.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs index 71bbe114..23af9ecd 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs @@ -32,7 +32,7 @@ namespace EShopOnAbp.PaymentService var configuration = context.Services.GetConfiguration(); /// Enable bypassing payment provider via uncommenting code line below. - /// If bypassing is enabled, all payment will be completed immediately. + /// If bypassing is enabled, all payments will be completed immediately. // PaymentServiceConsts.ByPassPaymentProvider = true; JwtBearerConfigurationHelper.Configure(context, "PaymentService");