mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 22:44:29 +01:00
Refactoring, make PaymentType a class instead of enum
This commit is contained in:
@@ -479,7 +479,7 @@ namespace BTCPayServer.Controllers
|
||||
{
|
||||
switch (paymentMethodId.PaymentType)
|
||||
{
|
||||
case PaymentTypes.BTCLike:
|
||||
case BitcoinPaymentType _:
|
||||
var strategy = derivationByCryptoCode.TryGet(paymentMethodId.CryptoCode);
|
||||
vm.DerivationSchemes.Add(new StoreViewModel.DerivationScheme()
|
||||
{
|
||||
@@ -489,7 +489,7 @@ namespace BTCPayServer.Controllers
|
||||
Enabled = !excludeFilters.Match(paymentMethodId)
|
||||
});
|
||||
break;
|
||||
case PaymentTypes.LightningLike:
|
||||
case LightningPaymentType _:
|
||||
var lightning = lightningByCryptoCode.TryGet(paymentMethodId.CryptoCode);
|
||||
vm.LightningNodes.Add(new StoreViewModel.LightningNode()
|
||||
{
|
||||
|
||||
@@ -10,10 +10,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
||||
{
|
||||
public class BitcoinLikeOnChainPaymentMethod : IPaymentMethodDetails
|
||||
{
|
||||
public PaymentTypes GetPaymentType()
|
||||
{
|
||||
return PaymentTypes.BTCLike;
|
||||
}
|
||||
public PaymentType GetPaymentType() => PaymentTypes.BTCLike;
|
||||
|
||||
public string GetPaymentDestination()
|
||||
{
|
||||
|
||||
@@ -123,7 +123,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
||||
};
|
||||
}
|
||||
|
||||
public override PaymentTypes PaymentType => PaymentTypes.BTCLike;
|
||||
public override PaymentType PaymentType => PaymentTypes.BTCLike;
|
||||
|
||||
public override async Task<IPaymentMethodDetails> CreatePaymentMethodDetails(
|
||||
DerivationSchemeSettings supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store,
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace BTCPayServer.Payments
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
string GetPaymentDestination();
|
||||
PaymentTypes GetPaymentType();
|
||||
PaymentType GetPaymentType();
|
||||
/// <summary>
|
||||
/// Returns fee that the merchant charge to the customer for the next payment
|
||||
/// </summary>
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace BTCPayServer.Payments
|
||||
where TSupportedPaymentMethod : ISupportedPaymentMethod
|
||||
where TBTCPayNetwork : BTCPayNetworkBase
|
||||
{
|
||||
public abstract PaymentTypes PaymentType { get; }
|
||||
public abstract PaymentType PaymentType { get; }
|
||||
|
||||
public abstract Task<IPaymentMethodDetails> CreatePaymentMethodDetails(
|
||||
TSupportedPaymentMethod supportedPaymentMethod,
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace BTCPayServer.Payments.Lightning
|
||||
_socketFactory = socketFactory;
|
||||
}
|
||||
|
||||
public override PaymentTypes PaymentType => PaymentTypes.LightningLike;
|
||||
public override PaymentType PaymentType => PaymentTypes.LightningLike;
|
||||
public override async Task<IPaymentMethodDetails> CreatePaymentMethodDetails(
|
||||
LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store,
|
||||
BTCPayNetwork network, object preparePaymentObject)
|
||||
|
||||
@@ -11,10 +11,12 @@ namespace BTCPayServer.Payments
|
||||
/// </summary>
|
||||
public class PaymentMethodId
|
||||
{
|
||||
public PaymentMethodId(string cryptoCode, PaymentTypes paymentType)
|
||||
public PaymentMethodId(string cryptoCode, PaymentType paymentType)
|
||||
{
|
||||
if (cryptoCode == null)
|
||||
throw new ArgumentNullException(nameof(cryptoCode));
|
||||
if (paymentType == null)
|
||||
throw new ArgumentNullException(nameof(paymentType));
|
||||
PaymentType = paymentType;
|
||||
CryptoCode = cryptoCode.ToUpperInvariant();
|
||||
}
|
||||
@@ -29,7 +31,7 @@ namespace BTCPayServer.Payments
|
||||
}
|
||||
|
||||
public string CryptoCode { get; private set; }
|
||||
public PaymentTypes PaymentType { get; private set; }
|
||||
public PaymentType PaymentType { get; private set; }
|
||||
|
||||
|
||||
public override bool Equals(object obj)
|
||||
@@ -66,22 +68,9 @@ namespace BTCPayServer.Payments
|
||||
return PaymentType == PaymentTypes.BTCLike ? CryptoCode : $"{CryptoCode}_{PaymentType}";
|
||||
}
|
||||
|
||||
public string ToPrettyPaymentType()
|
||||
{
|
||||
switch (PaymentType)
|
||||
{
|
||||
case PaymentTypes.BTCLike:
|
||||
return "On-Chain";
|
||||
case PaymentTypes.LightningLike:
|
||||
return "Off-Chain";
|
||||
default:
|
||||
return PaymentType.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public string ToPrettyString()
|
||||
{
|
||||
return $"{CryptoCode} ({ToPrettyPaymentType()})";
|
||||
return $"{CryptoCode} ({PaymentType.ToPrettyString()})";
|
||||
}
|
||||
|
||||
public static bool TryParse(string str, out PaymentMethodId paymentMethodId)
|
||||
@@ -90,28 +79,11 @@ namespace BTCPayServer.Payments
|
||||
var parts = str.Split('_', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length == 0 || parts.Length > 2)
|
||||
return false;
|
||||
PaymentTypes type = PaymentTypes.BTCLike;
|
||||
PaymentType type = PaymentTypes.BTCLike;
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
var typePart = parts[1].ToLowerInvariant();
|
||||
switch (typePart)
|
||||
{
|
||||
case "btclike":
|
||||
case "onchain":
|
||||
type = PaymentTypes.BTCLike;
|
||||
break;
|
||||
case "lightninglike":
|
||||
case "offchain":
|
||||
type = PaymentTypes.LightningLike;
|
||||
break;
|
||||
default:
|
||||
if (!Enum.TryParse(typePart, true, out type ))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
if (!PaymentTypes.TryParse(parts[1], out type))
|
||||
return false;
|
||||
}
|
||||
paymentMethodId = new PaymentMethodId(parts[0], type);
|
||||
return true;
|
||||
|
||||
19
BTCPayServer/Payments/PaymentTypes.Bitcoin.cs
Normal file
19
BTCPayServer/Payments/PaymentTypes.Bitcoin.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BTCPayServer.Payments
|
||||
{
|
||||
public class BitcoinPaymentType : PaymentType
|
||||
{
|
||||
public static BitcoinPaymentType Instance { get; } = new BitcoinPaymentType();
|
||||
private BitcoinPaymentType()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string ToPrettyString() => "On-Chain";
|
||||
public override string GetId() => "BTCLike";
|
||||
}
|
||||
}
|
||||
19
BTCPayServer/Payments/PaymentTypes.Lightning.cs
Normal file
19
BTCPayServer/Payments/PaymentTypes.Lightning.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BTCPayServer.Payments
|
||||
{
|
||||
public class LightningPaymentType : PaymentType
|
||||
{
|
||||
public static LightningPaymentType Instance { get; } = new LightningPaymentType();
|
||||
private LightningPaymentType()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string ToPrettyString() => "Off-Chain";
|
||||
public override string GetId() => "LightningLike";
|
||||
}
|
||||
}
|
||||
@@ -8,15 +8,51 @@ namespace BTCPayServer.Payments
|
||||
/// <summary>
|
||||
/// The different ways to pay an invoice
|
||||
/// </summary>
|
||||
public enum PaymentTypes
|
||||
public static class PaymentTypes
|
||||
{
|
||||
/// <summary>
|
||||
/// On-Chain UTXO based, bitcoin compatible
|
||||
/// </summary>
|
||||
BTCLike,
|
||||
public static BitcoinPaymentType BTCLike => BitcoinPaymentType.Instance;
|
||||
/// <summary>
|
||||
/// Lightning payment
|
||||
/// </summary>
|
||||
LightningLike
|
||||
public static LightningPaymentType LightningLike => LightningPaymentType.Instance;
|
||||
|
||||
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;
|
||||
default:
|
||||
type = null;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static PaymentType Parse(string paymentType)
|
||||
{
|
||||
if (!TryParse(paymentType, out var result))
|
||||
throw new FormatException("Invalid payment type");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class PaymentType
|
||||
{
|
||||
public abstract string ToPrettyString();
|
||||
public override string ToString()
|
||||
{
|
||||
return GetId();
|
||||
}
|
||||
|
||||
public abstract string GetId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace BTCPayServer.Services.Invoices.Export
|
||||
PaymentId = pdata.GetPaymentId(),
|
||||
CryptoCode = cryptoCode,
|
||||
ConversionRate = pmethod.Rate,
|
||||
PaymentType = payment.GetPaymentMethodId().ToPrettyPaymentType(),
|
||||
PaymentType = payment.GetPaymentMethodId().PaymentType.ToPrettyString(),
|
||||
Destination = payment.GetCryptoPaymentData().GetDestination(Networks.GetNetwork<BTCPayNetworkBase>(cryptoCode)),
|
||||
Paid = pdata.GetValue().ToString(CultureInfo.InvariantCulture),
|
||||
PaidCurrency = Math.Round(pdata.GetValue() * pmethod.Rate, currency.NumberDecimalDigits).ToString(CultureInfo.InvariantCulture),
|
||||
|
||||
@@ -494,7 +494,7 @@ namespace BTCPayServer.Services.Invoices
|
||||
GetPaymentMethods().TryGetValue(paymentMethodId, out var data);
|
||||
return data;
|
||||
}
|
||||
public PaymentMethod GetPaymentMethod(BTCPayNetworkBase network, PaymentTypes paymentType, BTCPayNetworkProvider networkProvider)
|
||||
public PaymentMethod GetPaymentMethod(BTCPayNetworkBase network, PaymentType paymentType, BTCPayNetworkProvider networkProvider)
|
||||
{
|
||||
return GetPaymentMethod(new PaymentMethodId(network.CryptoCode, paymentType), networkProvider);
|
||||
}
|
||||
@@ -722,7 +722,7 @@ namespace BTCPayServer.Services.Invoices
|
||||
public PaymentMethodId GetId()
|
||||
{
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
return new PaymentMethodId(CryptoCode, string.IsNullOrEmpty(PaymentType) ? PaymentTypes.BTCLike : Enum.Parse<PaymentTypes>(PaymentType));
|
||||
return new PaymentMethodId(CryptoCode, string.IsNullOrEmpty(PaymentType) ? PaymentTypes.BTCLike : PaymentTypes.Parse(PaymentType));
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
}
|
||||
|
||||
@@ -985,7 +985,7 @@ namespace BTCPayServer.Services.Invoices
|
||||
public PaymentMethodId GetPaymentMethodId()
|
||||
{
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
return new PaymentMethodId(CryptoCode ?? "BTC", string.IsNullOrEmpty(CryptoPaymentDataType) ? PaymentTypes.BTCLike : Enum.Parse<PaymentTypes>(CryptoPaymentDataType));
|
||||
return new PaymentMethodId(CryptoCode ?? "BTC", string.IsNullOrEmpty(CryptoPaymentDataType) ? PaymentTypes.BTCLike : PaymentTypes.Parse(CryptoPaymentDataType));
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
}
|
||||
|
||||
@@ -1020,7 +1020,7 @@ namespace BTCPayServer.Services.Invoices
|
||||
bool PaymentCompleted(PaymentEntity entity, BTCPayNetworkBase network);
|
||||
bool PaymentConfirmed(PaymentEntity entity, SpeedPolicy speedPolicy, BTCPayNetworkBase network);
|
||||
|
||||
PaymentTypes GetPaymentType();
|
||||
PaymentType GetPaymentType();
|
||||
string GetDestination(BTCPayNetworkBase network);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace BTCPayServer.Services.Invoices
|
||||
_Inner.TryGetValue(paymentMethodId, out var value);
|
||||
return value;
|
||||
}
|
||||
public PaymentMethod TryGet(string network, PaymentTypes paymentType)
|
||||
public PaymentMethod TryGet(string network, PaymentType paymentType)
|
||||
{
|
||||
if (network == null)
|
||||
throw new ArgumentNullException(nameof(network));
|
||||
|
||||
Reference in New Issue
Block a user