Do not crash when refunding an invoice that has been marked settled (Fix #6003) (#6086)

This commit is contained in:
Nicolas Dorier
2024-07-04 16:43:30 +09:00
committed by GitHub
parent 4a2f61de9f
commit 247532e3c4
4 changed files with 57 additions and 19 deletions

View File

@@ -302,12 +302,7 @@ namespace BTCPayServer.Controllers
// Find the most similar payment method to the one used for the invoice
var defaultRefund =
PaymentMethodId.GetSimilarities(
invoice.Payments.Select(o => o.GetPaymentMethodId()),
payoutMethodIds)
.OrderByDescending(o => o.similarity)
.Select(o => o.b)
.FirstOrDefault();
invoice.GetClosestPayoutMethodId(payoutMethodIds);
var refund = new RefundModel
{
@@ -353,11 +348,7 @@ namespace BTCPayServer.Controllers
return View("_RefundModal", model);
}
var availablePaymentMethodIds = invoice.GetPaymentPrompts().Select(p => p.PaymentMethodId).Where(p => _handlers.Support(p)).ToArray();
var paymentMethodId = PaymentMethodId.GetSimilarities([pmi], availablePaymentMethodIds)
.OrderByDescending(o => o.similarity)
.Select(o => o.b)
.FirstOrDefault();
var paymentMethodId = invoice.GetClosestPaymentMethodId([pmi]);
var paymentMethod = paymentMethodId is null ? null : invoice.GetPaymentPrompt(paymentMethodId);
if (paymentMethod?.Currency is null)
@@ -367,8 +358,16 @@ namespace BTCPayServer.Controllers
}
var accounting = paymentMethod.Calculate();
decimal cryptoPaid = accounting.Paid;
decimal dueAmount = accounting.TotalDue;
var cryptoPaid = accounting.Paid;
var dueAmount = accounting.TotalDue;
// If no payment, but settled and marked, assume it has been fully paid
if (cryptoPaid is 0 && invoice is { Status: InvoiceStatus.Settled, ExceptionStatus: InvoiceExceptionStatus.Marked })
{
cryptoPaid = accounting.TotalDue;
dueAmount = 0;
}
var paymentMethodCurrency = paymentMethod.Currency;
var isPaidOver = invoice.ExceptionStatus == InvoiceExceptionStatus.PaidOver;