Browse Source

#79 Remove IPayableChecker and introduce PaymentCreationAuthorizationHandler

pull/103/head
gdlcf88 6 years ago
parent
commit
636c7ec862
  1. 13
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application.Contracts/EasyAbp/EShop/Payments/Payments/PaymentAlreadyExistsException.cs
  2. 8
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/EShopPaymentsApplicationModule.cs
  3. 29
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPayableCheckProvider.cs
  4. 31
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPaymentCreationAuthorizationHandler.cs
  5. 13
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/IPayableCheckProvider.cs
  6. 14
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/IPayableChecker.cs
  7. 12
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/MultiCurrencyNotSupportedException.cs
  8. 32
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PayableChecker.cs
  9. 13
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentAppService.cs
  10. 22
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentCreationAuthorizationHandler.cs
  11. 16
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentCreationResource.cs
  12. 10
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentOperation.cs
  13. 15
      modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentOperationAuthorizationRequirement.cs
  14. 2
      modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Shared/EasyAbp/EShop/Stores/EShopStoresApplicationSharedModule.cs

13
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application.Contracts/EasyAbp/EShop/Payments/Payments/PaymentAlreadyExistsException.cs

@ -1,13 +0,0 @@
using System;
using Volo.Abp;
namespace EasyAbp.EShop.Payments.Payments
{
public class PaymentAlreadyExistsException : BusinessException
{
public PaymentAlreadyExistsException(Guid orderId)
: base(message: $"The order {orderId}'s payment is already exists.")
{
}
}
}

8
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/EShopPaymentsApplicationModule.cs

@ -1,4 +1,5 @@
using EasyAbp.PaymentService;
using EasyAbp.EShop.Payments.Payments;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
@ -14,6 +15,11 @@ namespace EasyAbp.EShop.Payments
)]
public class EShopPaymentsApplicationModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddSingleton<IAuthorizationHandler, BasicPaymentCreationAuthorizationHandler>();
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAutoMapperObjectMapper<EShopPaymentsApplicationModule>();

29
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPayableCheckProvider.cs

@ -1,29 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Payments.Payments.Dtos;
using EasyAbp.PaymentService.Payments;
using Volo.Abp.DependencyInjection;
namespace EasyAbp.EShop.Payments.Payments
{
public class BasicPayableCheckProvider : IPayableCheckProvider, ITransientDependency
{
public virtual Task CheckAsync(CreatePaymentDto input, List<OrderDto> orders, CreatePaymentEto createPaymentEto)
{
foreach (var order in orders.Where(order => order.PaymentId.HasValue || order.PaidTime.HasValue))
{
throw new PaymentAlreadyExistsException(order.Id);
}
if (orders.Select(order => order.Currency).Distinct().Count() != 1)
{
// Todo: convert to a single currency.
throw new MultiCurrencyNotSupportedException();
}
return Task.CompletedTask;
}
}
}

31
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPaymentCreationAuthorizationHandler.cs

@ -0,0 +1,31 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
namespace EasyAbp.EShop.Payments.Payments
{
public class BasicPaymentCreationAuthorizationHandler : PaymentCreationAuthorizationHandler
{
public BasicPaymentCreationAuthorizationHandler()
{
}
protected override Task HandlePaymentCreationAsync(AuthorizationHandlerContext context,
PaymentOperationAuthorizationRequirement requirement, PaymentCreationResource resource)
{
if (resource.Orders.Any(order => order.PaymentId.HasValue || order.PaidTime.HasValue))
{
context.Fail();
}
if (resource.Orders.Select(order => order.Currency).Distinct().Count() != 1)
{
// Todo: convert to a single currency.
context.Fail();
}
return Task.CompletedTask;
}
}
}

13
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/IPayableCheckProvider.cs

@ -1,13 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Payments.Payments.Dtos;
using EasyAbp.PaymentService.Payments;
namespace EasyAbp.EShop.Payments.Payments
{
public interface IPayableCheckProvider
{
Task CheckAsync(CreatePaymentDto input, List<OrderDto> orders, CreatePaymentEto createPaymentEto);
}
}

14
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/IPayableChecker.cs

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Payments.Payments.Dtos;
using EasyAbp.PaymentService.Payments;
namespace EasyAbp.EShop.Payments.Payments
{
public interface IPayableChecker
{
Task CheckAsync(CreatePaymentDto input, List<OrderDto> orders, CreatePaymentEto createPaymentEto);
}
}

12
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/MultiCurrencyNotSupportedException.cs

@ -1,12 +0,0 @@
using Volo.Abp;
namespace EasyAbp.EShop.Payments.Payments
{
public class MultiCurrencyNotSupportedException : BusinessException
{
public MultiCurrencyNotSupportedException() : base(message: $"The currency of all orders should be the same.")
{
}
}
}

