Allow plugins to do something before and after automatic payouts (#4224)

This commit is contained in:
Wouter Samaey 2023-03-17 13:50:37 +01:00 committed by GitHub
parent eff6be9643
commit e02abb509f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 5 deletions

View file

@ -0,0 +1,19 @@
using System.Collections.Generic;
using BTCPayServer.Data;
using BTCPayServer.Payments;
namespace BTCPayServer.PayoutProcessors;
public class AfterPayoutFilterData
{
private readonly StoreData _store;
private readonly ISupportedPaymentMethod _paymentMethod;
private readonly List<PayoutData> _payoutDatas;
public AfterPayoutFilterData(StoreData store, ISupportedPaymentMethod paymentMethod, List<PayoutData> payoutDatas)
{
_store = store;
_paymentMethod = paymentMethod;
_payoutDatas = payoutDatas;
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Client.Models; using BTCPayServer.Client.Models;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.HostedServices; using BTCPayServer.HostedServices;
@ -23,6 +24,7 @@ public abstract class BaseAutomatedPayoutProcessor<T> : BaseAsyncService where T
private readonly PullPaymentHostedService _pullPaymentHostedService; private readonly PullPaymentHostedService _pullPaymentHostedService;
protected readonly BTCPayNetworkProvider _btcPayNetworkProvider; protected readonly BTCPayNetworkProvider _btcPayNetworkProvider;
protected readonly PaymentMethodId PaymentMethodId; protected readonly PaymentMethodId PaymentMethodId;
private readonly IPluginHookService _pluginHookService;
protected BaseAutomatedPayoutProcessor( protected BaseAutomatedPayoutProcessor(
ILoggerFactory logger, ILoggerFactory logger,
@ -30,7 +32,8 @@ public abstract class BaseAutomatedPayoutProcessor<T> : BaseAsyncService where T
PayoutProcessorData payoutProcesserSettings, PayoutProcessorData payoutProcesserSettings,
ApplicationDbContextFactory applicationDbContextFactory, ApplicationDbContextFactory applicationDbContextFactory,
PullPaymentHostedService pullPaymentHostedService, PullPaymentHostedService pullPaymentHostedService,
BTCPayNetworkProvider btcPayNetworkProvider) : base(logger.CreateLogger($"{payoutProcesserSettings.Processor}:{payoutProcesserSettings.StoreId}:{payoutProcesserSettings.PaymentMethod}")) BTCPayNetworkProvider btcPayNetworkProvider,
IPluginHookService pluginHookService) : base(logger.CreateLogger($"{payoutProcesserSettings.Processor}:{payoutProcesserSettings.StoreId}:{payoutProcesserSettings.PaymentMethod}"))
{ {
_storeRepository = storeRepository; _storeRepository = storeRepository;
_PayoutProcesserSettings = payoutProcesserSettings; _PayoutProcesserSettings = payoutProcesserSettings;
@ -38,6 +41,7 @@ public abstract class BaseAutomatedPayoutProcessor<T> : BaseAsyncService where T
_applicationDbContextFactory = applicationDbContextFactory; _applicationDbContextFactory = applicationDbContextFactory;
_pullPaymentHostedService = pullPaymentHostedService; _pullPaymentHostedService = pullPaymentHostedService;
_btcPayNetworkProvider = btcPayNetworkProvider; _btcPayNetworkProvider = btcPayNetworkProvider;
_pluginHookService = pluginHookService;
this.NoLogsOnExit = true; this.NoLogsOnExit = true;
} }
@ -59,6 +63,10 @@ public abstract class BaseAutomatedPayoutProcessor<T> : BaseAsyncService where T
if (paymentMethod is not null) if (paymentMethod is not null)
{ {
// Allow plugins to do something before the automatic payouts are executed
await _pluginHookService.ApplyFilter("before-automated-payout-processing",
new BeforePayoutFilterData(store, paymentMethod));
await using var context = _applicationDbContextFactory.CreateContext(); await using var context = _applicationDbContextFactory.CreateContext();
var payouts = await PullPaymentHostedService.GetPayouts( var payouts = await PullPaymentHostedService.GetPayouts(
new PullPaymentHostedService.PayoutQuery() new PullPaymentHostedService.PayoutQuery()
@ -72,6 +80,10 @@ public abstract class BaseAutomatedPayoutProcessor<T> : BaseAsyncService where T
Logs.PayServer.LogInformation($"{payouts.Count} found to process. Starting (and after will sleep for {blob.Interval})"); Logs.PayServer.LogInformation($"{payouts.Count} found to process. Starting (and after will sleep for {blob.Interval})");
await Process(paymentMethod, payouts); await Process(paymentMethod, payouts);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
// Allow plugins do to something after automatic payout processing
await _pluginHookService.ApplyFilter("after-automated-payout-processing",
new AfterPayoutFilterData(store, paymentMethod, payouts));
} }
} }
await Task.Delay(blob.Interval, CancellationToken); await Task.Delay(blob.Interval, CancellationToken);

View file

@ -0,0 +1,16 @@
using BTCPayServer.Data;
using BTCPayServer.Payments;
namespace BTCPayServer.PayoutProcessors;
public class BeforePayoutFilterData
{
private readonly StoreData _store;
private readonly ISupportedPaymentMethod _paymentMethod;
public BeforePayoutFilterData(StoreData store, ISupportedPaymentMethod paymentMethod)
{
_store = store;
_paymentMethod = paymentMethod;
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Client.Models; using BTCPayServer.Client.Models;
using BTCPayServer.Configuration; using BTCPayServer.Configuration;
using BTCPayServer.Data; using BTCPayServer.Data;
@ -37,9 +38,10 @@ public class LightningAutomatedPayoutProcessor : BaseAutomatedPayoutProcessor<Au
UserService userService, UserService userService,
ILoggerFactory logger, IOptions<LightningNetworkOptions> options, ILoggerFactory logger, IOptions<LightningNetworkOptions> options,
StoreRepository storeRepository, PayoutProcessorData payoutProcesserSettings, StoreRepository storeRepository, PayoutProcessorData payoutProcesserSettings,
ApplicationDbContextFactory applicationDbContextFactory, PullPaymentHostedService pullPaymentHostedService, BTCPayNetworkProvider btcPayNetworkProvider) : ApplicationDbContextFactory applicationDbContextFactory, PullPaymentHostedService pullPaymentHostedService, BTCPayNetworkProvider btcPayNetworkProvider,
IPluginHookService pluginHookService) :
base(logger, storeRepository, payoutProcesserSettings, applicationDbContextFactory, pullPaymentHostedService, base(logger, storeRepository, payoutProcesserSettings, applicationDbContextFactory, pullPaymentHostedService,
btcPayNetworkProvider) btcPayNetworkProvider, pluginHookService)
{ {
_btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings; _btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings;
_lightningClientFactoryService = lightningClientFactoryService; _lightningClientFactoryService = lightningClientFactoryService;

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Client.Models; using BTCPayServer.Client.Models;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Events; using BTCPayServer.Events;
@ -41,9 +42,10 @@ namespace BTCPayServer.PayoutProcessors.OnChain
StoreRepository storeRepository, StoreRepository storeRepository,
PayoutProcessorData payoutProcesserSettings, PayoutProcessorData payoutProcesserSettings,
PullPaymentHostedService pullPaymentHostedService, PullPaymentHostedService pullPaymentHostedService,
BTCPayNetworkProvider btcPayNetworkProvider) : BTCPayNetworkProvider btcPayNetworkProvider,
IPluginHookService pluginHookService) :
base(logger, storeRepository, payoutProcesserSettings, applicationDbContextFactory, pullPaymentHostedService, base(logger, storeRepository, payoutProcesserSettings, applicationDbContextFactory, pullPaymentHostedService,
btcPayNetworkProvider) btcPayNetworkProvider, pluginHookService)
{ {
_explorerClientProvider = explorerClientProvider; _explorerClientProvider = explorerClientProvider;
_btcPayWalletProvider = btcPayWalletProvider; _btcPayWalletProvider = btcPayWalletProvider;