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);