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);