2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2017-09-13 15:47:34 +09:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2020-06-28 17:55:27 +09:00
|
|
|
using System.Threading.Channels;
|
|
|
|
using System.Threading.Tasks;
|
2020-07-24 09:40:37 +02:00
|
|
|
using BTCPayServer.Client.Models;
|
2017-12-17 14:17:42 +09:00
|
|
|
using BTCPayServer.Events;
|
2020-06-28 17:55:27 +09:00
|
|
|
using BTCPayServer.Logging;
|
2023-07-19 18:47:32 +09:00
|
|
|
using BTCPayServer.Payments;
|
2018-01-08 02:36:41 +09:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2020-06-22 09:32:51 +02:00
|
|
|
using BTCPayServer.Services.Notifications;
|
|
|
|
using BTCPayServer.Services.Notifications.Blobs;
|
2020-06-28 17:55:27 +09:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NBitcoin;
|
|
|
|
using NBXplorer;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2018-01-08 02:36:41 +09:00
|
|
|
namespace BTCPayServer.HostedServices
|
2017-09-13 15:47:34 +09:00
|
|
|
{
|
2017-10-27 17:53:04 +09:00
|
|
|
public class InvoiceWatcher : IHostedService
|
|
|
|
{
|
2018-01-07 02:16:42 +09:00
|
|
|
class UpdateInvoiceContext
|
|
|
|
{
|
2018-02-17 13:18:16 +09:00
|
|
|
public UpdateInvoiceContext(InvoiceEntity invoice)
|
2018-01-07 02:16:42 +09:00
|
|
|
{
|
2018-02-17 13:18:16 +09:00
|
|
|
Invoice = invoice;
|
2018-01-07 02:16:42 +09:00
|
|
|
}
|
|
|
|
public InvoiceEntity Invoice { get; set; }
|
2023-01-06 14:18:07 +01:00
|
|
|
public List<object> Events { get; set; } = new();
|
2018-01-07 02:16:42 +09:00
|
|
|
|
2022-06-15 04:17:10 +02:00
|
|
|
bool _dirty;
|
2020-10-17 08:57:21 +02:00
|
|
|
|
2018-01-07 02:16:42 +09:00
|
|
|
public void MarkDirty()
|
|
|
|
{
|
2022-06-15 04:17:10 +02:00
|
|
|
_dirty = true;
|
2018-01-07 02:16:42 +09:00
|
|
|
}
|
|
|
|
|
2022-06-15 04:17:10 +02:00
|
|
|
public bool Dirty => _dirty;
|
2021-08-03 17:03:00 +09:00
|
|
|
|
2023-12-20 18:41:28 +09:00
|
|
|
public bool IsPriceUpdated { get; private set; }
|
|
|
|
public void PriceUpdated()
|
2021-08-03 17:03:00 +09:00
|
|
|
{
|
2023-12-20 18:41:28 +09:00
|
|
|
IsPriceUpdated = true;
|
2021-08-03 17:03:00 +09:00
|
|
|
}
|
2018-01-07 02:16:42 +09:00
|
|
|
}
|
|
|
|
|
2021-10-05 11:10:41 +02:00
|
|
|
readonly InvoiceRepository _invoiceRepository;
|
|
|
|
readonly EventAggregator _eventAggregator;
|
|
|
|
readonly ExplorerClientProvider _explorerClientProvider;
|
2020-06-22 09:32:51 +02:00
|
|
|
private readonly NotificationSender _notificationSender;
|
2021-10-05 11:10:41 +02:00
|
|
|
private readonly PaymentService _paymentService;
|
2021-11-22 17:16:08 +09:00
|
|
|
public Logs Logs { get; }
|
2017-10-27 17:53:04 +09:00
|
|
|
|
2018-01-07 02:16:42 +09:00
|
|
|
public InvoiceWatcher(
|
2017-10-27 17:53:04 +09:00
|
|
|
InvoiceRepository invoiceRepository,
|
2019-05-07 11:37:57 -05:00
|
|
|
EventAggregator eventAggregator,
|
2020-06-28 17:55:27 +09:00
|
|
|
ExplorerClientProvider explorerClientProvider,
|
2021-10-05 11:10:41 +02:00
|
|
|
NotificationSender notificationSender,
|
2021-11-22 17:16:08 +09:00
|
|
|
PaymentService paymentService,
|
|
|
|
Logs logs)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2021-10-05 11:10:41 +02:00
|
|
|
_invoiceRepository = invoiceRepository ?? throw new ArgumentNullException(nameof(invoiceRepository));
|
|
|
|
_eventAggregator = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator));
|
|
|
|
_explorerClientProvider = explorerClientProvider;
|
2020-06-22 09:32:51 +02:00
|
|
|
_notificationSender = notificationSender;
|
2021-10-05 11:10:41 +02:00
|
|
|
_paymentService = paymentService;
|
2022-06-15 04:17:10 +02:00
|
|
|
Logs = logs;
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2020-06-28 22:07:48 -05:00
|
|
|
|
2023-01-06 14:18:07 +01:00
|
|
|
readonly CompositeDisposable _leases = new();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
|
|
|
|
2020-10-26 14:18:38 +09:00
|
|
|
private void UpdateInvoice(UpdateInvoiceContext context)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2018-01-07 02:16:42 +09:00
|
|
|
var invoice = context.Invoice;
|
2020-11-23 15:57:05 +09:00
|
|
|
if (invoice.Status == InvoiceStatusLegacy.New && invoice.ExpirationTime <= DateTimeOffset.UtcNow)
|
2018-01-07 02:16:42 +09:00
|
|
|
{
|
2018-01-10 15:43:07 +09:00
|
|
|
context.MarkDirty();
|
2020-11-23 15:57:05 +09:00
|
|
|
invoice.Status = InvoiceStatusLegacy.Expired;
|
2021-10-27 19:27:19 +09:00
|
|
|
var paidPartial = invoice.ExceptionStatus == InvoiceExceptionStatus.PaidPartial;
|
|
|
|
context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Expired) { PaidPartial = paidPartial });
|
2019-02-19 12:06:13 +09:00
|
|
|
if (invoice.ExceptionStatus == InvoiceExceptionStatus.PaidPartial)
|
2021-10-27 19:27:19 +09:00
|
|
|
context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.ExpiredPaidPartial) { PaidPartial = paidPartial });
|
2018-01-07 02:16:42 +09:00
|
|
|
}
|
2023-07-19 18:47:32 +09:00
|
|
|
|
|
|
|
var hasPayment = invoice.GetPayments(true).Any();
|
2020-11-23 15:57:05 +09:00
|
|
|
if (invoice.Status == InvoiceStatusLegacy.New || invoice.Status == InvoiceStatusLegacy.Expired)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2021-08-03 17:03:00 +09:00
|
|
|
var isPaid = invoice.IsUnsetTopUp() ?
|
2023-07-19 18:47:32 +09:00
|
|
|
hasPayment :
|
|
|
|
!invoice.IsUnderPaid;
|
2021-08-03 17:03:00 +09:00
|
|
|
if (isPaid)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2020-11-23 15:57:05 +09:00
|
|
|
if (invoice.Status == InvoiceStatusLegacy.New)
|
2018-01-10 15:43:07 +09:00
|
|
|
{
|
2020-07-24 09:40:37 +02:00
|
|
|
context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.PaidInFull));
|
2020-11-23 15:57:05 +09:00
|
|
|
invoice.Status = InvoiceStatusLegacy.Paid;
|
2021-08-03 17:03:00 +09:00
|
|
|
if (invoice.IsUnsetTopUp())
|
|
|
|
{
|
|
|
|
invoice.ExceptionStatus = InvoiceExceptionStatus.None;
|
2023-07-19 18:47:32 +09:00
|
|
|
// We know there is at least one payment because hasPayment is true
|
|
|
|
var payment = invoice.GetPayments(true).First();
|
|
|
|
invoice.Price = payment.InvoicePaidAmount.Net;
|
|
|
|
invoice.UpdateTotals();
|
2023-12-20 18:41:28 +09:00
|
|
|
context.PriceUpdated();
|
2021-08-03 17:03:00 +09:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-19 18:47:32 +09:00
|
|
|
invoice.ExceptionStatus = invoice.IsOverPaid ? InvoiceExceptionStatus.PaidOver : InvoiceExceptionStatus.None;
|
2021-08-03 17:03:00 +09:00
|
|
|
}
|
2018-02-19 18:54:21 +09:00
|
|
|
context.MarkDirty();
|
2018-01-10 15:43:07 +09:00
|
|
|
}
|
2020-11-23 15:57:05 +09:00
|
|
|
else if (invoice.Status == InvoiceStatusLegacy.Expired && invoice.ExceptionStatus != InvoiceExceptionStatus.PaidLate)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2018-12-10 21:48:28 +09:00
|
|
|
invoice.ExceptionStatus = InvoiceExceptionStatus.PaidLate;
|
2020-07-24 09:40:37 +02:00
|
|
|
context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.PaidAfterExpiration));
|
2018-01-07 02:16:42 +09:00
|
|
|
context.MarkDirty();
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:47:32 +09:00
|
|
|
if (hasPayment && invoice.IsUnderPaid && invoice.ExceptionStatus != InvoiceExceptionStatus.PaidPartial)
|
2018-02-19 15:09:05 +09:00
|
|
|
{
|
2019-04-08 13:28:13 +09:00
|
|
|
invoice.ExceptionStatus = InvoiceExceptionStatus.PaidPartial;
|
|
|
|
context.MarkDirty();
|
2018-02-19 18:54:21 +09:00
|
|
|
}
|
|
|
|
}
|
2018-02-19 15:09:05 +09:00
|
|
|
|
2018-02-19 18:54:21 +09:00
|
|
|
// Just make sure RBF did not cancelled a payment
|
2020-11-23 15:57:05 +09:00
|
|
|
if (invoice.Status == InvoiceStatusLegacy.Paid)
|
2018-02-19 18:54:21 +09:00
|
|
|
{
|
2023-07-19 18:47:32 +09:00
|
|
|
if (!invoice.IsUnderPaid && !invoice.IsOverPaid && invoice.ExceptionStatus == InvoiceExceptionStatus.PaidOver)
|
2018-02-19 18:54:21 +09:00
|
|
|
{
|
2018-12-10 21:48:28 +09:00
|
|
|
invoice.ExceptionStatus = InvoiceExceptionStatus.None;
|
2018-02-19 18:54:21 +09:00
|
|
|
context.MarkDirty();
|
|
|
|
}
|
2018-02-19 15:09:05 +09:00
|
|
|
|
2023-07-19 18:47:32 +09:00
|
|
|
if (invoice.IsOverPaid && invoice.ExceptionStatus != InvoiceExceptionStatus.PaidOver)
|
2018-02-19 18:54:21 +09:00
|
|
|
{
|
2018-12-10 21:48:28 +09:00
|
|
|
invoice.ExceptionStatus = InvoiceExceptionStatus.PaidOver;
|
2018-02-19 18:54:21 +09:00
|
|
|
context.MarkDirty();
|
2018-02-19 15:09:05 +09:00
|
|
|
}
|
|
|
|
|
2023-07-19 18:47:32 +09:00
|
|
|
if (invoice.IsUnderPaid)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2020-11-23 15:57:05 +09:00
|
|
|
invoice.Status = InvoiceStatusLegacy.New;
|
2023-07-19 18:47:32 +09:00
|
|
|
invoice.ExceptionStatus = hasPayment ? InvoiceExceptionStatus.PaidPartial : InvoiceExceptionStatus.None;
|
2018-02-19 18:54:21 +09:00
|
|
|
context.MarkDirty();
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
|
2020-11-23 15:57:05 +09:00
|
|
|
if (invoice.Status == InvoiceStatusLegacy.Paid)
|
2018-02-19 18:54:21 +09:00
|
|
|
{
|
2023-07-19 18:47:32 +09:00
|
|
|
var unconfPayments = invoice.GetPayments(true).Where(p => !p.GetCryptoPaymentData().PaymentConfirmed(p, invoice.SpeedPolicy)).ToList();
|
|
|
|
var unconfirmedPaid = unconfPayments.Select(p => p.InvoicePaidAmount.Net).Sum();
|
|
|
|
var minimumDue = invoice.MinimumNetDue + unconfirmedPaid;
|
2018-05-05 16:07:22 +02:00
|
|
|
|
2018-02-19 18:54:21 +09:00
|
|
|
if (// Is after the monitoring deadline
|
|
|
|
(invoice.MonitoringExpiration < DateTimeOffset.UtcNow)
|
|
|
|
&&
|
|
|
|
// And not enough amount confirmed
|
2023-07-19 18:47:32 +09:00
|
|
|
(minimumDue > 0.0m))
|
2018-02-19 18:54:21 +09:00
|
|
|
{
|
2020-07-24 09:40:37 +02:00
|
|
|
context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.FailedToConfirm));
|
2020-11-23 15:57:05 +09:00
|
|
|
invoice.Status = InvoiceStatusLegacy.Invalid;
|
2018-02-19 18:54:21 +09:00
|
|
|
context.MarkDirty();
|
|
|
|
}
|
2023-07-19 18:47:32 +09:00
|
|
|
else if (minimumDue <= 0.0m)
|
2018-02-19 18:54:21 +09:00
|
|
|
{
|
2020-11-23 15:57:05 +09:00
|
|
|
invoice.Status = InvoiceStatusLegacy.Confirmed;
|
2020-07-24 09:40:37 +02:00
|
|
|
context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Confirmed));
|
2018-02-19 18:54:21 +09:00
|
|
|
context.MarkDirty();
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2018-02-19 18:54:21 +09:00
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
|
2020-11-23 15:57:05 +09:00
|
|
|
if (invoice.Status == InvoiceStatusLegacy.Confirmed)
|
2018-02-19 18:54:21 +09:00
|
|
|
{
|
2023-07-19 18:47:32 +09:00
|
|
|
var unconfPayments = invoice.GetPayments(true).Where(p => !p.GetCryptoPaymentData().PaymentCompleted(p)).ToList();
|
|
|
|
var unconfirmedPaid = unconfPayments.Select(p => p.InvoicePaidAmount.Net).Sum();
|
|
|
|
var minimumDue = invoice.MinimumNetDue + unconfirmedPaid;
|
|
|
|
|
|
|
|
if (minimumDue <= 0.0m)
|
2017-12-03 14:43:52 +09:00
|
|
|
{
|
2020-07-24 09:40:37 +02:00
|
|
|
context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Completed));
|
2020-11-23 15:57:05 +09:00
|
|
|
invoice.Status = InvoiceStatusLegacy.Complete;
|
2018-02-19 18:54:21 +09:00
|
|
|
context.MarkDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-17 01:34:40 +09:00
|
|
|
private void Watch(string invoiceId)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2021-12-28 17:39:54 +09:00
|
|
|
ArgumentNullException.ThrowIfNull(invoiceId);
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2020-02-20 16:54:18 -06:00
|
|
|
if (!_WatchRequests.Writer.TryWrite(invoiceId))
|
|
|
|
{
|
|
|
|
Logs.PayServer.LogWarning($"Failed to write invoice {invoiceId} into WatchRequests channel");
|
|
|
|
}
|
2018-02-17 01:34:40 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task Wait(string invoiceId)
|
|
|
|
{
|
2021-10-05 11:10:41 +02:00
|
|
|
var invoice = await _invoiceRepository.GetInvoice(invoiceId);
|
2018-01-18 12:45:39 +09:00
|
|
|
try
|
|
|
|
{
|
2020-02-21 02:14:49 -06:00
|
|
|
// add 1 second to ensure watch won't trigger moments before invoice expires
|
|
|
|
var delay = invoice.ExpirationTime.AddSeconds(1) - DateTimeOffset.UtcNow;
|
2018-04-07 11:53:33 +09:00
|
|
|
if (delay > TimeSpan.Zero)
|
2018-01-18 12:45:39 +09:00
|
|
|
{
|
2018-04-07 11:53:33 +09:00
|
|
|
await Task.Delay(delay, _Cts.Token);
|
2018-01-18 12:45:39 +09:00
|
|
|
}
|
2018-02-17 13:18:16 +09:00
|
|
|
Watch(invoiceId);
|
2020-02-21 02:14:49 -06:00
|
|
|
|
|
|
|
// add 1 second to ensure watch won't trigger moments before monitoring expires
|
|
|
|
delay = invoice.MonitoringExpiration.AddSeconds(1) - DateTimeOffset.UtcNow;
|
2018-04-07 11:53:33 +09:00
|
|
|
if (delay > TimeSpan.Zero)
|
2018-02-17 01:34:40 +09:00
|
|
|
{
|
2018-04-07 11:53:33 +09:00
|
|
|
await Task.Delay(delay, _Cts.Token);
|
2018-02-17 01:34:40 +09:00
|
|
|
}
|
2018-02-17 13:18:16 +09:00
|
|
|
Watch(invoiceId);
|
2018-01-18 12:45:39 +09:00
|
|
|
}
|
|
|
|
catch when (_Cts.IsCancellationRequested)
|
|
|
|
{ }
|
2018-02-17 01:34:40 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
|
2020-06-28 22:07:48 -05:00
|
|
|
readonly Channel<string> _WatchRequests = Channel.CreateUnbounded<string>();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
2018-01-10 02:07:42 +09:00
|
|
|
Task _Loop;
|
2017-10-27 17:53:04 +09:00
|
|
|
CancellationTokenSource _Cts;
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
_Cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
2018-01-10 02:07:42 +09:00
|
|
|
_Loop = StartLoop(_Cts.Token);
|
2019-04-08 13:28:13 +09:00
|
|
|
_ = WaitPendingInvoices();
|
2017-12-17 14:17:42 +09:00
|
|
|
|
2022-06-15 04:17:10 +02:00
|
|
|
_leases.Add(_eventAggregator.Subscribe<Events.InvoiceNeedUpdateEvent>(b =>
|
2018-02-17 01:34:40 +09:00
|
|
|
{
|
2018-02-18 02:40:53 +09:00
|
|
|
Watch(b.InvoiceId);
|
2018-02-17 01:34:40 +09:00
|
|
|
}));
|
2022-06-15 04:17:10 +02:00
|
|
|
_leases.Add(_eventAggregator.SubscribeAsync<Events.InvoiceEvent>(async b =>
|
2018-01-18 20:56:55 +09:00
|
|
|
{
|
2020-06-22 09:32:51 +02:00
|
|
|
if (InvoiceEventNotification.HandlesEvent(b.Name))
|
|
|
|
{
|
|
|
|
await _notificationSender.SendNotification(new StoreScope(b.Invoice.StoreId),
|
|
|
|
new InvoiceEventNotification(b.Invoice.Id, b.Name));
|
|
|
|
}
|
2019-01-06 10:12:45 +01:00
|
|
|
if (b.Name == InvoiceEvent.Created)
|
2018-01-19 17:39:15 +09:00
|
|
|
{
|
2018-06-21 14:15:36 +09:00
|
|
|
Watch(b.Invoice.Id);
|
2019-04-08 13:28:13 +09:00
|
|
|
_ = Wait(b.Invoice.Id);
|
2018-01-18 20:56:55 +09:00
|
|
|
}
|
2017-12-17 14:17:42 +09:00
|
|
|
|
2019-01-06 10:12:45 +01:00
|
|
|
if (b.Name == InvoiceEvent.ReceivedPayment)
|
2018-02-17 01:34:40 +09:00
|
|
|
{
|
2018-06-21 14:15:36 +09:00
|
|
|
Watch(b.Invoice.Id);
|
2018-02-17 01:34:40 +09:00
|
|
|
}
|
|
|
|
}));
|
2017-10-27 17:53:04 +09:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2018-02-17 01:34:40 +09:00
|
|
|
private async Task WaitPendingInvoices()
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2022-05-23 15:15:26 +09:00
|
|
|
await Task.WhenAll((await _invoiceRepository.GetPendingInvoiceIds())
|
2018-02-17 01:34:40 +09:00
|
|
|
.Select(id => Wait(id)).ToArray());
|
2018-01-10 02:07:42 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
async Task StartLoop(CancellationToken cancellation)
|
|
|
|
{
|
|
|
|
Logs.PayServer.LogInformation("Start watching invoices");
|
2019-11-16 19:05:30 +09:00
|
|
|
while (await _WatchRequests.Reader.WaitToReadAsync(cancellation) && _WatchRequests.Reader.TryRead(out var invoiceId))
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2019-04-08 13:28:13 +09:00
|
|
|
int maxLoop = 5;
|
|
|
|
int loopCount = -1;
|
|
|
|
while (loopCount < maxLoop)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2019-04-08 13:28:13 +09:00
|
|
|
loopCount++;
|
|
|
|
try
|
2018-01-10 02:07:42 +09:00
|
|
|
{
|
2019-04-08 13:28:13 +09:00
|
|
|
cancellation.ThrowIfCancellationRequested();
|
2021-10-05 11:10:41 +02:00
|
|
|
var invoice = await _invoiceRepository.GetInvoice(invoiceId, true);
|
2019-04-08 13:28:13 +09:00
|
|
|
if (invoice == null)
|
|
|
|
break;
|
|
|
|
var updateContext = new UpdateInvoiceContext(invoice);
|
2020-10-26 14:18:38 +09:00
|
|
|
UpdateInvoice(updateContext);
|
2019-04-08 13:28:13 +09:00
|
|
|
if (updateContext.Dirty)
|
2018-01-10 02:07:42 +09:00
|
|
|
{
|
2021-10-05 11:10:41 +02:00
|
|
|
await _invoiceRepository.UpdateInvoiceStatus(invoice.Id, invoice.GetInvoiceState());
|
2019-04-08 13:28:13 +09:00
|
|
|
updateContext.Events.Insert(0, new InvoiceDataChangedEvent(invoice));
|
2018-01-10 02:07:42 +09:00
|
|
|
}
|
2023-12-20 18:41:28 +09:00
|
|
|
if (updateContext.IsPriceUpdated)
|
2021-08-03 17:03:00 +09:00
|
|
|
{
|
2023-12-20 18:41:28 +09:00
|
|
|
await _invoiceRepository.UpdateInvoicePrice(invoice.Id, invoice.Price);
|
2021-08-03 17:03:00 +09:00
|
|
|
}
|
2019-04-08 13:28:13 +09:00
|
|
|
|
|
|
|
foreach (var evt in updateContext.Events)
|
2018-01-10 02:07:42 +09:00
|
|
|
{
|
2021-10-05 11:10:41 +02:00
|
|
|
_eventAggregator.Publish(evt, evt.GetType());
|
2018-02-17 13:18:16 +09:00
|
|
|
}
|
2019-04-08 13:28:13 +09:00
|
|
|
|
2020-11-23 15:57:05 +09:00
|
|
|
if (invoice.Status == InvoiceStatusLegacy.Complete ||
|
|
|
|
((invoice.Status == InvoiceStatusLegacy.Invalid || invoice.Status == InvoiceStatusLegacy.Expired) && invoice.MonitoringExpiration < DateTimeOffset.UtcNow))
|
2018-02-17 13:18:16 +09:00
|
|
|
{
|
2019-05-25 17:20:17 -05:00
|
|
|
var extendInvoiceMonitoring = await UpdateConfirmationCount(invoice);
|
|
|
|
|
|
|
|
// we extend monitor time if we haven't reached max confirmation count
|
|
|
|
// say user used low fee and we only got 3 confirmations right before it's time to remove
|
|
|
|
if (extendInvoiceMonitoring)
|
|
|
|
{
|
2021-10-05 11:10:41 +02:00
|
|
|
await _invoiceRepository.ExtendInvoiceMonitor(invoice.Id);
|
2019-05-25 17:20:17 -05:00
|
|
|
}
|
2021-10-05 11:10:41 +02:00
|
|
|
else if (await _invoiceRepository.RemovePendingInvoice(invoice.Id))
|
2019-05-25 17:20:17 -05:00
|
|
|
{
|
2021-10-05 11:10:41 +02:00
|
|
|
_eventAggregator.Publish(new InvoiceStopWatchedEvent(invoice.Id));
|
2019-05-25 17:20:17 -05:00
|
|
|
}
|
2018-02-18 02:40:53 +09:00
|
|
|
break;
|
2018-01-18 18:33:26 +09:00
|
|
|
}
|
2019-04-08 13:28:13 +09:00
|
|
|
|
|
|
|
if (updateContext.Events.Count == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (Exception ex) when (!cancellation.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
Logs.PayServer.LogError(ex, "Unhandled error on watching invoice " + invoiceId);
|
|
|
|
_ = Task.Delay(10000, cancellation)
|
|
|
|
.ContinueWith(t => Watch(invoiceId), TaskScheduler.Default);
|
|
|
|
break;
|
2018-01-18 18:53:11 +09:00
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2018-01-10 02:07:42 +09:00
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
|
2019-05-25 17:20:17 -05:00
|
|
|
private async Task<bool> UpdateConfirmationCount(InvoiceEntity invoice)
|
|
|
|
{
|
|
|
|
bool extendInvoiceMonitoring = false;
|
|
|
|
var updateConfirmationCountIfNeeded = invoice
|
2021-05-14 16:16:19 +09:00
|
|
|
.GetPayments(false)
|
2019-05-25 17:20:17 -05:00
|
|
|
.Select<PaymentEntity, Task<PaymentEntity>>(async payment =>
|
|
|
|
{
|
|
|
|
var paymentData = payment.GetCryptoPaymentData();
|
|
|
|
if (paymentData is Payments.Bitcoin.BitcoinLikePaymentData onChainPaymentData)
|
|
|
|
{
|
2019-09-21 16:39:44 +02:00
|
|
|
var network = payment.Network as BTCPayNetwork;
|
2019-05-25 17:20:17 -05:00
|
|
|
// Do update if confirmation count in the paymentData is not up to date
|
2019-09-21 16:39:44 +02:00
|
|
|
if ((onChainPaymentData.ConfirmationCount < network.MaxTrackedConfirmation && payment.Accounted)
|
2019-05-25 17:20:17 -05:00
|
|
|
&& (onChainPaymentData.Legacy || invoice.MonitoringExpiration < DateTimeOffset.UtcNow))
|
|
|
|
{
|
2023-07-19 18:47:32 +09:00
|
|
|
var client = _explorerClientProvider.GetExplorerClient(payment.Currency);
|
2023-05-19 08:41:21 +09:00
|
|
|
var transactionResult = client is null ? null : await client.GetTransactionAsync(onChainPaymentData.Outpoint.Hash);
|
2019-05-25 17:20:17 -05:00
|
|
|
var confirmationCount = transactionResult?.Confirmations ?? 0;
|
|
|
|
onChainPaymentData.ConfirmationCount = confirmationCount;
|
|
|
|
payment.SetCryptoPaymentData(onChainPaymentData);
|
|
|
|
|
|
|
|
// we want to extend invoice monitoring until we reach max confirmations on all onchain payment methods
|
2019-09-21 16:39:44 +02:00
|
|
|
if (confirmationCount < network.MaxTrackedConfirmation)
|
2019-05-25 17:20:17 -05:00
|
|
|
extendInvoiceMonitoring = true;
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2019-05-25 17:20:17 -05:00
|
|
|
return payment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.ToArray();
|
|
|
|
await Task.WhenAll(updateConfirmationCountIfNeeded);
|
|
|
|
var updatedPaymentData = updateConfirmationCountIfNeeded.Where(a => a.Result != null).Select(a => a.Result).ToList();
|
|
|
|
if (updatedPaymentData.Count > 0)
|
|
|
|
{
|
2021-10-05 11:10:41 +02:00
|
|
|
await _paymentService.UpdatePayments(updatedPaymentData);
|
2019-05-25 17:20:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return extendInvoiceMonitoring;
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:28:13 +09:00
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2019-03-27 18:56:43 +09:00
|
|
|
if (_Cts == null)
|
2019-04-08 13:28:13 +09:00
|
|
|
return;
|
2022-06-15 04:17:10 +02:00
|
|
|
_leases.Dispose();
|
2017-10-27 17:53:04 +09:00
|
|
|
_Cts.Cancel();
|
2019-04-08 13:28:13 +09:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await _Loop;
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Logs.PayServer.LogInformation("Stop watching invoices");
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
}
|