Updating .editorconfig with new styles and running it again

This commit is contained in:
rockstardev
2025-06-02 00:11:35 -05:00
parent be543b76e4
commit a92a904de9
11 changed files with 72 additions and 70 deletions

View File

@@ -136,10 +136,10 @@ dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion dotnet_style_qualification_for_event = false:suggestion
# only use var when it's obvious what the variable type is # always use var when possible
csharp_style_var_for_built_in_types = false:none csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = false:none csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = false:suggestion csharp_style_var_elsewhere = true:suggestion
# use language keywords instead of BCL types # use language keywords instead of BCL types
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
@@ -186,6 +186,13 @@ csharp_style_expression_bodied_properties = true:none
csharp_style_expression_bodied_indexers = true:none csharp_style_expression_bodied_indexers = true:none
csharp_style_expression_bodied_accessors = true:none csharp_style_expression_bodied_accessors = true:none
# Property and accessor preferences
csharp_style_auto_properties = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_keep_existing_attribute_arrangement = true
csharp_keep_existing_initializer_arrangement = true
csharp_prefer_simple_auto_property = true:suggestion
# Pattern matching # Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

View File

@@ -4,7 +4,6 @@ using BTCPayServer.Client.Models;
using BTCPayServer.Controllers; using BTCPayServer.Controllers;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Invoices;
using MimeKit;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData; using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks; namespace BTCPayServer.HostedServices.Webhooks;
@@ -23,7 +22,7 @@ public class InvoiceWebhookDeliveryRequest(
UIStoresController.StoreEmailRule storeEmailRule) UIStoresController.StoreEmailRule storeEmailRule)
{ {
if (storeEmailRule.CustomerEmail && if (storeEmailRule.CustomerEmail &&
MailboxAddressValidator.TryParse(Invoice.Metadata.BuyerEmail, out MailboxAddress bmb)) MailboxAddressValidator.TryParse(Invoice.Metadata.BuyerEmail, out var bmb))
{ {
req.Email ??= string.Empty; req.Email ??= string.Empty;
req.Email += $",{bmb}"; req.Email += $",{bmb}";
@@ -36,7 +35,7 @@ public class InvoiceWebhookDeliveryRequest(
private string Interpolate(string str) private string Interpolate(string str)
{ {
string res = str.Replace("{Invoice.Id}", Invoice.Id) var res = str.Replace("{Invoice.Id}", Invoice.Id)
.Replace("{Invoice.StoreId}", Invoice.StoreId) .Replace("{Invoice.StoreId}", Invoice.StoreId)
.Replace("{Invoice.Price}", Invoice.Price.ToString(CultureInfo.InvariantCulture)) .Replace("{Invoice.Price}", Invoice.Price.ToString(CultureInfo.InvariantCulture))
.Replace("{Invoice.Currency}", Invoice.Currency) .Replace("{Invoice.Currency}", Invoice.Currency)

View File

@@ -5,7 +5,6 @@ using BTCPayServer.Controllers.Greenfield;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Events; using BTCPayServer.Events;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks; namespace BTCPayServer.HostedServices.Webhooks;
@@ -34,14 +33,14 @@ public class InvoiceWebhookProvider(
protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(InvoiceEvent invoiceEvent, protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(InvoiceEvent invoiceEvent,
WebhookData webhook) WebhookData webhook)
{ {
WebhookInvoiceEvent webhookEvent = GetWebhookEvent(invoiceEvent)!; var webhookEvent = GetWebhookEvent(invoiceEvent)!;
WebhookBlob webhookBlob = webhook?.GetBlob(); var webhookBlob = webhook?.GetBlob();
webhookEvent.InvoiceId = invoiceEvent.InvoiceId; webhookEvent.InvoiceId = invoiceEvent.InvoiceId;
webhookEvent.StoreId = invoiceEvent.Invoice.StoreId; webhookEvent.StoreId = invoiceEvent.Invoice.StoreId;
webhookEvent.Metadata = invoiceEvent.Invoice.Metadata.ToJObject(); webhookEvent.Metadata = invoiceEvent.Invoice.Metadata.ToJObject();
webhookEvent.WebhookId = webhook?.Id; webhookEvent.WebhookId = webhook?.Id;
webhookEvent.IsRedelivery = false; webhookEvent.IsRedelivery = false;
WebhookDeliveryData delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id); var delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id);
if (delivery is not null) if (delivery is not null)
{ {
webhookEvent.DeliveryId = delivery.Id; webhookEvent.DeliveryId = delivery.Id;
@@ -55,14 +54,14 @@ public class InvoiceWebhookProvider(
public override WebhookEvent CreateTestEvent(string type, params object[] args) public override WebhookEvent CreateTestEvent(string type, params object[] args)
{ {
string storeId = args[0].ToString(); var storeId = args[0].ToString();
return new WebhookInvoiceEvent(type, storeId) { InvoiceId = "__test__" + Guid.NewGuid() + "__test__" }; return new WebhookInvoiceEvent(type, storeId) { InvoiceId = "__test__" + Guid.NewGuid() + "__test__" };
} }
protected override WebhookInvoiceEvent GetWebhookEvent(InvoiceEvent invoiceEvent) protected override WebhookInvoiceEvent GetWebhookEvent(InvoiceEvent invoiceEvent)
{ {
InvoiceEventCode eventCode = invoiceEvent.EventCode; var eventCode = invoiceEvent.EventCode;
string storeId = invoiceEvent.Invoice.StoreId; var storeId = invoiceEvent.Invoice.StoreId;
switch (eventCode) switch (eventCode)
{ {
case InvoiceEventCode.Confirmed: case InvoiceEventCode.Confirmed:

View File

@@ -5,7 +5,6 @@ using BTCPayServer.Client.Models;
using BTCPayServer.Controllers; using BTCPayServer.Controllers;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Services.PaymentRequests; using BTCPayServer.Services.PaymentRequests;
using MimeKit;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData; using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks; namespace BTCPayServer.HostedServices.Webhooks;
@@ -21,9 +20,9 @@ public class PaymentRequestWebhookDeliveryRequest(
public override Task<SendEmailRequest?> Interpolate(SendEmailRequest req, public override Task<SendEmailRequest?> Interpolate(SendEmailRequest req,
UIStoresController.StoreEmailRule storeEmailRule) UIStoresController.StoreEmailRule storeEmailRule)
{ {
PaymentRequestBlob? blob = evt.Data.GetBlob(); var blob = evt.Data.GetBlob();
if (storeEmailRule.CustomerEmail && if (storeEmailRule.CustomerEmail &&
MailboxAddressValidator.TryParse(blob.Email, out MailboxAddress? bmb)) MailboxAddressValidator.TryParse(blob.Email, out var bmb))
{ {
req.Email ??= string.Empty; req.Email ??= string.Empty;
req.Email += $",{bmb}"; req.Email += $",{bmb}";
@@ -36,11 +35,11 @@ public class PaymentRequestWebhookDeliveryRequest(
private string Interpolate(string str, PaymentRequestData data) private string Interpolate(string str, PaymentRequestData data)
{ {
string? id = data.Id; var id = data.Id;
string trimmedId = $"{id.Substring(0, 7)}...{id.Substring(id.Length - 7)}"; var trimmedId = $"{id.Substring(0, 7)}...{id.Substring(id.Length - 7)}";
PaymentRequestBlob? blob = data.GetBlob(); var blob = data.GetBlob();
string res = str.Replace("{PaymentRequest.Id}", id) var res = str.Replace("{PaymentRequest.Id}", id)
.Replace("{PaymentRequest.TrimmedId}", trimmedId) .Replace("{PaymentRequest.TrimmedId}", trimmedId)
.Replace("{PaymentRequest.Amount}", data.Amount.ToString(CultureInfo.InvariantCulture)) .Replace("{PaymentRequest.Amount}", data.Amount.ToString(CultureInfo.InvariantCulture))
.Replace("{PaymentRequest.Currency}", data.Currency) .Replace("{PaymentRequest.Currency}", data.Currency)

View File

@@ -4,7 +4,6 @@ using BTCPayServer.Client.Models;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Services.PaymentRequests; using BTCPayServer.Services.PaymentRequests;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks; namespace BTCPayServer.HostedServices.Webhooks;
@@ -25,7 +24,7 @@ public class PaymentRequestWebhookProvider(EventAggregator eventAggregator, ILog
public override WebhookEvent CreateTestEvent(string type, object[] args) public override WebhookEvent CreateTestEvent(string type, object[] args)
{ {
string storeId = args[0].ToString(); var storeId = args[0].ToString();
return new WebhookPaymentRequestEvent(type, storeId) { PaymentRequestId = "__test__" + Guid.NewGuid() + "__test__" }; return new WebhookPaymentRequestEvent(type, storeId) { PaymentRequestId = "__test__" + Guid.NewGuid() + "__test__" };
} }
@@ -44,14 +43,14 @@ public class PaymentRequestWebhookProvider(EventAggregator eventAggregator, ILog
protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(PaymentRequestEvent paymentRequestEvent, WebhookData webhook) protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(PaymentRequestEvent paymentRequestEvent, WebhookData webhook)
{ {
WebhookBlob webhookBlob = webhook?.GetBlob(); var webhookBlob = webhook?.GetBlob();
WebhookPaymentRequestEvent webhookEvent = GetWebhookEvent(paymentRequestEvent)!; var webhookEvent = GetWebhookEvent(paymentRequestEvent)!;
webhookEvent.StoreId = paymentRequestEvent.Data.StoreDataId; webhookEvent.StoreId = paymentRequestEvent.Data.StoreDataId;
webhookEvent.PaymentRequestId = paymentRequestEvent.Data.Id; webhookEvent.PaymentRequestId = paymentRequestEvent.Data.Id;
webhookEvent.Status = paymentRequestEvent.Data.Status; webhookEvent.Status = paymentRequestEvent.Data.Status;
webhookEvent.WebhookId = webhook?.Id; webhookEvent.WebhookId = webhook?.Id;
webhookEvent.IsRedelivery = false; webhookEvent.IsRedelivery = false;
WebhookDeliveryData delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id); var delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id);
if (delivery is not null) if (delivery is not null)
{ {
webhookEvent.DeliveryId = delivery.Id; webhookEvent.DeliveryId = delivery.Id;

View File

@@ -27,8 +27,8 @@ public class PayoutWebhookDeliveryRequest(
private string Interpolate(string str) private string Interpolate(string str)
{ {
PayoutBlob? blob = evt.Payout.GetBlob(btcPayNetworkJsonSerializerSettings); var blob = evt.Payout.GetBlob(btcPayNetworkJsonSerializerSettings);
string res = str.Replace("{Payout.Id}", evt.Payout.Id) var res = str.Replace("{Payout.Id}", evt.Payout.Id)
.Replace("{Payout.PullPaymentId}", evt.Payout.PullPaymentDataId) .Replace("{Payout.PullPaymentId}", evt.Payout.PullPaymentDataId)
.Replace("{Payout.Destination}", evt.Payout.DedupId ?? blob.Destination) .Replace("{Payout.Destination}", evt.Payout.DedupId ?? blob.Destination)
.Replace("{Payout.State}", evt.Payout.State.ToString()); .Replace("{Payout.State}", evt.Payout.State.ToString());

View File

@@ -4,7 +4,6 @@ using BTCPayServer.Client.Models;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Services; using BTCPayServer.Services;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks; namespace BTCPayServer.HostedServices.Webhooks;
@@ -17,16 +16,16 @@ public class PayoutWebhookProvider(
{ {
protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(PayoutEvent payoutEvent, WebhookData webhook) protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(PayoutEvent payoutEvent, WebhookData webhook)
{ {
WebhookBlob webhookBlob = webhook?.GetBlob(); var webhookBlob = webhook?.GetBlob();
WebhookPayoutEvent webhookEvent = GetWebhookEvent(payoutEvent)!; var webhookEvent = GetWebhookEvent(payoutEvent)!;
webhookEvent.StoreId = payoutEvent.Payout.StoreDataId; webhookEvent.StoreId = payoutEvent.Payout.StoreDataId;
webhookEvent.PayoutId = payoutEvent.Payout.Id; webhookEvent.PayoutId = payoutEvent.Payout.Id;
webhookEvent.PayoutState = payoutEvent.Payout.State; webhookEvent.PayoutState = payoutEvent.Payout.State;
webhookEvent.PullPaymentId = payoutEvent.Payout.PullPaymentDataId; webhookEvent.PullPaymentId = payoutEvent.Payout.PullPaymentDataId;
webhookEvent.WebhookId = webhook?.Id; webhookEvent.WebhookId = webhook?.Id;
webhookEvent.IsRedelivery = false; webhookEvent.IsRedelivery = false;
WebhookDeliveryData delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id); var delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id);
if (delivery is not null) if (delivery is not null)
{ {
webhookEvent.DeliveryId = delivery.Id; webhookEvent.DeliveryId = delivery.Id;
@@ -49,7 +48,7 @@ public class PayoutWebhookProvider(
public override WebhookEvent CreateTestEvent(string type, object[] args) public override WebhookEvent CreateTestEvent(string type, object[] args)
{ {
string storeId = args[0].ToString(); var storeId = args[0].ToString();
return new WebhookPayoutEvent(type, storeId) { PayoutId = "__test__" + Guid.NewGuid() + "__test__" }; return new WebhookPayoutEvent(type, storeId) { PayoutId = "__test__" + Guid.NewGuid() + "__test__" };
} }

View File

@@ -17,7 +17,7 @@ public class PendingTransactionDeliveryRequest(
public override Task<SendEmailRequest> Interpolate(SendEmailRequest req, public override Task<SendEmailRequest> Interpolate(SendEmailRequest req,
UIStoresController.StoreEmailRule storeEmailRule) UIStoresController.StoreEmailRule storeEmailRule)
{ {
PendingTransactionBlob blob = evt.Data.GetBlob(); var blob = evt.Data.GetBlob();
// if (storeEmailRule.CustomerEmail && // if (storeEmailRule.CustomerEmail &&
// MailboxAddressValidator.TryParse(Invoice.Metadata.BuyerEmail, out var bmb)) // MailboxAddressValidator.TryParse(Invoice.Metadata.BuyerEmail, out var bmb))
// { // {
@@ -32,10 +32,10 @@ public class PendingTransactionDeliveryRequest(
private string Interpolate(string str, PendingTransactionBlob blob) private string Interpolate(string str, PendingTransactionBlob blob)
{ {
string id = evt.Data.TransactionId; var id = evt.Data.TransactionId;
string trimmedId = $"{id.Substring(0, 7)}...{id.Substring(id.Length - 7)}"; var trimmedId = $"{id.Substring(0, 7)}...{id.Substring(id.Length - 7)}";
string res = str.Replace("{PendingTransaction.Id}", id) var res = str.Replace("{PendingTransaction.Id}", id)
.Replace("{PendingTransaction.TrimmedId}", trimmedId) .Replace("{PendingTransaction.TrimmedId}", trimmedId)
.Replace("{PendingTransaction.StoreId}", evt.Data.StoreId) .Replace("{PendingTransaction.StoreId}", evt.Data.StoreId)
.Replace("{PendingTransaction.SignaturesCollected}", blob.SignaturesCollected?.ToString()) .Replace("{PendingTransaction.SignaturesCollected}", blob.SignaturesCollected?.ToString())

View File

@@ -4,7 +4,6 @@ using BTCPayServer.Client.Models;
using BTCPayServer.Data; using BTCPayServer.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks; namespace BTCPayServer.HostedServices.Webhooks;
@@ -33,13 +32,13 @@ public class PendingTransactionWebhookProvider(
protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(PendingTransactionService.PendingTransactionEvent evt, protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(PendingTransactionService.PendingTransactionEvent evt,
WebhookData webhook) WebhookData webhook)
{ {
WebhookBlob webhookBlob = webhook?.GetBlob(); var webhookBlob = webhook?.GetBlob();
WebhookPendingTransactionEvent webhookEvent = GetWebhookEvent(evt)!; var webhookEvent = GetWebhookEvent(evt)!;
webhookEvent.StoreId = evt.Data.StoreId; webhookEvent.StoreId = evt.Data.StoreId;
webhookEvent.WebhookId = webhook?.Id; webhookEvent.WebhookId = webhook?.Id;
webhookEvent.IsRedelivery = false; webhookEvent.IsRedelivery = false;
WebhookDeliveryData delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id); var delivery = webhook is null ? null : WebhookExtensions.NewWebhookDelivery(webhook.Id);
if (delivery is not null) if (delivery is not null)
{ {
webhookEvent.DeliveryId = delivery.Id; webhookEvent.DeliveryId = delivery.Id;
@@ -68,7 +67,7 @@ public class PendingTransactionWebhookProvider(
public override WebhookEvent CreateTestEvent(string type, params object[] args) public override WebhookEvent CreateTestEvent(string type, params object[] args)
{ {
string storeId = args[0].ToString(); var storeId = args[0].ToString();
return new WebhookInvoiceEvent(type, storeId) { InvoiceId = "__test__" + Guid.NewGuid() + "__test__" }; return new WebhookInvoiceEvent(type, storeId) { InvoiceId = "__test__" + Guid.NewGuid() + "__test__" };
} }

View File

@@ -31,8 +31,9 @@ public abstract class WebhookProvider<T>(EventAggregator eventAggregator, ILogge
if (GetWebhookEvent(tEvt) is not { } webhookEvent) if (GetWebhookEvent(tEvt) is not { } webhookEvent)
return; return;
WebhookData[] webhooks = await webhookSender.GetWebhooks(webhookEvent.StoreId, webhookEvent.Type); var webhooks = await webhookSender.GetWebhooks(webhookEvent.StoreId, webhookEvent.Type);
foreach (WebhookData webhook in webhooks) webhookSender.EnqueueDelivery(CreateDeliveryRequest(tEvt, webhook)); foreach (var webhook in webhooks)
webhookSender.EnqueueDelivery(CreateDeliveryRequest(tEvt, webhook));
EventAggregator.Publish(CreateDeliveryRequest(tEvt, null)); EventAggregator.Publish(CreateDeliveryRequest(tEvt, null));
} }

View File

@@ -63,7 +63,7 @@ public class WebhookSender(
public async Task StopAsync(CancellationToken cancellationToken) public async Task StopAsync(CancellationToken cancellationToken)
{ {
Task stopping = _processingQueue.Abort(cancellationToken); var stopping = _processingQueue.Abort(cancellationToken);
await stopping; await stopping;
} }
@@ -76,7 +76,7 @@ public class WebhookSender(
public async Task<string?> Redeliver(string deliveryId) public async Task<string?> Redeliver(string deliveryId)
{ {
WebhookDeliveryRequest? deliveryRequest = await CreateRedeliveryRequest(deliveryId); var deliveryRequest = await CreateRedeliveryRequest(deliveryId);
if (deliveryRequest is null) if (deliveryRequest is null)
return null; return null;
EnqueueDelivery(deliveryRequest); EnqueueDelivery(deliveryRequest);
@@ -85,18 +85,18 @@ public class WebhookSender(
private async Task<WebhookDeliveryRequest?> CreateRedeliveryRequest(string deliveryId) private async Task<WebhookDeliveryRequest?> CreateRedeliveryRequest(string deliveryId)
{ {
await using ApplicationDbContext? ctx = dbContextFactory.CreateContext(); await using var ctx = dbContextFactory.CreateContext();
var webhookDelivery = await ctx.WebhookDeliveries.AsNoTracking() var webhookDelivery = await ctx.WebhookDeliveries.AsNoTracking()
.Where(o => o.Id == deliveryId) .Where(o => o.Id == deliveryId)
.Select(o => new { o.Webhook, Delivery = o }) .Select(o => new { o.Webhook, Delivery = o })
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (webhookDelivery is null) if (webhookDelivery is null)
return null; return null;
WebhookDeliveryBlob? oldDeliveryBlob = webhookDelivery.Delivery.GetBlob(); var oldDeliveryBlob = webhookDelivery.Delivery.GetBlob();
WebhookDeliveryData? newDelivery = WebhookExtensions.NewWebhookDelivery(webhookDelivery.Webhook.Id); var newDelivery = WebhookExtensions.NewWebhookDelivery(webhookDelivery.Webhook.Id);
WebhookDeliveryBlob newDeliveryBlob = new(); WebhookDeliveryBlob newDeliveryBlob = new();
newDeliveryBlob.Request = oldDeliveryBlob.Request; newDeliveryBlob.Request = oldDeliveryBlob.Request;
WebhookEvent? webhookEvent = newDeliveryBlob.ReadRequestAs<WebhookEvent>(); var webhookEvent = newDeliveryBlob.ReadRequestAs<WebhookEvent>();
if (webhookEvent.IsPruned()) if (webhookEvent.IsPruned())
return null; return null;
webhookEvent.DeliveryId = newDelivery.Id; webhookEvent.DeliveryId = newDelivery.Id;
@@ -113,13 +113,13 @@ public class WebhookSender(
private WebhookEvent GetTestWebHook(string storeId, string webhookId, string webhookEventType, private WebhookEvent GetTestWebHook(string storeId, string webhookId, string webhookEventType,
WebhookDeliveryData delivery) WebhookDeliveryData delivery)
{ {
IWebhookProvider? webhookProvider = serviceProvider.GetServices<IWebhookProvider>() var webhookProvider = serviceProvider.GetServices<IWebhookProvider>()
.FirstOrDefault(provider => provider.GetSupportedWebhookTypes().ContainsKey(webhookEventType)); .FirstOrDefault(provider => provider.GetSupportedWebhookTypes().ContainsKey(webhookEventType));
if (webhookProvider is null) if (webhookProvider is null)
throw new ArgumentException($"Unknown webhook event type {webhookEventType}", webhookEventType); throw new ArgumentException($"Unknown webhook event type {webhookEventType}", webhookEventType);
WebhookEvent? webhookEvent = webhookProvider.CreateTestEvent(webhookEventType, storeId); var webhookEvent = webhookProvider.CreateTestEvent(webhookEventType, storeId);
if (webhookEvent is null) if (webhookEvent is null)
throw new ArgumentException("Webhook provider does not support tests"); throw new ArgumentException("Webhook provider does not support tests");
@@ -135,8 +135,8 @@ public class WebhookSender(
public async Task<DeliveryResult> TestWebhook(string storeId, string webhookId, string webhookEventType, public async Task<DeliveryResult> TestWebhook(string storeId, string webhookId, string webhookEventType,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
WebhookDeliveryData? delivery = WebhookExtensions.NewWebhookDelivery(webhookId); var delivery = WebhookExtensions.NewWebhookDelivery(webhookId);
WebhookData? webhook = (await StoreRepository.GetWebhooks(storeId)).FirstOrDefault(w => w.Id == webhookId); var webhook = (await StoreRepository.GetWebhooks(storeId)).FirstOrDefault(w => w.Id == webhookId);
WebhookDeliveryRequest deliveryRequest = new( WebhookDeliveryRequest deliveryRequest = new(
webhookId, webhookId,
GetTestWebHook(storeId, webhookId, webhookEventType, delivery), GetTestWebHook(storeId, webhookId, webhookEventType, delivery),
@@ -155,16 +155,16 @@ public class WebhookSender(
{ {
try try
{ {
WebhookBlob? wh = (await StoreRepository.GetWebhook(ctx.WebhookId))?.GetBlob(); var wh = (await StoreRepository.GetWebhook(ctx.WebhookId))?.GetBlob();
if (wh is null || !wh.ShouldDeliver(ctx.WebhookEvent.Type)) if (wh is null || !wh.ShouldDeliver(ctx.WebhookEvent.Type))
return; return;
DeliveryResult result = await SendAndSaveDelivery(ctx, cancellationToken); var result = await SendAndSaveDelivery(ctx, cancellationToken);
if (ctx.WebhookBlob.AutomaticRedelivery && if (ctx.WebhookBlob.AutomaticRedelivery &&
!result.Success && !result.Success &&
result.DeliveryId is not null) result.DeliveryId is not null)
{ {
string? originalDeliveryId = result.DeliveryId; var originalDeliveryId = result.DeliveryId;
foreach (TimeSpan wait in new[] foreach (var wait in new[]
{ {
TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(1), 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) TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)
@@ -194,22 +194,22 @@ public class WebhookSender(
private async Task<DeliveryResult> SendDelivery(WebhookDeliveryRequest ctx, CancellationToken cancellationToken) private async Task<DeliveryResult> SendDelivery(WebhookDeliveryRequest ctx, CancellationToken cancellationToken)
{ {
Uri uri = new(ctx.WebhookBlob.Url, UriKind.Absolute); Uri uri = new(ctx.WebhookBlob.Url, UriKind.Absolute);
HttpClient httpClient = GetClient(uri); var httpClient = GetClient(uri);
using HttpRequestMessage request = new(); using HttpRequestMessage request = new();
request.RequestUri = uri; request.RequestUri = uri;
request.Method = HttpMethod.Post; request.Method = HttpMethod.Post;
byte[] bytes = ToBytes(ctx.WebhookEvent); var bytes = ToBytes(ctx.WebhookEvent);
ByteArrayContent content = new(bytes); ByteArrayContent content = new(bytes);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using HMACSHA256 hmac = new(_utf8.GetBytes(ctx.WebhookBlob.Secret ?? string.Empty)); using HMACSHA256 hmac = new(_utf8.GetBytes(ctx.WebhookBlob.Secret ?? string.Empty));
string? sig = Encoders.Hex.EncodeData(hmac.ComputeHash(bytes)); var sig = Encoders.Hex.EncodeData(hmac.ComputeHash(bytes));
content.Headers.Add("BTCPay-Sig", $"sha256={sig}"); content.Headers.Add("BTCPay-Sig", $"sha256={sig}");
request.Content = content; request.Content = content;
WebhookDeliveryBlob deliveryBlob = ctx.Delivery.GetBlob() ?? new WebhookDeliveryBlob(); var deliveryBlob = ctx.Delivery.GetBlob() ?? new WebhookDeliveryBlob();
deliveryBlob.Request = bytes; deliveryBlob.Request = bytes;
try try
{ {
using HttpResponseMessage response = await httpClient.SendAsync(request, cancellationToken); using var response = await httpClient.SendAsync(request, cancellationToken);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
deliveryBlob.Status = WebhookDeliveryStatus.HttpError; deliveryBlob.Status = WebhookDeliveryStatus.HttpError;
@@ -236,7 +236,7 @@ public class WebhookSender(
private async Task<DeliveryResult> SendAndSaveDelivery(WebhookDeliveryRequest ctx, private async Task<DeliveryResult> SendAndSaveDelivery(WebhookDeliveryRequest ctx,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
DeliveryResult result = await SendDelivery(ctx, cancellationToken); var result = await SendDelivery(ctx, cancellationToken);
await StoreRepository.AddWebhookDelivery(ctx.Delivery); await StoreRepository.AddWebhookDelivery(ctx.Delivery);
return result; return result;
@@ -244,8 +244,8 @@ public class WebhookSender(
private byte[] ToBytes(WebhookEvent webhookEvent) private byte[] ToBytes(WebhookEvent webhookEvent)
{ {
string str = JsonConvert.SerializeObject(webhookEvent, Formatting.Indented, DefaultSerializerSettings); var str = JsonConvert.SerializeObject(webhookEvent, Formatting.Indented, DefaultSerializerSettings);
byte[] bytes = _utf8.GetBytes(str); var bytes = _utf8.GetBytes(str);
return bytes; return bytes;
} }
@@ -291,15 +291,15 @@ public class WebhookSender(
//find all instance of {fieldName*} instead str, then run obj.SelectToken(*) on it //find all instance of {fieldName*} instead str, then run obj.SelectToken(*) on it
while (true) while (true)
{ {
int start = str.IndexOf($"{{{fieldName}", StringComparison.InvariantCultureIgnoreCase); var start = str.IndexOf($"{{{fieldName}", StringComparison.InvariantCultureIgnoreCase);
if (start == -1) if (start == -1)
break; break;
start += fieldName.Length + 1; start += fieldName.Length + 1;
int end = str.IndexOf("}", start, StringComparison.InvariantCultureIgnoreCase); var end = str.IndexOf("}", start, StringComparison.InvariantCultureIgnoreCase);
if (end == -1) if (end == -1)
break; break;
string jsonpath = str.Substring(start, end - start); var jsonpath = str.Substring(start, end - start);
string? result = string.Empty; var result = string.Empty;
try try
{ {
if (string.IsNullOrEmpty(jsonpath)) if (string.IsNullOrEmpty(jsonpath))