mirror of https://github.com/EasyAbp/EShop.git
3 changed files with 67 additions and 2 deletions
@ -1,10 +1,9 @@ |
|||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using EasyAbp.EShop.Payments.Payments; |
|
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace EasyAbp.EShop.Payments |
namespace EasyAbp.EShop.Payments.Payments |
||||
{ |
{ |
||||
[Dependency(ServiceLifetime.Transient, TryRegister = true)] |
[Dependency(ServiceLifetime.Transient, TryRegister = true)] |
||||
public class FreePaymentServiceProvider : IPaymentServiceProvider |
public class FreePaymentServiceProvider : IPaymentServiceProvider |
||||
@ -0,0 +1,15 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace EasyAbp.EShop.Payments.Payments |
||||
|
{ |
||||
|
public interface IPaymentServiceResolver |
||||
|
{ |
||||
|
Task<bool> TryRegisterProviderAsync(string paymentMethod, Type providerType); |
||||
|
|
||||
|
Task<List<string>> GetPaymentMethodsAsync(); |
||||
|
|
||||
|
Task<Type> GetProviderTypeOrDefaultAsync(string paymentMethod); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,51 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace EasyAbp.EShop.Payments.Payments |
||||
|
{ |
||||
|
public class PaymentServiceResolver : IPaymentServiceResolver, ISingletonDependency |
||||
|
{ |
||||
|
protected readonly Dictionary<string, Type> Providers = new Dictionary<string, Type>(); |
||||
|
|
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
|
||||
|
public PaymentServiceResolver(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
} |
||||
|
|
||||
|
public virtual Task<bool> TryRegisterProviderAsync(string paymentMethod, Type providerType) |
||||
|
{ |
||||
|
if (Providers.ContainsKey(paymentMethod)) |
||||
|
{ |
||||
|
return Task.FromResult(false); |
||||
|
} |
||||
|
|
||||
|
using (var scope = _serviceProvider.CreateScope()) |
||||
|
{ |
||||
|
if (scope.ServiceProvider.GetService(providerType) == null) |
||||
|
{ |
||||
|
return Task.FromResult(false); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
Providers.Add(paymentMethod, providerType); |
||||
|
|
||||
|
return Task.FromResult(true); |
||||
|
} |
||||
|
|
||||
|
public virtual Task<List<string>> GetPaymentMethodsAsync() |
||||
|
{ |
||||
|
return Task.FromResult(Providers.Keys.ToList()); |
||||
|
} |
||||
|
|
||||
|
public virtual Task<Type> GetProviderTypeOrDefaultAsync(string paymentMethod) |
||||
|
{ |
||||
|
return Task.FromResult(Providers.GetOrDefault(paymentMethod)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue