From 2262acf12bb21aea966ca7f646c082d30528b102 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 18 Apr 2021 17:53:57 -0700 Subject: [PATCH 01/12] Add ability to send test webhook call --- .../StoresController.Integrations.cs | 19 +++++- .../WebhookNotificationManager.cs | 61 ++++++++++++++++++- .../StoreViewModels/TestWebhookViewModel.cs | 9 +++ BTCPayServer/Views/Stores/TestWebhook.cshtml | 31 ++++++++++ BTCPayServer/Views/Stores/Webhooks.cshtml | 4 +- 5 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 BTCPayServer/Models/StoreViewModels/TestWebhookViewModel.cs create mode 100644 BTCPayServer/Views/Stores/TestWebhook.cshtml diff --git a/BTCPayServer/Controllers/StoresController.Integrations.cs b/BTCPayServer/Controllers/StoresController.Integrations.cs index 57fd55e06..7d8916249 100644 --- a/BTCPayServer/Controllers/StoresController.Integrations.cs +++ b/BTCPayServer/Controllers/StoresController.Integrations.cs @@ -1,3 +1,4 @@ +#nullable enable using System.Linq; using System.Threading.Tasks; using BTCPayServer.Data; @@ -14,7 +15,7 @@ namespace BTCPayServer.Controllers [HttpGet("{storeId}/integrations")] public IActionResult Integrations() { - return View("Integrations",new IntegrationsViewModel()); + return View("Integrations", new IntegrationsViewModel()); } [HttpGet("{storeId}/webhooks")] @@ -109,6 +110,22 @@ namespace BTCPayServer.Controllers return RedirectToAction(nameof(Webhooks), new { storeId = CurrentStore.Id }); } + [HttpGet("{storeId}/webhooks/{webhookId}/test")] + public async Task TestWebhook(string webhookId) + { + var webhook = await _Repo.GetWebhook(CurrentStore.Id, webhookId); + if (webhook is null) + return NotFound(); + + return View(nameof(TestWebhook)); + } + + [HttpPost("{storeId}/webhooks/{webhookId}/test")] + public async void TestWebhook(string webhookId, TestWebhookViewModel viewModel) + { + await WebhookNotificationManager.TestWebhook(CurrentStore.Id, webhookId, viewModel.Type); + } + [HttpPost("{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliver")] public async Task RedeliverWebhook(string webhookId, string deliveryId) { diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index ee4d90f2a..a8afa8f1a 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Collections.Generic; using System.Linq; @@ -70,7 +71,7 @@ namespace BTCPayServer.HostedServices Subscribe(); } - public async Task Redeliver(string deliveryId) + public async Task Redeliver(string deliveryId) { var deliveryRequest = await CreateRedeliveryRequest(deliveryId); if (deliveryRequest is null) @@ -79,7 +80,7 @@ namespace BTCPayServer.HostedServices return deliveryRequest.Delivery.Id; } - private async Task CreateRedeliveryRequest(string deliveryId) + private async Task CreateRedeliveryRequest(string deliveryId) { using var ctx = StoreRepository.CreateDbContext(); var webhookDelivery = await ctx.WebhookDeliveries.AsNoTracking() @@ -107,6 +108,39 @@ namespace BTCPayServer.HostedServices newDelivery.SetBlob(newDeliveryBlob); return new WebhookDeliveryRequest(webhookDelivery.Webhook.Id, webhookEvent, newDelivery, webhookDelivery.Webhook.GetBlob()); } + + private WebhookEvent GetTestWebHook(string storeId, string webhookId, WebhookEventType webhookEventType, Data.WebhookDeliveryData delivery) + { + var webhookEvent = GetWebhookEvent(webhookEventType); + webhookEvent.InvoiceId = "__test__" + Guid.NewGuid().ToString() + "__test__"; + webhookEvent.StoreId = storeId; + webhookEvent.DeliveryId = delivery.Id; + webhookEvent.WebhookId = webhookId; + webhookEvent.OriginalDeliveryId = "__test__" + Guid.NewGuid().ToString() + "__test__"; + webhookEvent.IsRedelivery = false; + webhookEvent.Timestamp = delivery.Timestamp; + + return webhookEvent; + } + + public async Task TestWebhook(string storeId, string webhookId, WebhookEventType webhookEventType) + { + var delivery = NewDelivery(); + delivery.WebhookId = webhookId; + + var webhook = (await StoreRepository.GetWebhooks(storeId)).Where(w => w.Id == webhookId).FirstOrDefault(); + + var channel = Channel.CreateUnbounded(); + var deliveryRequest = new WebhookDeliveryRequest( + webhookId, + GetTestWebHook(storeId, webhookId, webhookEventType, delivery), + delivery, + webhook.GetBlob() + ); + channel.Writer.TryWrite(deliveryRequest); + _ = Process(webhookId, channel); + } + protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken) { if (evt is InvoiceEvent invoiceEvent) @@ -147,7 +181,28 @@ namespace BTCPayServer.HostedServices _ = Process(context.WebhookId, channel); } - private WebhookInvoiceEvent GetWebhookEvent(InvoiceEvent invoiceEvent) + private WebhookInvoiceEvent GetWebhookEvent(WebhookEventType webhookEventType) + { + switch (webhookEventType) + { + case WebhookEventType.InvoiceCreated: + return new WebhookInvoiceEvent(WebhookEventType.InvoiceCreated); + case WebhookEventType.InvoiceReceivedPayment: + return new WebhookInvoiceReceivedPaymentEvent(WebhookEventType.InvoiceReceivedPayment); + case WebhookEventType.InvoiceProcessing: + return new WebhookInvoiceProcessingEvent(WebhookEventType.InvoiceProcessing); + case WebhookEventType.InvoiceExpired: + return new WebhookInvoiceExpiredEvent(WebhookEventType.InvoiceExpired); + case WebhookEventType.InvoiceSettled: + return new WebhookInvoiceSettledEvent(WebhookEventType.InvoiceSettled); + case WebhookEventType.InvoiceInvalid: + return new WebhookInvoiceInvalidEvent(WebhookEventType.InvoiceInvalid); + default: + return new WebhookInvoiceEvent(WebhookEventType.InvoiceCreated); + } + } + + private WebhookInvoiceEvent? GetWebhookEvent(InvoiceEvent invoiceEvent) { var eventCode = invoiceEvent.EventCode; switch (eventCode) diff --git a/BTCPayServer/Models/StoreViewModels/TestWebhookViewModel.cs b/BTCPayServer/Models/StoreViewModels/TestWebhookViewModel.cs new file mode 100644 index 000000000..d5fe1c284 --- /dev/null +++ b/BTCPayServer/Models/StoreViewModels/TestWebhookViewModel.cs @@ -0,0 +1,9 @@ +using BTCPayServer.Client.Models; + +namespace BTCPayServer.Models.StoreViewModels +{ + public class TestWebhookViewModel + { + public WebhookEventType Type { get; set; } + } +} diff --git a/BTCPayServer/Views/Stores/TestWebhook.cshtml b/BTCPayServer/Views/Stores/TestWebhook.cshtml new file mode 100644 index 000000000..28920b2c0 --- /dev/null +++ b/BTCPayServer/Views/Stores/TestWebhook.cshtml @@ -0,0 +1,31 @@ +@model EditWebhookViewModel +@using BTCPayServer.Client.Models; +@{ + Layout = "../Shared/_NavLayout.cshtml"; + ViewData.SetActivePageAndTitle(StoreNavPages.Webhooks, "Test Webhook", Context.GetStoreData().StoreName); +} + +
+
+
+

