Skip to content

Commit

Permalink
(fix) Feature/refund endpoint (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredpetersen authored Sep 25, 2024
1 parent 6fc0181 commit 41d1a4a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
4 changes: 3 additions & 1 deletion coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ public async Task<SimplePurchaseResponse> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -47,6 +48,12 @@ public interface IMobilePayPaymentsService
/// <returns>All Payment Points</returns>
Task<PaymentPointsList> GetPaymentPoints();

Task<bool> RefundPayment(Guid paymentId);
/// <summary>
/// Refund a payment to the customer
/// </summary>
/// <param name="purchase">Purchase to refund</param>
/// <param name="amount">Amount to refund in oere</param>
/// <returns>True if refund was successful, false otherwise</returns>
Task<bool> RefundPayment(Purchase purchase, int amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,13 +85,18 @@ public async Task<MobilePayPaymentDetails> GetPayment(Guid paymentId)
}
}

public async Task<bool> RefundPayment(Guid paymentId)
public async Task<bool> 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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public async Task RefundPurchaseRefundsAPurchase(Product product)
await context.SaveChangesAsync();

var mobilePayService = new Mock<IMobilePayPaymentsService>();
mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny<Guid>()))
mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny<Purchase>(), It.IsAny<int>()))
.ReturnsAsync(true);
var mailService = new Mock<Library.Services.IEmailService>();
var productService = new ProductService(context);
Expand Down Expand Up @@ -378,7 +378,7 @@ public async Task RefundPurchaseThrowsExceptionWhenNotAllowed(Product product)
await context.SaveChangesAsync();

var mobilePayService = new Mock<IMobilePayPaymentsService>();
mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny<Guid>()))
mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny<Purchase>(), It.IsAny<int>()))
.ReturnsAsync(true);
var mailService = new Mock<Library.Services.IEmailService>();
var productService = new ProductService(context);
Expand Down Expand Up @@ -442,7 +442,7 @@ public async Task RefundPurchaseThrowsExceptionWhenAlreadyRefunded(Product produ
await context.SaveChangesAsync();

var mobilePayService = new Mock<IMobilePayPaymentsService>();
mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny<Guid>()))
mobilePayService.Setup(mps => mps.RefundPayment(It.IsAny<Purchase>(), It.IsAny<int>()))
.ReturnsAsync(true);
var mailService = new Mock<Library.Services.IEmailService>();
var productService = new ProductService(context);
Expand Down

0 comments on commit 41d1a4a

Please sign in to comment.