Merge pull request #2301 from btcpayserver/gf/paymenttypeparse

Make payment type parsing more dynamic
This commit is contained in:
Nicolas Dorier 2021-02-26 11:00:27 +09:00 committed by GitHub
commit 6843b0eaab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 23 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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
/// </summary>
public static class PaymentTypes
{
private static PaymentType[] _paymentTypes =
{
BTCLike, LightningLike,
#if ALTCOINS
MoneroLike,
EthereumPaymentType.Instance
#endif
};
/// <summary>
/// On-Chain UTXO based, bitcoin compatible
/// </summary>
@ -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);
}
}
}