mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-21 22:11:48 +01:00
Add ability to send test webhook call
This commit is contained in:
parent
a830c1e812
commit
2262acf12b
5 changed files with 119 additions and 5 deletions
|
@ -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<IActionResult> 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<IActionResult> RedeliverWebhook(string webhookId, string deliveryId)
|
||||
{
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -70,7 +71,7 @@ namespace BTCPayServer.HostedServices
|
|||
Subscribe<InvoiceEvent>();
|
||||
}
|
||||
|
||||
public async Task<string> Redeliver(string deliveryId)
|
||||
public async Task<string?> 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<WebhookDeliveryRequest> CreateRedeliveryRequest(string deliveryId)
|
||||
private async Task<WebhookDeliveryRequest?> 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<WebhookDeliveryRequest>();
|
||||
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)
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
using BTCPayServer.Client.Models;
|
||||
|
||||
namespace BTCPayServer.Models.StoreViewModels
|
||||
{
|
||||
public class TestWebhookViewModel
|
||||
{
|
||||
public WebhookEventType Type { get; set; }
|
||||
}
|
||||
}
|
31
BTCPayServer/Views/Stores/TestWebhook.cshtml
Normal file
31
BTCPayServer/Views/Stores/TestWebhook.cshtml
Normal file
|
@ -0,0 +1,31 @@
|
|||
@model EditWebhookViewModel
|
||||
@using BTCPayServer.Client.Models;
|
||||
@{
|
||||
Layout = "../Shared/_NavLayout.cshtml";
|
||||
ViewData.SetActivePageAndTitle(StoreNavPages.Webhooks, "Test Webhook", Context.GetStoreData().StoreName);
|
||||
}
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-8">
|
||||
<form method="post">
|
||||
<h4 class="mb-3">@ViewData["PageTitle"]</h4>
|
||||
|
||||
<ul class="list-group">
|
||||
@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)
|
||||
})
|
||||
{
|
||||
<li class="list-group-item">
|
||||
<button type="submit" name="Type" class="btn btn-primary" value="@evt.Item2">@evt.Item1</button>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
|
@ -30,7 +30,9 @@
|
|||
<tr>
|
||||
<td class="text-truncate d-block" style="max-width:300px;">@wh.Url</td>
|
||||
<td class="text-end">
|
||||
<a asp-action="ModifyWebhook" asp-route-storeId="@this.Context.GetRouteValue("storeId")" asp-route-webhookId="@wh.Id">Modify</a> - <a asp-action="DeleteWebhook" asp-route-storeId="@this.Context.GetRouteValue("storeId")" asp-route-webhookId="@wh.Id">Delete</a>
|
||||
<a asp-action="TestWebhook" asp-route-storeId="@this.Context.GetRouteValue("storeId")" asp-route-webhookId="@wh.Id">Test</a> -
|
||||
<a asp-action="ModifyWebhook" asp-route-storeId="@this.Context.GetRouteValue("storeId")" asp-route-webhookId="@wh.Id">Modify</a> -
|
||||
<a asp-action="DeleteWebhook" asp-route-storeId="@this.Context.GetRouteValue("storeId")" asp-route-webhookId="@wh.Id">Delete</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue