diff --git a/BTCPayServer/Controllers/PaymentRequestController.cs b/BTCPayServer/Controllers/PaymentRequestController.cs index 39743c9a5..cedf4309d 100644 --- a/BTCPayServer/Controllers/PaymentRequestController.cs +++ b/BTCPayServer/Controllers/PaymentRequestController.cs @@ -233,17 +233,22 @@ namespace BTCPayServer.Controllers return BadRequest("Payment Request has expired"); } - var statusesAllowedToDisplay = new List() { InvoiceStatus.New }; - var validInvoice = result.Invoices.FirstOrDefault(invoice => statusesAllowedToDisplay.Contains(invoice.Status)); - - if (validInvoice != null) + var stateAllowedToDisplay = new HashSet() + { + new InvoiceState(InvoiceStatus.New, InvoiceExceptionStatus.None), + new InvoiceState(InvoiceStatus.New, InvoiceExceptionStatus.PaidPartial), + }; + var currentInvoice = result + .Invoices + .FirstOrDefault(invoice => stateAllowedToDisplay.Contains(invoice.State)); + if (currentInvoice != null) { if (redirectToInvoice) { - return RedirectToAction("Checkout", "Invoice", new { Id = validInvoice.Id }); + return RedirectToAction("Checkout", "Invoice", new { Id = currentInvoice.Id }); } - return Ok(validInvoice.Id); + return Ok(currentInvoice.Id); } if (result.AllowCustomPaymentAmounts && amount != null) @@ -295,8 +300,7 @@ namespace BTCPayServer.Controllers } var invoices = result.Invoices.Where(requestInvoice => - requestInvoice.StatusFormatted.Equals(InvoiceState.ToString(InvoiceStatus.New), - StringComparison.InvariantCulture) && !requestInvoice.Payments.Any()); + requestInvoice.State.Status == InvoiceStatus.New && !requestInvoice.Payments.Any()); if (!invoices.Any()) { diff --git a/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs b/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs index a3105cd21..42fb5241d 100644 --- a/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs +++ b/BTCPayServer/Models/PaymentRequestViewModels/ListPaymentRequestsViewModel.cs @@ -143,7 +143,7 @@ namespace BTCPayServer.Models.PaymentRequestViewModels public string AmountFormatted { get; set; } public InvoiceState State { get; set; } public InvoiceStatus Status { get; set; } - public string StatusFormatted { get; set; } + public string StateFormatted { get; set; } public List Payments { get; set; } public string Currency { get; set; } diff --git a/BTCPayServer/PaymentRequest/PaymentRequestService.cs b/BTCPayServer/PaymentRequest/PaymentRequestService.cs index 889435199..59ab8819a 100644 --- a/BTCPayServer/PaymentRequest/PaymentRequestService.cs +++ b/BTCPayServer/PaymentRequest/PaymentRequestService.cs @@ -109,8 +109,7 @@ namespace BTCPayServer.PaymentRequest Currency = entity.Currency, ExpiryDate = entity.ExpirationTime.DateTime, State = state, - Status = state.Status, - StatusFormatted = state.ToString(), + StateFormatted = state.ToString(), Payments = entity .GetPayments() .Select(paymentEntity => diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index cd80d7d31..b613e4163 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -826,6 +826,40 @@ namespace BTCPayServer.Services.Invoices (Status != InvoiceStatus.Invalid && ExceptionStatus == InvoiceExceptionStatus.Marked); #pragma warning restore CA1305 // Specify IFormatProvider; } + + public override int GetHashCode() + { + return HashCode.Combine(Status, ExceptionStatus); + } + + public static bool operator ==(InvoiceState a, InvoiceState b) + { + if (a is null && b is null) + return true; + if (a is null) + return false; + return a.Equals(b); + } + + public static bool operator !=(InvoiceState a, InvoiceState b) + { + return !(a == b); + } + + public bool Equals(InvoiceState o) + { + if (o is null) + return false; + return o.Status == Status && o.ExceptionStatus == ExceptionStatus; + } + public override bool Equals(object obj) + { + if (obj is InvoiceState o) + { + return this.Equals(o); + } + return false; + } public override string ToString() { return ToString(Status) + (ExceptionStatus == InvoiceExceptionStatus.None ? string.Empty : $" ({ToString(ExceptionStatus)})"); diff --git a/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml b/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml index d97edadee..4e9dba7b9 100644 --- a/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml +++ b/BTCPayServer/Views/PaymentRequest/ViewPaymentRequest.cshtml @@ -1,4 +1,4 @@ -@using BTCPayServer.Services.Invoices +@using BTCPayServer.Services.Invoices @using BTCPayServer.Client.Models @model BTCPayServer.Models.PaymentRequestViewModels.ViewPaymentRequestViewModel @@ -264,7 +264,7 @@ @invoice.ExpiryDate.ToString("g") @invoice.AmountFormatted - @invoice.StatusFormatted + @invoice.StateFormatted if (invoice.Payments != null && invoice.Payments.Any()) { @@ -322,7 +322,7 @@ {{invoice.amountFormatted}} - {{invoice.statusFormatted}} + {{invoice.stateFormatted}}