mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 22:14:26 +01:00
Isolating code of on chain specific payment in its own folder
This commit is contained in:
@@ -74,7 +74,7 @@ namespace BTCPayServer.Controllers
|
|||||||
cryptoPayment.Due = accounting.Due.ToString() + $" {paymentNetwork.CryptoCode}";
|
cryptoPayment.Due = accounting.Due.ToString() + $" {paymentNetwork.CryptoCode}";
|
||||||
cryptoPayment.Paid = accounting.CryptoPaid.ToString() + $" {paymentNetwork.CryptoCode}";
|
cryptoPayment.Paid = accounting.CryptoPaid.ToString() + $" {paymentNetwork.CryptoCode}";
|
||||||
|
|
||||||
var onchainMethod = data.GetPaymentMethod() as BitcoinLikeOnChainPaymentMethod;
|
var onchainMethod = data.GetPaymentMethod() as Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod;
|
||||||
if(onchainMethod != null)
|
if(onchainMethod != null)
|
||||||
{
|
{
|
||||||
cryptoPayment.Address = onchainMethod.DepositAddress.ToString();
|
cryptoPayment.Address = onchainMethod.DepositAddress.ToString();
|
||||||
@@ -89,7 +89,7 @@ namespace BTCPayServer.Controllers
|
|||||||
.Where(p => p.GetCryptoDataId().PaymentType == PaymentTypes.BTCLike)
|
.Where(p => p.GetCryptoDataId().PaymentType == PaymentTypes.BTCLike)
|
||||||
.Select(async payment =>
|
.Select(async payment =>
|
||||||
{
|
{
|
||||||
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
|
var paymentData = (Payments.Bitcoin.BitcoinLikePaymentData)payment.GetCryptoPaymentData();
|
||||||
var m = new InvoiceDetailsModel.Payment();
|
var m = new InvoiceDetailsModel.Payment();
|
||||||
var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
|
var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
|
||||||
m.CryptoCode = payment.GetCryptoCode();
|
m.CryptoCode = payment.GetCryptoCode();
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ namespace BTCPayServer.Controllers
|
|||||||
{
|
{
|
||||||
CryptoData cryptoData = new CryptoData();
|
CryptoData cryptoData = new CryptoData();
|
||||||
cryptoData.SetId(new CryptoDataId(q.network.CryptoCode, PaymentTypes.BTCLike));
|
cryptoData.SetId(new CryptoDataId(q.network.CryptoCode, PaymentTypes.BTCLike));
|
||||||
BitcoinLikeOnChainPaymentMethod onchainMethod = new BitcoinLikeOnChainPaymentMethod();
|
Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod onchainMethod = new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod();
|
||||||
onchainMethod.FeeRate = (await q.getFeeRate);
|
onchainMethod.FeeRate = (await q.getFeeRate);
|
||||||
onchainMethod.TxFee = GetTxFee(storeBlob, onchainMethod.FeeRate); // assume price for 100 bytes
|
onchainMethod.TxFee = GetTxFee(storeBlob, onchainMethod.FeeRate); // assume price for 100 bytes
|
||||||
cryptoData.Rate = await q.getRate;
|
cryptoData.Rate = await q.getRate;
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ namespace BTCPayServer.Hosting
|
|||||||
});
|
});
|
||||||
|
|
||||||
services.AddSingleton<IHostedService, NBXplorerWaiters>();
|
services.AddSingleton<IHostedService, NBXplorerWaiters>();
|
||||||
services.AddSingleton<IHostedService, NBXplorerListener>();
|
services.AddSingleton<IHostedService, Payments.Bitcoin.NBXplorerListener>();
|
||||||
services.AddSingleton<IHostedService, InvoiceNotificationManager>();
|
services.AddSingleton<IHostedService, InvoiceNotificationManager>();
|
||||||
services.AddSingleton<IHostedService, InvoiceWatcher>();
|
services.AddSingleton<IHostedService, InvoiceWatcher>();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BTCPayServer.Services.Invoices;
|
||||||
|
using NBitcoin;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace BTCPayServer.Payments.Bitcoin
|
||||||
|
{
|
||||||
|
public class BitcoinLikeOnChainPaymentMethod : IPaymentMethod
|
||||||
|
{
|
||||||
|
public PaymentTypes GetPaymentType()
|
||||||
|
{
|
||||||
|
return PaymentTypes.BTCLike;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetPaymentDestination()
|
||||||
|
{
|
||||||
|
return DepositAddress?.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Money GetTxFee()
|
||||||
|
{
|
||||||
|
return TxFee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetPaymentDestination(string newPaymentDestination)
|
||||||
|
{
|
||||||
|
if (newPaymentDestination == null)
|
||||||
|
DepositAddress = null;
|
||||||
|
else
|
||||||
|
DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network);
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonIgnore]
|
||||||
|
public FeeRate FeeRate { get; set; }
|
||||||
|
[JsonIgnore]
|
||||||
|
public Money TxFee { get; set; }
|
||||||
|
[JsonIgnore]
|
||||||
|
public BitcoinAddress DepositAddress { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,40 +6,8 @@ using BTCPayServer.Services.Invoices;
|
|||||||
using NBitcoin;
|
using NBitcoin;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace BTCPayServer.Payments
|
namespace BTCPayServer.Payments.Bitcoin
|
||||||
{
|
{
|
||||||
public class BitcoinLikeOnChainPaymentMethod : IPaymentMethod
|
|
||||||
{
|
|
||||||
public PaymentTypes GetPaymentType()
|
|
||||||
{
|
|
||||||
return PaymentTypes.BTCLike;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string GetPaymentDestination()
|
|
||||||
{
|
|
||||||
return DepositAddress?.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Money GetTxFee()
|
|
||||||
{
|
|
||||||
return TxFee;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetPaymentDestination(string newPaymentDestination)
|
|
||||||
{
|
|
||||||
if (newPaymentDestination == null)
|
|
||||||
DepositAddress = null;
|
|
||||||
else
|
|
||||||
DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network);
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonIgnore]
|
|
||||||
public FeeRate FeeRate { get; set; }
|
|
||||||
[JsonIgnore]
|
|
||||||
public Money TxFee { get; set; }
|
|
||||||
[JsonIgnore]
|
|
||||||
public BitcoinAddress DepositAddress { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class BitcoinLikePaymentData : CryptoPaymentData
|
public class BitcoinLikePaymentData : CryptoPaymentData
|
||||||
{
|
{
|
||||||
@@ -17,8 +17,9 @@ using BTCPayServer.Services.Wallets;
|
|||||||
using NBitcoin;
|
using NBitcoin;
|
||||||
using NBXplorer.Models;
|
using NBXplorer.Models;
|
||||||
using BTCPayServer.Payments;
|
using BTCPayServer.Payments;
|
||||||
|
using BTCPayServer.HostedServices;
|
||||||
|
|
||||||
namespace BTCPayServer.HostedServices
|
namespace BTCPayServer.Payments.Bitcoin
|
||||||
{
|
{
|
||||||
public class NBXplorerListener : IHostedService
|
public class NBXplorerListener : IHostedService
|
||||||
{
|
{
|
||||||
@@ -353,7 +354,7 @@ namespace BTCPayServer.HostedServices
|
|||||||
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
|
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
|
||||||
var invoice = (await UpdatePaymentStates(wallet, invoiceId));
|
var invoice = (await UpdatePaymentStates(wallet, invoiceId));
|
||||||
var cryptoData = invoice.GetCryptoData(wallet.Network, PaymentTypes.BTCLike, _ExplorerClients.NetworkProviders);
|
var cryptoData = invoice.GetCryptoData(wallet.Network, PaymentTypes.BTCLike, _ExplorerClients.NetworkProviders);
|
||||||
var method = cryptoData.GetPaymentMethod() as Payments.BitcoinLikeOnChainPaymentMethod;
|
var method = cryptoData.GetPaymentMethod() as BitcoinLikeOnChainPaymentMethod;
|
||||||
if (method.DepositAddress.ScriptPubKey == paymentData.Output.ScriptPubKey && cryptoData.Calculate().Due > Money.Zero)
|
if (method.DepositAddress.ScriptPubKey == paymentData.Output.ScriptPubKey && cryptoData.Calculate().Due > Money.Zero)
|
||||||
{
|
{
|
||||||
var address = await wallet.ReserveAddressAsync(strategy);
|
var address = await wallet.ReserveAddressAsync(strategy);
|
||||||
16
BTCPayServer/Payments/IPaymentMethod.cs
Normal file
16
BTCPayServer/Payments/IPaymentMethod.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using NBitcoin;
|
||||||
|
|
||||||
|
namespace BTCPayServer.Payments
|
||||||
|
{
|
||||||
|
public interface IPaymentMethod
|
||||||
|
{
|
||||||
|
string GetPaymentDestination();
|
||||||
|
PaymentTypes GetPaymentType();
|
||||||
|
Money GetTxFee();
|
||||||
|
void SetPaymentDestination(string newPaymentDestination);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -366,8 +366,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
cryptoInfo.TxCount = accounting.TxCount;
|
cryptoInfo.TxCount = accounting.TxCount;
|
||||||
cryptoInfo.CryptoPaid = accounting.CryptoPaid.ToString();
|
cryptoInfo.CryptoPaid = accounting.CryptoPaid.ToString();
|
||||||
|
|
||||||
if (info.GetPaymentMethod() is BitcoinLikeOnChainPaymentMethod onchainMethod)
|
cryptoInfo.Address = info.GetPaymentMethod()?.GetPaymentDestination();
|
||||||
cryptoInfo.Address = onchainMethod.DepositAddress?.ToString();
|
|
||||||
cryptoInfo.ExRates = new Dictionary<string, double>
|
cryptoInfo.ExRates = new Dictionary<string, double>
|
||||||
{
|
{
|
||||||
{ ProductInformation.Currency, (double)cryptoInfo.Rate }
|
{ ProductInformation.Currency, (double)cryptoInfo.Rate }
|
||||||
@@ -524,14 +523,6 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
public Money NetworkFee { get; set; }
|
public Money NetworkFee { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IPaymentMethod
|
|
||||||
{
|
|
||||||
string GetPaymentDestination();
|
|
||||||
PaymentTypes GetPaymentType();
|
|
||||||
Money GetTxFee();
|
|
||||||
void SetPaymentDestination(string newPaymentDestination);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class CryptoDataId
|
public class CryptoDataId
|
||||||
{
|
{
|
||||||
public CryptoDataId(string cryptoCode, PaymentTypes paymentType)
|
public CryptoDataId(string cryptoCode, PaymentTypes paymentType)
|
||||||
@@ -627,7 +618,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
// Legacy, old code does not have PaymentMethods
|
// Legacy, old code does not have PaymentMethods
|
||||||
if (string.IsNullOrEmpty(PaymentType) || PaymentMethod == null)
|
if (string.IsNullOrEmpty(PaymentType) || PaymentMethod == null)
|
||||||
{
|
{
|
||||||
return new BitcoinLikeOnChainPaymentMethod()
|
return new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod()
|
||||||
{
|
{
|
||||||
FeeRate = FeeRate,
|
FeeRate = FeeRate,
|
||||||
DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork),
|
DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork),
|
||||||
@@ -639,7 +630,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
|
|
||||||
if (GetId().PaymentType == PaymentTypes.BTCLike)
|
if (GetId().PaymentType == PaymentTypes.BTCLike)
|
||||||
{
|
{
|
||||||
var method = DeserializePaymentMethod<BitcoinLikeOnChainPaymentMethod>(PaymentMethod);
|
var method = DeserializePaymentMethod<Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod>(PaymentMethod);
|
||||||
method.TxFee = TxFee;
|
method.TxFee = TxFee;
|
||||||
method.DepositAddress = BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork);
|
method.DepositAddress = BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork);
|
||||||
method.FeeRate = FeeRate;
|
method.FeeRate = FeeRate;
|
||||||
@@ -665,7 +656,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
else if (PaymentType != paymentMethod.GetPaymentType().ToString())
|
else if (PaymentType != paymentMethod.GetPaymentType().ToString())
|
||||||
throw new InvalidOperationException("Invalid payment method affected");
|
throw new InvalidOperationException("Invalid payment method affected");
|
||||||
|
|
||||||
if (paymentMethod is BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod)
|
if (paymentMethod is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod)
|
||||||
{
|
{
|
||||||
TxFee = bitcoinPaymentMethod.TxFee;
|
TxFee = bitcoinPaymentMethod.TxFee;
|
||||||
FeeRate = bitcoinPaymentMethod.FeeRate;
|
FeeRate = bitcoinPaymentMethod.FeeRate;
|
||||||
@@ -673,7 +664,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
}
|
}
|
||||||
var jobj = JObject.Parse(JsonConvert.SerializeObject(paymentMethod));
|
var jobj = JObject.Parse(JsonConvert.SerializeObject(paymentMethod));
|
||||||
PaymentMethod = jobj;
|
PaymentMethod = jobj;
|
||||||
|
|
||||||
#pragma warning restore CS0618 // Type or member is obsolete
|
#pragma warning restore CS0618 // Type or member is obsolete
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -792,7 +783,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
if (string.IsNullOrEmpty(CryptoPaymentDataType))
|
if (string.IsNullOrEmpty(CryptoPaymentDataType))
|
||||||
{
|
{
|
||||||
// In case this is a payment done before this update, consider it unconfirmed with RBF for safety
|
// In case this is a payment done before this update, consider it unconfirmed with RBF for safety
|
||||||
var paymentData = new BitcoinLikePaymentData();
|
var paymentData = new Payments.Bitcoin.BitcoinLikePaymentData();
|
||||||
paymentData.Outpoint = Outpoint;
|
paymentData.Outpoint = Outpoint;
|
||||||
paymentData.Output = Output;
|
paymentData.Output = Output;
|
||||||
paymentData.RBF = true;
|
paymentData.RBF = true;
|
||||||
@@ -802,7 +793,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
}
|
}
|
||||||
if (GetCryptoDataId().PaymentType == PaymentTypes.BTCLike)
|
if (GetCryptoDataId().PaymentType == PaymentTypes.BTCLike)
|
||||||
{
|
{
|
||||||
var paymentData = JsonConvert.DeserializeObject<BitcoinLikePaymentData>(CryptoPaymentData);
|
var paymentData = JsonConvert.DeserializeObject<Payments.Bitcoin.BitcoinLikePaymentData>(CryptoPaymentData);
|
||||||
// legacy
|
// legacy
|
||||||
paymentData.Output = Output;
|
paymentData.Output = Output;
|
||||||
paymentData.Outpoint = Outpoint;
|
paymentData.Outpoint = Outpoint;
|
||||||
@@ -816,7 +807,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
public void SetCryptoPaymentData(CryptoPaymentData cryptoPaymentData)
|
public void SetCryptoPaymentData(CryptoPaymentData cryptoPaymentData)
|
||||||
{
|
{
|
||||||
#pragma warning disable CS0618
|
#pragma warning disable CS0618
|
||||||
if (cryptoPaymentData is BitcoinLikePaymentData paymentData)
|
if (cryptoPaymentData is Payments.Bitcoin.BitcoinLikePaymentData paymentData)
|
||||||
{
|
{
|
||||||
// Legacy
|
// Legacy
|
||||||
Outpoint = paymentData.Outpoint;
|
Outpoint = paymentData.Outpoint;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ using BTCPayServer.Data;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using BTCPayServer.Models.InvoicingModels;
|
using BTCPayServer.Models.InvoicingModels;
|
||||||
using BTCPayServer.Logging;
|
using BTCPayServer.Logging;
|
||||||
|
using BTCPayServer.Payments;
|
||||||
|
|
||||||
namespace BTCPayServer.Services.Invoices
|
namespace BTCPayServer.Services.Invoices
|
||||||
{
|
{
|
||||||
@@ -166,7 +167,7 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
ScriptId hash = null;
|
ScriptId hash = null;
|
||||||
if (cryptoData.GetId().PaymentType == Payments.PaymentTypes.BTCLike)
|
if (cryptoData.GetId().PaymentType == Payments.PaymentTypes.BTCLike)
|
||||||
{
|
{
|
||||||
hash = ((Payments.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash;
|
hash = ((Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash;
|
||||||
}
|
}
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user