From d1ea4e4fa431fa668367f8834d2ee48aab83c283 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 22 Aug 2021 23:13:26 -0700 Subject: [PATCH] Fix bug with top-up invoices when used with "Only enable the payment method after user explicitly chooses it" enabled (#2780) * Fix bug with top-up invoices when used with "Only enable the payment method after user explicitly chooses it" enabled * Remove unused "using" directives * Add "#nullable enable" directive * check for top-up invoice in LightningLikePaymentHandler --- BTCPayServer/Controllers/InvoiceController.cs | 21 ++++++++----------- .../Lightning/LightningLikePaymentHandler.cs | 13 ++++++++---- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.cs b/BTCPayServer/Controllers/InvoiceController.cs index fed5c192a..87e0d31fc 100644 --- a/BTCPayServer/Controllers/InvoiceController.cs +++ b/BTCPayServer/Controllers/InvoiceController.cs @@ -1,6 +1,6 @@ +#nullable enable using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading; @@ -22,10 +22,7 @@ using BTCPayServer.Services.Stores; using BTCPayServer.Validation; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using NBitcoin; using NBitpayClient; -using Newtonsoft.Json; using BitpayCreateInvoiceRequest = BTCPayServer.Models.BitpayCreateInvoiceRequest; using StoreData = BTCPayServer.Data.StoreData; @@ -81,7 +78,7 @@ namespace BTCPayServer.Controllers internal async Task> CreateInvoiceCore(BitpayCreateInvoiceRequest invoice, - StoreData store, string serverUrl, List additionalTags = null, + StoreData store, string serverUrl, List? additionalTags = null, CancellationToken cancellationToken = default) { var entity = await CreateInvoiceCoreRaw(invoice, store, serverUrl, additionalTags, cancellationToken); @@ -89,7 +86,7 @@ namespace BTCPayServer.Controllers return new DataWrapper(resp) { Facade = "pos/invoice" }; } - internal async Task CreateInvoiceCoreRaw(BitpayCreateInvoiceRequest invoice, StoreData store, string serverUrl, List additionalTags = null, CancellationToken cancellationToken = default) + internal async Task CreateInvoiceCoreRaw(BitpayCreateInvoiceRequest invoice, StoreData store, string serverUrl, List? additionalTags = null, CancellationToken cancellationToken = default) { var storeBlob = store.GetStoreBlob(); var entity = _InvoiceRepository.CreateNewInvoice(); @@ -135,7 +132,7 @@ namespace BTCPayServer.Controllers invoice.RedirectAutomatically.GetValueOrDefault(storeBlob.RedirectAutomatically); entity.SpeedPolicy = ParseSpeedPolicy(invoice.TransactionSpeed, store.SpeedPolicy); - IPaymentFilter excludeFilter = null; + IPaymentFilter? excludeFilter = null; if (invoice.PaymentCurrencies?.Any() is true) { invoice.SupportedTransactionCurrencies ??= @@ -159,7 +156,7 @@ namespace BTCPayServer.Controllers return await CreateInvoiceCoreRaw(entity, store, excludeFilter, null, cancellationToken); } - internal async Task CreateInvoiceCoreRaw(CreateInvoiceRequest invoice, StoreData store, string serverUrl, List additionalTags = null, CancellationToken cancellationToken = default) + internal async Task CreateInvoiceCoreRaw(CreateInvoiceRequest invoice, StoreData store, string serverUrl, List? additionalTags = null, CancellationToken cancellationToken = default) { var storeBlob = store.GetStoreBlob(); var entity = _InvoiceRepository.CreateNewInvoice(); @@ -183,7 +180,7 @@ namespace BTCPayServer.Controllers entity.SpeedPolicy = invoice.Checkout.SpeedPolicy ?? store.SpeedPolicy; entity.DefaultLanguage = invoice.Checkout.DefaultLanguage; entity.RedirectAutomatically = invoice.Checkout.RedirectAutomatically ?? storeBlob.RedirectAutomatically; - IPaymentFilter excludeFilter = null; + IPaymentFilter? excludeFilter = null; if (invoice.Checkout.PaymentMethods != null) { var supportedTransactionCurrencies = invoice.Checkout.PaymentMethods @@ -198,7 +195,7 @@ namespace BTCPayServer.Controllers return await CreateInvoiceCoreRaw(entity, store, excludeFilter, invoice.AdditionalSearchTerms, cancellationToken); } - internal async Task CreateInvoiceCoreRaw(InvoiceEntity entity, StoreData store, IPaymentFilter invoicePaymentMethodFilter, string[] additionalSearchTerms = null, CancellationToken cancellationToken = default) + internal async Task CreateInvoiceCoreRaw(InvoiceEntity entity, StoreData store, IPaymentFilter? invoicePaymentMethodFilter, string[]? additionalSearchTerms = null, CancellationToken cancellationToken = default) { InvoiceLogs logs = new InvoiceLogs(); logs.Write("Creation of invoice starting", InvoiceEventData.EventSeverity.Info); @@ -339,7 +336,7 @@ namespace BTCPayServer.Controllers }).ToArray()); } - private async Task CreatePaymentMethodAsync(Dictionary> fetchingByCurrencyPair, + private async Task CreatePaymentMethodAsync(Dictionary> fetchingByCurrencyPair, IPaymentMethodHandler handler, ISupportedPaymentMethod supportedPaymentMethod, BTCPayNetworkBase network, InvoiceEntity entity, StoreData store, InvoiceLogs logs) { @@ -348,7 +345,7 @@ namespace BTCPayServer.Controllers var logPrefix = $"{supportedPaymentMethod.PaymentId.ToPrettyString()}:"; var storeBlob = store.GetStoreBlob(); - object preparePayment; + object? preparePayment; if (storeBlob.LazyPaymentMethods) { preparePayment = null; diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs index d710c76e8..420f0d04a 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Collections.Generic; using System.Globalization; @@ -11,7 +12,7 @@ using BTCPayServer.Lightning; using BTCPayServer.Logging; using BTCPayServer.Models; using BTCPayServer.Models.InvoicingModels; -using BTCPayServer.Rating; +using BTCPayServer.Client.Models; using BTCPayServer.Services; using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Rates; @@ -51,9 +52,13 @@ namespace BTCPayServer.Payments.Lightning public override async Task CreatePaymentMethodDetails( InvoiceLogs logs, - LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, + LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, Data.StoreData store, BTCPayNetwork network, object preparePaymentObject) { + if (paymentMethod.ParentEntity.Type == InvoiceType.TopUp) { + throw new PaymentMethodUnavailableException("Lightning Network payment method is not available for top-up invoices"); + } + if (preparePaymentObject is null) { return new LightningLikePaymentMethodDetails() @@ -80,7 +85,7 @@ namespace BTCPayServer.Payments.Lightning if (expiry < TimeSpan.Zero) expiry = TimeSpan.FromSeconds(1); - LightningInvoice lightningInvoice = null; + LightningInvoice? lightningInvoice = null; string description = storeBlob.LightningDescriptionTemplate; description = description.Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase) @@ -247,7 +252,7 @@ namespace BTCPayServer.Payments.Lightning return $"{network.DisplayName} (Lightning)"; } - public override object PreparePayment(LightningSupportedPaymentMethod supportedPaymentMethod, StoreData store, + public override object PreparePayment(LightningSupportedPaymentMethod supportedPaymentMethod, Data.StoreData store, BTCPayNetworkBase network) { // pass a non null obj, so that if lazy payment feature is used, it has a marker to trigger activation