From 4f76ee3fc41644887c691203248c7c0a19ebb343 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Mon, 31 Aug 2020 15:15:03 +0800 Subject: [PATCH] Order.PaymentId should be set to null when the payment is canceled. Close #78 --- .../Orders/PaymentCanceledEventHandler.cs | 35 ++++++++++++++ .../Payments/EShopPaymentCanceledEto.cs | 10 ++++ .../Payments/Payments/PaymentSynchronizer.cs | 48 +++++++++++++------ 3 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/PaymentCanceledEventHandler.cs create mode 100644 modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/EShopPaymentCanceledEto.cs diff --git a/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/PaymentCanceledEventHandler.cs b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/PaymentCanceledEventHandler.cs new file mode 100644 index 00000000..c7963b14 --- /dev/null +++ b/modules/EasyAbp.EShop.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/PaymentCanceledEventHandler.cs @@ -0,0 +1,35 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using EasyAbp.EShop.Payments; +using EasyAbp.EShop.Payments.Payments; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Uow; + +namespace EasyAbp.EShop.Orders.Orders +{ + public class PaymentCanceledEventHandler : IDistributedEventHandler, ITransientDependency + { + private readonly IOrderRepository _orderRepository; + + public PaymentCanceledEventHandler(IOrderRepository orderRepository) + { + _orderRepository = orderRepository; + } + + [UnitOfWork(true)] + public virtual async Task HandleEventAsync(EShopPaymentCanceledEto eventData) + { + foreach (var paymentItem in eventData.Payment.PaymentItems.Where(item => + item.ItemType == PaymentsConsts.PaymentItemType)) + { + var order = await _orderRepository.GetAsync(Guid.Parse(paymentItem.ItemKey)); + + order.SetPaymentId(null); + + await _orderRepository.UpdateAsync(order, true); + } + } + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/EShopPaymentCanceledEto.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/EShopPaymentCanceledEto.cs new file mode 100644 index 00000000..cbd27968 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain.Shared/EasyAbp/EShop/Payments/Payments/EShopPaymentCanceledEto.cs @@ -0,0 +1,10 @@ +using System; + +namespace EasyAbp.EShop.Payments.Payments +{ + [Serializable] + public class EShopPaymentCanceledEto + { + public EShopPaymentEto Payment { get; set; } + } +} \ No newline at end of file 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 7f6662e4..3371ee38 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 @@ -68,16 +68,37 @@ namespace EasyAbp.EShop.Payments.Payments if (payment.CompletionTime.HasValue) { - _unitOfWorkManager.Current.OnCompleted(async () => + PublishPaymentCompletedEventOnUowCompleted(payment); + } + + if (payment.CanceledTime.HasValue) + { + PublishPaymentCanceledEventOnUowCompleted(payment); + } + } + + protected virtual void PublishPaymentCanceledEventOnUowCompleted(Payment payment) + { + _unitOfWorkManager.Current.OnCompleted(async () => + { + await _distributedEventBus.PublishAsync(new EShopPaymentCanceledEto { - await _distributedEventBus.PublishAsync(new EShopPaymentCompletedEto - { - Payment = _objectMapper.Map(payment) - }); + Payment = _objectMapper.Map(payment) }); - } + }); } - + + protected virtual void PublishPaymentCompletedEventOnUowCompleted(Payment payment) + { + _unitOfWorkManager.Current.OnCompleted(async () => + { + await _distributedEventBus.PublishAsync(new EShopPaymentCompletedEto + { + Payment = _objectMapper.Map(payment) + }); + }); + } + [UnitOfWork(true)] public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) { @@ -97,13 +118,12 @@ namespace EasyAbp.EShop.Payments.Payments if (eventData.Entity.CompletionTime.HasValue && !payment.CompletionTime.HasValue) { - _unitOfWorkManager.Current.OnCompleted(async () => - { - await _distributedEventBus.PublishAsync(new EShopPaymentCompletedEto - { - Payment = _objectMapper.Map(payment) - }); - }); + PublishPaymentCompletedEventOnUowCompleted(payment); + } + + if (eventData.Entity.CanceledTime.HasValue && !payment.CanceledTime.HasValue) + { + PublishPaymentCanceledEventOnUowCompleted(payment); } _objectMapper.Map(eventData.Entity, payment);