Refactor: Remove PrepareInvoiceDTO

This commit is contained in:
nicolas.dorier 2019-06-04 10:11:52 +09:00
parent ac76220349
commit 01787e2662
No known key found for this signature in database
GPG key ID: 6618763EF09186FE
7 changed files with 53 additions and 86 deletions

View file

@ -107,7 +107,7 @@ namespace BTCPayServer.Tests
new BitcoinLikePaymentHandler(null, networkProvider, null, null), new BitcoinLikePaymentHandler(null, networkProvider, null, null),
new LightningLikePaymentHandler(null, null, networkProvider, null), new LightningLikePaymentHandler(null, null, networkProvider, null),
}); });
InvoiceEntity invoiceEntity = new InvoiceEntity() { PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; InvoiceEntity invoiceEntity = new InvoiceEntity();
invoiceEntity.Payments = new System.Collections.Generic.List<PaymentEntity>(); invoiceEntity.Payments = new System.Collections.Generic.List<PaymentEntity>();
invoiceEntity.ProductInformation = new ProductInformation() {Price = 100}; invoiceEntity.ProductInformation = new ProductInformation() {Price = 100};
PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary(); PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
@ -131,8 +131,7 @@ namespace BTCPayServer.Tests
{ {
Accounted = true, Accounted = true,
CryptoCode = "BTC", CryptoCode = "BTC",
NetworkFee = 0.00000100m, NetworkFee = 0.00000100m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
} }
.SetCryptoPaymentData(new BitcoinLikePaymentData() .SetCryptoPaymentData(new BitcoinLikePaymentData()
{ {
@ -144,8 +143,7 @@ namespace BTCPayServer.Tests
{ {
Accounted = true, Accounted = true,
CryptoCode = "BTC", CryptoCode = "BTC",
NetworkFee = 0.00000100m, NetworkFee = 0.00000100m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
} }
.SetCryptoPaymentData(new BitcoinLikePaymentData() .SetCryptoPaymentData(new BitcoinLikePaymentData()
{ {
@ -216,7 +214,7 @@ namespace BTCPayServer.Tests
new BitcoinLikePaymentHandler(null, networkProvider, null, null), new BitcoinLikePaymentHandler(null, networkProvider, null, null),
new LightningLikePaymentHandler(null, null, networkProvider, null), new LightningLikePaymentHandler(null, null, networkProvider, null),
}); });
var entity = new InvoiceEntity() {PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; var entity = new InvoiceEntity();
#pragma warning disable CS0618 #pragma warning disable CS0618
entity.Payments = new System.Collections.Generic.List<PaymentEntity>(); entity.Payments = new System.Collections.Generic.List<PaymentEntity>();
entity.SetPaymentMethod(new PaymentMethod() entity.SetPaymentMethod(new PaymentMethod()
@ -234,8 +232,7 @@ namespace BTCPayServer.Tests
{ {
Output = new TxOut(Money.Coins(0.5m), new Key()), Output = new TxOut(Money.Coins(0.5m), new Key()),
Accounted = true, Accounted = true,
NetworkFee = 0.1m, NetworkFee = 0.1m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
}); });
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
@ -247,8 +244,7 @@ namespace BTCPayServer.Tests
{ {
Output = new TxOut(Money.Coins(0.2m), new Key()), Output = new TxOut(Money.Coins(0.2m), new Key()),
Accounted = true, Accounted = true,
NetworkFee = 0.1m, NetworkFee = 0.1m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
}); });
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
@ -259,8 +255,7 @@ namespace BTCPayServer.Tests
{ {
Output = new TxOut(Money.Coins(0.6m), new Key()), Output = new TxOut(Money.Coins(0.6m), new Key()),
Accounted = true, Accounted = true,
NetworkFee = 0.1m, NetworkFee = 0.1m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
}); });
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
@ -270,15 +265,14 @@ namespace BTCPayServer.Tests
entity.Payments.Add(new PaymentEntity() entity.Payments.Add(new PaymentEntity()
{ {
Output = new TxOut(Money.Coins(0.2m), new Key()), Output = new TxOut(Money.Coins(0.2m), new Key()),
Accounted = true, Accounted = true
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
}); });
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
Assert.Equal(Money.Zero, accounting.Due); Assert.Equal(Money.Zero, accounting.Due);
Assert.Equal(Money.Coins(1.3m), accounting.TotalDue); Assert.Equal(Money.Coins(1.3m), accounting.TotalDue);
entity = new InvoiceEntity() {PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; entity = new InvoiceEntity();
entity.ProductInformation = new ProductInformation() {Price = 5000}; entity.ProductInformation = new ProductInformation() {Price = 5000};
PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary(); PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
paymentMethods.Add( paymentMethods.Add(
@ -301,8 +295,7 @@ namespace BTCPayServer.Tests
CryptoCode = "BTC", CryptoCode = "BTC",
Output = new TxOut(Money.Coins(1.0m), new Key()), Output = new TxOut(Money.Coins(1.0m), new Key()),
Accounted = true, Accounted = true,
NetworkFee = 0.1m, NetworkFee = 0.1m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
}); });
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
@ -325,8 +318,7 @@ namespace BTCPayServer.Tests
CryptoCode = "LTC", CryptoCode = "LTC",
Output = new TxOut(Money.Coins(1.0m), new Key()), Output = new TxOut(Money.Coins(1.0m), new Key()),
Accounted = true, Accounted = true,
NetworkFee = 0.01m, NetworkFee = 0.01m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
}); });
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
@ -351,8 +343,7 @@ namespace BTCPayServer.Tests
CryptoCode = "BTC", CryptoCode = "BTC",
Output = new TxOut(remaining, new Key()), Output = new TxOut(remaining, new Key()),
Accounted = true, Accounted = true,
NetworkFee = 0.1m, NetworkFee = 0.1m
PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary
}); });
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
@ -423,7 +414,7 @@ namespace BTCPayServer.Tests
new BitcoinLikePaymentHandler(null, networkProvider, null, null), new BitcoinLikePaymentHandler(null, networkProvider, null, null),
new LightningLikePaymentHandler(null, null, networkProvider, null), new LightningLikePaymentHandler(null, null, networkProvider, null),
}); });
var entity = new InvoiceEntity() {PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; var entity = new InvoiceEntity();
#pragma warning disable CS0618 #pragma warning disable CS0618
entity.Payments = new List<PaymentEntity>(); entity.Payments = new List<PaymentEntity>();
entity.SetPaymentMethod(new PaymentMethod() entity.SetPaymentMethod(new PaymentMethod()

View file

@ -266,7 +266,8 @@ namespace BTCPayServer.Controllers
(1m + (changelly.AmountMarkupPercentage / 100m))) (1m + (changelly.AmountMarkupPercentage / 100m)))
: (decimal?)null; : (decimal?)null;
var paymentMethodHandler = invoice.PaymentMethodHandlerDictionary[paymentMethodId];
var paymentMethodHandler = _paymentMethodHandlerDictionary[paymentMethodId];
var model = new PaymentModel() var model = new PaymentModel()
{ {
CryptoCode = network.CryptoCode, CryptoCode = network.CryptoCode,
@ -314,8 +315,7 @@ namespace BTCPayServer.Controllers
.Select(kv => .Select(kv =>
{ {
var availableCryptoPaymentMethodId = kv.GetId(); var availableCryptoPaymentMethodId = kv.GetId();
var availableCryptoHandler = var availableCryptoHandler = _paymentMethodHandlerDictionary[availableCryptoPaymentMethodId];
invoice.PaymentMethodHandlerDictionary[availableCryptoPaymentMethodId];
return new PaymentModel.AvailableCrypto() return new PaymentModel.AvailableCrypto()
{ {
PaymentMethodId = kv.GetId().ToString(), PaymentMethodId = kv.GetId().ToString(),

View file

@ -141,35 +141,5 @@ namespace BTCPayServer.Payments.Bitcoin
onchainMethod.DepositAddress = (await prepare.ReserveAddress).ToString(); onchainMethod.DepositAddress = (await prepare.ReserveAddress).ToString();
return onchainMethod; return onchainMethod;
} }
public override void PrepareInvoiceDto(InvoiceResponse invoiceResponse, InvoiceEntity invoiceEntity,
InvoiceCryptoInfo invoiceCryptoInfo,
PaymentMethodAccounting accounting, PaymentMethod info)
{
var scheme = info.Network.UriScheme;
var minerInfo = new MinerFeeInfo();
minerInfo.TotalFee = accounting.NetworkFee.Satoshi;
minerInfo.SatoshiPerBytes = ((BitcoinLikeOnChainPaymentMethod)info.GetPaymentMethodDetails()).FeeRate
.GetFee(1).Satoshi;
invoiceResponse.MinerFees.TryAdd(invoiceCryptoInfo.CryptoCode, minerInfo);
invoiceCryptoInfo.PaymentUrls = new NBitpayClient.InvoicePaymentUrls()
{
BIP21 = $"{scheme}:{invoiceCryptoInfo.Address}?amount={invoiceCryptoInfo.Due}",
};
#pragma warning disable 618
if (info.CryptoCode == "BTC")
{
invoiceResponse.BTCPrice = invoiceCryptoInfo.Price;
invoiceResponse.Rate = invoiceCryptoInfo.Rate;
invoiceResponse.ExRates = invoiceCryptoInfo.ExRates;
invoiceResponse.BitcoinAddress = invoiceCryptoInfo.Address;
invoiceResponse.BTCPaid = invoiceCryptoInfo.Paid;
invoiceResponse.BTCDue = invoiceCryptoInfo.Due;
invoiceResponse.PaymentUrls = invoiceCryptoInfo.PaymentUrls;
}
#pragma warning restore 618
}
} }
} }

View file

@ -39,11 +39,6 @@ namespace BTCPayServer.Payments
/// <returns></returns> /// <returns></returns>
object PreparePayment(ISupportedPaymentMethod supportedPaymentMethod, StoreData store, BTCPayNetworkBase network); object PreparePayment(ISupportedPaymentMethod supportedPaymentMethod, StoreData store, BTCPayNetworkBase network);
void PrepareInvoiceDto(InvoiceResponse invoiceResponse, InvoiceEntity invoiceEntity,
InvoiceCryptoInfo invoiceCryptoInfo,
PaymentMethodAccounting accounting, PaymentMethod info);
void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse); void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse);
string GetCryptoImage(PaymentMethodId paymentMethodId); string GetCryptoImage(PaymentMethodId paymentMethodId);
string GetPaymentMethodName(PaymentMethodId paymentMethodId); string GetPaymentMethodName(PaymentMethodId paymentMethodId);
@ -74,9 +69,6 @@ namespace BTCPayServer.Payments
TSupportedPaymentMethod supportedPaymentMethod, TSupportedPaymentMethod supportedPaymentMethod,
PaymentMethod paymentMethod, StoreData store, TBTCPayNetwork network, object preparePaymentObject); PaymentMethod paymentMethod, StoreData store, TBTCPayNetwork network, object preparePaymentObject);
public abstract void PrepareInvoiceDto(InvoiceResponse invoiceResponse, InvoiceEntity invoiceEntity,
InvoiceCryptoInfo invoiceCryptoInfo, PaymentMethodAccounting accounting, PaymentMethod info);
public abstract void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse); public abstract void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse);
public abstract string GetCryptoImage(PaymentMethodId paymentMethodId); public abstract string GetCryptoImage(PaymentMethodId paymentMethodId);
public abstract string GetPaymentMethodName(PaymentMethodId paymentMethodId); public abstract string GetPaymentMethodName(PaymentMethodId paymentMethodId);

