From 1f9cb6f2a004f119c230ef0b33f807c550d28ab1 Mon Sep 17 00:00:00 2001 From: support Date: Sun, 22 Nov 2026 20:37:27 +0100 Subject: [PATCH] Refuse a full refund once part of the payment has been refunded The full refund command refunded and recorded the whole paid amount without checking the transaction. After a partial refund, a full refund posted from the payment transaction screen counted the paid amount on top of what was already refunded, and providers without their own check refunded it again. The command now asks CanRefundQuery first, as the partial and offline refunds already ask their queries, and stops with an error when the refund is not allowed. --- .../Handlers/Orders/RefundCommandHandler.cs | 8 ++++++++ .../Handlers/Orders/RefundCommandHandlerTests.cs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/RefundCommandHandler.cs b/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/RefundCommandHandler.cs index 8d467a832..9ba23927b 100644 --- a/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/RefundCommandHandler.cs +++ b/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/RefundCommandHandler.cs @@ -3,9 +3,11 @@ using Grand.Business.Core.Interfaces.Checkout.Orders; using Grand.Business.Core.Interfaces.Checkout.Payments; using Grand.Business.Core.Interfaces.Messages; +using Grand.Business.Core.Queries.Checkout.Orders; using Grand.Business.Core.Utilities.Checkout; using Grand.Domain.Localization; using Grand.Domain.Payments; +using Grand.SharedKernel; using Grand.Mediator; using Microsoft.Extensions.Logging; @@ -48,6 +50,12 @@ public async Task> Handle(RefundCommand command, CancellationToken var paymentTransaction = command.PaymentTransaction; ArgumentNullException.ThrowIfNull(paymentTransaction); + //the whole paid amount is refunded, so nothing may have been refunded before + var canRefund = await _mediator.Send(new CanRefundQuery { PaymentTransaction = paymentTransaction }, + cancellationToken); + if (!canRefund) + throw new GrandException("You can't refund this payment transaction"); + var request = new RefundPaymentRequest(); RefundPaymentResult result = null; try diff --git a/src/Tests/Grand.Business.Checkout.Tests/Commands/Handlers/Orders/RefundCommandHandlerTests.cs b/src/Tests/Grand.Business.Checkout.Tests/Commands/Handlers/Orders/RefundCommandHandlerTests.cs index b83592891..5841d910e 100644 --- a/src/Tests/Grand.Business.Checkout.Tests/Commands/Handlers/Orders/RefundCommandHandlerTests.cs +++ b/src/Tests/Grand.Business.Checkout.Tests/Commands/Handlers/Orders/RefundCommandHandlerTests.cs @@ -9,6 +9,7 @@ using Grand.Domain.Orders; using Grand.Domain.Payments; using Grand.Mediator; +using Grand.SharedKernel; using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -37,6 +38,7 @@ public void Init() _orderMessageProviderServiceMock = new Mock(); _paymentServiceMock = new Mock(); _loggerMock = new Mock>(); + _mediatorMock.Setup(x => x.Send(It.IsAny(), default)).ReturnsAsync(true); _handler = new RefundCommandHandler(_mediatorMock.Object, _paymentServiceMock.Object, _paymentTransactionMock.Object, _orderServiceMock.Object, _orderMessageProviderServiceMock.Object, @@ -80,6 +82,20 @@ await _handler.Handle(new RefundCommand { PaymentTransaction = transaction, Crea "staff@shop.com", null, null), Times.Once); } + [TestMethod] + public async Task Handle_CannotRefund_Throws_AndRefundsNothing() + { + //Arrange: part of the payment was refunded already, so a full refund would count it twice + var transaction = new PaymentTransaction { TransactionAmount = 100, PaidAmount = 100, RefundedAmount = 30 }; + _mediatorMock.Setup(x => x.Send(It.IsAny(), default)).ReturnsAsync(false); + + //Act & Assert + await Assert.ThrowsExactlyAsync(() => + _handler.Handle(new RefundCommand { PaymentTransaction = transaction }, CancellationToken.None)); + _paymentServiceMock.Verify(x => x.Refund(It.IsAny()), Times.Never); + _paymentTransactionMock.Verify(x => x.UpdatePaymentTransaction(It.IsAny()), Times.Never); + } + [TestMethod] public async Task Handle_ProviderRefuses_RecordsNothing() {