From 83b54b7be1d902aee38ee3bbce362bc540400f4f Mon Sep 17 00:00:00 2001 From: Nicolas Dorier Date: Fri, 24 Sep 2021 00:00:55 +0900 Subject: [PATCH] Fix infinite loop happening if payment method unavailable with on invoices with lazy activation (#2914) --- BTCPayServer/Controllers/InvoiceController.UI.cs | 8 +++++--- BTCPayServer/Services/Invoices/InvoiceExtensions.cs | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index c0f27133c..45ea48083 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -517,9 +517,11 @@ namespace BTCPayServer.Controllers var paymentMethodDetails = paymentMethod.GetPaymentMethodDetails(); if (!paymentMethodDetails.Activated) { - await _InvoiceRepository.ActivateInvoicePaymentMethod(_EventAggregator, _NetworkProvider, - _paymentMethodHandlerDictionary, store, invoice, paymentMethod.GetId()); - return await GetInvoiceModel(invoiceId, paymentMethodId, lang); + if (await _InvoiceRepository.ActivateInvoicePaymentMethod(_EventAggregator, _NetworkProvider, + _paymentMethodHandlerDictionary, store, invoice, paymentMethod.GetId())) + { + return await GetInvoiceModel(invoiceId, paymentMethodId, lang); + } } var dto = invoice.EntityToDTO(); var storeBlob = store.GetStoreBlob(); diff --git a/BTCPayServer/Services/Invoices/InvoiceExtensions.cs b/BTCPayServer/Services/Invoices/InvoiceExtensions.cs index 280387d63..39e1037e8 100644 --- a/BTCPayServer/Services/Invoices/InvoiceExtensions.cs +++ b/BTCPayServer/Services/Invoices/InvoiceExtensions.cs @@ -11,10 +11,11 @@ namespace BTCPayServer.Services.Invoices public static class InvoiceExtensions { - public static async Task ActivateInvoicePaymentMethod(this InvoiceRepository invoiceRepository, + public static async Task ActivateInvoicePaymentMethod(this InvoiceRepository invoiceRepository, EventAggregator eventAggregator, BTCPayNetworkProvider btcPayNetworkProvider, PaymentMethodHandlerDictionary paymentMethodHandlerDictionary, StoreData store,InvoiceEntity invoice, PaymentMethodId paymentMethodId) { + bool success = false; var eligibleMethodToActivate = invoice.GetPaymentMethod(paymentMethodId); if (!eligibleMethodToActivate.GetPaymentMethodDetails().Activated) { @@ -34,6 +35,8 @@ namespace BTCPayServer.Services.Invoices eligibleMethodToActivate.SetPaymentMethodDetails(newDetails); await invoiceRepository.UpdateInvoicePaymentMethod(invoice.Id, eligibleMethodToActivate); eventAggregator.Publish(new InvoicePaymentMethodActivated(paymentMethodId, invoice)); + eventAggregator.Publish(new InvoiceNeedUpdateEvent(invoice.Id)); + success = true; } catch (PaymentMethodUnavailableException ex) { @@ -45,8 +48,8 @@ namespace BTCPayServer.Services.Invoices } await invoiceRepository.AddInvoiceLogs(invoice.Id, logs); - eventAggregator.Publish(new InvoiceNeedUpdateEvent(invoice.Id)); } + return success; } } }