mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-03-03 17:36:59 +01:00
Isolate PaymentMethodId in its own class, generalise DerivationStrategy
This commit is contained in:
parent
b4f4401cdc
commit
2e45c8b190
10 changed files with 131 additions and 69 deletions
|
@ -10,6 +10,7 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Services.Invoices;
|
||||
using BTCPayServer.Payments;
|
||||
|
||||
namespace BTCPayServer.Controllers
|
||||
{
|
||||
|
@ -71,7 +72,7 @@ namespace BTCPayServer.Controllers
|
|||
if (cryptoCode == null)
|
||||
cryptoCode = "BTC";
|
||||
var network = _NetworkProvider.GetNetwork(cryptoCode);
|
||||
if (network == null || invoice == null || invoice.IsExpired() || !invoice.Support(new Services.Invoices.PaymentMethodId(cryptoCode, Payments.PaymentTypes.BTCLike)))
|
||||
if (network == null || invoice == null || invoice.IsExpired() || !invoice.Support(new PaymentMethodId(cryptoCode, Payments.PaymentTypes.BTCLike)))
|
||||
return NotFound();
|
||||
|
||||
var wallet = _WalletProvider.GetWallet(network);
|
||||
|
|
|
@ -88,7 +88,7 @@ namespace BTCPayServer.Controllers
|
|||
{
|
||||
InvoiceTime = DateTimeOffset.UtcNow
|
||||
};
|
||||
entity.SetDerivationStrategies(derivationStrategies);
|
||||
entity.SetPaymentMethodFactories(derivationStrategies);
|
||||
|
||||
var storeBlob = store.GetStoreBlob();
|
||||
Uri notificationUri = Uri.IsWellFormedUriString(invoice.NotificationURL, UriKind.Absolute) ? new Uri(invoice.NotificationURL, UriKind.Absolute) : null;
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Payments;
|
||||
using BTCPayServer.Services.Invoices;
|
||||
using NBitcoin;
|
||||
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Payments;
|
||||
using BTCPayServer.Services.Invoices;
|
||||
using NBitcoin;
|
||||
using NBXplorer.DerivationStrategy;
|
||||
|
||||
namespace BTCPayServer
|
||||
{
|
||||
public class DerivationStrategy
|
||||
public class DerivationStrategy : IPaymentMethodFactory
|
||||
{
|
||||
private DerivationStrategyBase _DerivationStrategy;
|
||||
private BTCPayNetwork _Network;
|
||||
|
@ -32,6 +34,8 @@ namespace BTCPayServer
|
|||
|
||||
public DerivationStrategyBase DerivationStrategyBase { get { return this._DerivationStrategy; } }
|
||||
|
||||
public PaymentMethodId PaymentId => new PaymentMethodId(Network.CryptoCode, PaymentTypes.BTCLike);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return _DerivationStrategy.ToString();
|
||||
|
|
|
@ -19,6 +19,7 @@ using Microsoft.Extensions.Hosting;
|
|||
using BTCPayServer.Events;
|
||||
using NBXplorer;
|
||||
using BTCPayServer.Services.Invoices;
|
||||
using BTCPayServer.Payments;
|
||||
|
||||
namespace BTCPayServer.HostedServices
|
||||
{
|
||||
|
|
|
@ -351,7 +351,8 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||
|
||||
private DerivationStrategyBase GetDerivationStrategy(InvoiceEntity invoice, BTCPayNetwork network)
|
||||
{
|
||||
return invoice.GetDerivationStrategies(_ExplorerClients.NetworkProviders)
|
||||
return invoice.GetPaymentMethodFactories(_ExplorerClients.NetworkProviders)
|
||||
.OfType<DerivationStrategy>()
|
||||
.Where(d => d.Network.CryptoCode == network.CryptoCode)
|
||||
.Select(d => d.DerivationStrategyBase)
|
||||
.FirstOrDefault();
|
||||
|
@ -395,7 +396,8 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||
|
||||
private DerivationStrategyBase GetStrategy(string cryptoCode, InvoiceEntity invoice)
|
||||
{
|
||||
foreach (var derivationStrategy in invoice.GetDerivationStrategies(_ExplorerClients.NetworkProviders))
|
||||
foreach (var derivationStrategy in invoice.GetPaymentMethodFactories(_ExplorerClients.NetworkProviders)
|
||||
.OfType<DerivationStrategy>())
|
||||
{
|
||||
if (derivationStrategy.Network.CryptoCode == cryptoCode)
|
||||
{
|
||||
|
|
13
BTCPayServer/Payments/IPaymentMethodFactory.cs
Normal file
13
BTCPayServer/Payments/IPaymentMethodFactory.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Services.Invoices;
|
||||
|
||||
namespace BTCPayServer.Payments
|
||||
{
|
||||
public interface IPaymentMethodFactory
|
||||
{
|
||||
PaymentMethodId PaymentId { get; }
|
||||
}
|
||||
}
|
62
BTCPayServer/Payments/PaymentMethodId.cs
Normal file
62
BTCPayServer/Payments/PaymentMethodId.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BTCPayServer.Payments
|
||||
{
|
||||
public class PaymentMethodId
|
||||
{
|
||||
public PaymentMethodId(string cryptoCode, PaymentTypes paymentType)
|
||||
{
|
||||
if (cryptoCode == null)
|
||||
throw new ArgumentNullException(nameof(cryptoCode));
|
||||
PaymentType = paymentType;
|
||||
CryptoCode = cryptoCode;
|
||||
}
|
||||
public string CryptoCode { get; private set; }
|
||||
public PaymentTypes PaymentType { get; private set; }
|
||||
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
PaymentMethodId item = obj as PaymentMethodId;
|
||||
if (item == null)
|
||||
return false;
|
||||
return ToString().Equals(item.ToString(), StringComparison.InvariantCulture);
|
||||
}
|
||||
public static bool operator ==(PaymentMethodId a, PaymentMethodId b)
|
||||
{
|
||||
if (System.Object.ReferenceEquals(a, b))
|
||||
return true;
|
||||
if (((object)a == null) || ((object)b == null))
|
||||
return false;
|
||||
return a.ToString() == b.ToString();
|
||||
}
|
||||
|
||||
public static bool operator !=(PaymentMethodId a, PaymentMethodId b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
#pragma warning disable CA1307 // Specify StringComparison
|
||||
return ToString().GetHashCode();
|
||||
#pragma warning restore CA1307 // Specify StringComparison
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (PaymentType == PaymentTypes.BTCLike)
|
||||
return CryptoCode;
|
||||
return CryptoCode + "_" + PaymentType.ToString();
|
||||
}
|
||||
|
||||
public static PaymentMethodId Parse(string str)
|
||||
{
|
||||
var parts = str.Split('_');
|
||||
return new PaymentMethodId(parts[0], parts.Length == 1 ? PaymentTypes.BTCLike : Enum.Parse<PaymentTypes>(parts[1]));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -162,14 +162,14 @@ namespace BTCPayServer.Services.Invoices
|
|||
set;
|
||||
}
|
||||
|
||||
[Obsolete("Use GetDerivationStrategies instead")]
|
||||
[Obsolete("Use GetPaymentMethodFactories() instead")]
|
||||
public string DerivationStrategies
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public IEnumerable<DerivationStrategy> GetDerivationStrategies(BTCPayNetworkProvider networks)
|
||||
public IEnumerable<IPaymentMethodFactory> GetPaymentMethodFactories(BTCPayNetworkProvider networks)
|
||||
{
|
||||
#pragma warning disable CS0618
|
||||
bool btcReturned = false;
|
||||
|
@ -178,12 +178,13 @@ namespace BTCPayServer.Services.Invoices
|
|||
JObject strategies = JObject.Parse(DerivationStrategies);
|
||||
foreach (var strat in strategies.Properties())
|
||||
{
|
||||
var network = networks.GetNetwork(strat.Name);
|
||||
var paymentMethodId = PaymentMethodId.Parse(strat.Name);
|
||||
var network = networks.GetNetwork(paymentMethodId.CryptoCode);
|
||||
if (network != null)
|
||||
{
|
||||
if (network == networks.BTC)
|
||||
if (network == networks.BTC && paymentMethodId.PaymentType == PaymentTypes.BTCLike)
|
||||
btcReturned = true;
|
||||
yield return BTCPayServer.DerivationStrategy.Parse(strat.Value.Value<string>(), network);
|
||||
yield return Deserialize(paymentMethodId, strat.Value, network);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -198,20 +199,52 @@ namespace BTCPayServer.Services.Invoices
|
|||
#pragma warning restore CS0618
|
||||
}
|
||||
|
||||
internal void SetDerivationStrategies(IEnumerable<DerivationStrategy> derivationStrategies)
|
||||
internal void SetPaymentMethodFactories(IEnumerable<IPaymentMethodFactory> derivationStrategies)
|
||||
{
|
||||
JObject obj = new JObject();
|
||||
foreach (var strat in derivationStrategies)
|
||||
{
|
||||
obj.Add(strat.Network.CryptoCode, new JValue(strat.DerivationStrategyBase.ToString()));
|
||||
obj.Add(strat.PaymentId.ToString(), Serialize(strat));
|
||||
#pragma warning disable CS0618
|
||||
if (strat.Network.IsBTC)
|
||||
DerivationStrategy = strat.DerivationStrategyBase.ToString();
|
||||
if (strat.PaymentId.PaymentType == PaymentTypes.BTCLike && strat.PaymentId.CryptoCode == "BTC")
|
||||
DerivationStrategy = ((JValue)Serialize(strat)).Value<string>();
|
||||
}
|
||||
DerivationStrategies = JsonConvert.SerializeObject(obj);
|
||||
#pragma warning restore CS0618
|
||||
}
|
||||
|
||||
|
||||
private IPaymentMethodFactory Deserialize(PaymentMethodId paymentMethodId, JToken value, BTCPayNetwork network)
|
||||
{
|
||||
// Legacy
|
||||
if (paymentMethodId.PaymentType == PaymentTypes.BTCLike)
|
||||
{
|
||||
return BTCPayServer.DerivationStrategy.Parse(((JValue)value).Value<string>(), network);
|
||||
}
|
||||
//////////
|
||||
else // if(paymentMethodId.PaymentType == PaymentTypes.Lightning)
|
||||
{
|
||||
// return JsonConvert.Deserialize<T>();
|
||||
}
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
JToken Serialize(IPaymentMethodFactory factory)
|
||||
{
|
||||
// Legacy
|
||||
if(factory.PaymentId.PaymentType == PaymentTypes.BTCLike)
|
||||
{
|
||||
return new JValue(((DerivationStrategy)factory).DerivationStrategyBase.ToString());
|
||||
}
|
||||
//////////////
|
||||
else
|
||||
{
|
||||
var str = JsonConvert.SerializeObject(factory);
|
||||
return JObject.Parse(str);
|
||||
}
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public string Status
|
||||
{
|
||||
get;
|
||||
|
@ -512,61 +545,6 @@ namespace BTCPayServer.Services.Invoices
|
|||
public Money NetworkFee { get; set; }
|
||||
}
|
||||
|
||||
public class PaymentMethodId
|
||||
{
|
||||
public PaymentMethodId(string cryptoCode, PaymentTypes paymentType)
|
||||
{
|
||||
if (cryptoCode == null)
|
||||
throw new ArgumentNullException(nameof(cryptoCode));
|
||||
PaymentType = paymentType;
|
||||
CryptoCode = cryptoCode;
|
||||
}
|
||||
public string CryptoCode { get; private set; }
|
||||
public PaymentTypes PaymentType { get; private set; }
|
||||
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
PaymentMethodId item = obj as PaymentMethodId;
|
||||
if (item == null)
|
||||
return false;
|
||||
return ToString().Equals(item.ToString(), StringComparison.InvariantCulture);
|
||||
}
|
||||
public static bool operator ==(PaymentMethodId a, PaymentMethodId b)
|
||||
{
|
||||
if (System.Object.ReferenceEquals(a, b))
|
||||
return true;
|
||||
if (((object)a == null) || ((object)b == null))
|
||||
return false;
|
||||
return a.ToString() == b.ToString();
|
||||
}
|
||||
|
||||
public static bool operator !=(PaymentMethodId a, PaymentMethodId b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
#pragma warning disable CA1307 // Specify StringComparison
|
||||
return ToString().GetHashCode();
|
||||
#pragma warning restore CA1307 // Specify StringComparison
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (PaymentType == PaymentTypes.BTCLike)
|
||||
return CryptoCode;
|
||||
return CryptoCode + "_" + PaymentType.ToString();
|
||||
}
|
||||
|
||||
public static PaymentMethodId Parse(string str)
|
||||
{
|
||||
var parts = str.Split('_');
|
||||
return new PaymentMethodId(parts[0], parts.Length == 1 ? PaymentTypes.BTCLike : Enum.Parse<PaymentTypes>(parts[1]));
|
||||
}
|
||||
}
|
||||
|
||||
public class PaymentMethod
|
||||
{
|
||||
[JsonIgnore]
|
||||
|
|
Loading…
Add table
Reference in a new issue