From 7ab97311be45bba60ff4029d44733f10130b8944 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Wed, 11 Sep 2019 07:49:06 +0200 Subject: [PATCH] Re-enable lightning sats feature through C# (#1014) --- BTCPayServer.Tests/CheckoutUITests.cs | 31 ++++++++++--------- BTCPayServer.Tests/SeleniumTester.cs | 8 +++++ .../Controllers/InvoiceController.UI.cs | 3 +- .../Bitcoin/BitcoinLikePaymentHandler.cs | 3 +- .../Payments/IPaymentMethodHandler.cs | 5 +-- .../Lightning/LightningLikePaymentHandler.cs | 13 ++++++-- BTCPayServer/Views/Invoice/Checkout.cshtml | 6 ---- 7 files changed, 41 insertions(+), 28 deletions(-) diff --git a/BTCPayServer.Tests/CheckoutUITests.cs b/BTCPayServer.Tests/CheckoutUITests.cs index 699957a70..8d0dd0d5f 100644 --- a/BTCPayServer.Tests/CheckoutUITests.cs +++ b/BTCPayServer.Tests/CheckoutUITests.cs @@ -156,21 +156,22 @@ namespace BTCPayServer.Tests [Fact] public void CanUseLightningSatsFeature() { - //uncomment after https://github.com/btcpayserver/btcpayserver/pull/1014 -// using (var s = SeleniumTester.Create()) -// { -// s.Start(); -// s.RegisterNewUser(); -// var store = s.CreateNewStore(); -// s.AddInternalLightningNode("BTC"); -// s.GoToStore(store.storeId, StoreNavPages.Checkout); -// s.SetCheckbox(s, "LightningAmountInSatoshi", true); -// s.Driver.FindElement(By.Name("command")).Click(); -// var invoiceId = s.CreateInvoice(store.storeName, 10, "USD", "a@g.com"); -// s.GoToInvoiceCheckout(invoiceId); -// Assert.Contains("Sats", s.Driver.FindElement(By.ClassName("payment__currencies_noborder")).Text); -// -// } + using (var s = SeleniumTester.Create()) + { + s.Start(); + s.RegisterNewUser(); + var store = s.CreateNewStore(); + s.AddInternalLightningNode("BTC"); + s.GoToStore(store.storeId, StoreNavPages.Checkout); + s.SetCheckbox(s, "LightningAmountInSatoshi", true); + var command = s.Driver.FindElement(By.Name("command")); + + command.ForceClick(); + var invoiceId = s.CreateInvoice(store.storeName, 10, "USD", "a@g.com"); + s.GoToInvoiceCheckout(invoiceId); + Assert.Contains("Sats", s.Driver.FindElement(By.ClassName("payment__currencies_noborder")).Text); + + } } } } diff --git a/BTCPayServer.Tests/SeleniumTester.cs b/BTCPayServer.Tests/SeleniumTester.cs index 42acb5753..3507b6b9f 100644 --- a/BTCPayServer.Tests/SeleniumTester.cs +++ b/BTCPayServer.Tests/SeleniumTester.cs @@ -21,6 +21,7 @@ using BTCPayServer.Lightning.CLightning; using BTCPayServer.Views.Stores; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using OpenQA.Selenium.Interactions; namespace BTCPayServer.Tests { @@ -216,6 +217,13 @@ namespace BTCPayServer.Tests SetCheckbox(s.Driver.FindElement(By.Name(inputName)), value); } + public void ScrollToElement(IWebElement element) + { + Actions actions = new Actions(Driver); + actions.MoveToElement(element); + actions.Perform(); + } + public void GoToInvoices() { Driver.FindElement(By.Id("Invoices")).Click(); diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index d22973140..bb1b76014 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -238,7 +238,6 @@ namespace BTCPayServer.Controllers CustomCSSLink = storeBlob.CustomCSS?.AbsoluteUri, CustomLogoLink = storeBlob.CustomLogo?.AbsoluteUri, CryptoImage = Request.GetRelativePathOrAbsolute(paymentMethodHandler.GetCryptoImage(paymentMethodId)), - LightningAmountInSatoshi = storeBlob.LightningAmountInSatoshi, BtcAddress = paymentMethodDetails.GetPaymentDestination(), BtcDue = accounting.Due.ToString(), OrderAmount = (accounting.TotalDue - accounting.NetworkFee).ToString(), @@ -295,7 +294,7 @@ namespace BTCPayServer.Controllers .ToList() }; - paymentMethodHandler.PreparePaymentModel(model, dto); + paymentMethodHandler.PreparePaymentModel(model, dto, storeBlob); model.UISettings = paymentMethodHandler.GetCheckoutUISettings(); model.PaymentMethodId = paymentMethodId.ToString(); var expiration = TimeSpan.FromSeconds(model.ExpirationSeconds); diff --git a/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs index 369ea9f2c..cc5b3c7d4 100644 --- a/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs @@ -37,7 +37,8 @@ namespace BTCPayServer.Payments.Bitcoin public Task ReserveAddress; } - public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse) + public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse, + StoreBlob storeBlob) { var paymentMethodId = new PaymentMethodId(model.CryptoCode, PaymentTypes.BTCLike); diff --git a/BTCPayServer/Payments/IPaymentMethodHandler.cs b/BTCPayServer/Payments/IPaymentMethodHandler.cs index 4dba06a0d..dde02e8ba 100644 --- a/BTCPayServer/Payments/IPaymentMethodHandler.cs +++ b/BTCPayServer/Payments/IPaymentMethodHandler.cs @@ -37,7 +37,7 @@ namespace BTCPayServer.Payments /// object PreparePayment(ISupportedPaymentMethod supportedPaymentMethod, StoreData store, BTCPayNetworkBase network); - void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse); + void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse, StoreBlob storeBlob); string GetCryptoImage(PaymentMethodId paymentMethodId); string GetPaymentMethodName(PaymentMethodId paymentMethodId); @@ -68,7 +68,8 @@ namespace BTCPayServer.Payments TSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, TBTCPayNetwork network, object preparePaymentObject); - public abstract void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse); + public abstract void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse, + StoreBlob storeBlob); public abstract string GetCryptoImage(PaymentMethodId paymentMethodId); public abstract string GetPaymentMethodName(PaymentMethodId paymentMethodId); diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs index ea3320d66..601f6fd91 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs @@ -162,7 +162,8 @@ namespace BTCPayServer.Payments.Lightning return string.Empty; } - public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse) + public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse, + StoreBlob storeBlob) { var paymentMethodId = new PaymentMethodId(model.CryptoCode, PaymentTypes.LightningLike); @@ -172,8 +173,16 @@ namespace BTCPayServer.Payments.Lightning model.PaymentMethodName = GetPaymentMethodName(network); model.InvoiceBitcoinUrl = cryptoInfo.PaymentUrls.BOLT11; model.InvoiceBitcoinUrlQR = cryptoInfo.PaymentUrls.BOLT11.ToUpperInvariant(); + model.LightningAmountInSatoshi = storeBlob.LightningAmountInSatoshi; + if (storeBlob.LightningAmountInSatoshi && model.CryptoCode == "BTC" ) + { + model.CryptoCode = "Sats"; + model.BtcDue = Money.Parse(model.BtcDue).ToUnit(MoneyUnit.Satoshi).ToString(); + model.BtcPaid = Money.Parse(model.BtcPaid).ToUnit(MoneyUnit.Satoshi).ToString(); + model.NetworkFee = new Money(model.NetworkFee, MoneyUnit.BTC).ToUnit(MoneyUnit.Satoshi); + model.OrderAmount = Money.Parse(model.OrderAmount).ToUnit(MoneyUnit.Satoshi).ToString(); + } } - public override string GetCryptoImage(PaymentMethodId paymentMethodId) { var network = _networkProvider.GetNetwork(paymentMethodId.CryptoCode); diff --git a/BTCPayServer/Views/Invoice/Checkout.cshtml b/BTCPayServer/Views/Invoice/Checkout.cshtml index e130b65e6..9d9288ec2 100644 --- a/BTCPayServer/Views/Invoice/Checkout.cshtml +++ b/BTCPayServer/Views/Invoice/Checkout.cshtml @@ -213,12 +213,6 @@ this.lineItemsExpanded ? $("line-items").slideUp() : $("line-items").slideDown(); this.lineItemsExpanded = !this.lineItemsExpanded; }, - numberFormatted: function(x) { - var rounded = Math.round(x); - var parts = rounded.toString().split("."); - parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " "); - return parts.join("."); - }, openPaymentMethodDialog: function() { var content = $("#vexPopupDialog").html(); vex.open({