public Task StartAsync(CancellationToken cancellationToken)
        {
            leases.Add(_EventAggregator.Subscribe <InvoiceStatusChangedEvent>(async e =>
            {
                var invoice = await _InvoiceRepository.GetInvoice(null, e.InvoiceId);

                // we need to use the status in the event and not in the invoice. The invoice might now be in another status.
                if (invoice.FullNotifications)
                {
                    if (e.NewState == "expired" ||
                        e.NewState == "paid" ||
                        e.NewState == "invalid" ||
                        e.NewState == "complete"
                        )
                    {
                        await Notify(invoice);
                    }
                }

                if (e.NewState == "confirmed")
                {
                    await Notify(invoice);
                }
            }));
            return(Task.CompletedTask);
        }
Esempio n. 2
0
        private async Task UpdateInvoice(string invoiceId)
        {
            UTXOChanges changes = null;

            while (true)
            {
                try
                {
                    var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId).ConfigureAwait(false);

                    if (invoice == null)
                    {
                        break;
                    }
                    var stateBefore = invoice.Status;
                    var result      = await UpdateInvoice(changes, invoice).ConfigureAwait(false);

                    changes = result.Changes;
                    if (result.NeedSave)
                    {
                        await _InvoiceRepository.UpdateInvoiceStatus(invoice.Id, invoice.Status, invoice.ExceptionStatus).ConfigureAwait(false);
                    }

                    var changed = stateBefore != invoice.Status;
                    if (changed)
                    {
                        Logs.PayServer.LogInformation($"Invoice {invoice.Id}: {stateBefore} => {invoice.Status}");
                    }

                    var expirationMonitoring = invoice.MonitoringExpiration.HasValue ? invoice.MonitoringExpiration.Value : invoice.InvoiceTime + TimeSpan.FromMinutes(60);
                    if (invoice.Status == "complete" ||
                        ((invoice.Status == "invalid" || invoice.Status == "expired") && expirationMonitoring < DateTimeOffset.UtcNow))
                    {
                        if (await _InvoiceRepository.RemovePendingInvoice(invoice.Id).ConfigureAwait(false))
                        {
                            Logs.PayServer.LogInformation("Stopped watching invoice " + invoiceId);
                        }
                        break;
                    }

                    if (!changed || _Cts.Token.IsCancellationRequested)
                    {
                        break;
                    }
                }
                catch (OperationCanceledException) when(_Cts.Token.IsCancellationRequested)
                {
                    break;
                }
                catch (Exception ex)
                {
                    Logs.PayServer.LogError(ex, "Unhandled error on watching invoice " + invoiceId);
                    await Task.Delay(10000, _Cts.Token).ConfigureAwait(false);
                }
            }
        }
Esempio n. 3
0
        private async Task UpdateInvoice(string invoiceId)
        {
            UTXOChanges changes = null;

            while (true)
            {
                try
                {
                    var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId, true).ConfigureAwait(false);

                    if (invoice == null)
                    {
                        break;
                    }
                    var stateBefore     = invoice.Status;
                    var postSaveActions = new List <Action>();
                    var result          = await UpdateInvoice(changes, invoice, postSaveActions).ConfigureAwait(false);

                    changes = result.Changes;
                    if (result.NeedSave)
                    {
                        await _InvoiceRepository.UpdateInvoiceStatus(invoice.Id, invoice.Status, invoice.ExceptionStatus).ConfigureAwait(false);

                        _EventAggregator.Publish(new InvoiceDataChangedEvent()
                        {
                            InvoiceId = invoice.Id
                        });
                    }

                    var changed = stateBefore != invoice.Status;

                    foreach (var saveAction in postSaveActions)
                    {
                        saveAction();
                    }

                    if (invoice.Status == "complete" ||
                        ((invoice.Status == "invalid" || invoice.Status == "expired") && invoice.MonitoringExpiration < DateTimeOffset.UtcNow))
                    {
                        if (await _InvoiceRepository.RemovePendingInvoice(invoice.Id).ConfigureAwait(false))
                        {
                            Logs.PayServer.LogInformation("Stopped watching invoice " + invoiceId);
                        }
                        break;
                    }

                    if (!changed || _Cts.Token.IsCancellationRequested)
                    {
                        break;
                    }
                }
                catch (OperationCanceledException) when(_Cts.Token.IsCancellationRequested)
                {
                    break;
                }
                catch (Exception ex)
                {
                    Logs.PayServer.LogError(ex, "Unhandled error on watching invoice " + invoiceId);
                    await Task.Delay(10000, _Cts.Token).ConfigureAwait(false);
                }
            }
        }