From 01787e2662efb4eece7c0144bce9f9a0c19e3aa8 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Tue, 4 Jun 2019 10:11:52 +0900 Subject: [PATCH] Refactor: Remove PrepareInvoiceDTO --- BTCPayServer.Tests/UnitTest1.cs | 35 ++++++---------- .../Controllers/InvoiceController.UI.cs | 6 +-- .../Bitcoin/BitcoinLikePaymentHandler.cs | 30 -------------- .../Payments/IPaymentMethodHandler.cs | 8 ---- .../Lightning/LightningLikePaymentHandler.cs | 9 ---- .../Services/Invoices/InvoiceEntity.cs | 41 ++++++++++++++++--- .../Services/Invoices/InvoiceRepository.cs | 10 +---- 7 files changed, 53 insertions(+), 86 deletions(-) diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 3ed2723c3..dff7fb06e 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -107,7 +107,7 @@ namespace BTCPayServer.Tests new BitcoinLikePaymentHandler(null, networkProvider, null, null), new LightningLikePaymentHandler(null, null, networkProvider, null), }); - InvoiceEntity invoiceEntity = new InvoiceEntity() { PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; + InvoiceEntity invoiceEntity = new InvoiceEntity(); invoiceEntity.Payments = new System.Collections.Generic.List(); invoiceEntity.ProductInformation = new ProductInformation() {Price = 100}; PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary(); @@ -131,8 +131,7 @@ namespace BTCPayServer.Tests { Accounted = true, CryptoCode = "BTC", - NetworkFee = 0.00000100m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.00000100m } .SetCryptoPaymentData(new BitcoinLikePaymentData() { @@ -144,8 +143,7 @@ namespace BTCPayServer.Tests { Accounted = true, CryptoCode = "BTC", - NetworkFee = 0.00000100m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.00000100m } .SetCryptoPaymentData(new BitcoinLikePaymentData() { @@ -216,7 +214,7 @@ namespace BTCPayServer.Tests new BitcoinLikePaymentHandler(null, networkProvider, null, null), new LightningLikePaymentHandler(null, null, networkProvider, null), }); - var entity = new InvoiceEntity() {PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; + var entity = new InvoiceEntity(); #pragma warning disable CS0618 entity.Payments = new System.Collections.Generic.List(); entity.SetPaymentMethod(new PaymentMethod() @@ -234,8 +232,7 @@ namespace BTCPayServer.Tests { Output = new TxOut(Money.Coins(0.5m), new Key()), Accounted = true, - NetworkFee = 0.1m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.1m }); accounting = paymentMethod.Calculate(); @@ -247,8 +244,7 @@ namespace BTCPayServer.Tests { Output = new TxOut(Money.Coins(0.2m), new Key()), Accounted = true, - NetworkFee = 0.1m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.1m }); accounting = paymentMethod.Calculate(); @@ -259,8 +255,7 @@ namespace BTCPayServer.Tests { Output = new TxOut(Money.Coins(0.6m), new Key()), Accounted = true, - NetworkFee = 0.1m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.1m }); accounting = paymentMethod.Calculate(); @@ -270,15 +265,14 @@ namespace BTCPayServer.Tests entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.2m), new Key()), - Accounted = true, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + Accounted = true }); accounting = paymentMethod.Calculate(); Assert.Equal(Money.Zero, accounting.Due); Assert.Equal(Money.Coins(1.3m), accounting.TotalDue); - entity = new InvoiceEntity() {PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; + entity = new InvoiceEntity(); entity.ProductInformation = new ProductInformation() {Price = 5000}; PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary(); paymentMethods.Add( @@ -301,8 +295,7 @@ namespace BTCPayServer.Tests CryptoCode = "BTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true, - NetworkFee = 0.1m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.1m }); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); @@ -325,8 +318,7 @@ namespace BTCPayServer.Tests CryptoCode = "LTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true, - NetworkFee = 0.01m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.01m }); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); @@ -351,8 +343,7 @@ namespace BTCPayServer.Tests CryptoCode = "BTC", Output = new TxOut(remaining, new Key()), Accounted = true, - NetworkFee = 0.1m, - PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary + NetworkFee = 0.1m }); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); @@ -423,7 +414,7 @@ namespace BTCPayServer.Tests new BitcoinLikePaymentHandler(null, networkProvider, null, null), new LightningLikePaymentHandler(null, null, networkProvider, null), }); - var entity = new InvoiceEntity() {PaymentMethodHandlerDictionary = paymentMethodHandlerDictionary}; + var entity = new InvoiceEntity(); #pragma warning disable CS0618 entity.Payments = new List(); entity.SetPaymentMethod(new PaymentMethod() diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index fc31700a2..15861b6f3 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -266,7 +266,8 @@ namespace BTCPayServer.Controllers (1m + (changelly.AmountMarkupPercentage / 100m))) : (decimal?)null; - var paymentMethodHandler = invoice.PaymentMethodHandlerDictionary[paymentMethodId]; + + var paymentMethodHandler = _paymentMethodHandlerDictionary[paymentMethodId]; var model = new PaymentModel() { CryptoCode = network.CryptoCode, @@ -314,8 +315,7 @@ namespace BTCPayServer.Controllers .Select(kv => { var availableCryptoPaymentMethodId = kv.GetId(); - var availableCryptoHandler = - invoice.PaymentMethodHandlerDictionary[availableCryptoPaymentMethodId]; + var availableCryptoHandler = _paymentMethodHandlerDictionary[availableCryptoPaymentMethodId]; return new PaymentModel.AvailableCrypto() { PaymentMethodId = kv.GetId().ToString(), diff --git a/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs index 6dafb3027..9b0823c78 100644 --- a/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentHandler.cs @@ -141,35 +141,5 @@ namespace BTCPayServer.Payments.Bitcoin onchainMethod.DepositAddress = (await prepare.ReserveAddress).ToString(); 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 - } } } diff --git a/BTCPayServer/Payments/IPaymentMethodHandler.cs b/BTCPayServer/Payments/IPaymentMethodHandler.cs index 14529c588..5cf3403cc 100644 --- a/BTCPayServer/Payments/IPaymentMethodHandler.cs +++ b/BTCPayServer/Payments/IPaymentMethodHandler.cs @@ -39,11 +39,6 @@ namespace BTCPayServer.Payments /// 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); string GetCryptoImage(PaymentMethodId paymentMethodId); string GetPaymentMethodName(PaymentMethodId paymentMethodId); @@ -74,9 +69,6 @@ namespace BTCPayServer.Payments TSupportedPaymentMethod supportedPaymentMethod, 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 string GetCryptoImage(PaymentMethodId paymentMethodId); public abstract string GetPaymentMethodName(PaymentMethodId paymentMethodId); diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs index b0f097d1e..4d65c8936 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs @@ -168,15 +168,6 @@ namespace BTCPayServer.Payments.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) { var paymentMethodId = new PaymentMethodId(model.CryptoCode, PaymentTypes.LightningLike); diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index 5d43a7654..6a346e56c 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -114,9 +114,6 @@ namespace BTCPayServer.Services.Invoices } public class InvoiceEntity { - - [JsonIgnore] - public PaymentMethodHandlerDictionary PaymentMethodHandlerDictionary { get; set; } [JsonIgnore] public BTCPayNetworkProvider Networks { get; set; } public const int InternalTagSupport_Version = 1; @@ -443,7 +440,41 @@ namespace BTCPayServer.Services.Invoices }; }).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.PaymentCodes.Add(paymentId.ToString(), cryptoInfo.PaymentUrls); @@ -867,8 +898,6 @@ namespace BTCPayServer.Services.Invoices public class PaymentEntity { - [JsonIgnore] - public PaymentMethodHandlerDictionary PaymentMethodHandlerDictionary { get; set; } public int Version { get; set; } public DateTimeOffset ReceivedTime { diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 4fe281969..17adb3701 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -61,7 +61,6 @@ retry: { return new InvoiceEntity() { - PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary, Networks = _Networks, Version = InvoiceEntity.Lastest_Version, InvoiceTime = DateTimeOffset.UtcNow, @@ -153,7 +152,6 @@ retry: { List textSearch = new List(); invoice = ToObject(ToBytes(invoice)); - invoice.PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary; invoice.Networks = _Networks; invoice.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16)); #pragma warning disable CS0618 @@ -446,7 +444,6 @@ retry: { var paymentEntity = ToObject(p.Blob, null); 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. // 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) @@ -651,7 +648,6 @@ retry: if (invoice == null) return null; InvoiceEntity invoiceEntity = ToObject(invoice.Blob); - invoiceEntity.PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary; PaymentMethod paymentMethod = invoiceEntity.GetPaymentMethod(new PaymentMethodId(network.CryptoCode, paymentData.GetPaymentType()), null); IPaymentMethodDetails paymentMethodDetails = paymentMethod.GetPaymentMethodDetails(); PaymentEntity entity = new PaymentEntity @@ -662,8 +658,7 @@ retry: #pragma warning restore CS0618 ReceivedTime = date.UtcDateTime, Accounted = accounted, - NetworkFee = paymentMethodDetails.GetNextNetworkFee(), - PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary + NetworkFee = paymentMethodDetails.GetNextNetworkFee() }; entity.SetCryptoPaymentData(paymentData); @@ -720,8 +715,7 @@ retry: private InvoiceEntity ToObject(byte[] value) { var entity = NBitcoin.JsonConverters.Serializer.ToObject(ZipUtils.Unzip(value), null); - entity.PaymentMethodHandlerDictionary = _paymentMethodHandlerDictionary; - entity.Networks = _Networks; + entity.Networks = _Networks?.UnfilteredNetworks; return entity; } private T ToObject(byte[] value, BTCPayNetworkBase network)