diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp.EShop.Orders.Domain.csproj b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp.EShop.Orders.Domain.csproj index 2ab7572f..2822adf2 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp.EShop.Orders.Domain.csproj +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp.EShop.Orders.Domain.csproj @@ -8,7 +8,6 @@ - diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/EShopOrdersDomainModule.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/EShopOrdersDomainModule.cs index e1ef593c..97caaf32 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/EShopOrdersDomainModule.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/EShopOrdersDomainModule.cs @@ -1,4 +1,6 @@ using EasyAbp.EShop.Orders.Orders; +using EasyAbp.EShop.Payments; +using EasyAbp.EShop.Products; using EasyAbp.EShop.Stores; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AutoMapper; @@ -10,6 +12,8 @@ namespace EasyAbp.EShop.Orders { [DependsOn( typeof(AbpAutoMapperModule), + typeof(EShopPaymentsDomainSharedModule), + typeof(EShopProductsDomainSharedModule), typeof(EShopOrdersDomainSharedModule) )] public class EShopOrdersDomainModule : AbpModule diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentChecker.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentChecker.cs index 6a8ed084..26bfed31 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentChecker.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentChecker.cs @@ -1,10 +1,10 @@ using System.Threading.Tasks; -using EasyAbp.PaymentService.Payments; +using EasyAbp.EShop.Payments.Payments; namespace EasyAbp.EShop.Orders.Orders { - public interface IOrderPaymentChecker + public interface IEShopPaymentChecker { - Task IsValidPaymentAsync(Order order, PaymentEto payment, PaymentItemEto paymentItem); + Task IsValidPaymentAsync(Order order, EShopPaymentEto payment, EShopPaymentItemEto paymentItem); } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCompletedEventHandler.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCompletedEventHandler.cs index 7d51d667..5d9abb8d 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCompletedEventHandler.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCompletedEventHandler.cs @@ -1,10 +1,9 @@ -using EasyAbp.PaymentService.Payments; -using Volo.Abp.Domain.Entities.Events.Distributed; +using EasyAbp.EShop.Payments.Payments; using Volo.Abp.EventBus.Distributed; namespace EasyAbp.EShop.Orders.Orders { - public interface IOrderPaymentCompletedEventHandler : IDistributedEventHandler + public interface IEShopPaymentCompletedEventHandler : IDistributedEventHandler { } diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCreatedEventHandler.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCreatedEventHandler.cs index c15f8b9f..4edcc08e 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCreatedEventHandler.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IOrderPaymentCreatedEventHandler.cs @@ -1,10 +1,10 @@ -using EasyAbp.PaymentService.Payments; +using EasyAbp.EShop.Payments.Payments; using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.EventBus.Distributed; namespace EasyAbp.EShop.Orders.Orders { - public interface IOrderPaymentCreatedEventHandler : IDistributedEventHandler> + public interface IEShopPaymentCreatedEventHandler : IDistributedEventHandler> { } diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentChecker.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentChecker.cs index 830f4021..ce4dd864 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentChecker.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentChecker.cs @@ -1,33 +1,33 @@ using System; using System.Threading.Tasks; -using EasyAbp.PaymentService.Payments; +using EasyAbp.EShop.Payments.Payments; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; namespace EasyAbp.EShop.Orders.Orders { - public class OrderPaymentChecker : IOrderPaymentChecker, ITransientDependency + public class EShopPaymentChecker : IEShopPaymentChecker, ITransientDependency { - public virtual async Task IsValidPaymentAsync(Order order, PaymentEto payment, PaymentItemEto paymentItem) + public virtual async Task IsValidPaymentAsync(Order order, EShopPaymentEto payment, EShopPaymentItemEto paymentItem) { return await IsStoreIdCorrectAsync(order, paymentItem) && await IsPaymentPriceCorrectAsync(order, paymentItem) && await IsUserAllowedToPayAsync(order, payment); } - protected virtual Task IsStoreIdCorrectAsync(Order order, PaymentItemEto paymentItem) + protected virtual Task IsStoreIdCorrectAsync(Order order, EShopPaymentItemEto paymentItem) { return Task.FromResult( Guid.TryParse(paymentItem.GetProperty("StoreId"), out var paymentStoreId) && order.StoreId == paymentStoreId); } - protected virtual Task IsPaymentPriceCorrectAsync(Order order, PaymentItemEto paymentItem) + protected virtual Task IsPaymentPriceCorrectAsync(Order order, EShopPaymentItemEto paymentItem) { return Task.FromResult(order.TotalPrice == paymentItem.OriginalPaymentAmount); } - protected virtual Task IsUserAllowedToPayAsync(Order order, PaymentEto payment) + protected virtual Task IsUserAllowedToPayAsync(Order order, EShopPaymentEto payment) { return Task.FromResult(order.CustomerUserId == payment.UserId); } diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCompletedEventHandler.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCompletedEventHandler.cs index dbc59fff..89dac229 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCompletedEventHandler.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCompletedEventHandler.cs @@ -2,9 +2,8 @@ using System.Linq; using System.Threading.Tasks; using EasyAbp.EShop.Payments; -using EasyAbp.PaymentService.Payments; +using EasyAbp.EShop.Payments.Payments; using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.ObjectMapping; @@ -13,22 +12,22 @@ using Volo.Abp.Uow; namespace EasyAbp.EShop.Orders.Orders { - public class OrderPaymentCompletedEventHandler : IOrderPaymentCompletedEventHandler, ITransientDependency + public class EShopPaymentCompletedEventHandler : IEShopPaymentCompletedEventHandler, ITransientDependency { private readonly IClock _clock; private readonly ICurrentTenant _currentTenant; private readonly IObjectMapper _objectMapper; private readonly IUnitOfWorkManager _unitOfWorkManager; - private readonly IOrderPaymentChecker _orderPaymentChecker; + private readonly IEShopPaymentChecker _eShopPaymentChecker; private readonly IDistributedEventBus _distributedEventBus; private readonly IOrderRepository _orderRepository; - public OrderPaymentCompletedEventHandler( + public EShopPaymentCompletedEventHandler( IClock clock, ICurrentTenant currentTenant, IObjectMapper objectMapper, IUnitOfWorkManager unitOfWorkManager, - IOrderPaymentChecker orderPaymentChecker, + IEShopPaymentChecker eShopPaymentChecker, IDistributedEventBus distributedEventBus, IOrderRepository orderRepository) { @@ -36,12 +35,13 @@ namespace EasyAbp.EShop.Orders.Orders _currentTenant = currentTenant; _objectMapper = objectMapper; _unitOfWorkManager = unitOfWorkManager; - _orderPaymentChecker = orderPaymentChecker; + _eShopPaymentChecker = eShopPaymentChecker; _distributedEventBus = distributedEventBus; _orderRepository = orderRepository; } - public virtual async Task HandleEventAsync(PaymentCompletedEto eventData) + [UnitOfWork(true)] + public virtual async Task HandleEventAsync(EShopPaymentCompletedEto eventData) { var payment = eventData.Payment; @@ -52,8 +52,6 @@ namespace EasyAbp.EShop.Orders.Orders using var currentTenant = _currentTenant.Change(payment.TenantId); - using var uow = _unitOfWorkManager.Begin(isTransactional: true); - foreach (var item in payment.PaymentItems.Where(item => item.ItemType == PaymentsConsts.PaymentItemType)) { var orderId = Guid.Parse(item.ItemKey); @@ -66,9 +64,9 @@ namespace EasyAbp.EShop.Orders.Orders throw new OrderIsInWrongStageException(order.Id); } - if (!await _orderPaymentChecker.IsValidPaymentAsync(order, payment, item)) + if (!await _eShopPaymentChecker.IsValidPaymentAsync(order, payment, item)) { - throw new OrderPaymentInvalidException(payment.Id, orderId); + throw new EShopPaymentInvalidException(payment.Id, orderId); } order.SetPaidTime(_clock.Now); @@ -76,7 +74,7 @@ namespace EasyAbp.EShop.Orders.Orders await _orderRepository.UpdateAsync(order, true); - uow.OnCompleted(async () => await _distributedEventBus.PublishAsync(new OrderPaidEto + _unitOfWorkManager.Current.OnCompleted(async () => await _distributedEventBus.PublishAsync(new OrderPaidEto { Order = _objectMapper.Map(order), PaymentId = payment.Id, @@ -84,7 +82,7 @@ namespace EasyAbp.EShop.Orders.Orders })); } - await uow.CompleteAsync(); + await _unitOfWorkManager.Current.CompleteAsync(); } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCreatedEventHandler.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCreatedEventHandler.cs index e621536c..4e2b641c 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCreatedEventHandler.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentCreatedEventHandler.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Threading.Tasks; using EasyAbp.EShop.Payments; -using EasyAbp.PaymentService.Payments; +using EasyAbp.EShop.Payments.Payments; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.MultiTenancy; @@ -10,24 +10,24 @@ using Volo.Abp.Uow; namespace EasyAbp.EShop.Orders.Orders { - public class OrderPaymentCreatedEventHandler : IOrderPaymentCreatedEventHandler, ITransientDependency + public class EShopPaymentCreatedEventHandler : IEShopPaymentCreatedEventHandler, ITransientDependency { private readonly ICurrentTenant _currentTenant; - private readonly IOrderPaymentChecker _orderPaymentChecker; + private readonly IEShopPaymentChecker _eShopPaymentChecker; private readonly IOrderRepository _orderRepository; - public OrderPaymentCreatedEventHandler( + public EShopPaymentCreatedEventHandler( ICurrentTenant currentTenant, - IOrderPaymentChecker orderPaymentChecker, + IEShopPaymentChecker eShopPaymentChecker, IOrderRepository orderRepository) { _currentTenant = currentTenant; - _orderPaymentChecker = orderPaymentChecker; + _eShopPaymentChecker = eShopPaymentChecker; _orderRepository = orderRepository; } [UnitOfWork(true)] - public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) { using var currentTenant = _currentTenant.Change(eventData.Entity.TenantId); @@ -43,10 +43,10 @@ namespace EasyAbp.EShop.Orders.Orders throw new OrderIsInWrongStageException(order.Id); } - if (!await _orderPaymentChecker.IsValidPaymentAsync(order, eventData.Entity, item)) + if (!await _eShopPaymentChecker.IsValidPaymentAsync(order, eventData.Entity, item)) { // Todo: should cancel the payment? - throw new OrderPaymentInvalidException(eventData.Entity.Id, orderId); + throw new EShopPaymentInvalidException(eventData.Entity.Id, orderId); } order.SetPaymentId(eventData.Entity.Id); diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentInvalidException.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentInvalidException.cs index 9f82003a..20e80840 100644 --- a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentInvalidException.cs +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/OrderPaymentInvalidException.cs @@ -3,9 +3,9 @@ using Volo.Abp; namespace EasyAbp.EShop.Orders.Orders { - public class OrderPaymentInvalidException : BusinessException + public class EShopPaymentInvalidException : BusinessException { - public OrderPaymentInvalidException(Guid paymentId, Guid orderId) + public EShopPaymentInvalidException(Guid paymentId, Guid orderId) : base(message: $"The payment {paymentId} has invalid configurations for the order {orderId}.") { } diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application.Contracts/EasyAbp/EShop/Payments/Payments/OrderPaymentAlreadyExistsException.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application.Contracts/EasyAbp/EShop/Payments/Payments/OrderPaymentAlreadyExistsException.cs index 913c6735..f755deb6 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application.Contracts/EasyAbp/EShop/Payments/Payments/OrderPaymentAlreadyExistsException.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application.Contracts/EasyAbp/EShop/Payments/Payments/OrderPaymentAlreadyExistsException.cs @@ -3,9 +3,9 @@ using Volo.Abp; namespace EasyAbp.EShop.Payments.Payments { - public class OrderPaymentAlreadyExistsException : BusinessException + public class EShopPaymentAlreadyExistsException : BusinessException { - public OrderPaymentAlreadyExistsException(Guid orderId) + public EShopPaymentAlreadyExistsException(Guid orderId) : base(message: $"The order {orderId}'s payment is already exists.") { } diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPayableCheckProvider.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPayableCheckProvider.cs index 8d3cf040..cdf82bd9 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPayableCheckProvider.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/BasicPayableCheckProvider.cs @@ -14,7 +14,7 @@ namespace EasyAbp.EShop.Payments.Payments { foreach (var order in orders.Where(order => order.PaymentId.HasValue || order.PaidTime.HasValue)) { - throw new OrderPaymentAlreadyExistsException(order.Id); + throw new EShopPaymentAlreadyExistsException(order.Id); } if (orders.Select(order => order.Currency).Distinct().Count() != 1) diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentCompletedEto.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentCompletedEto.cs new file mode 100644 index 00000000..0f09a2af --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentCompletedEto.cs @@ -0,0 +1,10 @@ +using System; + +namespace EasyAbp.EShop.Payments.Payments +{ + [Serializable] + public class EShopPaymentCompletedEto + { + public EShopPaymentEto Payment { get; set; } + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentEto.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentEto.cs new file mode 100644 index 00000000..504f4499 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentEto.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using EasyAbp.PaymentService.Payments; +using Volo.Abp.Data; +using Volo.Abp.MultiTenancy; + +namespace EasyAbp.EShop.Payments.Payments +{ + [Serializable] + public class EShopPaymentEto : IPayment, IMultiTenant, IHasExtraProperties + { + #region Base properties + + public Guid Id { get; set; } + + public Guid? TenantId { get; set; } + + public Guid UserId { get; set; } + + public string PaymentMethod { get; set; } + + public string PayeeAccount { get; set; } + + public string ExternalTradingCode { get; set; } + + public string Currency { get; set; } + + public decimal OriginalPaymentAmount { get; set; } + + public decimal PaymentDiscount { get; set; } + + public decimal ActualPaymentAmount { get; set; } + + public decimal RefundAmount { get; set; } + + public decimal PendingRefundAmount { get; set; } + + public DateTime? CompletionTime { get; set; } + + public DateTime? CanceledTime { get; set; } + + public Dictionary ExtraProperties { get; set; } + + #endregion + + public List PaymentItems { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentItemEto.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentItemEto.cs new file mode 100644 index 00000000..bb72dd13 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/OrderPaymentItemEto.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using EasyAbp.PaymentService.Payments; +using Volo.Abp.Data; + +namespace EasyAbp.EShop.Payments.Payments +{ + [Serializable] + public class EShopPaymentItemEto : IPaymentItem, IHasExtraProperties + { + #region Base properties + + public Guid Id { get; set; } + + public string ItemType { get; set; } + + public string ItemKey { get; set; } + + public decimal OriginalPaymentAmount { get; set; } + + public decimal PaymentDiscount { get; set; } + + public decimal ActualPaymentAmount { get; set; } + + public decimal RefundAmount { get; set; } + + public decimal PendingRefundAmount { get; set; } + + public Dictionary ExtraProperties { get; set; } + + #endregion + + public Guid StoreId { get; set; } + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs index 89c61456..d7dcd14b 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs @@ -1,8 +1,6 @@ using EasyAbp.EShop.Payments.Payments; using EasyAbp.EShop.Payments.Refunds; using EasyAbp.EShop.Stores; -using EasyAbp.PaymentService.Payments; -using EasyAbp.PaymentService.Refunds; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AutoMapper; using Volo.Abp.Domain.Entities.Events.Distributed; @@ -14,9 +12,21 @@ namespace EasyAbp.EShop.Payments typeof(AbpAutoMapperModule), typeof(EShopPaymentsDomainSharedModule), typeof(EShopStoresDomainSharedModule) - )] + )] public class EShopPaymentsDomainModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.EtoMappings.Add(typeof(EShopPaymentsDomainModule)); + options.EtoMappings.Add(typeof(EShopPaymentsDomainModule)); + + options.AutoEventSelectors.Add(); + options.AutoEventSelectors.Add(); + }); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAutoMapperObjectMapper(); diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentSynchronizer.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentSynchronizer.cs index 75d06cc9..7f6662e4 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentSynchronizer.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentSynchronizer.cs @@ -14,6 +14,7 @@ using Volo.Abp.Uow; namespace EasyAbp.EShop.Payments.Payments { public class PaymentSynchronizer : + IDistributedEventHandler>, IDistributedEventHandler>, IDistributedEventHandler>, IPaymentSynchronizer, @@ -21,71 +22,132 @@ namespace EasyAbp.EShop.Payments.Payments { private readonly IObjectMapper _objectMapper; private readonly ICurrentTenant _currentTenant; + private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IPaymentRepository _paymentRepository; + private readonly IDistributedEventBus _distributedEventBus; public PaymentSynchronizer( IObjectMapper objectMapper, ICurrentTenant currentTenant, - IPaymentRepository paymentRepository) + IUnitOfWorkManager unitOfWorkManager, + IPaymentRepository paymentRepository, + IDistributedEventBus distributedEventBus) { _objectMapper = objectMapper; _currentTenant = currentTenant; + _unitOfWorkManager = unitOfWorkManager; _paymentRepository = paymentRepository; + _distributedEventBus = distributedEventBus; } [UnitOfWork(true)] - public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) { + if (eventData.Entity.PaymentItems.All(item => item.ItemType != PaymentsConsts.PaymentItemType)) + { + return; + } + using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); var payment = await _paymentRepository.FindAsync(eventData.Entity.Id); - if (payment == null) + if (payment != null) { - payment = _objectMapper.Map(eventData.Entity); + return; + } + + payment = _objectMapper.Map(eventData.Entity); - payment.SetPaymentItems( - _objectMapper.Map, List>(eventData.Entity.PaymentItems)); + payment.SetPaymentItems( + _objectMapper.Map, List>(eventData.Entity.PaymentItems)); + + payment.PaymentItems.ForEach(FillPaymentItemStoreId); - await _paymentRepository.InsertAsync(payment, true); + await _paymentRepository.InsertAsync(payment, true); + + if (payment.CompletionTime.HasValue) + { + _unitOfWorkManager.Current.OnCompleted(async () => + { + await _distributedEventBus.PublishAsync(new EShopPaymentCompletedEto + { + Payment = _objectMapper.Map(payment) + }); + }); } - else + } + + [UnitOfWork(true)] + public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) + { + if (eventData.Entity.PaymentItems.All(item => item.ItemType != PaymentsConsts.PaymentItemType)) { - _objectMapper.Map(eventData.Entity, payment); + return; + } + + using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); - foreach (var etoItem in eventData.Entity.PaymentItems) + var payment = await _paymentRepository.FindAsync(eventData.Entity.Id); + + if (payment == null) + { + return; + } + + if (eventData.Entity.CompletionTime.HasValue && !payment.CompletionTime.HasValue) + { + _unitOfWorkManager.Current.OnCompleted(async () => { - var item = payment.PaymentItems.FirstOrDefault(i => i.Id == etoItem.Id); - - if (item == null) + await _distributedEventBus.PublishAsync(new EShopPaymentCompletedEto { - if (!Guid.TryParse(etoItem.GetProperty("StoreId"), out var storeId)) - { - throw new StoreIdNotFoundException(); - } - - item = _objectMapper.Map(etoItem); - - item.SetStoreId(storeId); - - payment.PaymentItems.Add(item); - } - else - { - _objectMapper.Map(etoItem, item); - } - } + Payment = _objectMapper.Map(payment) + }); + }); + } - var etoPaymentItemIds = eventData.Entity.PaymentItems.Select(i => i.Id).ToList(); + _objectMapper.Map(eventData.Entity, payment); - payment.PaymentItems.RemoveAll(i => !etoPaymentItemIds.Contains(i.Id)); + foreach (var etoItem in eventData.Entity.PaymentItems) + { + var item = payment.PaymentItems.FirstOrDefault(i => i.Id == etoItem.Id); + + if (item == null) + { + item = _objectMapper.Map(etoItem); + + FillPaymentItemStoreId(item); + + payment.PaymentItems.Add(item); + } + else + { + _objectMapper.Map(etoItem, item); + } } + + var etoPaymentItemIds = eventData.Entity.PaymentItems.Select(i => i.Id).ToList(); + + payment.PaymentItems.RemoveAll(i => !etoPaymentItemIds.Contains(i.Id)); await _paymentRepository.UpdateAsync(payment, true); } + protected virtual void FillPaymentItemStoreId(PaymentItem item) + { + if (!Guid.TryParse(item.GetProperty("StoreId"), out var storeId)) + { + throw new StoreIdNotFoundException(); + } + + item.SetStoreId(storeId); + } + + [UnitOfWork(true)] public virtual async Task HandleEventAsync(EntityDeletedEto eventData) { + using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); + var payment = await _paymentRepository.FindAsync(eventData.Entity.Id); if (payment == null) diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/PaymentsDomainAutoMapperProfile.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/PaymentsDomainAutoMapperProfile.cs index c6d2b514..2974ef07 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/PaymentsDomainAutoMapperProfile.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/PaymentsDomainAutoMapperProfile.cs @@ -25,6 +25,9 @@ namespace EasyAbp.EShop.Payments CreateMap(MemberList.Source) .Ignore(x => x.StoreId) .Ignore(x => x.OrderId); + + CreateMap(); + CreateMap(); CreateMap(); CreateMap(); CreateMap(); diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Refunds/RefundSynchronizer.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Refunds/RefundSynchronizer.cs index 398dc9cb..702f42d8 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Refunds/RefundSynchronizer.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Refunds/RefundSynchronizer.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using EasyAbp.EShop.Payments.Payments; using EasyAbp.PaymentService.Refunds; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; @@ -16,6 +17,7 @@ using Volo.Abp.Uow; namespace EasyAbp.EShop.Payments.Refunds { public class RefundSynchronizer : + IDistributedEventHandler>, IDistributedEventHandler>, IDistributedEventHandler>, IRefundSynchronizer, @@ -27,6 +29,7 @@ namespace EasyAbp.EShop.Payments.Refunds private readonly IJsonSerializer _jsonSerializer; private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IDistributedEventBus _distributedEventBus; + private readonly IPaymentRepository _paymentRepository; private readonly IRefundRepository _refundRepository; public RefundSynchronizer( @@ -36,6 +39,7 @@ namespace EasyAbp.EShop.Payments.Refunds IJsonSerializer jsonSerializer, IUnitOfWorkManager unitOfWorkManager, IDistributedEventBus distributedEventBus, + IPaymentRepository paymentRepository, IRefundRepository refundRepository) { _objectMapper = objectMapper; @@ -44,71 +48,103 @@ namespace EasyAbp.EShop.Payments.Refunds _jsonSerializer = jsonSerializer; _unitOfWorkManager = unitOfWorkManager; _distributedEventBus = distributedEventBus; + _paymentRepository = paymentRepository; _refundRepository = refundRepository; } + + [UnitOfWork(true)] + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + { + using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); + + var refund = await _refundRepository.FindAsync(eventData.Entity.Id); + + if (refund != null) + { + return; + } + + var payment = await _paymentRepository.FindAsync(eventData.Entity.PaymentId); + + if (payment == null) + { + return; + } + + refund = _objectMapper.Map(eventData.Entity); + + refund.SetRefundItems( + _objectMapper.Map, List>(eventData.Entity.RefundItems)); + + refund.RefundItems.ForEach(item => + { + FillRefundItemStoreId(item); + FillRefundItemOrderId(item); + }); + + FillRefundItemOrderLines(refund); + + await _refundRepository.InsertAsync(refund, true); + + if (refund.CompletedTime.HasValue) + { + _unitOfWorkManager.Current.OnCompleted(async () => + await _distributedEventBus.PublishAsync(new OrderRefundCompletedEto + {Refund = _objectMapper.Map(refund)})); + } + } [UnitOfWork(true)] public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) { using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); - var publishOrderRefundCompletedEvent = false; - var refund = await _refundRepository.FindAsync(eventData.Entity.Id); if (refund == null) { - refund = _objectMapper.Map(eventData.Entity); - - refund.SetRefundItems( - _objectMapper.Map, List>(eventData.Entity.RefundItems)); + return; + } - refund.RefundItems.ForEach(item => - { - FillRefundItemStoreId(item); - FillRefundItemOrderId(item); - }); + if (eventData.Entity.CompletedTime.HasValue && !refund.CompletedTime.HasValue) + { + _unitOfWorkManager.Current.OnCompleted(async () => + await _distributedEventBus.PublishAsync(new OrderRefundCompletedEto + {Refund = _objectMapper.Map(refund)})); + } - if (refund.CompletedTime.HasValue) + _objectMapper.Map(eventData.Entity, refund); + + foreach (var etoItem in eventData.Entity.RefundItems) + { + var item = refund.RefundItems.FirstOrDefault(i => i.Id == etoItem.Id); + + if (item == null) { - publishOrderRefundCompletedEvent = true; + item = _objectMapper.Map(etoItem); + + refund.RefundItems.Add(item); } - - await _refundRepository.InsertAsync(refund, true); - } - else - { - if (eventData.Entity.CompletedTime.HasValue && !refund.CompletedTime.HasValue) + else { - publishOrderRefundCompletedEvent = true; + _objectMapper.Map(etoItem, item); } + + FillRefundItemStoreId(item); + FillRefundItemOrderId(item); + } - _objectMapper.Map(eventData.Entity, refund); + var etoRefundItemIds = eventData.Entity.RefundItems.Select(i => i.Id).ToList(); - foreach (var etoItem in eventData.Entity.RefundItems) - { - var item = refund.RefundItems.FirstOrDefault(i => i.Id == etoItem.Id); + refund.RefundItems.RemoveAll(i => !etoRefundItemIds.Contains(i.Id)); - if (item == null) - { - item = _objectMapper.Map(etoItem); - - refund.RefundItems.Add(item); - } - else - { - _objectMapper.Map(etoItem, item); - } + FillRefundItemOrderLines(refund); - FillRefundItemStoreId(item); - FillRefundItemOrderId(item); - } - - var etoRefundItemIds = eventData.Entity.RefundItems.Select(i => i.Id).ToList(); - - refund.RefundItems.RemoveAll(i => !etoRefundItemIds.Contains(i.Id)); - } + await _refundRepository.UpdateAsync(refund, true); + } + protected virtual void FillRefundItemOrderLines(Refund refund) + { foreach (var refundItem in refund.RefundItems) { var orderLineInfoModels = @@ -135,16 +171,6 @@ namespace EasyAbp.EShop.Payments.Refunds refundItem.RefundItemOrderLines.RemoveAll(i => !orderLineIds.Contains(i.OrderLineId)); } } - - await _refundRepository.UpdateAsync(refund, true); - - var orderRefundEto = _objectMapper.Map(refund); - - if (publishOrderRefundCompletedEvent) - { - _unitOfWorkManager.Current.OnCompleted(async () => - await _distributedEventBus.PublishAsync(new OrderRefundCompletedEto {Refund = orderRefundEto})); - } } protected virtual void FillRefundItemStoreId(RefundItem item) @@ -167,8 +193,11 @@ namespace EasyAbp.EShop.Payments.Refunds item.SetOrderId(orderId); } + [UnitOfWork(true)] public virtual async Task HandleEventAsync(EntityDeletedEto eventData) { + using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); + var refund = await _refundRepository.FindAsync(eventData.Entity.Id); if (refund == null) diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.EntityFrameworkCore/EasyAbp/EShop/Payments/Refunds/RefundRepository.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.EntityFrameworkCore/EasyAbp/EShop/Payments/Refunds/RefundRepository.cs index ba552bfd..ab680473 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.EntityFrameworkCore/EasyAbp/EShop/Payments/Refunds/RefundRepository.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.EntityFrameworkCore/EasyAbp/EShop/Payments/Refunds/RefundRepository.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using EasyAbp.EShop.Payments.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; @@ -12,6 +13,11 @@ namespace EasyAbp.EShop.Payments.Refunds { } + public override IQueryable WithDetails() + { + return base.WithDetails().Include(x => x.RefundItems); + } + public IQueryable GetQueryableByUserId(Guid userId) { return from refund in DbContext.Refunds diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs index e797b8f0..3331cbe2 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderCreatedEventHandler.cs @@ -32,12 +32,11 @@ namespace EasyAbp.EShop.Products.Products _productManager = productManager; } + [UnitOfWork(true)] public virtual async Task HandleEventAsync(EntityCreatedEto eventData) { using var changeTenant = _currentTenant.Change(eventData.Entity.TenantId); - using var uow = _unitOfWorkManager.Begin(isTransactional: true); - var models = new List(); foreach (var orderLine in eventData.Entity.OrderLines) @@ -84,14 +83,14 @@ namespace EasyAbp.EShop.Products.Products continue; } - await uow.RollbackAsync(); + await _unitOfWorkManager.Current.RollbackAsync(); await PublishResultEventAsync(eventData, false); return; } - await uow.CompleteAsync(); + await _unitOfWorkManager.Current.CompleteAsync(); await PublishResultEventAsync(eventData, true); } diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderPaidEventHandler.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderPaidEventHandler.cs index 11a76d58..abad9812 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderPaidEventHandler.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/OrderPaidEventHandler.cs @@ -31,12 +31,11 @@ namespace EasyAbp.EShop.Products.Products _productManager = productManager; } + [UnitOfWork(true)] public virtual async Task HandleEventAsync(OrderPaidEto eventData) { using var changeTenant = _currentTenant.Change(eventData.Order.TenantId); - using var uow = _unitOfWorkManager.Begin(isTransactional: true); - var models = new List(); foreach (var orderLine in eventData.Order.OrderLines) @@ -83,14 +82,14 @@ namespace EasyAbp.EShop.Products.Products continue; } - await uow.RollbackAsync(); + await _unitOfWorkManager.Current.RollbackAsync(); await PublishResultEventAsync(eventData, false); return; } - await uow.CompleteAsync(); + await _unitOfWorkManager.Current.CompleteAsync(); await PublishResultEventAsync(eventData, true); }