From 71c11b34f4aac74cf805d52f031a1dc74929cd82 Mon Sep 17 00:00:00 2001 From: Kukks Date: Fri, 19 Feb 2021 08:23:55 +0100 Subject: [PATCH] Make payment type parsing more dynamic fixes bug described in #2297 --- BTCPayServer/Payments/PaymentTypes.Bitcoin.cs | 4 ++ .../Payments/PaymentTypes.Lightning.cs | 4 ++ BTCPayServer/Payments/PaymentTypes.cs | 46 +++++++++---------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs b/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs index b41961398..24c521943 100644 --- a/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs +++ b/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs @@ -79,5 +79,9 @@ namespace BTCPayServer.Payments } public override string InvoiceViewPaymentPartialName { get; } = "Bitcoin/ViewBitcoinLikePaymentData"; + public override bool IsPaymentType(string paymentType) + { + return string.IsNullOrEmpty(paymentType) || base.IsPaymentType(paymentType); + } } } diff --git a/BTCPayServer/Payments/PaymentTypes.Lightning.cs b/BTCPayServer/Payments/PaymentTypes.Lightning.cs index 7400a5a9f..2e63f6773 100644 --- a/BTCPayServer/Payments/PaymentTypes.Lightning.cs +++ b/BTCPayServer/Payments/PaymentTypes.Lightning.cs @@ -59,5 +59,9 @@ namespace BTCPayServer.Payments } public override string InvoiceViewPaymentPartialName { get; } = "Lightning/ViewLightningLikePaymentData"; + public override bool IsPaymentType(string paymentType) + { + return paymentType?.Equals("offchain", StringComparison.InvariantCultureIgnoreCase) is true || base.IsPaymentType(paymentType); + } } } diff --git a/BTCPayServer/Payments/PaymentTypes.cs b/BTCPayServer/Payments/PaymentTypes.cs index 52c294428..c52ccf5ec 100644 --- a/BTCPayServer/Payments/PaymentTypes.cs +++ b/BTCPayServer/Payments/PaymentTypes.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; #if ALTCOINS using BTCPayServer.Services.Altcoins.Ethereum.Payments; using BTCPayServer.Services.Altcoins.Monero.Payments; @@ -14,6 +15,14 @@ namespace BTCPayServer.Payments /// public static class PaymentTypes { + private static PaymentType[] _paymentTypes = + { + BTCLike, LightningLike, +#if ALTCOINS + MoneroLike, + EthereumPaymentType.Instance +#endif + }; /// /// On-Chain UTXO based, bitcoin compatible /// @@ -32,29 +41,8 @@ namespace BTCPayServer.Payments public static bool TryParse(string paymentType, out PaymentType type) { - switch (paymentType.ToLowerInvariant()) - { - case "btclike": - case "onchain": - type = PaymentTypes.BTCLike; - break; - case "lightninglike": - case "offchain": - type = PaymentTypes.LightningLike; - break; -#if ALTCOINS - case "monerolike": - type = PaymentTypes.MoneroLike; - break; - case "ethereumlike": - type = EthereumPaymentType.Instance; - break; -#endif - default: - type = null; - return false; - } - return true; + type = _paymentTypes.FirstOrDefault(type1 => type1.IsPaymentType(paymentType)); + return type != null; } public static PaymentType Parse(string paymentType) { @@ -92,5 +80,17 @@ namespace BTCPayServer.Payments public abstract string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails, Money cryptoInfoDue, string serverUri); public abstract string InvoiceViewPaymentPartialName { get; } + + public virtual bool IsPaymentType(string paymentType) + { + paymentType = paymentType?.ToLowerInvariant(); + return new[] + { + GetId().Replace("-", "", StringComparison.InvariantCulture), + ToStringNormalized() + }.Contains( + paymentType, + StringComparer.InvariantCultureIgnoreCase); + } } }