From 08bd13b2cd26d20f88ed9a5ade1398d54e2324ca Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 6 Oct 2021 11:25:21 +0900 Subject: [PATCH] Make CanUseWebhooks more resilient --- BTCPayServer.Client/BTCPayServerClient.Webhooks.cs | 2 ++ BTCPayServer.Tests/GreenfieldAPITests.cs | 3 ++- BTCPayServer/Controllers/InvoiceController.UI.cs | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs b/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs index e6e754209..189048f02 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs @@ -42,6 +42,8 @@ namespace BTCPayServer.Client public virtual async Task GetWebhookDelivery(string storeId, string webhookId, string deliveryId, CancellationToken token = default) { var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}"), token); + if (response.StatusCode == System.Net.HttpStatusCode.NotFound) + return null; return await HandleResponse(response); } public virtual async Task RedeliverWebhook(string storeId, string webhookId, string deliveryId, CancellationToken token = default) diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index 721bdb494..c4f11fa4b 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -781,9 +781,10 @@ namespace BTCPayServer.Tests var newDeliveryId = await clientProfile.RedeliverWebhook(user.StoreId, hook.Id, delivery.Id); req = await fakeServer.GetNextRequest(); req.Response.StatusCode = 404; - fakeServer.Done(); await TestUtils.EventuallyAsync(async () => { + // Releasing semaphore several times may help making this test less flaky + fakeServer.Done(); var newDelivery = await clientProfile.GetWebhookDelivery(user.StoreId, hook.Id, newDeliveryId); Assert.NotNull(newDelivery); Assert.Equal(404, newDelivery.HttpCode); diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 6f0690c6d..1d41b4455 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -172,7 +172,7 @@ namespace BTCPayServer.Controllers .Include(i => i.CurrentRefund) .Include(i => i.CurrentRefund.PullPaymentData) .Where(i => i.Id == invoiceId) - .FirstOrDefaultAsync(); + .FirstOrDefaultAsync(cancellationToken: cancellationToken); if (invoice is null) return NotFound(); if (invoice.CurrentRefund?.PullPaymentDataId is null && GetUserId() is null) @@ -342,7 +342,7 @@ namespace BTCPayServer.Controllers Html = "Refund successfully created!
Share the link to this page with a customer.
The customer needs to enter their address and claim the refund.
Once a customer claims the refund, you will get a notification and would need to approve and initiate it from your Wallet > Manage > Payouts.", Severity = StatusMessageModel.StatusSeverity.Success }); - (await ctx.Invoices.FindAsync(invoice.Id)).CurrentRefundId = ppId; + (await ctx.Invoices.FindAsync(new[] { invoice.Id }, cancellationToken: cancellationToken)).CurrentRefundId = ppId; ctx.Refunds.Add(new RefundData() { InvoiceDataId = invoice.Id,