32
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PayableChecker.cs

@ -1,32 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Payments.Payments.Dtos;
using EasyAbp.PaymentService.Payments;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
namespace EasyAbp.EShop.Payments.Payments
{
public class PayableChecker : IPayableChecker, ITransientDependency
{
private readonly IServiceProvider _serviceProvider;
public PayableChecker(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public virtual async Task CheckAsync(CreatePaymentDto input, List<OrderDto> orders, CreatePaymentEto createPaymentEto)
{
var providers = _serviceProvider.GetServices<IPayableCheckProvider>();
foreach (var provider in providers)
{
await provider.CheckAsync(input, orders, createPaymentEto);
}
}
}
}

13
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentAppService.cs

@ -2,14 +2,12 @@ using EasyAbp.EShop.Orders.Orders;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Payments.Authorization;
using EasyAbp.EShop.Payments.Payments.Dtos;
using EasyAbp.EShop.Stores.Permissions;
using EasyAbp.PaymentService.Payments;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.EShop.Stores.Stores;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.EventBus.Distributed;
@ -24,18 +22,15 @@ namespace EasyAbp.EShop.Payments.Payments
protected override string GetPolicyName { get; set; } = PaymentsPermissions.Payments.Manage;
protected override string GetListPolicyName { get; set; } = PaymentsPermissions.Payments.Manage;
private readonly IPayableChecker _payableChecker;
private readonly IDistributedEventBus _distributedEventBus;
private readonly IOrderAppService _orderAppService;
private readonly IPaymentRepository _repository;
public PaymentAppService(
IPayableChecker payableChecker,
IDistributedEventBus distributedEventBus,
IOrderAppService orderAppService,
IPaymentRepository repository) : base(repository)
{
_payableChecker = payableChecker;
_distributedEventBus = distributedEventBus;
_orderAppService = orderAppService;
_repository = repository;
@ -105,7 +100,13 @@ namespace EasyAbp.EShop.Payments.Payments
}).ToList()
};
await _payableChecker.CheckAsync(input, orders, createPaymentEto);
await AuthorizationService.CheckAsync(new PaymentCreationResource
{
Input = input,
Orders = orders,
CreatePaymentEto = createPaymentEto
},
new PaymentOperationAuthorizationRequirement(PaymentOperation.Creation));
await _distributedEventBus.PublishAsync(createPaymentEto);
}

22
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentCreationAuthorizationHandler.cs

@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
namespace EasyAbp.EShop.Payments.Payments
{
public abstract class PaymentCreationAuthorizationHandler : AuthorizationHandler<PaymentOperationAuthorizationRequirement, PaymentCreationResource>
{
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PaymentOperationAuthorizationRequirement requirement,
PaymentCreationResource resource)
{
if (requirement.PaymentOperation != PaymentOperation.Creation)
{
return;
}
await HandlePaymentCreationAsync(context, requirement, resource);
}
protected abstract Task HandlePaymentCreationAsync(AuthorizationHandlerContext context,
PaymentOperationAuthorizationRequirement requirement, PaymentCreationResource resource);
}
}

16
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentCreationResource.cs

@ -0,0 +1,16 @@
using System.Collections.Generic;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Payments.Payments.Dtos;
using EasyAbp.PaymentService.Payments;
namespace EasyAbp.EShop.Payments.Payments
{
public class PaymentCreationResource
{
public CreatePaymentDto Input { get; set; }
public List<OrderDto> Orders { get; set; }
public CreatePaymentEto CreatePaymentEto { get; set; }
}
}

10
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentOperation.cs

@ -0,0 +1,10 @@
using System;
namespace EasyAbp.EShop.Payments.Payments
{
[Flags]
public enum PaymentOperation
{
Creation = 0
}
}

15
modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentOperationAuthorizationRequirement.cs

@ -0,0 +1,15 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
namespace EasyAbp.EShop.Payments.Payments
{
public class PaymentOperationAuthorizationRequirement : IAuthorizationRequirement
{
public PaymentOperation PaymentOperation { get; }
public PaymentOperationAuthorizationRequirement(PaymentOperation paymentOperation)
{
PaymentOperation = paymentOperation;
}
}
}

2
modules/EasyAbp.EShop.Stores/src/EasyAbp.EShop.Stores.Application.Shared/EasyAbp/EShop/Stores/EShopStoresApplicationSharedModule.cs

@ -14,7 +14,7 @@ namespace EasyAbp.EShop.Stores
)]
public class EShopStoresApplicationSharedModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
public override void PreConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddSingleton<IAuthorizationHandler, BasicStorePermissionAuthorizationHandler>();
}

Loading…
Cancel
Save