7 changed files with 60 additions and 43 deletions
@ -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<IPaymentMethod> _paymentMethods; |
||||
|
private readonly ILogger<PaymentMethodResolver> _logger; |
||||
|
|
||||
|
public PaymentMethodResolver(IEnumerable<IPaymentMethod> paymentMethods, ILogger<PaymentMethodResolver> 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; |
||||
|
} |
||||
|
} |
||||
@ -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<DemoService>(); |
|
||||
} |
|
||||
|
|
||||
if (paymentTypeId == 1) |
|
||||
{ |
|
||||
return _serviceProvider.GetRequiredService<PaypalService>(); |
|
||||
} |
|
||||
|
|
||||
return _serviceProvider.GetRequiredService<DemoService>(); |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue