From 557594e34d7ff7464a3d7208c1007a62ebfd0249 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Mon, 21 Feb 2022 13:21:33 +0900 Subject: [PATCH] Test LNUrl Pay payment method information, add doc --- .../Models/InvoicePaymentMethodDataModel.cs | 2 +- BTCPayServer.Tests/SeleniumTester.cs | 7 +++++ BTCPayServer.Tests/SeleniumTests.cs | 15 ++++++++--- BTCPayServer/Controllers/UILNURLController.cs | 2 +- .../PullPaymentHostedService.cs | 2 +- .../Payments/IPaymentMethodDetails.cs | 2 +- .../LNURLPay/LNURLPayPaymentMethodDetails.cs | 12 +++------ .../LightningLikePaymentMethodDetails.cs | 2 +- .../swagger/v1/swagger.template.invoices.json | 27 +++++++++++++++++-- 9 files changed, 51 insertions(+), 20 deletions(-) diff --git a/BTCPayServer.Client/Models/InvoicePaymentMethodDataModel.cs b/BTCPayServer.Client/Models/InvoicePaymentMethodDataModel.cs index 3166ef968..898da8780 100644 --- a/BTCPayServer.Client/Models/InvoicePaymentMethodDataModel.cs +++ b/BTCPayServer.Client/Models/InvoicePaymentMethodDataModel.cs @@ -35,7 +35,7 @@ namespace BTCPayServer.Client.Models public string PaymentMethod { get; set; } public string CryptoCode { get; set; } - public Dictionary AdditionalData { get; set; } + public JObject AdditionalData { get; set; } public class Payment { diff --git a/BTCPayServer.Tests/SeleniumTester.cs b/BTCPayServer.Tests/SeleniumTester.cs index 6e795d8cd..f1eede5a7 100644 --- a/BTCPayServer.Tests/SeleniumTester.cs +++ b/BTCPayServer.Tests/SeleniumTester.cs @@ -145,8 +145,15 @@ namespace BTCPayServer.Tests Driver.FindElement(By.Id("IsAdmin")).Click(); Driver.FindElement(By.Id("RegisterButton")).Click(); Driver.AssertNoError(); + CreatedUser = usr; return usr; } + string CreatedUser; + + public TestAccount AsTestAccount() + { + return new TestAccount(Server) { RegisterDetails = new Models.AccountViewModels.RegisterViewModel() { Password = "123456", Email = CreatedUser } }; + } public (string storeName, string storeId) CreateNewStore(bool keepId = true) { diff --git a/BTCPayServer.Tests/SeleniumTests.cs b/BTCPayServer.Tests/SeleniumTests.cs index ff54b3490..7cf982f6f 100644 --- a/BTCPayServer.Tests/SeleniumTests.cs +++ b/BTCPayServer.Tests/SeleniumTests.cs @@ -1467,7 +1467,9 @@ namespace BTCPayServer.Tests s.GoToLightningSettings(); // LNURL is true by default Assert.True(s.Driver.FindElement(By.Id("LNURLEnabled")).Selected); - + s.Driver.SetCheckbox(By.Name("LUD12Enabled"), true); + s.Driver.FindElement(By.Id("save")).Click(); + // Topup Invoice test var i = s.CreateInvoice(storeId, null, cryptoCode); s.GoToInvoiceCheckout(i); @@ -1479,13 +1481,13 @@ namespace BTCPayServer.Tests Assert.Equal(1m, fetchedReuqest.MinSendable.ToDecimal(LightMoneyUnit.Satoshi)); Assert.NotEqual(1m, fetchedReuqest.MaxSendable.ToDecimal(LightMoneyUnit.Satoshi)); var lnurlResponse = await fetchedReuqest.SendRequest(new LightMoney(0.000001m, LightMoneyUnit.BTC), - network, new HttpClient()); + network, new HttpClient(), comment: "lol"); Assert.Equal(new LightMoney(0.000001m, LightMoneyUnit.BTC), lnurlResponse.GetPaymentRequest(network).MinimumAmount); var lnurlResponse2 = await fetchedReuqest.SendRequest(new LightMoney(0.000002m, LightMoneyUnit.BTC), - network, new HttpClient()); + network, new HttpClient(), comment: "lol2"); Assert.Equal(new LightMoney(0.000002m, LightMoneyUnit.BTC), lnurlResponse2.GetPaymentRequest(network).MinimumAmount); await Assert.ThrowsAnyAsync(async () => { @@ -1499,7 +1501,11 @@ namespace BTCPayServer.Tests var inv = await s.Server.PayTester.InvoiceRepository.GetInvoice(i); Assert.Equal(InvoiceStatusLegacy.Complete, inv.Status); }); - + var greenfield = await s.AsTestAccount().CreateClient(); + var paymentMethods = await greenfield.GetInvoicePaymentMethods(s.StoreId, i); + Assert.Single(paymentMethods, p => { + return p.AdditionalData["providedComment"].Value() == "lol2"; + }); // Standard invoice test s.GoToStore(storeId); s.GoToLightningSettings(); @@ -1608,6 +1614,7 @@ namespace BTCPayServer.Tests s.Driver.FindElement(By.Id("Amount")).Clear(); s.Driver.FindElement(By.Id("Amount")).SendKeys("0.0000001"); s.Driver.FindElement(By.Id("Create")).Click(); + s.Driver.TakeScreenshot().SaveAsFile(@"C:\Users\NicolasDorier\AppData\Local\Temp\1279276918\wfew.png"); s.Driver.FindElement(By.LinkText("View")).Click(); s.Driver.FindElement(By.Id("Destination")).SendKeys(lnurl); diff --git a/BTCPayServer/Controllers/UILNURLController.cs b/BTCPayServer/Controllers/UILNURLController.cs index 4364ebc10..0de53ae9a 100644 --- a/BTCPayServer/Controllers/UILNURLController.cs +++ b/BTCPayServer/Controllers/UILNURLController.cs @@ -241,7 +241,7 @@ namespace BTCPayServer return NotFound(); } - var lnAddress = username is null ? null : $"{username}@{Request.Host.ToString()}"; + var lnAddress = username is null ? null : $"{username}@{Request.Host}"; List lnurlMetadata = new List(); var i = await _invoiceController.CreateInvoiceCoreRaw( diff --git a/BTCPayServer/HostedServices/PullPaymentHostedService.cs b/BTCPayServer/HostedServices/PullPaymentHostedService.cs index 2520bdacd..bb35a5a58 100644 --- a/BTCPayServer/HostedServices/PullPaymentHostedService.cs +++ b/BTCPayServer/HostedServices/PullPaymentHostedService.cs @@ -96,7 +96,7 @@ namespace BTCPayServer.HostedServices throw new ArgumentException("Amount out of bound", nameof(create)); using var ctx = this._dbContextFactory.CreateContext(); var o = new Data.PullPaymentData(); - o.StartDate = create.StartsAt is DateTimeOffset date ? date : DateTimeOffset.UtcNow; + o.StartDate = create.StartsAt is DateTimeOffset date ? date : DateTimeOffset.UtcNow - TimeSpan.FromSeconds(1.0); o.EndDate = create.ExpiresAt is DateTimeOffset date2 ? new DateTimeOffset?(date2) : null; o.Period = create.Period is TimeSpan period ? (long?)period.TotalSeconds : null; o.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(20)); diff --git a/BTCPayServer/Payments/IPaymentMethodDetails.cs b/BTCPayServer/Payments/IPaymentMethodDetails.cs index 3c27297ad..7d537d39c 100644 --- a/BTCPayServer/Payments/IPaymentMethodDetails.cs +++ b/BTCPayServer/Payments/IPaymentMethodDetails.cs @@ -22,6 +22,6 @@ namespace BTCPayServer.Payments bool Activated { get; set; } virtual string GetAdditionalDataPartialName() => null; - virtual Dictionary GetAdditionalData() => new(); + virtual JObject GetAdditionalData() => new(); } } diff --git a/BTCPayServer/Payments/LNURLPay/LNURLPayPaymentMethodDetails.cs b/BTCPayServer/Payments/LNURLPay/LNURLPayPaymentMethodDetails.cs index 63f31959a..99201fc85 100644 --- a/BTCPayServer/Payments/LNURLPay/LNURLPayPaymentMethodDetails.cs +++ b/BTCPayServer/Payments/LNURLPay/LNURLPayPaymentMethodDetails.cs @@ -35,19 +35,13 @@ namespace BTCPayServer.Payments return "LNURL/AdditionalPaymentMethodDetails"; } - public override Dictionary GetAdditionalData() + public override JObject GetAdditionalData() { var result = base.GetAdditionalData(); if (!string.IsNullOrEmpty(ProvidedComment)) - { - result.TryAdd(nameof(ProvidedComment), new JObject(ProvidedComment)); - } - + result.Add("providedComment", new JValue(ProvidedComment)); if (!string.IsNullOrEmpty(ConsumedLightningAddress)) - { - result.TryAdd(nameof(ConsumedLightningAddress), new JObject(ConsumedLightningAddress)); - } - + result.Add("consumedLightningAddress", new JValue(ConsumedLightningAddress)); return result; } } diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs index f580c4737..6863487eb 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs @@ -43,7 +43,7 @@ namespace BTCPayServer.Payments.Lightning return null; } - public virtual Dictionary GetAdditionalData() + public virtual JObject GetAdditionalData() { return new(); } diff --git a/BTCPayServer/wwwroot/swagger/v1/swagger.template.invoices.json b/BTCPayServer/wwwroot/swagger/v1/swagger.template.invoices.json index a565ab3f5..9b18d7cab 100644 --- a/BTCPayServer/wwwroot/swagger/v1/swagger.template.invoices.json +++ b/BTCPayServer/wwwroot/swagger/v1/swagger.template.invoices.json @@ -1142,8 +1142,31 @@ "description": "If the payment method is activated (when lazy payments option is enabled" }, "additionalData": { - "type": "object", - "description": "Additional data provided by the payment method." + "description": "Additional data provided by the payment method.", + "anyOf": [ + { + "type": "object", + "description": "LNURL Pay information", + "properties": { + "providedComment": { + "type": "string", + "nullable": true, + "description": "The provided comment to a LNUrl payment with comments enabled", + "example": "Thank you!" + }, + "consumedLightningAddress": { + "type": "string", + "nullable": true, + "description": "The consumed lightning address of a LN Address payment", + "example": "customer@example.com" + } + } + }, + { + "type": "object", + "description": "No additional information" + } + ] } } },