diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs index da80df90..15b62860 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentRequests/PaymentRequestAppService.cs @@ -10,7 +10,7 @@ namespace EShopOnAbp.PaymentService.PaymentRequests { public class PaymentRequestAppService : PaymentServiceAppService, IPaymentRequestAppService { - private readonly PaymentServiceFactory _paymentServiceFactory; + private readonly PaymentMethodResolver _paymentMethodResolver; private readonly PaymentRequestDomainService _paymentRequestDomainService; protected IPaymentRequestRepository PaymentRequestRepository { get; } protected PayPalHttpClient PayPalHttpClient { get; } @@ -18,12 +18,12 @@ namespace EShopOnAbp.PaymentService.PaymentRequests public PaymentRequestAppService( IPaymentRequestRepository paymentRequestRepository, PayPalHttpClient payPalHttpClient, - PaymentServiceFactory paymentServiceFactory, + PaymentMethodResolver paymentMethodResolver, PaymentRequestDomainService paymentRequestDomainService) { PaymentRequestRepository = paymentRequestRepository; PayPalHttpClient = payPalHttpClient; - _paymentServiceFactory = paymentServiceFactory; + _paymentMethodResolver = paymentMethodResolver; _paymentRequestDomainService = paymentRequestDomainService; } @@ -56,13 +56,13 @@ namespace EShopOnAbp.PaymentService.PaymentRequests PaymentRequest paymentRequest = await PaymentRequestRepository.GetAsync(input.PaymentRequestId, includeDetails: true); - var paymentService = _paymentServiceFactory.Create(input.PaymentTypeId); + var paymentService = _paymentMethodResolver.Resolve(input.PaymentTypeId); return await paymentService.StartAsync(paymentRequest, input); } public virtual async Task CompleteAsync(PaymentRequestCompleteInputDto input) { - var paymentService = _paymentServiceFactory.Create(input.PaymentTypeId); + var paymentService = _paymentMethodResolver.Resolve(input.PaymentTypeId); var paymentRequest = await paymentService.CompleteAsync(PaymentRequestRepository, input.Token); 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 d4a30160..f0b21e0f 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServiceApplicationModule.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServiceApplicationModule.cs @@ -8,6 +8,7 @@ using PayPalCheckoutSdk.Core; using System; using EShopOnAbp.PaymentService.PaymentRequests; using EShopOnAbp.PaymentService.PaymentServices; +using Microsoft.Extensions.Logging; namespace EShopOnAbp.PaymentService { @@ -16,7 +17,7 @@ namespace EShopOnAbp.PaymentService typeof(PaymentServiceApplicationContractsModule), typeof(AbpDddApplicationModule), typeof(AbpAutoMapperModule) - )] + )] public class PaymentServiceApplicationModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) @@ -40,6 +41,11 @@ namespace EShopOnAbp.PaymentService return new PayPalHttpClient(new LiveEnvironment(options.ClientId, options.Secret)); }); + + context.Services.AddTransient(provider => new PaymentMethodResolver( + provider.GetServices(), + provider.GetRequiredService>() + )); } } -} +} \ No newline at end of file diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/DemoService.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/DemoPaymentMethod.cs similarity index 78% rename from services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/DemoService.cs rename to services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/DemoPaymentMethod.cs index 403d0eb5..46c8e24d 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/DemoService.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/DemoPaymentMethod.cs @@ -5,9 +5,16 @@ using Volo.Abp.DependencyInjection; namespace EShopOnAbp.PaymentService.PaymentServices; -[ExposeServices(typeof(IPaymentStrategy), typeof(DemoService))] -public class DemoService : IPaymentStrategy +[ExposeServices(typeof(IPaymentMethod), typeof(DemoPaymentMethod))] +public class DemoPaymentMethod : IPaymentMethod { + public int PaymentTypeId { get; } + + public DemoPaymentMethod() + { + PaymentTypeId = 0; + } + public Task StartAsync(PaymentRequest paymentRequest, PaymentRequestStartDto input) { return Task.FromResult(new PaymentRequestStartResultDto diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/IPaymentStrategy.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/IPaymentMethod.cs similarity index 81% rename from services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/IPaymentStrategy.cs rename to services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/IPaymentMethod.cs index 4bb0a065..16d1ed6c 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/IPaymentStrategy.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/IPaymentMethod.cs @@ -4,8 +4,9 @@ using Volo.Abp.DependencyInjection; namespace EShopOnAbp.PaymentService.PaymentServices; -public interface IPaymentStrategy : ITransientDependency +public interface IPaymentMethod : ITransientDependency { + public int PaymentTypeId { get; } public Task StartAsync(PaymentRequest paymentRequest, PaymentRequestStartDto input); public Task CompleteAsync(IPaymentRequestRepository paymentRequestRepository, string token); } \ No newline at end of file diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaymentMethodResolver.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaymentMethodResolver.cs new file mode 100644 index 00000000..7973da79 --- /dev/null +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaymentMethodResolver.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.Logging; +using Volo.Abp.DependencyInjection; + +namespace EShopOnAbp.PaymentService.PaymentServices; + +public class PaymentMethodResolver : ITransientDependency +{ + private readonly IEnumerable _paymentMethods; + private readonly ILogger _logger; + + public PaymentMethodResolver(IEnumerable paymentMethods, ILogger logger) + { + _paymentMethods = paymentMethods; + _logger = logger; + } + + public IPaymentMethod Resolve(int paymentTypeId) + { + IPaymentMethod paymentMethod = _paymentMethods.FirstOrDefault(q => q.PaymentTypeId == paymentTypeId); + if (paymentMethod == null) + { + _logger.LogError($"Couldn't find Payment method with id:{paymentTypeId}"); + throw new ArgumentException("Payment method not found", paymentTypeId.ToString()); + } + + return paymentMethod; + } +} \ No newline at end of file diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaymentServiceFactory.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaymentServiceFactory.cs deleted file mode 100644 index 0a4c15b4..00000000 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaymentServiceFactory.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.DependencyInjection; - -namespace EShopOnAbp.PaymentService.PaymentServices; - -public class PaymentServiceFactory : ITransientDependency -{ - private readonly IServiceProvider _serviceProvider; - - public PaymentServiceFactory(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } - - public IPaymentStrategy Create(int paymentTypeId) - { - if (paymentTypeId == 0) - { - return _serviceProvider.GetRequiredService(); - } - - if (paymentTypeId == 1) - { - return _serviceProvider.GetRequiredService(); - } - - return _serviceProvider.GetRequiredService(); - } -} \ No newline at end of file diff --git a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaypalService.cs b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaypalMethod.cs similarity index 92% rename from services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaypalService.cs rename to services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaypalMethod.cs index 17cf81ef..c660f112 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaypalService.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.Application/PaymentServices/PaypalMethod.cs @@ -10,16 +10,18 @@ using Volo.Abp.DependencyInjection; namespace EShopOnAbp.PaymentService.PaymentServices; -[ExposeServices(typeof(IPaymentStrategy), typeof(PaypalService))] -public class PaypalService : IPaymentStrategy +[ExposeServices(typeof(IPaymentMethod), typeof(PaypalMethod))] +public class PaypalMethod : IPaymentMethod { private readonly PayPalHttpClient _payPalHttpClient; private readonly PaymentRequestDomainService _paymentRequestDomainService; + public int PaymentTypeId { get; } - public PaypalService(PayPalHttpClient payPalHttpClient, PaymentRequestDomainService paymentRequestDomainService) + public PaypalMethod(PayPalHttpClient payPalHttpClient, PaymentRequestDomainService paymentRequestDomainService) { _payPalHttpClient = payPalHttpClient; _paymentRequestDomainService = paymentRequestDomainService; + PaymentTypeId = 1; } public async Task StartAsync(PaymentRequest paymentRequest,