Refactor: Add DeserializePaymentData at the PaymentType level

This commit is contained in:
nicolas.dorier 2019-06-04 09:16:18 +09:00
parent 1e77546251
commit 8ffd182b98
No known key found for this signature in database
GPG key ID: 6618763EF09186FE
5 changed files with 45 additions and 30 deletions

View file

@ -22,13 +22,14 @@ namespace BTCPayServer
}
}
BTCPayNetworkProvider(BTCPayNetworkProvider filtered, string[] cryptoCodes)
BTCPayNetworkProvider(BTCPayNetworkProvider unfiltered, string[] cryptoCodes)
{
NetworkType = filtered.NetworkType;
_NBXplorerNetworkProvider = new NBXplorerNetworkProvider(filtered.NetworkType);
UnfilteredNetworks = unfiltered.UnfilteredNetworks ?? unfiltered;
NetworkType = unfiltered.NetworkType;
_NBXplorerNetworkProvider = new NBXplorerNetworkProvider(unfiltered.NetworkType);
_Networks = new Dictionary<string, BTCPayNetworkBase>();
cryptoCodes = cryptoCodes.Select(c => c.ToUpperInvariant()).ToArray();
foreach (var network in filtered._Networks)
foreach (var network in unfiltered._Networks)
{
if(cryptoCodes.Contains(network.Key))
{
@ -37,9 +38,12 @@ namespace BTCPayServer
}
}
public BTCPayNetworkProvider UnfilteredNetworks { get; }
public NetworkType NetworkType { get; private set; }
public BTCPayNetworkProvider(NetworkType networkType)
{
UnfilteredNetworks = this;
_NBXplorerNetworkProvider = new NBXplorerNetworkProvider(networkType);
NetworkType = networkType;
InitBitcoin();

View file

@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Payments.Bitcoin;
using BTCPayServer.Services.Invoices;
using Newtonsoft.Json;
namespace BTCPayServer.Payments
{
@ -15,5 +18,10 @@ namespace BTCPayServer.Payments
public override string ToPrettyString() => "On-Chain";
public override string GetId() => "BTCLike";
public override CryptoPaymentData DeserializePaymentData(string cryptoPaymentData)
{
return JsonConvert.DeserializeObject<BitcoinLikePaymentData>(cryptoPaymentData);
}
}
}

View file

@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Services.Invoices;
using Newtonsoft.Json;
namespace BTCPayServer.Payments
{
@ -15,5 +17,10 @@ namespace BTCPayServer.Payments
public override string ToPrettyString() => "Off-Chain";
public override string GetId() => "LightningLike";
public override CryptoPaymentData DeserializePaymentData(string cryptoPaymentData)
{
return JsonConvert.DeserializeObject<Payments.Lightning.LightningLikePaymentData>(cryptoPaymentData);
}
}
}

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Services.Invoices;
namespace BTCPayServer.Payments
{
@ -54,5 +55,6 @@ namespace BTCPayServer.Payments
}
public abstract string GetId();
public abstract CryptoPaymentData DeserializePaymentData(string cryptoPaymentData);
}
}

View file

@ -513,7 +513,7 @@ namespace BTCPayServer.Services.Invoices
r.CryptoCode = paymentMethodId.CryptoCode;
r.PaymentType = paymentMethodId.PaymentType.ToString();
r.ParentEntity = this;
r.Network = Networks?.GetNetwork<BTCPayNetworkBase>(r.CryptoCode);
r.Network = Networks?.UnfilteredNetworks.GetNetwork<BTCPayNetworkBase>(r.CryptoCode);
paymentMethods.Add(r);
}
}
@ -918,37 +918,31 @@ namespace BTCPayServer.Services.Invoices
public CryptoPaymentData GetCryptoPaymentData()
{
var paymentMethodId = GetPaymentMethodId();
if (paymentMethodId.PaymentType == PaymentTypes.LightningLike)
CryptoPaymentData paymentData = null;
#pragma warning disable CS0618 // Type or member is obsolete
return JsonConvert.DeserializeObject<Payments.Lightning.LightningLikePaymentData>(CryptoPaymentData);
#pragma warning restore CS0618 // Type or member is obsolete
if (string.IsNullOrEmpty(CryptoPaymentData))
{
// For invoices created when CryptoPaymentDataType was not existing, we just consider that it is a RBFed payment for safety
var bitcoin = new BitcoinLikePaymentData();
bitcoin.Outpoint = Outpoint;
bitcoin.Output = Output;
bitcoin.RBF = true;
bitcoin.ConfirmationCount = 0;
bitcoin.Legacy = true;
bitcoin.Output = Output;
bitcoin.Outpoint = Outpoint;
paymentData = bitcoin;
}
else
{
#pragma warning disable CS0618
BitcoinLikePaymentData paymentData;
if (string.IsNullOrEmpty(CryptoPaymentDataType))
paymentData = GetPaymentMethodId().PaymentType.DeserializePaymentData(CryptoPaymentData);
if (paymentData is BitcoinLikePaymentData bitcoin)
{
// For invoices created when CryptoPaymentDataType was not existing, we just consider that it is a RBFed payment for safety
paymentData = new BitcoinLikePaymentData();
paymentData.Outpoint = Outpoint;
paymentData.Output = Output;
paymentData.RBF = true;
paymentData.ConfirmationCount = 0;
paymentData.Legacy = true;
return paymentData;
bitcoin.Output = Output;
bitcoin.Outpoint = Outpoint;
}
paymentData =
JsonConvert.DeserializeObject<BitcoinLikePaymentData>(CryptoPaymentData);
// legacy
paymentData.Output = Output;
paymentData.Outpoint = Outpoint;
#pragma warning restore CS0618
return paymentData;
}
return paymentData;
}
public PaymentEntity SetCryptoPaymentData(CryptoPaymentData cryptoPaymentData)