Skip to content

Commit

Permalink
Support better templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Nov 22, 2023
1 parent 06e8bd3 commit 6827cbc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Globalization;
using System;
using System.Globalization;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers;
using BTCPayServer.Data;
using BTCPayServer.Services.Invoices;
using Newtonsoft.Json.Linq;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;

namespace BTCPayServer.HostedServices.Webhooks;
Expand Down Expand Up @@ -35,12 +37,17 @@ public class InvoiceWebhookDeliveryRequest : WebhookSender.WebhookDeliveryReques

private string Interpolate(string str)
{
return str.Replace("{Invoice.Id}", Invoice.Id)
var res = str.Replace("{Invoice.Id}", Invoice.Id)
.Replace("{Invoice.StoreId}", Invoice.StoreId)
.Replace("{Invoice.Price}", Invoice.Price.ToString(CultureInfo.InvariantCulture))
.Replace("{Invoice.Currency}", Invoice.Currency)
.Replace("{Invoice.Status}", Invoice.Status.ToString())
.Replace("{Invoice.AdditionalStatus}", Invoice.ExceptionStatus.ToString())
.Replace("{Invoice.OrderId}", Invoice.Metadata.OrderId);


res = InterpolateJsonField(str, "Invoice.Metadata", Invoice.Metadata.ToJObject());
return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ public class PaymentRequestWebhookDeliveryRequest : WebhookSender.WebhookDeliver

private string Interpolate(string str, PaymentRequestBaseData blob)
{
return str.Replace("{PaymentRequest.Id}", _evt.Data.Id)
var res= str.Replace("{PaymentRequest.Id}", _evt.Data.Id)
.Replace("{PaymentRequest.Price}", blob.Amount.ToString(CultureInfo.InvariantCulture))
.Replace("{PaymentRequest.Currency}", blob.Currency)
.Replace("{PaymentRequest.Title}", blob.Title)
.Replace("{PaymentRequest.Description}", blob.Description)
.Replace("{PaymentRequest.Status}", _evt.Data.Status.ToString());

res= InterpolateJsonField(res, "PaymentRequest.FormResponse", blob.FormResponse);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers;
using BTCPayServer.Data;
using BTCPayServer.Services;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;

namespace BTCPayServer.HostedServices.Webhooks;

public class PayoutWebhookDeliveryRequest : WebhookSender.WebhookDeliveryRequest
{
private readonly PayoutEvent _evt;
private readonly BTCPayNetworkJsonSerializerSettings _btcPayNetworkJsonSerializerSettings;

public PayoutWebhookDeliveryRequest(PayoutEvent evt, string webhookId, WebhookEvent webhookEvent,
WebhookDeliveryData delivery, WebhookBlob webhookBlob) : base(webhookId, webhookEvent, delivery, webhookBlob)
WebhookDeliveryData delivery, WebhookBlob webhookBlob, BTCPayNetworkJsonSerializerSettings btcPayNetworkJsonSerializerSettings) : base(webhookId, webhookEvent, delivery, webhookBlob)
{
_evt = evt;
_btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings;
}

public override Task<SendEmailRequest?> Interpolate(SendEmailRequest req,
Expand All @@ -28,9 +31,14 @@ public class PayoutWebhookDeliveryRequest : WebhookSender.WebhookDeliveryRequest

private string Interpolate(string str)
{
return str.Replace("{Payout.Id}", _evt.Payout.Id)
var res= str.Replace("{Payout.Id}", _evt.Payout.Id)
.Replace("{Payout.PullPaymentId}", _evt.Payout.PullPaymentDataId)
.Replace("{Payout.Destination}", _evt.Payout.Destination)
.Replace("{Payout.State}", _evt.Payout.State.ToString());

var blob = _evt.Payout.GetBlob(_btcPayNetworkJsonSerializerSettings);

res = InterpolateJsonField(str, "Payout.Metadata", blob.Metadata);
return res;
}
}
7 changes: 5 additions & 2 deletions BTCPayServer/HostedServices/Webhooks/PayoutWebhookProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Services;
using Microsoft.Extensions.Logging;

namespace BTCPayServer.HostedServices.Webhooks;

public class PayoutWebhookProvider : EventHostedServiceBase, IWebhookProvider
{
private readonly WebhookSender _webhookSender;
private BTCPayNetworkJsonSerializerSettings _btcPayNetworkJsonSerializerSettings;

public PayoutWebhookProvider(WebhookSender webhookSender, EventAggregator eventAggregator, ILogger<PayoutWebhookProvider> logger) : base(
public PayoutWebhookProvider(WebhookSender webhookSender, EventAggregator eventAggregator, ILogger<PayoutWebhookProvider> logger, BTCPayNetworkJsonSerializerSettings btcPayNetworkJsonSerializerSettings) : base(
eventAggregator, logger)
{
_webhookSender = webhookSender;
_btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings;
}

protected override void SubscribeToEvents()
Expand Down Expand Up @@ -48,7 +51,7 @@ protected override async Task ProcessEvent(object evt, CancellationToken cancell
webhookEvent.OriginalDeliveryId = delivery.Id;
webhookEvent.IsRedelivery = false;
webhookEvent.Timestamp = delivery.Timestamp;
var context = new PayoutWebhookDeliveryRequest(payoutEvent,webhook.Id, webhookEvent, delivery, webhookBlob);
var context = new PayoutWebhookDeliveryRequest(payoutEvent,webhook.Id, webhookEvent, delivery, webhookBlob, _btcPayNetworkJsonSerializerSettings);
_webhookSender.EnqueueDelivery(context);
}
}
Expand Down
30 changes: 30 additions & 0 deletions BTCPayServer/HostedServices/Webhooks/WebhookSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.Logging;
using NBitcoin.DataEncoders;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace BTCPayServer.HostedServices.Webhooks
{
Expand Down Expand Up @@ -93,6 +94,35 @@ public class WebhookDeliveryRequest
{
return Task.FromResult(req)!;
}

protected static string InterpolateJsonField(string str, string fieldName, JObject obj)
{
fieldName += ".";
//find all instance of {fieldName*} instead str, then run obj.SelectToken(*) on it
while (true)
{

var start = str.IndexOf($"{{{fieldName}", StringComparison.InvariantCultureIgnoreCase)+ fieldName.Length + 2;
if(start == -1)
break;
var end = str.IndexOf("}", start, StringComparison.InvariantCultureIgnoreCase);
if(end == -1)
break;
var jsonpath = str.Substring(start, end - start);
string result = string.Empty;
try
{
var resultToken = obj.SelectToken(jsonpath);
result = resultToken?.ToString();
}
catch (Exception e)
{
}
str = str.Replace($"{{{fieldName}{jsonpath}}}", result);
}

return str;
}
}


Expand Down
5 changes: 5 additions & 0 deletions BTCPayServer/Views/UIStores/StoreEmails.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<code>{Invoice.Status}</code>,
<code>{Invoice.AdditionalStatus}</code>,
<code>{Invoice.OrderId}</code>
<code>{Invoice.Metadata}*</code>
</td>
</tr>
<tr>
Expand All @@ -103,6 +104,7 @@
<code>{PaymentRequest.Title}</code>,
<code>{PaymentRequest.Description}</code>,
<code>{PaymentRequest.Status}</code>
<code>{PaymentRequest.FormResponse}*</code>
</td>
</tr>
<tr>
Expand All @@ -112,8 +114,11 @@
<code>{Payout.PullPaymentId}</code>,
<code>{Payout.Destination}</code>,
<code>{Payout.State}</code>
<code>{Payout.Metadata}*</code>
</td>
</tr>
<tr><td colspan="2">* These fields are JSON objects. You can access properties within them using <a href="https://www.newtonsoft.com/json/help/html/SelectToken.htm#SelectTokenJSONPath" target="_blank">this syntax</a>. One example is <code>{Invoice.Metadata.itemCode}</code>
</td></tr>
</table>
</div>
</div>
Expand Down

0 comments on commit 6827cbc

Please sign in to comment.