diff --git a/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs b/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs index e6ae0fee..edf02f6b 100644 --- a/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs +++ b/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs @@ -339,7 +339,9 @@ public async Task RefundPurchase(int paymentId) throw new IllegalUserOperationException($"Purchase {purchase.Id} is not in state Completed. Cannot refund"); } - var refundSuccess = await _mobilePayPaymentsService.RefundPayment(Guid.Parse(purchase.ExternalTransactionId)); + // MobilePay requires the amount to be in oere, we store the amount in kroner + var amountToRefund = purchase.Price * 100; + var refundSuccess = await _mobilePayPaymentsService.RefundPayment(purchase, amountToRefund); if (!refundSuccess) { Log.Error("Refund of Purchase {PurchaseId} failed", purchase.Id); diff --git a/coffeecard/CoffeeCard.MobilePay/Service/v2/IMobilePayPaymentsService.cs b/coffeecard/CoffeeCard.MobilePay/Service/v2/IMobilePayPaymentsService.cs index a960594b..fb2abc2f 100644 --- a/coffeecard/CoffeeCard.MobilePay/Service/v2/IMobilePayPaymentsService.cs +++ b/coffeecard/CoffeeCard.MobilePay/Service/v2/IMobilePayPaymentsService.cs @@ -5,6 +5,7 @@ using CoffeeCard.MobilePay.Generated.Api.WebhooksApi; using CoffeeCard.Models.DataTransferObjects.v2.MobilePay; using CoffeeCard.Models.DataTransferObjects.v2.Purchase; +using CoffeeCard.Models.Entities; namespace CoffeeCard.MobilePay.Service.v2 { @@ -47,6 +48,12 @@ public interface IMobilePayPaymentsService /// All Payment Points Task GetPaymentPoints(); - Task RefundPayment(Guid paymentId); + /// + /// Refund a payment to the customer + /// + /// Purchase to refund + /// Amount to refund in oere + /// True if refund was successful, false otherwise + Task RefundPayment(Purchase purchase, int amount); } } \ No newline at end of file diff --git a/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs b/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs index 7fa166b5..6a08fc82 100644 --- a/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs +++ b/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs @@ -5,6 +5,7 @@ using CoffeeCard.MobilePay.Generated.Api.PaymentsApi; using CoffeeCard.Models.DataTransferObjects.v2.MobilePay; using CoffeeCard.Models.DataTransferObjects.v2.Purchase; +using CoffeeCard.Models.Entities; using Serilog; namespace CoffeeCard.MobilePay.Service.v2 @@ -84,13 +85,18 @@ public async Task GetPayment(Guid paymentId) } } - public async Task RefundPayment(Guid paymentId) + public async Task RefundPayment(Purchase purchase, int amount) { + if (purchase == null || purchase.ExternalTransactionId == null) throw new ArgumentNullException(nameof(purchase)); try { IssueRefundRequest issueRefundRequest = new IssueRefundRequest { - PaymentId = paymentId + PaymentId = Guid.Parse(purchase.ExternalTransactionId), + Amount = amount, + Description = "Refund of purchase " + purchase.Id, + IdempotencyKey = Guid.NewGuid(), + Reference = purchase.OrderId.ToString() }; try { diff --git a/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs b/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs index f2a42968..6b02f019 100644 --- a/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs +++ b/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs @@ -312,7 +312,7 @@ public async Task RefundPurchaseRefundsAPurchase(Product product) await context.SaveChangesAsync(); var mobilePayService = new Mock(); - mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny())) + mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny(), It.IsAny())) .ReturnsAsync(true); var mailService = new Mock(); var productService = new ProductService(context); @@ -378,7 +378,7 @@ public async Task RefundPurchaseThrowsExceptionWhenNotAllowed(Product product) await context.SaveChangesAsync(); var mobilePayService = new Mock(); - mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny())) + mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny(), It.IsAny())) .ReturnsAsync(true); var mailService = new Mock(); var productService = new ProductService(context); @@ -442,7 +442,7 @@ public async Task RefundPurchaseThrowsExceptionWhenAlreadyRefunded(Product produ await context.SaveChangesAsync(); var mobilePayService = new Mock(); - mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny())) + mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny(), It.IsAny())) .ReturnsAsync(true); var mailService = new Mock(); var productService = new ProductService(context);