@ViewData["PageTitle"]

+ +
    + @foreach (var evt in new[] + { + ("Test InvoiceCreated event", WebhookEventType.InvoiceCreated), + ("Test InvoiceReceivedPayment event", WebhookEventType.InvoiceReceivedPayment), + ("Test InvoiceProcessing event", WebhookEventType.InvoiceProcessing), + ("Test InvoiceExpired event", WebhookEventType.InvoiceExpired), + ("Test InvoiceSettled event", WebhookEventType.InvoiceSettled), + ("Test InvoiceInvalid event", WebhookEventType.InvoiceInvalid) + }) + { +
  • + +
  • + } +
+
+
+
diff --git a/BTCPayServer/Views/Stores/Webhooks.cshtml b/BTCPayServer/Views/Stores/Webhooks.cshtml index dbc290ffb..2bafad1c1 100644 --- a/BTCPayServer/Views/Stores/Webhooks.cshtml +++ b/BTCPayServer/Views/Stores/Webhooks.cshtml @@ -30,7 +30,9 @@ @wh.Url - Modify - Delete + Test - + Modify - + Delete } From 7cf6c97d3f01e913ca9dae6a7074888b21ab4d61 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 18 Apr 2021 22:52:08 -0700 Subject: [PATCH 02/12] Return view after webhook test POST call --- BTCPayServer/Controllers/StoresController.Integrations.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/BTCPayServer/Controllers/StoresController.Integrations.cs b/BTCPayServer/Controllers/StoresController.Integrations.cs index 7d8916249..993706cec 100644 --- a/BTCPayServer/Controllers/StoresController.Integrations.cs +++ b/BTCPayServer/Controllers/StoresController.Integrations.cs @@ -121,9 +121,11 @@ namespace BTCPayServer.Controllers } [HttpPost("{storeId}/webhooks/{webhookId}/test")] - public async void TestWebhook(string webhookId, TestWebhookViewModel viewModel) + public async Task TestWebhook(string webhookId, TestWebhookViewModel viewModel) { await WebhookNotificationManager.TestWebhook(CurrentStore.Id, webhookId, viewModel.Type); + + return View(nameof(TestWebhook)); } [HttpPost("{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliver")] From 120b82ae00a41ec18435dbde7dac443867ad18d5 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 18 Apr 2021 22:52:32 -0700 Subject: [PATCH 03/12] Update webhook test page design --- BTCPayServer/Views/Stores/TestWebhook.cshtml | 39 +++++++++++--------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/BTCPayServer/Views/Stores/TestWebhook.cshtml b/BTCPayServer/Views/Stores/TestWebhook.cshtml index 28920b2c0..789fcbb95 100644 --- a/BTCPayServer/Views/Stores/TestWebhook.cshtml +++ b/BTCPayServer/Views/Stores/TestWebhook.cshtml @@ -2,7 +2,7 @@ @using BTCPayServer.Client.Models; @{ Layout = "../Shared/_NavLayout.cshtml"; - ViewData.SetActivePageAndTitle(StoreNavPages.Webhooks, "Test Webhook", Context.GetStoreData().StoreName); + ViewData.SetActivePageAndTitle(StoreNavPages.Webhooks, "Send a test event to a webhook endpoint", Context.GetStoreData().StoreName); }
@@ -10,22 +10,27 @@

