extract logic of accounting to accounting and remove bitpay breaking changes

This commit is contained in:
Andrew Camilleri 2018-05-04 17:47:33 +02:00
parent dfb79ef96e
commit 0fc770bbb1
4 changed files with 28 additions and 23 deletions

View file

@ -312,7 +312,6 @@ namespace BTCPayServer.HostedServices
{ {
if (e.Name == "invoice_expired" || if (e.Name == "invoice_expired" ||
e.Name == "invoice_paidInFull" || e.Name == "invoice_paidInFull" ||
e.Name == "invoice_paidWithinTolerance" ||
e.Name == "invoice_failedToConfirm" || e.Name == "invoice_failedToConfirm" ||
e.Name == "invoice_markedInvalid" || e.Name == "invoice_markedInvalid" ||
e.Name == "invoice_failedToConfirm" || e.Name == "invoice_failedToConfirm" ||

View file

@ -76,9 +76,10 @@ namespace BTCPayServer.HostedServices
if (paymentMethod == null) if (paymentMethod == null)
return; return;
var network = _NetworkProvider.GetNetwork(paymentMethod.GetId().CryptoCode); var network = _NetworkProvider.GetNetwork(paymentMethod.GetId().CryptoCode);
var isPaid = accounting.IsPaid(invoice.PaymentTolerance, out var paidOver);
if (invoice.Status == "new" || invoice.Status == "expired") if (invoice.Status == "new" || invoice.Status == "expired")
{ {
if (accounting.Paid >= accounting.TotalDue) if (isPaid)
{ {
if (invoice.Status == "new") if (invoice.Status == "new")
{ {
@ -96,20 +97,8 @@ namespace BTCPayServer.HostedServices
} }
} }
if (accounting.Paid < accounting.TotalDue && invoice.GetPayments().Count != 0) if (!isPaid && invoice.GetPayments().Count != 0)
{ {
if (invoice.PaymentTolerance > 0)
{
var tolerantAmount = (accounting.TotalDue.Satoshi * (invoice.PaymentTolerance / 100));
var minimumTotalDue = accounting.TotalDue.Satoshi - tolerantAmount;
if (accounting.Paid.Satoshi >= minimumTotalDue)
{
context.Events.Add(new InvoiceEvent(invoice, 1003, "invoide_paidWithinTolerance"));
invoice.ExceptionStatus = "paidWithinTolerance";
invoice.Status = "paid";
}
}
if (invoice.ExceptionStatus != "paidPartial") if (invoice.ExceptionStatus != "paidPartial")
{ {
invoice.ExceptionStatus = "paidPartial"; invoice.ExceptionStatus = "paidPartial";
@ -123,19 +112,19 @@ namespace BTCPayServer.HostedServices
// Just make sure RBF did not cancelled a payment // Just make sure RBF did not cancelled a payment
if (invoice.Status == "paid") if (invoice.Status == "paid")
{ {
if (accounting.Paid == accounting.TotalDue && invoice.ExceptionStatus == "paidOver") if (!paidOver && invoice.ExceptionStatus == "paidOver")
{ {
invoice.ExceptionStatus = null; invoice.ExceptionStatus = null;
context.MarkDirty(); context.MarkDirty();
} }
if (accounting.Paid > accounting.TotalDue && invoice.ExceptionStatus != "paidOver") if (paidOver&& invoice.ExceptionStatus != "paidOver")
{ {
invoice.ExceptionStatus = "paidOver"; invoice.ExceptionStatus = "paidOver";
context.MarkDirty(); context.MarkDirty();
} }
if (accounting.Paid < accounting.TotalDue) if (!isPaid)
{ {
invoice.Status = "new"; invoice.Status = "new";
invoice.ExceptionStatus = accounting.Paid == Money.Zero ? null : "paidPartial"; invoice.ExceptionStatus = accounting.Paid == Money.Zero ? null : "paidPartial";
@ -146,19 +135,19 @@ namespace BTCPayServer.HostedServices
if (invoice.Status == "paid") if (invoice.Status == "paid")
{ {
var confirmedAccounting = paymentMethod.Calculate(p => p.GetCryptoPaymentData().PaymentConfirmed(p, invoice.SpeedPolicy, network)); var confirmedAccounting = paymentMethod.Calculate(p => p.GetCryptoPaymentData().PaymentConfirmed(p, invoice.SpeedPolicy, network));
var confirmedIsPaid = confirmedAccounting.IsPaid(invoice.PaymentTolerance, out var confirmedPaidOver);
if (// Is after the monitoring deadline if (// Is after the monitoring deadline
(invoice.MonitoringExpiration < DateTimeOffset.UtcNow) (invoice.MonitoringExpiration < DateTimeOffset.UtcNow)
&& &&
// And not enough amount confirmed // And not enough amount confirmed
(confirmedAccounting.Paid < accounting.TotalDue)) (!confirmedIsPaid))
{ {
await _InvoiceRepository.UnaffectAddress(invoice.Id); await _InvoiceRepository.UnaffectAddress(invoice.Id);
context.Events.Add(new InvoiceEvent(invoice, 1013, "invoice_failedToConfirm")); context.Events.Add(new InvoiceEvent(invoice, 1013, "invoice_failedToConfirm"));
invoice.Status = "invalid"; invoice.Status = "invalid";
context.MarkDirty(); context.MarkDirty();
} }
else if (confirmedAccounting.Paid >= accounting.TotalDue) else if (confirmedIsPaid)
{ {
await _InvoiceRepository.UnaffectAddress(invoice.Id); await _InvoiceRepository.UnaffectAddress(invoice.Id);
context.Events.Add(new InvoiceEvent(invoice, 1005, "invoice_confirmed")); context.Events.Add(new InvoiceEvent(invoice, 1005, "invoice_confirmed"));
@ -170,7 +159,8 @@ namespace BTCPayServer.HostedServices
if (invoice.Status == "confirmed") if (invoice.Status == "confirmed")
{ {
var completedAccounting = paymentMethod.Calculate(p => p.GetCryptoPaymentData().PaymentCompleted(p, network)); var completedAccounting = paymentMethod.Calculate(p => p.GetCryptoPaymentData().PaymentCompleted(p, network));
if (completedAccounting.Paid >= accounting.TotalDue) var confirmedIsPaid = completedAccounting.IsPaid(invoice.PaymentTolerance, out var completedPaidOver);
if (confirmedIsPaid)
{ {
context.Events.Add(new InvoiceEvent(invoice, 1006, "invoice_completed")); context.Events.Add(new InvoiceEvent(invoice, 1006, "invoice_completed"));
invoice.Status = "complete"; invoice.Status = "complete";

View file

@ -178,7 +178,7 @@ namespace BTCPayServer.Models
} }
//"exceptionStatus":false //"exceptionStatus":false
//Can be `paidPartial`, `paidOver`, `paidWithinTolerance` or false //Can be `paidPartial`, `paidOver`, or false
[JsonProperty("exceptionStatus")] [JsonProperty("exceptionStatus")]
public JToken ExceptionStatus public JToken ExceptionStatus
{ {

View file

@ -524,6 +524,22 @@ namespace BTCPayServer.Services.Invoices
/// Total amount of network fee to pay to the invoice /// Total amount of network fee to pay to the invoice
/// </summary> /// </summary>
public Money NetworkFee { get; set; } public Money NetworkFee { get; set; }
public bool IsPaid(double tolerance, out bool paidOver)
{
paidOver = false;
if (Paid < TotalDue)
{
var tolerantAmount = (TotalDue.Satoshi * (tolerance == 0 ? 1 : (tolerance / 100)));
var minimumTotalDue = TotalDue.Satoshi - tolerantAmount;
return Paid.Satoshi >= minimumTotalDue;
}else if (Paid > TotalDue)
{
paidOver = true;
}
return true;
}
} }
public class PaymentMethod public class PaymentMethod