Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

feat(arch001): add partial refund/paid popups and Delete region, fix RefreshPage bug

KrzysztofPajak81e79ceec0c3
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePaymentTransactionControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePaymentTransactionControllerTests.cs
index 6b072024f..5364764e3 100644
--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePaymentTransactionControllerTests.cs
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePaymentTransactionControllerTests.cs
@@ -12,8 +12,12 @@ using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Orders;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Localization;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
+using Microsoft.AspNetCore.Mvc.Routing;
+using Microsoft.AspNetCore.Mvc.ViewFeatures;
+using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
@@ -69,6 +73,21 @@ public class BasePaymentTransactionControllerTests
_controller = new TestPaymentTransactionController(
_paymentTransactionServiceMock.Object, _orderServiceMock.Object, translationServiceMock.Object,
dateTimeServiceMock.Object, _mediatorMock.Object, enumTranslationServiceMock.Object, _scopeMock.Object);
+
+ // Error()/Success() (used by this task's popup and Delete actions) reach into
+ // HttpContext.RequestServices for ILoggerFactory and into TempData - wire up the same
+ // minimal harness other Base*ControllerTests use (see BaseOrderControllerTests.Setup).
+ var httpContext = new DefaultHttpContext();
+ var loggerFactoryMock = new Mock<ILoggerFactory>();
+ loggerFactoryMock.Setup(l => l.CreateLogger(It.IsAny<string>())).Returns(new Mock<ILogger>().Object);
+ var urlHelperFactoryMock = new Mock<IUrlHelperFactory>();
+ urlHelperFactoryMock.Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>())).Returns(new Mock<IUrlHelper>().Object);
+ var requestServicesMock = new Mock<IServiceProvider>();
+ requestServicesMock.Setup(s => s.GetService(typeof(ILoggerFactory))).Returns(loggerFactoryMock.Object);
+ requestServicesMock.Setup(s => s.GetService(typeof(IUrlHelperFactory))).Returns(urlHelperFactoryMock.Object);
+ httpContext.RequestServices = requestServicesMock.Object;
+ _controller.ControllerContext = new ControllerContext { HttpContext = httpContext };
+ _controller.TempData = new TempDataDictionary(httpContext, new Mock<ITempDataProvider>().Object);
}
[TestMethod]
@@ -189,4 +208,73 @@ public class BasePaymentTransactionControllerTests
Assert.AreEqual("Edit", result.ActionName);
Assert.AreEqual("pt-1", result.RouteValues["id"]);
}
+
+ [TestMethod]
+ public async Task PartiallyRefundPopup_Post_Success_SetsModelRefreshPage()
+ {
+ var transaction = new PaymentTransaction { Id = "pt-1", TransactionAmount = 100, RefundedAmount = 0 };
+ _paymentTransactionServiceMock.Setup(s => s.GetById("pt-1")).ReturnsAsync(transaction);
+ _mediatorMock.Setup(m => m.Send(It.IsAny<PartiallyRefundOfflineCommand>(), default)).ReturnsAsync(true);
+
+ var model = new PaymentTransactionModel { AmountToRefund = 50 };
+ var result = await _controller.PartiallyRefundPopup("pt-1", false, model) as ViewResult;
+
+ Assert.IsNotNull(result);
+ var resultModel = result.Model as PaymentTransactionModel;
+ Assert.IsTrue(resultModel.RefreshPage);
+ }
+
+ [TestMethod]
+ public async Task PartiallyRefundPopup_Post_ZeroAmount_ErrorsAndDoesNotSetRefreshPage()
+ {
+ var transaction = new PaymentTransaction { Id = "pt-1", TransactionAmount = 100, RefundedAmount = 0 };
+ _paymentTransactionServiceMock.Setup(s => s.GetById("pt-1")).ReturnsAsync(transaction);
+
+ var model = new PaymentTransactionModel { AmountToRefund = 0 };
+ var result = await _controller.PartiallyRefundPopup("pt-1", false, model) as ViewResult;
+
+ Assert.IsNotNull(result);
+ var resultModel = result.Model as PaymentTransactionModel;
+ Assert.IsFalse(resultModel.RefreshPage);
+ }
+
+ [TestMethod]
+ public async Task PartiallyPaidPopup_Post_Success_SetsModelRefreshPage()
+ {
+ var transaction = new PaymentTransaction { Id = "pt-1", TransactionAmount = 100, PaidAmount = 0 };
+ _paymentTransactionServiceMock.Setup(s => s.GetById("pt-1")).ReturnsAsync(transaction);
+ _mediatorMock.Setup(m => m.Send(It.IsAny<PartiallyPaidOfflineCommand>(), default)).ReturnsAsync(true);
+
+ var model = new PaymentTransactionModel { AmountToPaid = 50 };
+ var result = await _controller.PartiallyPaidPopup("pt-1", false, model) as ViewResult;
+
+ Assert.IsNotNull(result);
+ var resultModel = result.Model as PaymentTransactionModel;
+ Assert.IsTrue(resultModel.RefreshPage);
+ }
+
+ [TestMethod]
+ public async Task Delete_DeniedByScope_RedirectsToList()
+ {
+ _paymentTransactionServiceMock.Setup(s => s.GetById("pt-1")).ReturnsAsync(new PaymentTransaction { Id = "pt-1" });
+ _scopeMock.Setup(s => s.HasAccess(It.IsAny<PaymentTransaction>())).ReturnsAsync(false);
+
+ var result = await _controller.Delete("pt-1") as RedirectToActionResult;
+
+ Assert.IsNotNull(result);
+ Assert.AreEqual("List", result.ActionName);
+ }
+
+ [TestMethod]
+ public async Task Delete_Authorized_DeletesAndRedirectsToList()
+ {
+ var transaction = new PaymentTransaction { Id = "pt-1" };
+ _paymentTransactionServiceMock.Setup(s => s.GetById("pt-1")).ReturnsAsync(transaction);
+
+ var result = await _controller.Delete("pt-1") as RedirectToActionResult;
+
+ Assert.IsNotNull(result);
+ Assert.AreEqual("List", result.ActionName);
+ _paymentTransactionServiceMock.Verify(s => s.DeletePaymentTransaction(transaction), Times.Once);
+ }
}
diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BasePaymentTransactionController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BasePaymentTransactionController.cs
index 992edcad7..4e833f5f0 100644
--- a/src/Web/Grand.Web.AdminShared/Controllers/BasePaymentTransactionController.cs
+++ b/src/Web/Grand.Web.AdminShared/Controllers/BasePaymentTransactionController.cs
@@ -7,6 +7,7 @@ using Grand.Business.Core.Queries.Checkout.Orders;
using Grand.Domain.Payments;
using Grand.Domain.Permissions;
using Grand.Mediator;
+using Grand.SharedKernel;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Orders;
using Grand.Web.Common.Controllers;
@@ -339,4 +340,136 @@ public abstract class BasePaymentTransactionController(
}
#endregion
+
+ #region Partial refund / paid popups
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ public async Task<IActionResult> PartiallyRefundPopup(string id, bool online)
+ {
+ var (paymentTransaction, denied) = await LoadAuthorizedPaymentTransaction(id);
+ if (denied != null) return denied;
+
+ var model = new PaymentTransactionModel {
+ Id = paymentTransaction.Id,
+ MaxAmountToRefund = paymentTransaction.TransactionAmount - paymentTransaction.RefundedAmount,
+ CurrencyCode = paymentTransaction.CurrencyCode
+ };
+
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ public async Task<IActionResult> PartiallyRefundPopup(string id, bool online, PaymentTransactionModel model)
+ {
+ var (paymentTransaction, denied) = await LoadAuthorizedPaymentTransaction(id);
+ if (denied != null) return denied;
+
+ try
+ {
+ var amountToRefund = model.AmountToRefund;
+ if (amountToRefund <= 0)
+ throw new GrandException("Enter amount to refund");
+
+ var maxAmountToRefund = paymentTransaction.TransactionAmount - paymentTransaction.RefundedAmount;
+ if (amountToRefund > maxAmountToRefund)
+ amountToRefund = maxAmountToRefund;
+
+ var errors = new List<string>();
+ if (online)
+ errors = (await mediator.Send(new PartiallyRefundCommand { PaymentTransaction = paymentTransaction, AmountToRefund = amountToRefund })).ToList();
+ else
+ await mediator.Send(new PartiallyRefundOfflineCommand { PaymentTransaction = paymentTransaction, AmountToRefund = amountToRefund });
+
+ if (errors.Count == 0)
+ {
+ //success — Model.RefreshPage, read by both hosts' PartiallyRefundPopup.cshtml script
+ //block after this task's view migration (Task 5). Admin's pre-consolidation
+ //controller set ViewBag.RefreshPage here instead, which its own view never read
+ //(a dead write, since the view already checked Model.RefreshPage) — deliberate bug
+ //fix, unifying onto Store's already-correct behavior. See spec §2.
+ model.RefreshPage = true;
+ return View(model);
+ }
+
+ foreach (var error in errors)
+ Error(error);
+
+ return View(model);
+ }
+ catch (Exception exc)
+ {
+ Error(exc, false);
+ return View(model);
+ }
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ public async Task<IActionResult> PartiallyPaidPopup(string id, bool online)
+ {
+ var (paymentTransaction, denied) = await LoadAuthorizedPaymentTransaction(id);
+ if (denied != null) return denied;
+
+ var model = new PaymentTransactionModel {
+ Id = paymentTransaction.Id,
+ MaxAmountToPaid = paymentTransaction.TransactionAmount - paymentTransaction.PaidAmount,
+ CurrencyCode = paymentTransaction.CurrencyCode
+ };
+
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ public async Task<IActionResult> PartiallyPaidPopup(string id, bool online, PaymentTransactionModel model)
+ {
+ var (paymentTransaction, denied) = await LoadAuthorizedPaymentTransaction(id);
+ if (denied != null) return denied;
+
+ try
+ {
+ var amountToPaid = model.AmountToPaid;
+ if (amountToPaid <= 0)
+ throw new GrandException("Enter amount to refund");
+
+ var maxAmountToPaid = paymentTransaction.TransactionAmount - paymentTransaction.PaidAmount;
+ if (amountToPaid > maxAmountToPaid)
+ amountToPaid = maxAmountToPaid;
+
+ await mediator.Send(new PartiallyPaidOfflineCommand { PaymentTransaction = paymentTransaction, AmountToPaid = amountToPaid });
+
+ //same RefreshPage fix as PartiallyRefundPopup above.
+ model.RefreshPage = true;
+ return View(model);
+ }
+ catch (Exception exc)
+ {
+ Error(exc, false);
+ return View(model);
+ }
+ }
+
+ #endregion
+
+ #region Delete
+
+ [PermissionAuthorizeAction(PermissionActionName.Delete)]
+ [HttpPost]
+ public async Task<IActionResult> Delete(string id)
+ {
+ var (paymentTransaction, denied) = await LoadAuthorizedPaymentTransaction(id);
+ if (denied != null) return denied;
+
+ if (ModelState.IsValid)
+ {
+ await paymentTransactionService.DeletePaymentTransaction(paymentTransaction);
+ Success(translationService.GetResource("Admin.Orders.PaymentTransaction.Deleted"));
+ return RedirectToAction("List", "PaymentTransaction");
+ }
+
+ Error(ModelState);
+ return RedirectToAction("Edit", new { id = paymentTransaction.Id });
+ }
+
+ #endregion
}