mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-19 06:54:19 +01:00
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
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
|
#nullable enable
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@@ -22,10 +22,7 @@ using BTCPayServer.Services.Stores;
|
|||||||
using BTCPayServer.Validation;
|
using BTCPayServer.Validation;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
using NBitcoin;
|
|
||||||
using NBitpayClient;
|
using NBitpayClient;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using BitpayCreateInvoiceRequest = BTCPayServer.Models.BitpayCreateInvoiceRequest;
|
using BitpayCreateInvoiceRequest = BTCPayServer.Models.BitpayCreateInvoiceRequest;
|
||||||
using StoreData = BTCPayServer.Data.StoreData;
|
using StoreData = BTCPayServer.Data.StoreData;
|
||||||
|
|
||||||
@@ -81,7 +78,7 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
|
|
||||||
internal async Task<DataWrapper<InvoiceResponse>> CreateInvoiceCore(BitpayCreateInvoiceRequest invoice,
|
internal async Task<DataWrapper<InvoiceResponse>> CreateInvoiceCore(BitpayCreateInvoiceRequest invoice,
|
||||||
StoreData store, string serverUrl, List<string> additionalTags = null,
|
StoreData store, string serverUrl, List<string>? additionalTags = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entity = await CreateInvoiceCoreRaw(invoice, store, serverUrl, additionalTags, cancellationToken);
|
var entity = await CreateInvoiceCoreRaw(invoice, store, serverUrl, additionalTags, cancellationToken);
|
||||||
@@ -89,7 +86,7 @@ namespace BTCPayServer.Controllers
|
|||||||
return new DataWrapper<InvoiceResponse>(resp) { Facade = "pos/invoice" };
|
return new DataWrapper<InvoiceResponse>(resp) { Facade = "pos/invoice" };
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(BitpayCreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string> additionalTags = null, CancellationToken cancellationToken = default)
|
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(BitpayCreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string>? additionalTags = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var storeBlob = store.GetStoreBlob();
|
var storeBlob = store.GetStoreBlob();
|
||||||
var entity = _InvoiceRepository.CreateNewInvoice();
|
var entity = _InvoiceRepository.CreateNewInvoice();
|
||||||
@@ -135,7 +132,7 @@ namespace BTCPayServer.Controllers
|
|||||||
invoice.RedirectAutomatically.GetValueOrDefault(storeBlob.RedirectAutomatically);
|
invoice.RedirectAutomatically.GetValueOrDefault(storeBlob.RedirectAutomatically);
|
||||||
entity.SpeedPolicy = ParseSpeedPolicy(invoice.TransactionSpeed, store.SpeedPolicy);
|
entity.SpeedPolicy = ParseSpeedPolicy(invoice.TransactionSpeed, store.SpeedPolicy);
|
||||||
|
|
||||||
IPaymentFilter excludeFilter = null;
|
IPaymentFilter? excludeFilter = null;
|
||||||
if (invoice.PaymentCurrencies?.Any() is true)
|
if (invoice.PaymentCurrencies?.Any() is true)
|
||||||
{
|
{
|
||||||
invoice.SupportedTransactionCurrencies ??=
|
invoice.SupportedTransactionCurrencies ??=
|
||||||
@@ -159,7 +156,7 @@ namespace BTCPayServer.Controllers
|
|||||||
return await CreateInvoiceCoreRaw(entity, store, excludeFilter, null, cancellationToken);
|
return await CreateInvoiceCoreRaw(entity, store, excludeFilter, null, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(CreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string> additionalTags = null, CancellationToken cancellationToken = default)
|
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(CreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string>? additionalTags = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var storeBlob = store.GetStoreBlob();
|
var storeBlob = store.GetStoreBlob();
|
||||||
var entity = _InvoiceRepository.CreateNewInvoice();
|
var entity = _InvoiceRepository.CreateNewInvoice();
|
||||||
@@ -183,7 +180,7 @@ namespace BTCPayServer.Controllers
|
|||||||
entity.SpeedPolicy = invoice.Checkout.SpeedPolicy ?? store.SpeedPolicy;
|
entity.SpeedPolicy = invoice.Checkout.SpeedPolicy ?? store.SpeedPolicy;
|
||||||
entity.DefaultLanguage = invoice.Checkout.DefaultLanguage;
|
entity.DefaultLanguage = invoice.Checkout.DefaultLanguage;
|
||||||
entity.RedirectAutomatically = invoice.Checkout.RedirectAutomatically ?? storeBlob.RedirectAutomatically;
|
entity.RedirectAutomatically = invoice.Checkout.RedirectAutomatically ?? storeBlob.RedirectAutomatically;
|
||||||
IPaymentFilter excludeFilter = null;
|
IPaymentFilter? excludeFilter = null;
|
||||||
if (invoice.Checkout.PaymentMethods != null)
|
if (invoice.Checkout.PaymentMethods != null)
|
||||||
{
|
{
|
||||||
var supportedTransactionCurrencies = invoice.Checkout.PaymentMethods
|
var supportedTransactionCurrencies = invoice.Checkout.PaymentMethods
|
||||||
@@ -198,7 +195,7 @@ namespace BTCPayServer.Controllers
|
|||||||
return await CreateInvoiceCoreRaw(entity, store, excludeFilter, invoice.AdditionalSearchTerms, cancellationToken);
|
return await CreateInvoiceCoreRaw(entity, store, excludeFilter, invoice.AdditionalSearchTerms, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(InvoiceEntity entity, StoreData store, IPaymentFilter invoicePaymentMethodFilter, string[] additionalSearchTerms = null, CancellationToken cancellationToken = default)
|
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(InvoiceEntity entity, StoreData store, IPaymentFilter? invoicePaymentMethodFilter, string[]? additionalSearchTerms = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
InvoiceLogs logs = new InvoiceLogs();
|
InvoiceLogs logs = new InvoiceLogs();
|
||||||
logs.Write("Creation of invoice starting", InvoiceEventData.EventSeverity.Info);
|
logs.Write("Creation of invoice starting", InvoiceEventData.EventSeverity.Info);
|
||||||
@@ -339,7 +336,7 @@ namespace BTCPayServer.Controllers
|
|||||||
}).ToArray());
|
}).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<PaymentMethod> CreatePaymentMethodAsync(Dictionary<CurrencyPair, Task<RateResult>> fetchingByCurrencyPair,
|
private async Task<PaymentMethod?> CreatePaymentMethodAsync(Dictionary<CurrencyPair, Task<RateResult>> fetchingByCurrencyPair,
|
||||||
IPaymentMethodHandler handler, ISupportedPaymentMethod supportedPaymentMethod, BTCPayNetworkBase network, InvoiceEntity entity,
|
IPaymentMethodHandler handler, ISupportedPaymentMethod supportedPaymentMethod, BTCPayNetworkBase network, InvoiceEntity entity,
|
||||||
StoreData store, InvoiceLogs logs)
|
StoreData store, InvoiceLogs logs)
|
||||||
{
|
{
|
||||||
@@ -348,7 +345,7 @@ namespace BTCPayServer.Controllers
|
|||||||
var logPrefix = $"{supportedPaymentMethod.PaymentId.ToPrettyString()}:";
|
var logPrefix = $"{supportedPaymentMethod.PaymentId.ToPrettyString()}:";
|
||||||
var storeBlob = store.GetStoreBlob();
|
var storeBlob = store.GetStoreBlob();
|
||||||
|
|
||||||
object preparePayment;
|
object? preparePayment;
|
||||||
if (storeBlob.LazyPaymentMethods)
|
if (storeBlob.LazyPaymentMethods)
|
||||||
{
|
{
|
||||||
preparePayment = null;
|
preparePayment = null;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#nullable enable
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
@@ -11,7 +12,7 @@ using BTCPayServer.Lightning;
|
|||||||
using BTCPayServer.Logging;
|
using BTCPayServer.Logging;
|
||||||
using BTCPayServer.Models;
|
using BTCPayServer.Models;
|
||||||
using BTCPayServer.Models.InvoicingModels;
|
using BTCPayServer.Models.InvoicingModels;
|
||||||
using BTCPayServer.Rating;
|
using BTCPayServer.Client.Models;
|
||||||
using BTCPayServer.Services;
|
using BTCPayServer.Services;
|
||||||
using BTCPayServer.Services.Invoices;
|
using BTCPayServer.Services.Invoices;
|
||||||
using BTCPayServer.Services.Rates;
|
using BTCPayServer.Services.Rates;
|
||||||
@@ -51,9 +52,13 @@ namespace BTCPayServer.Payments.Lightning
|
|||||||
|
|
||||||
public override async Task<IPaymentMethodDetails> CreatePaymentMethodDetails(
|
public override async Task<IPaymentMethodDetails> CreatePaymentMethodDetails(
|
||||||
InvoiceLogs logs,
|
InvoiceLogs logs,
|
||||||
LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store,
|
LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, Data.StoreData store,
|
||||||
BTCPayNetwork network, object preparePaymentObject)
|
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)
|
if (preparePaymentObject is null)
|
||||||
{
|
{
|
||||||
return new LightningLikePaymentMethodDetails()
|
return new LightningLikePaymentMethodDetails()
|
||||||
@@ -80,7 +85,7 @@ namespace BTCPayServer.Payments.Lightning
|
|||||||
if (expiry < TimeSpan.Zero)
|
if (expiry < TimeSpan.Zero)
|
||||||
expiry = TimeSpan.FromSeconds(1);
|
expiry = TimeSpan.FromSeconds(1);
|
||||||
|
|
||||||
LightningInvoice lightningInvoice = null;
|
LightningInvoice? lightningInvoice = null;
|
||||||
|
|
||||||
string description = storeBlob.LightningDescriptionTemplate;
|
string description = storeBlob.LightningDescriptionTemplate;
|
||||||
description = description.Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase)
|
description = description.Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase)
|
||||||
@@ -247,7 +252,7 @@ namespace BTCPayServer.Payments.Lightning
|
|||||||
return $"{network.DisplayName} (Lightning)";
|
return $"{network.DisplayName} (Lightning)";
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object PreparePayment(LightningSupportedPaymentMethod supportedPaymentMethod, StoreData store,
|
public override object PreparePayment(LightningSupportedPaymentMethod supportedPaymentMethod, Data.StoreData store,
|
||||||
BTCPayNetworkBase network)
|
BTCPayNetworkBase network)
|
||||||
{
|
{
|
||||||
// pass a non null obj, so that if lazy payment feature is used, it has a marker to trigger activation
|
// pass a non null obj, so that if lazy payment feature is used, it has a marker to trigger activation
|
||||||
|
|||||||
Reference in New Issue
Block a user