Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,6 +50,12 @@ public async Task<IList<string>> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,6 +38,7 @@ public void Init()
_orderMessageProviderServiceMock = new Mock<IOrderMessageProviderService>();
_paymentServiceMock = new Mock<IPaymentService>();
_loggerMock = new Mock<ILogger<RefundCommandHandler>>();
_mediatorMock.Setup(x => x.Send(It.IsAny<CanRefundQuery>(), default)).ReturnsAsync(true);

_handler = new RefundCommandHandler(_mediatorMock.Object, _paymentServiceMock.Object,
_paymentTransactionMock.Object, _orderServiceMock.Object, _orderMessageProviderServiceMock.Object,
Expand Down Expand Up @@ -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<CanRefundQuery>(), default)).ReturnsAsync(false);

//Act & Assert
await Assert.ThrowsExactlyAsync<GrandException>(() =>
_handler.Handle(new RefundCommand { PaymentTransaction = transaction }, CancellationToken.None));
_paymentServiceMock.Verify(x => x.Refund(It.IsAny<RefundPaymentRequest>()), Times.Never);
_paymentTransactionMock.Verify(x => x.UpdatePaymentTransaction(It.IsAny<PaymentTransaction>()), Times.Never);
}

[TestMethod]
public async Task Handle_ProviderRefuses_RecordsNothing()
{
Expand Down
Loading