diff --git a/BTCPayServer/Events/InvoicePaymentEvent.cs b/BTCPayServer/Events/InvoicePaymentEvent.cs new file mode 100644 index 000000000..9af0aea51 --- /dev/null +++ b/BTCPayServer/Events/InvoicePaymentEvent.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using BTCPayServer.Services.Invoices; + +namespace BTCPayServer.Events +{ + public class InvoicePaymentEvent + { + + public InvoicePaymentEvent(string invoiceId) + { + InvoiceId = invoiceId; + } + + public string InvoiceId { get; set; } + + public override string ToString() + { + return $"Invoice {InvoiceId} received a payment"; + } + } +} diff --git a/BTCPayServer/Events/InvoiceStatusChangedEvent.cs b/BTCPayServer/Events/InvoiceStatusChangedEvent.cs index f3a31d051..925f5fc6b 100644 --- a/BTCPayServer/Events/InvoiceStatusChangedEvent.cs +++ b/BTCPayServer/Events/InvoiceStatusChangedEvent.cs @@ -2,11 +2,22 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using BTCPayServer.Services.Invoices; namespace BTCPayServer.Events { public class InvoiceStatusChangedEvent { + public InvoiceStatusChangedEvent() + { + + } + public InvoiceStatusChangedEvent(InvoiceEntity invoice, string newState) + { + OldState = invoice.Status; + InvoiceId = invoice.Id; + NewState = newState; + } public string InvoiceId { get; set; } public string OldState { get; set; } public string NewState { get; set; } diff --git a/BTCPayServer/Services/Invoices/InvoiceWatcher.cs b/BTCPayServer/Services/Invoices/InvoiceWatcher.cs index b29c0b0dd..3d97e6a35 100644 --- a/BTCPayServer/Services/Invoices/InvoiceWatcher.cs +++ b/BTCPayServer/Services/Invoices/InvoiceWatcher.cs @@ -79,8 +79,8 @@ namespace BTCPayServer.Services.Invoices if (invoice == null) break; var stateBefore = invoice.Status; - var stateChanges = new List(); - var result = await UpdateInvoice(changes, invoice, stateChanges).ConfigureAwait(false); + var postSaveActions = new List(); + var result = await UpdateInvoice(changes, invoice, postSaveActions).ConfigureAwait(false); changes = result.Changes; if (result.NeedSave) { @@ -90,12 +90,10 @@ namespace BTCPayServer.Services.Invoices var changed = stateBefore != invoice.Status; - foreach(var stateChange in stateChanges) + foreach(var saveAction in postSaveActions) { - _EventAggregator.Publish(new InvoiceStatusChangedEvent() { InvoiceId = invoice.Id, NewState = stateChange, OldState = stateBefore }); - stateBefore = stateChange; + saveAction(); } - if (invoice.Status == "complete" || ((invoice.Status == "invalid" || invoice.Status == "expired") && invoice.MonitoringExpiration < DateTimeOffset.UtcNow)) @@ -121,7 +119,7 @@ namespace BTCPayServer.Services.Invoices } - private async Task<(bool NeedSave, UTXOChanges Changes)> UpdateInvoice(UTXOChanges changes, InvoiceEntity invoice, List stateChanges) + private async Task<(bool NeedSave, UTXOChanges Changes)> UpdateInvoice(UTXOChanges changes, InvoiceEntity invoice, List postSaveActions) { bool needSave = false; //Fetch unknown payments @@ -140,6 +138,7 @@ namespace BTCPayServer.Services.Invoices { var payment = await _InvoiceRepository.AddPayment(invoice.Id, coin).ConfigureAwait(false); invoice.Payments.Add(payment); + postSaveActions.Add(() => _EventAggregator.Publish(new InvoicePaymentEvent(invoice.Id))); dirtyAddress = true; } ////// @@ -148,8 +147,9 @@ namespace BTCPayServer.Services.Invoices { needSave = true; await _InvoiceRepository.UnaffectAddress(invoice.Id); + + postSaveActions.Add(() => _EventAggregator.Publish(new InvoiceStatusChangedEvent(invoice, "expired"))); invoice.Status = "expired"; - stateChanges.Add(invoice.Status); } if (invoice.Status == "new" || invoice.Status == "expired") @@ -159,8 +159,8 @@ namespace BTCPayServer.Services.Invoices { if (invoice.Status == "new") { + postSaveActions.Add(() => _EventAggregator.Publish(new InvoiceStatusChangedEvent(invoice, "paid"))); invoice.Status = "paid"; - stateChanges.Add(invoice.Status); invoice.ExceptionStatus = null; await _InvoiceRepository.UnaffectAddress(invoice.Id); needSave = true; @@ -219,8 +219,8 @@ namespace BTCPayServer.Services.Invoices (chainTotalConfirmed < invoice.GetTotalCryptoDue())) { await _InvoiceRepository.UnaffectAddress(invoice.Id); + postSaveActions.Add(() => _EventAggregator.Publish(new InvoiceStatusChangedEvent(invoice, "invalid"))); invoice.Status = "invalid"; - stateChanges.Add(invoice.Status); needSave = true; } else @@ -229,8 +229,8 @@ namespace BTCPayServer.Services.Invoices if (totalConfirmed >= invoice.GetTotalCryptoDue()) { await _InvoiceRepository.UnaffectAddress(invoice.Id); + postSaveActions.Add(() => _EventAggregator.Publish(new InvoiceStatusChangedEvent(invoice, "confirmed"))); invoice.Status = "confirmed"; - stateChanges.Add(invoice.Status); needSave = true; } } @@ -243,8 +243,8 @@ namespace BTCPayServer.Services.Invoices var totalConfirmed = transactions.Select(t => t.Payment.Output.Value).Sum(); if (totalConfirmed >= invoice.GetTotalCryptoDue()) { + postSaveActions.Add(() => _EventAggregator.Publish(new InvoiceStatusChangedEvent(invoice, "complete"))); invoice.Status = "complete"; - stateChanges.Add(invoice.Status); needSave = true; } }