From d73e26e0c49b6eaa097be2dd639e0ba727c8c270 Mon Sep 17 00:00:00 2001 From: d11n Date: Wed, 10 Jul 2024 17:11:04 +0200 Subject: [PATCH] Fix null pointer exception on receipt print page (#6045) (#6085) --- BTCPayServer.Tests/SeleniumTests.cs | 49 +++++++++++++++- .../Views/UIInvoice/InvoiceReceipt.cshtml | 2 +- .../UIInvoice/InvoiceReceiptPrint.cshtml | 56 +++++++++---------- 3 files changed, 77 insertions(+), 30 deletions(-) diff --git a/BTCPayServer.Tests/SeleniumTests.cs b/BTCPayServer.Tests/SeleniumTests.cs index 4f6db53ac..8673364aa 100644 --- a/BTCPayServer.Tests/SeleniumTests.cs +++ b/BTCPayServer.Tests/SeleniumTests.cs @@ -2696,6 +2696,33 @@ namespace BTCPayServer.Tests Assert.Contains("Total", sums[3].FindElement(By.CssSelector("th")).Text); Assert.Contains("1 222,21 €", sums[3].FindElement(By.CssSelector("td")).Text); + // Receipt print + s.Driver.FindElement(By.Id("ReceiptLinkPrint")).Click(); + windows = s.Driver.WindowHandles; + Assert.Equal(3, windows.Count); + s.Driver.SwitchTo().Window(windows[2]); + var paymentDetails = s.Driver.WaitForElement(By.CssSelector("#PaymentDetails table")); + items = paymentDetails.FindElements(By.CssSelector("tr.cart-data")); + sums = paymentDetails.FindElements(By.CssSelector("tr.sums-data")); + Assert.Equal(2, items.Count); + Assert.Equal(4, sums.Count); + Assert.Contains("Manual entry 1", items[0].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("1 234,00 €", items[0].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Manual entry 2", items[1].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("0,56 €", items[1].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Subtotal", sums[0].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("1 234,56 €", sums[0].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Discount", sums[1].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("10% = 123,46 €", sums[1].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Tip", sums[2].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("10% = 111,11 €", sums[2].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Total", sums[3].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("1 222,21 €", sums[3].FindElement(By.CssSelector(".val")).Text); + s.Driver.Close(); + s.Driver.SwitchTo().Window(windows[1]); + s.Driver.Close(); + s.Driver.SwitchTo().Window(s.Driver.WindowHandles.First()); + // Once more with items s.GoToUrl(editUrl); s.Driver.FindElement(By.Id("ShowItems")).Click(); @@ -2733,7 +2760,6 @@ namespace BTCPayServer.Tests // Receipt s.Driver.WaitForElement(By.Id("ReceiptLink")).Click(); - cartData = s.Driver.FindElement(By.CssSelector("#CartData table")); items = cartData.FindElements(By.CssSelector("tbody tr")); sums = cartData.FindElements(By.CssSelector("tfoot tr")); @@ -2748,6 +2774,27 @@ namespace BTCPayServer.Tests Assert.Contains("Total", sums[0].FindElement(By.CssSelector("th")).Text); Assert.Contains("4,23 €", sums[0].FindElement(By.CssSelector("td")).Text); + // Receipt print + s.Driver.FindElement(By.Id("ReceiptLinkPrint")).Click(); + windows = s.Driver.WindowHandles; + Assert.Equal(2, windows.Count); + s.Driver.SwitchTo().Window(windows[1]); + paymentDetails = s.Driver.WaitForElement(By.CssSelector("#PaymentDetails table")); + items = paymentDetails.FindElements(By.CssSelector("tr.cart-data")); + sums = paymentDetails.FindElements(By.CssSelector("tr.sums-data")); + Assert.Equal(3, items.Count); + Assert.Single(sums); + Assert.Contains("Black Tea", items[0].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("1 x 1,00 € = 1,00 €", items[0].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Green Tea", items[1].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("2 x 1,00 € = 2,00 €", items[1].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Manual entry 1", items[2].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("1,23 €", items[2].FindElement(By.CssSelector(".val")).Text); + Assert.Contains("Total", sums[0].FindElement(By.CssSelector(".key")).Text); + Assert.Contains("4,23 €", sums[0].FindElement(By.CssSelector(".val")).Text); + s.Driver.Close(); + s.Driver.SwitchTo().Window(s.Driver.WindowHandles.First()); + // Guest user can access recent transactions s.GoToHome(); s.Logout(); diff --git a/BTCPayServer/Views/UIInvoice/InvoiceReceipt.cshtml b/BTCPayServer/Views/UIInvoice/InvoiceReceipt.cshtml index 3dc58d4c6..e3d459b82 100644 --- a/BTCPayServer/Views/UIInvoice/InvoiceReceipt.cshtml +++ b/BTCPayServer/Views/UIInvoice/InvoiceReceipt.cshtml @@ -82,7 +82,7 @@ } - Print + Print } diff --git a/BTCPayServer/Views/UIInvoice/InvoiceReceiptPrint.cshtml b/BTCPayServer/Views/UIInvoice/InvoiceReceiptPrint.cshtml index 10e9f9c7d..2f8e96d8b 100644 --- a/BTCPayServer/Views/UIInvoice/InvoiceReceiptPrint.cshtml +++ b/BTCPayServer/Views/UIInvoice/InvoiceReceiptPrint.cshtml @@ -104,7 +104,7 @@ { @foreach (var (key, value) in Model.AdditionalData) { - + @key @value @@ -113,20 +113,20 @@
} - @if (hasCart) + @if (hasCart) { _ = Model.CartData.TryGetValue("cart", out var cart) || Model.CartData.TryGetValue("Cart", out cart); var hasTotal = Model.CartData.TryGetValue("total", out var total) || Model.CartData.TryGetValue("Total", out total); var hasSubtotal = Model.CartData.TryGetValue("subtotal", out var subtotal) || Model.CartData.TryGetValue("subTotal", out subtotal) || Model.CartData.TryGetValue("Subtotal", out subtotal); var hasDiscount = Model.CartData.TryGetValue("discount", out var discount) || Model.CartData.TryGetValue("Discount", out discount); var hasTip = Model.CartData.TryGetValue("tip", out var tip) || Model.CartData.TryGetValue("Tip", out tip); - if (cart is Dictionary { Keys.Count: > 0 } cartDict) + if (cart is Dictionary { Keys.Count: > 0 } cartDict) { @foreach (var (key, value) in cartDict) { - - @key - @value + + @key + @value } } @@ -135,7 +135,7 @@ @foreach (var value in cartCollection) { - @value + @value } } @@ -144,23 +144,23 @@
- - Subtotal - @subtotal + + Subtotal + @subtotal } if (hasDiscount) { - - Discount - @discount + + Discount + @discount } if (hasTip) { - - Tip - @tip + + Tip + @tip } if (hasTotal) @@ -168,17 +168,17 @@
- - Total - @total + + Total + @total } } else { - - Total - @DisplayFormatter.Currency(Model.Amount, Model.Currency, DisplayFormatter.CurrencyFormat.Symbol) + + Total + @DisplayFormatter.Currency(Model.Amount, Model.Currency, DisplayFormatter.CurrencyFormat.Symbol) } @if (Model.Payments?.Any() is true) @@ -194,25 +194,25 @@ Payment @(i + 1) - + Received @payment.ReceivedDate.ToBrowserDate() } - + @(Model.Payments.Count == 1 ? "Paid" : "") @payment.AmountFormatted - + @payment.PaidFormatted - + Rate @payment.RateFormatted @if (!string.IsNullOrEmpty(payment.Destination)) { - + Destination @if (payment.Destination.Length > 69) @@ -232,7 +232,7 @@ } @if (!string.IsNullOrEmpty(payment.PaymentProof)) { - + Pay Proof @payment.PaymentProof