@ViewData["PageTitle"]

-
    - @foreach (var evt in new[] - { - ("Test InvoiceCreated event", WebhookEventType.InvoiceCreated), - ("Test InvoiceReceivedPayment event", WebhookEventType.InvoiceReceivedPayment), - ("Test InvoiceProcessing event", WebhookEventType.InvoiceProcessing), - ("Test InvoiceExpired event", WebhookEventType.InvoiceExpired), - ("Test InvoiceSettled event", WebhookEventType.InvoiceSettled), - ("Test InvoiceInvalid event", WebhookEventType.InvoiceInvalid) - }) - { -
  • - -
  • - } -
+
+ + +
+ +
From ad11b61af2eaa3bf9a177157aa6fef592ad9468a Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Tue, 20 Apr 2021 18:49:46 -0700 Subject: [PATCH 04/12] Formatting --- .../WebhookNotificationManager.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index a8afa8f1a..4c42a1a28 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -263,15 +263,15 @@ namespace BTCPayServer.HostedServices var originalDeliveryId = result.DeliveryId; foreach (var wait in new[] { - TimeSpan.FromSeconds(10), - TimeSpan.FromMinutes(1), - TimeSpan.FromMinutes(10), - TimeSpan.FromMinutes(10), - TimeSpan.FromMinutes(10), - TimeSpan.FromMinutes(10), - TimeSpan.FromMinutes(10), - TimeSpan.FromMinutes(10), - }) + TimeSpan.FromSeconds(10), + TimeSpan.FromMinutes(1), + TimeSpan.FromMinutes(10), + TimeSpan.FromMinutes(10), + TimeSpan.FromMinutes(10), + TimeSpan.FromMinutes(10), + TimeSpan.FromMinutes(10), + TimeSpan.FromMinutes(10), + }) { await Task.Delay(wait, CancellationToken); ctx = await CreateRedeliveryRequest(originalDeliveryId); From 7c16f8f134c9d1d11fd5187ac41f4f73272f6db1 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Tue, 20 Apr 2021 18:52:09 -0700 Subject: [PATCH 05/12] Allow specifying WebhookDeliveryData webhookid right away --- .../WebhookNotificationManager.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index 4c42a1a28..1c253557c 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -94,8 +94,7 @@ namespace BTCPayServer.HostedServices if (webhookDelivery is null) return null; var oldDeliveryBlob = webhookDelivery.Delivery.GetBlob(); - var newDelivery = NewDelivery(); - newDelivery.WebhookId = webhookDelivery.Webhook.Id; + var newDelivery = NewDelivery(webhookDelivery.Webhook.Id); var newDeliveryBlob = new WebhookDeliveryBlob(); newDeliveryBlob.Request = oldDeliveryBlob.Request; var webhookEvent = newDeliveryBlob.ReadRequestAs(); @@ -125,9 +124,7 @@ namespace BTCPayServer.HostedServices public async Task TestWebhook(string storeId, string webhookId, WebhookEventType webhookEventType) { - var delivery = NewDelivery(); - delivery.WebhookId = webhookId; - + var delivery = NewDelivery(webhookId); var webhook = (await StoreRepository.GetWebhooks(storeId)).Where(w => w.Id == webhookId).FirstOrDefault(); var channel = Channel.CreateUnbounded(); @@ -153,8 +150,7 @@ namespace BTCPayServer.HostedServices continue; if (!ShouldDeliver(webhookEvent.Type, webhookBlob)) continue; - Data.WebhookDeliveryData delivery = NewDelivery(); - delivery.WebhookId = webhook.Id; + Data.WebhookDeliveryData delivery = NewDelivery(webhook.Id); webhookEvent.InvoiceId = invoiceEvent.InvoiceId; webhookEvent.StoreId = invoiceEvent.Invoice.StoreId; webhookEvent.DeliveryId = delivery.Id; @@ -353,12 +349,14 @@ namespace BTCPayServer.HostedServices return bytes; } - private static Data.WebhookDeliveryData NewDelivery() + private static Data.WebhookDeliveryData NewDelivery(string webhookId) { - var delivery = new Data.WebhookDeliveryData(); - delivery.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16)); - delivery.Timestamp = DateTimeOffset.UtcNow; - return delivery; + return new Data.WebhookDeliveryData + { + Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16)), + Timestamp = DateTimeOffset.UtcNow, + WebhookId = webhookId + }; } } } From cb6e8a69373a2f085dab379ecfb91d195266fb08 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Tue, 20 Apr 2021 18:54:51 -0700 Subject: [PATCH 06/12] Simplify TestWebhook method --- BTCPayServer/HostedServices/WebhookNotificationManager.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index 1c253557c..8d14db504 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -126,16 +126,14 @@ namespace BTCPayServer.HostedServices { var delivery = NewDelivery(webhookId); var webhook = (await StoreRepository.GetWebhooks(storeId)).Where(w => w.Id == webhookId).FirstOrDefault(); - - var channel = Channel.CreateUnbounded(); var deliveryRequest = new WebhookDeliveryRequest( webhookId, GetTestWebHook(storeId, webhookId, webhookEventType, delivery), delivery, webhook.GetBlob() ); - channel.Writer.TryWrite(deliveryRequest); - _ = Process(webhookId, channel); + + var result = await SendDelivery(deliveryRequest); } protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken) From 872c99e033442d0c2770adacd8bc22886e88ca56 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 25 Apr 2021 19:37:52 -0700 Subject: [PATCH 07/12] Rename SendDelivery to SendAndSaveDelivery --- BTCPayServer/HostedServices/WebhookNotificationManager.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index 8d14db504..ce7045237 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -133,7 +133,7 @@ namespace BTCPayServer.HostedServices webhook.GetBlob() ); - var result = await SendDelivery(deliveryRequest); + var result = await SendAndSaveDelivery(deliveryRequest); } protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken) @@ -249,7 +249,7 @@ namespace BTCPayServer.HostedServices var wh = (await StoreRepository.GetWebhook(ctx.WebhookId))?.GetBlob(); if (wh is null || !ShouldDeliver(ctx.WebhookEvent.Type, wh)) continue; - var result = await SendDelivery(ctx); + var result = await SendAndSaveDelivery(ctx); if (ctx.WebhookBlob.AutomaticRedelivery && !result.Success && result.DeliveryId is string) @@ -273,7 +273,7 @@ namespace BTCPayServer.HostedServices if (!ctx.WebhookBlob.AutomaticRedelivery || !ShouldDeliver(ctx.WebhookEvent.Type, ctx.WebhookBlob)) break; - result = await SendDelivery(ctx); + result = await SendAndSaveDelivery(ctx); if (result.Success) break; } @@ -300,7 +300,7 @@ namespace BTCPayServer.HostedServices public string DeliveryId { get; set; } public bool Success { get; set; } } - private async Task SendDelivery(WebhookDeliveryRequest ctx) + private async Task SendAndSaveDelivery(WebhookDeliveryRequest ctx) { var uri = new Uri(ctx.WebhookBlob.Url, UriKind.Absolute); var httpClient = GetClient(uri); From 9f6f74df59fb6aea1978554d59466364a8f80edf Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 25 Apr 2021 20:09:26 -0700 Subject: [PATCH 08/12] Show success/error message after sending test webhook --- .../StoresController.Integrations.cs | 8 ++++++- .../WebhookNotificationManager.cs | 22 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/BTCPayServer/Controllers/StoresController.Integrations.cs b/BTCPayServer/Controllers/StoresController.Integrations.cs index 993706cec..05685e514 100644 --- a/BTCPayServer/Controllers/StoresController.Integrations.cs +++ b/BTCPayServer/Controllers/StoresController.Integrations.cs @@ -123,7 +123,13 @@ namespace BTCPayServer.Controllers [HttpPost("{storeId}/webhooks/{webhookId}/test")] public async Task TestWebhook(string webhookId, TestWebhookViewModel viewModel) { - await WebhookNotificationManager.TestWebhook(CurrentStore.Id, webhookId, viewModel.Type); + var result = await WebhookNotificationManager.TestWebhook(CurrentStore.Id, webhookId, viewModel.Type); + + if (result.Success) { + TempData[WellKnownTempData.SuccessMessage] = $"{viewModel.Type.ToString()} event delivered successfully!"; + } else { + TempData[WellKnownTempData.ErrorMessage] = $"{viewModel.Type.ToString()} event could not be delivered"; + } return View(nameof(TestWebhook)); } diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index ce7045237..a7f3430b0 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -122,7 +122,7 @@ namespace BTCPayServer.HostedServices return webhookEvent; } - public async Task TestWebhook(string storeId, string webhookId, WebhookEventType webhookEventType) + public async Task TestWebhook(string storeId, string webhookId, WebhookEventType webhookEventType) { var delivery = NewDelivery(webhookId); var webhook = (await StoreRepository.GetWebhooks(storeId)).Where(w => w.Id == webhookId).FirstOrDefault(); @@ -133,7 +133,7 @@ namespace BTCPayServer.HostedServices webhook.GetBlob() ); - var result = await SendAndSaveDelivery(deliveryRequest); + return await SendDelivery(deliveryRequest); } protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken) @@ -295,12 +295,13 @@ namespace BTCPayServer.HostedServices return wh.Active && wh.AuthorizedEvents.Match(type); } - class DeliveryResult + public class DeliveryResult { - public string DeliveryId { get; set; } + public string? DeliveryId { get; set; } public bool Success { get; set; } } - private async Task SendAndSaveDelivery(WebhookDeliveryRequest ctx) + + private async Task SendDelivery(WebhookDeliveryRequest ctx) { var uri = new Uri(ctx.WebhookBlob.Url, UriKind.Absolute); var httpClient = GetClient(uri); @@ -336,10 +337,19 @@ namespace BTCPayServer.HostedServices deliveryBlob.ErrorMessage = ex.Message; } ctx.Delivery.SetBlob(deliveryBlob); - await StoreRepository.AddWebhookDelivery(ctx.Delivery); + return new DeliveryResult() { Success = deliveryBlob.ErrorMessage is null, DeliveryId = ctx.Delivery.Id }; } + + private async Task SendAndSaveDelivery(WebhookDeliveryRequest ctx) + { + var result = await SendDelivery(ctx); + await StoreRepository.AddWebhookDelivery(ctx.Delivery); + + return result; + } + private byte[] ToBytes(WebhookEvent webhookEvent) { var str = JsonConvert.SerializeObject(webhookEvent, Formatting.Indented, DefaultSerializerSettings); From 4671f08b648fb7ffa0ce9589a0bc4580c44d871a Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 25 Apr 2021 20:30:10 -0700 Subject: [PATCH 09/12] Augment webhook test error message --- BTCPayServer/Controllers/StoresController.Integrations.cs | 2 +- BTCPayServer/HostedServices/WebhookNotificationManager.cs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/BTCPayServer/Controllers/StoresController.Integrations.cs b/BTCPayServer/Controllers/StoresController.Integrations.cs index 05685e514..a6387aa71 100644 --- a/BTCPayServer/Controllers/StoresController.Integrations.cs +++ b/BTCPayServer/Controllers/StoresController.Integrations.cs @@ -128,7 +128,7 @@ namespace BTCPayServer.Controllers if (result.Success) { TempData[WellKnownTempData.SuccessMessage] = $"{viewModel.Type.ToString()} event delivered successfully!"; } else { - TempData[WellKnownTempData.ErrorMessage] = $"{viewModel.Type.ToString()} event could not be delivered"; + TempData[WellKnownTempData.ErrorMessage] = $"{viewModel.Type.ToString()} event could not be delivered. Error message received: {(result.ErrorMessage ?? "unknown")}"; } return View(nameof(TestWebhook)); diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index a7f3430b0..1c9037380 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -299,6 +299,7 @@ namespace BTCPayServer.HostedServices { public string? DeliveryId { get; set; } public bool Success { get; set; } + public string? ErrorMessage { get; set; } } private async Task SendDelivery(WebhookDeliveryRequest ctx) @@ -338,7 +339,12 @@ namespace BTCPayServer.HostedServices } ctx.Delivery.SetBlob(deliveryBlob); - return new DeliveryResult() { Success = deliveryBlob.ErrorMessage is null, DeliveryId = ctx.Delivery.Id }; + return new DeliveryResult() + { + Success = deliveryBlob.ErrorMessage is null, + DeliveryId = ctx.Delivery.Id, + ErrorMessage = deliveryBlob.ErrorMessage + }; } From bc33b32522a4e3dba72b541daaa4c6541db2c02f Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 25 Apr 2021 21:19:17 -0700 Subject: [PATCH 10/12] Add delivery ID to test webhook succes message --- BTCPayServer/Controllers/StoresController.Integrations.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BTCPayServer/Controllers/StoresController.Integrations.cs b/BTCPayServer/Controllers/StoresController.Integrations.cs index a6387aa71..b21addc63 100644 --- a/BTCPayServer/Controllers/StoresController.Integrations.cs +++ b/BTCPayServer/Controllers/StoresController.Integrations.cs @@ -126,7 +126,7 @@ namespace BTCPayServer.Controllers var result = await WebhookNotificationManager.TestWebhook(CurrentStore.Id, webhookId, viewModel.Type); if (result.Success) { - TempData[WellKnownTempData.SuccessMessage] = $"{viewModel.Type.ToString()} event delivered successfully!"; + TempData[WellKnownTempData.SuccessMessage] = $"{viewModel.Type.ToString()} event delivered successfully! Delivery ID is {result.DeliveryId}"; } else { TempData[WellKnownTempData.ErrorMessage] = $"{viewModel.Type.ToString()} event could not be delivered. Error message received: {(result.ErrorMessage ?? "unknown")}"; } From 1b39742919acd2a0797db4f1205bda96905597f7 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Thu, 3 Jun 2021 20:56:28 -0700 Subject: [PATCH 11/12] Simplify FirstOrDefault method call --- BTCPayServer/HostedServices/WebhookNotificationManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BTCPayServer/HostedServices/WebhookNotificationManager.cs b/BTCPayServer/HostedServices/WebhookNotificationManager.cs index 1c9037380..a73c96c2d 100644 --- a/BTCPayServer/HostedServices/WebhookNotificationManager.cs +++ b/BTCPayServer/HostedServices/WebhookNotificationManager.cs @@ -125,7 +125,7 @@ namespace BTCPayServer.HostedServices public async Task TestWebhook(string storeId, string webhookId, WebhookEventType webhookEventType) { var delivery = NewDelivery(webhookId); - var webhook = (await StoreRepository.GetWebhooks(storeId)).Where(w => w.Id == webhookId).FirstOrDefault(); + var webhook = (await StoreRepository.GetWebhooks(storeId)).FirstOrDefault(w => w.Id == webhookId); var deliveryRequest = new WebhookDeliveryRequest( webhookId, GetTestWebHook(storeId, webhookId, webhookEventType, delivery), From c345e81902470ba373560430e47b6ab4ce3158b5 Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Thu, 3 Jun 2021 21:15:53 -0700 Subject: [PATCH 12/12] Simplify template --- BTCPayServer/Views/Stores/TestWebhook.cshtml | 24 ++++++-------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/BTCPayServer/Views/Stores/TestWebhook.cshtml b/BTCPayServer/Views/Stores/TestWebhook.cshtml index 789fcbb95..aee8540b2 100644 --- a/BTCPayServer/Views/Stores/TestWebhook.cshtml +++ b/BTCPayServer/Views/Stores/TestWebhook.cshtml @@ -11,23 +11,13 @@

@ViewData["PageTitle"]

- - + +