From 0b5d0349d431ff674f395df2dccea4ca71cdf670 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 27 Oct 2021 19:27:19 +0900 Subject: [PATCH] Do not fire InvoiceExpired twice if invoice partially paid (Fix #3004) --- BTCPayServer/Events/InvoiceEvent.cs | 4 ++++ BTCPayServer/HostedServices/InvoiceWatcher.cs | 5 +++-- BTCPayServer/HostedServices/WebhookNotificationManager.cs | 3 +-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/BTCPayServer/Events/InvoiceEvent.cs b/BTCPayServer/Events/InvoiceEvent.cs index 1d3f75e2a..29813b43c 100644 --- a/BTCPayServer/Events/InvoiceEvent.cs +++ b/BTCPayServer/Events/InvoiceEvent.cs @@ -62,6 +62,10 @@ namespace BTCPayServer.Events public string Name { get; set; } public PaymentEntity Payment { get; set; } + /// + /// Only set for Expired event + /// + public bool PaidPartial { get; internal set; } public override string ToString() { diff --git a/BTCPayServer/HostedServices/InvoiceWatcher.cs b/BTCPayServer/HostedServices/InvoiceWatcher.cs index 9d923db47..4c8e3fb94 100644 --- a/BTCPayServer/HostedServices/InvoiceWatcher.cs +++ b/BTCPayServer/HostedServices/InvoiceWatcher.cs @@ -83,9 +83,10 @@ namespace BTCPayServer.HostedServices context.MarkDirty(); context.UnaffectAddresses(); invoice.Status = InvoiceStatusLegacy.Expired; - context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Expired)); + var paidPartial = invoice.ExceptionStatus == InvoiceExceptionStatus.PaidPartial; + context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Expired) { PaidPartial = paidPartial }); if (invoice.ExceptionStatus == InvoiceExceptionStatus.PaidPartial) - context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.ExpiredPaidPartial)); + context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.ExpiredPaidPartial) { PaidPartial = paidPartial }); } var allPaymentMethods = invoice.GetPaymentMethods(); var paymentMethod = GetNearestClearedPayment(allPaymentMethods, out var accounting); diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index 47ab12269..75b2d6382 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -216,10 +216,9 @@ namespace BTCPayServer.HostedServices case InvoiceEventCode.Created: return new WebhookInvoiceEvent(WebhookEventType.InvoiceCreated); case InvoiceEventCode.Expired: - case InvoiceEventCode.ExpiredPaidPartial: return new WebhookInvoiceExpiredEvent(WebhookEventType.InvoiceExpired) { - PartiallyPaid = eventCode == InvoiceEventCode.ExpiredPaidPartial + PartiallyPaid = invoiceEvent.PaidPartial }; case InvoiceEventCode.FailedToConfirm: case InvoiceEventCode.MarkedInvalid: