Пример #1
0
        private async Task EnsureListening(string invoiceId, bool poll)
        {
            if (Listening(invoiceId))
            {
                return;
            }

            var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);

            foreach (var paymentMethod in invoice.GetPaymentMethods(_NetworkProvider)
                     .Where(c => c.GetId().PaymentType == PaymentTypes.LightningLike))
            {
                var lightningMethod = paymentMethod.GetPaymentMethodDetails() as LightningLikePaymentMethodDetails;
                if (lightningMethod == null)
                {
                    continue;
                }
                var lightningSupportedMethod = invoice.GetSupportedPaymentMethod <LightningSupportedPaymentMethod>(_NetworkProvider)
                                               .FirstOrDefault(c => c.CryptoCode == paymentMethod.GetId().CryptoCode);
                if (lightningSupportedMethod == null)
                {
                    continue;
                }
                var network = _NetworkProvider.GetNetwork(paymentMethod.GetId().CryptoCode);

                var listenedInvoice = new ListenedInvoice()
                {
                    Uri = lightningSupportedMethod.GetLightningChargeUrl(false).AbsoluteUri,
                    PaymentMethodDetails   = lightningMethod,
                    SupportedPaymentMethod = lightningSupportedMethod,
                    PaymentMethod          = paymentMethod,
                    Network   = network,
                    InvoiceId = invoice.Id
                };

                if (poll)
                {
                    var charge        = GetChargeClient(lightningSupportedMethod, network);
                    var chargeInvoice = await charge.GetInvoice(lightningMethod.InvoiceId);

                    if (chargeInvoice == null)
                    {
                        continue;
                    }
                    if (chargeInvoice.Status == "paid")
                    {
                        await AddPayment(network, chargeInvoice, listenedInvoice);
                    }
                    if (chargeInvoice.Status == "paid" || chargeInvoice.Status == "expired")
                    {
                        continue;
                    }
                }

                StartListening(listenedInvoice);
            }
        }
Пример #2
0
        private async Task Listen(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network)
        {
            ILightningInvoiceListener session = null;

            try
            {
                Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Start listening {supportedPaymentMethod.GetLightningUrl().BaseUri}");
                var lightningClient = supportedPaymentMethod.CreateClient(network);
                session = await lightningClient.Listen(_Cts.Token);

                while (true)
                {
                    var notification = await session.WaitInvoice(_Cts.Token);

                    ListenedInvoice listenedInvoice = GetListenedInvoice(notification.Id);
                    if (listenedInvoice == null)
                    {
                        continue;
                    }
                    if (notification.Id == listenedInvoice.PaymentMethodDetails.InvoiceId &&
                        notification.BOLT11 == listenedInvoice.PaymentMethodDetails.BOLT11)
                    {
                        if (notification.Status == LightningInvoiceStatus.Paid &&
                            notification.PaidAt.HasValue && notification.Amount != null)
                        {
                            await AddPayment(network, notification, listenedInvoice);

                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                        if (notification.Status == LightningInvoiceStatus.Expired)
                        {
                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                    }
                }
            }
            catch when(_Cts.IsCancellationRequested)
            {
            }
Пример #3
0
        private async Task Listen(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network)
        {
            try
            {
                Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Start listening {supportedPaymentMethod.GetLightningChargeUrl(false)}");
                var charge  = GetChargeClient(supportedPaymentMethod, network);
                var session = await charge.Listen(_Cts.Token);

                while (true)
                {
                    var notification = await session.NextEvent(_Cts.Token);

                    ListenedInvoice listenedInvoice = GetListenedInvoice(notification.Id);
                    if (listenedInvoice == null)
                    {
                        continue;
                    }

                    if (notification.Id == listenedInvoice.PaymentMethodDetails.InvoiceId &&
                        notification.PaymentRequest == listenedInvoice.PaymentMethodDetails.BOLT11)
                    {
                        if (notification.Status == "paid" && notification.PaidAt.HasValue)
                        {
                            await AddPayment(network, notification, listenedInvoice);

                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                        if (notification.Status == "expired")
                        {
                            if (DoneListening(listenedInvoice))
                            {
                                break;
                            }
                        }
                    }
                }
            }
            catch when(_Cts.IsCancellationRequested)
            {
            }
Пример #4
0
        private async Task EnsureListening(string invoiceId, bool poll)
        {
            if (Listening(invoiceId))
            {
                return;
            }
            var invoice = await _InvoiceRepository.GetInvoice(invoiceId);

            foreach (var paymentMethod in invoice.GetPaymentMethods(_NetworkProvider)
                     .Where(c => c.GetId().PaymentType == PaymentTypes.LightningLike))
            {
                var lightningMethod = paymentMethod.GetPaymentMethodDetails() as LightningLikePaymentMethodDetails;
                if (lightningMethod == null)
                {
                    continue;
                }
                var lightningSupportedMethod = invoice.GetSupportedPaymentMethod <LightningSupportedPaymentMethod>(_NetworkProvider)
                                               .FirstOrDefault(c => c.CryptoCode == paymentMethod.GetId().CryptoCode);
                if (lightningSupportedMethod == null)
                {
                    continue;
                }
                var network = _NetworkProvider.GetNetwork(paymentMethod.GetId().CryptoCode);

                var listenedInvoice = new ListenedInvoice()
                {
                    Uri = lightningSupportedMethod.GetLightningUrl().BaseUri.AbsoluteUri,
                    PaymentMethodDetails   = lightningMethod,
                    SupportedPaymentMethod = lightningSupportedMethod,
                    PaymentMethod          = paymentMethod,
                    Network   = network,
                    InvoiceId = invoice.Id
                };

                if (poll)
                {
                    var charge = lightningSupportedMethod.CreateClient(network);
                    LightningInvoice chargeInvoice = null;
                    try
                    {
                        chargeInvoice = await charge.GetInvoice(lightningMethod.InvoiceId);
                    }
                    catch (Exception ex)
                    {
                        Logs.PayServer.LogError(ex, $"{lightningSupportedMethod.CryptoCode} (Lightning): Can't connect to the lightning server");
                        continue;
                    }
                    if (chargeInvoice == null)
                    {
                        continue;
                    }
                    if (chargeInvoice.Status == LightningInvoiceStatus.Paid)
                    {
                        await AddPayment(network, chargeInvoice, listenedInvoice);
                    }
                    if (chargeInvoice.Status == LightningInvoiceStatus.Paid || chargeInvoice.Status == LightningInvoiceStatus.Expired)
                    {
                        continue;
                    }
                }

                StartListening(listenedInvoice);
            }
        }