View file

@ -168,15 +168,6 @@ namespace BTCPayServer.Payments.Lightning
return "The amount of the invoice is too high to be paid with lightning"; return "The amount of the invoice is too high to be paid with lightning";
} }
public override void PrepareInvoiceDto(InvoiceResponse invoiceResponse, InvoiceEntity invoiceEntity,
InvoiceCryptoInfo invoiceCryptoInfo, PaymentMethodAccounting accounting, PaymentMethod info)
{
invoiceCryptoInfo.PaymentUrls = new InvoicePaymentUrls()
{
BOLT11 = $"lightning:{invoiceCryptoInfo.Address}"
};
}
public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse) public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse)
{ {
var paymentMethodId = new PaymentMethodId(model.CryptoCode, PaymentTypes.LightningLike); var paymentMethodId = new PaymentMethodId(model.CryptoCode, PaymentTypes.LightningLike);

View file

@ -114,9 +114,6 @@ namespace BTCPayServer.Services.Invoices
} }
public class InvoiceEntity public class InvoiceEntity
{ {
[JsonIgnore]
public PaymentMethodHandlerDictionary PaymentMethodHandlerDictionary { get; set; }
[JsonIgnore] [JsonIgnore]
public BTCPayNetworkProvider Networks { get; set; } public BTCPayNetworkProvider Networks { get; set; }
public const int InternalTagSupport_Version = 1; public const int InternalTagSupport_Version = 1;
@ -443,7 +440,41 @@ namespace BTCPayServer.Services.Invoices
}; };
}).ToList(); }).ToList();
PaymentMethodHandlerDictionary[paymentId].PrepareInvoiceDto(dto, this, cryptoInfo, accounting, info);
if (paymentId.PaymentType == PaymentTypes.LightningLike)
{
cryptoInfo.PaymentUrls = new InvoicePaymentUrls()
{
BOLT11 = $"lightning:{cryptoInfo.Address}"
};
}
else if (paymentId.PaymentType == PaymentTypes.BTCLike)
{
var scheme = info.Network.UriScheme;
var minerInfo = new MinerFeeInfo();
minerInfo.TotalFee = accounting.NetworkFee.Satoshi;
minerInfo.SatoshiPerBytes = ((BitcoinLikeOnChainPaymentMethod)info.GetPaymentMethodDetails()).FeeRate
.GetFee(1).Satoshi;
dto.MinerFees.TryAdd(cryptoInfo.CryptoCode, minerInfo);
cryptoInfo.PaymentUrls = new NBitpayClient.InvoicePaymentUrls()
{
BIP21 = $"{scheme}:{cryptoInfo.Address}?amount={cryptoInfo.Due}",
};
#pragma warning disable 618
if (info.CryptoCode == "BTC")
{
dto.BTCPrice = cryptoInfo.Price;
dto.Rate = cryptoInfo.Rate;
dto.ExRates = cryptoInfo.ExRates;
dto.BitcoinAddress = cryptoInfo.Address;
dto.BTCPaid = cryptoInfo.Paid;
dto.BTCDue = cryptoInfo.Due;
dto.PaymentUrls = cryptoInfo.PaymentUrls;
}
#pragma warning restore 618
}
dto.CryptoInfo.Add(cryptoInfo); dto.CryptoInfo.Add(cryptoInfo);
dto.PaymentCodes.Add(paymentId.ToString(), cryptoInfo.PaymentUrls); dto.PaymentCodes.Add(paymentId.ToString(), cryptoInfo.PaymentUrls);
@ -867,8 +898,6 @@ namespace BTCPayServer.Services.Invoices
public class PaymentEntity public class PaymentEntity
{ {
[JsonIgnore]
public PaymentMethodHandlerDictionary PaymentMethodHandlerDictionary { get; set; }
public int Version { get; set; } public int Version { get; set; }
public DateTimeOffset ReceivedTime public DateTimeOffset ReceivedTime
{ {

View file

@ -61,7 +61,6 @@ retry:
{ {
return new InvoiceEntity() return new InvoiceEntity()
{ {
PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary,
Networks = _Networks, Networks = _Networks,
Version = InvoiceEntity.Lastest_Version, Version = InvoiceEntity.Lastest_Version,
InvoiceTime = DateTimeOffset.UtcNow, InvoiceTime = DateTimeOffset.UtcNow,
@ -153,7 +152,6 @@ retry:
{ {
List<string> textSearch = new List<string>(); List<string> textSearch = new List<string>();
invoice = ToObject(ToBytes(invoice)); invoice = ToObject(ToBytes(invoice));
invoice.PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary;
invoice.Networks = _Networks; invoice.Networks = _Networks;
invoice.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16)); invoice.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16));
#pragma warning disable CS0618 #pragma warning disable CS0618
@ -446,7 +444,6 @@ retry:
{ {
var paymentEntity = ToObject<PaymentEntity>(p.Blob, null); var paymentEntity = ToObject<PaymentEntity>(p.Blob, null);
paymentEntity.Accounted = p.Accounted; paymentEntity.Accounted = p.Accounted;
paymentEntity.PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary;
// PaymentEntity on version 0 does not have their own fee, because it was assumed that the payment method have fixed fee. // PaymentEntity on version 0 does not have their own fee, because it was assumed that the payment method have fixed fee.
// We want to hide this legacy detail in InvoiceRepository, so we fetch the fee from the PaymentMethod and assign it to the PaymentEntity. // We want to hide this legacy detail in InvoiceRepository, so we fetch the fee from the PaymentMethod and assign it to the PaymentEntity.
if (paymentEntity.Version == 0) if (paymentEntity.Version == 0)
@ -651,7 +648,6 @@ retry:
if (invoice == null) if (invoice == null)
return null; return null;
InvoiceEntity invoiceEntity = ToObject(invoice.Blob); InvoiceEntity invoiceEntity = ToObject(invoice.Blob);
invoiceEntity.PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary;
PaymentMethod paymentMethod = invoiceEntity.GetPaymentMethod(new PaymentMethodId(network.CryptoCode, paymentData.GetPaymentType()), null); PaymentMethod paymentMethod = invoiceEntity.GetPaymentMethod(new PaymentMethodId(network.CryptoCode, paymentData.GetPaymentType()), null);
IPaymentMethodDetails paymentMethodDetails = paymentMethod.GetPaymentMethodDetails(); IPaymentMethodDetails paymentMethodDetails = paymentMethod.GetPaymentMethodDetails();
PaymentEntity entity = new PaymentEntity PaymentEntity entity = new PaymentEntity
@ -662,8 +658,7 @@ retry:
#pragma warning restore CS0618 #pragma warning restore CS0618
ReceivedTime = date.UtcDateTime, ReceivedTime = date.UtcDateTime,
Accounted = accounted, Accounted = accounted,
NetworkFee = paymentMethodDetails.GetNextNetworkFee(), NetworkFee = paymentMethodDetails.GetNextNetworkFee()
PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary
}; };
entity.SetCryptoPaymentData(paymentData); entity.SetCryptoPaymentData(paymentData);
@ -720,8 +715,7 @@ retry:
private InvoiceEntity ToObject(byte[] value) private InvoiceEntity ToObject(byte[] value)
{ {
var entity = NBitcoin.JsonConverters.Serializer.ToObject<InvoiceEntity>(ZipUtils.Unzip(value), null); var entity = NBitcoin.JsonConverters.Serializer.ToObject<InvoiceEntity>(ZipUtils.Unzip(value), null);
entity.PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary; entity.Networks = _Networks?.UnfilteredNetworks;
entity.Networks = _Networks;
return entity; return entity;
} }
private T ToObject<T>(byte[] value, BTCPayNetworkBase network) private T ToObject<T>(byte[] value, BTCPayNetworkBase network)