From 98b7ad62afb3ef22cd77d2c4c0f09546c13c468f Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Tue, 9 Jan 2018 17:27:26 +0900 Subject: [PATCH] Fix order accounting --- BTCPayServer/Controllers/InvoiceController.UI.cs | 11 +++++++---- BTCPayServer/Models/InvoicingModels/PaymentModel.cs | 5 ++--- BTCPayServer/Services/Invoices/InvoiceEntity.cs | 12 ++++++++---- BTCPayServer/Views/Invoice/Checkout.cshtml | 6 +++--- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 830b14edc..0d6f1b617 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -139,8 +139,7 @@ namespace BTCPayServer.Controllers OrderId = invoice.OrderId, InvoiceId = invoice.Id, BtcAddress = cryptoData.DepositAddress, - BtcAmount = (accounting.TotalDue - cryptoData.TxFee).ToString(), - BtcTotalDue = accounting.TotalDue.ToString(), + OrderAmount = (accounting.TotalDue - accounting.NetworkFee).ToString(), BtcDue = accounting.Due.ToString(), CustomerEmail = invoice.RefundMail, ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds), @@ -149,14 +148,18 @@ namespace BTCPayServer.Controllers Rate = FormatCurrency(cryptoData), MerchantRefLink = invoice.RedirectURL ?? "/", StoreName = store.StoreName, - TxFees = cryptoData.TxFee.ToString(), InvoiceBitcoinUrl = cryptoInfo.PaymentUrls.BIP21, TxCount = accounting.TxCount, BtcPaid = accounting.Paid.ToString(), Status = invoice.Status, - CryptoImage = "/" + Url.Content(network.CryptoImagePath) + CryptoImage = "/" + Url.Content(network.CryptoImagePath), + NetworkFeeDescription = $"{accounting.TxCount} transaction{(accounting.TxCount > 1 ? "s" : "")} x {cryptoData.TxFee} {network.CryptoCode}" }; + var isMultiCurrency = invoice.Payments.Select(p=>p.GetCryptoCode()).Concat(new[] { network.CryptoCode }).Distinct().Count() > 1; + if (isMultiCurrency) + model.NetworkFeeDescription = $"{accounting.NetworkFee} {network.CryptoCode}"; + var expiration = TimeSpan.FromSeconds(model.ExpirationSeconds); model.TimeLeft = PrettyPrint(expiration); return model; diff --git a/BTCPayServer/Models/InvoicingModels/PaymentModel.cs b/BTCPayServer/Models/InvoicingModels/PaymentModel.cs index 0db08f484..99a1eac13 100644 --- a/BTCPayServer/Models/InvoicingModels/PaymentModel.cs +++ b/BTCPayServer/Models/InvoicingModels/PaymentModel.cs @@ -23,15 +23,14 @@ namespace BTCPayServer.Models.InvoicingModels public string ItemDesc { get; set; } public string TimeLeft { get; set; } public string Rate { get; set; } - public string BtcAmount { get; set; } - public string TxFees { get; set; } + public string OrderAmount { get; set; } public string InvoiceBitcoinUrl { get; set; } - public string BtcTotalDue { get; set; } public int TxCount { get; set; } public string BtcPaid { get; set; } public string StoreEmail { get; set; } public string OrderId { get; set; } public string CryptoImage { get; set; } + public string NetworkFeeDescription { get; internal set; } } } diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index 2602a34b3..142938da2 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -495,10 +495,13 @@ namespace BTCPayServer.Services.Invoices .OrderByDescending(p => p.ReceivedTime) .Select(_ => { - paidTxFee = _.GetValue(cryptoData, CryptoCode, cryptoData[_.GetCryptoCode()].TxFee); + var txFee = _.GetValue(cryptoData, CryptoCode, cryptoData[_.GetCryptoCode()].TxFee); paid += _.GetValue(cryptoData, CryptoCode); - if(!paidEnough) - totalDue += paidTxFee; + if (!paidEnough) + { + totalDue += txFee; + paidTxFee += txFee; + } paidEnough |= totalDue <= paid; if (CryptoCode == _.GetCryptoCode()) { @@ -513,6 +516,7 @@ namespace BTCPayServer.Services.Invoices { txCount++; totalDue += TxFee; + paidTxFee += TxFee; } var accounting = new CryptoDataAccounting(); accounting.TotalDue = totalDue; @@ -520,7 +524,7 @@ namespace BTCPayServer.Services.Invoices accounting.TxCount = txCount; accounting.CryptoPaid = cryptoPaid; accounting.Due = Money.Max(accounting.TotalDue - accounting.Paid, Money.Zero); - accounting.NetworkFee = TxFee * txCount; + accounting.NetworkFee = paidTxFee; return accounting; } diff --git a/BTCPayServer/Views/Invoice/Checkout.cshtml b/BTCPayServer/Views/Invoice/Checkout.cshtml index 50643732b..9e00ea6e4 100644 --- a/BTCPayServer/Views/Invoice/Checkout.cshtml +++ b/BTCPayServer/Views/Invoice/Checkout.cshtml @@ -123,14 +123,14 @@
-
Payment Amount
-
{{srvModel.btcAmount}} {{ srvModel.cryptoCode }}
+
Order Amount
+
{{srvModel.orderAmount}} {{ srvModel.cryptoCode }}
Network Cost
-
{{srvModel.txCount }} transaction x {{ srvModel.txFees}} {{ srvModel.cryptoCode }}
+
{{srvModel.networkFeeDescription }}