mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-01-19 13:43:46 +01:00
51690b47a3
* Automated Transfer processors This PR introduces a few things: * Payouts can now be directly nested under a store instead of through a pull payment. * The Wallet Send screen now has an option to "schedule" instead of simply creating a transaction. When you click on schedule, all transaction destinations are converted into approved payouts. Any options relating to fees or coin selection are discarded. * There is a new concept introduced, called "Transfer Processors". Transfer Processors are services for stores that process payouts that are awaiting payment. Each processor specifies which payment methods it can handle. BTCPay Server will have some forms of transfer processors baked in but it has been designed to allow the Plugin System to provide additional processors. * The initial transfer processors provided are "automated processors", for on chain and lightning payment methods. They can be configured to process payouts every X amount of minutes. For on-chain, this means payments are batched into one transaction, resulting in more efficient and cheaper fees for processing. * * fix build * extract * remove magic string stuff * fix error message when scheduling * Paginate migration * add payout count to payment method tab * remove unused var * add protip * optimzie payout migration dramatically * Remove useless double condition * Fix bunch of warnings * Remove warning * Remove warnigns * Rename to Payout processors * fix typo Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com>
166 lines
5.3 KiB
C#
166 lines
5.3 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Data;
|
|
using BTCPayServer.Data.Data;
|
|
using BTCPayServer.HostedServices;
|
|
using BTCPayServer.Logging;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace BTCPayServer.PayoutProcessors;
|
|
|
|
public class PayoutProcessorUpdated
|
|
{
|
|
public string Id { get; set; }
|
|
public PayoutProcessorData Data { get; set; }
|
|
|
|
public TaskCompletionSource Processed { get; set; }
|
|
}
|
|
|
|
public class PayoutProcessorService : EventHostedServiceBase
|
|
{
|
|
private readonly ApplicationDbContextFactory _applicationDbContextFactory;
|
|
private readonly IEnumerable<IPayoutProcessorFactory> _payoutProcessorFactories;
|
|
|
|
|
|
private ConcurrentDictionary<string, IHostedService> Services { get; set; } = new();
|
|
public PayoutProcessorService(
|
|
ApplicationDbContextFactory applicationDbContextFactory,
|
|
EventAggregator eventAggregator,
|
|
Logs logs,
|
|
IEnumerable<IPayoutProcessorFactory> payoutProcessorFactories) : base(eventAggregator, logs)
|
|
{
|
|
_applicationDbContextFactory = applicationDbContextFactory;
|
|
_payoutProcessorFactories = payoutProcessorFactories;
|
|
}
|
|
|
|
public class PayoutProcessorQuery
|
|
{
|
|
public string[] Stores { get; set; }
|
|
public string[] Processors { get; set; }
|
|
public string[] PaymentMethods { get; set; }
|
|
}
|
|
|
|
public async Task<List<PayoutProcessorData>> GetProcessors(PayoutProcessorQuery query)
|
|
{
|
|
|
|
await using var context = _applicationDbContextFactory.CreateContext();
|
|
var queryable = context.PayoutProcessors.AsQueryable();
|
|
if (query.Processors is not null)
|
|
{
|
|
queryable = queryable.Where(data => query.Processors.Contains(data.Processor));
|
|
}
|
|
if (query.Stores is not null)
|
|
{
|
|
queryable = queryable.Where(data => query.Stores.Contains(data.StoreId));
|
|
}
|
|
if (query.PaymentMethods is not null)
|
|
{
|
|
queryable = queryable.Where(data => query.PaymentMethods.Contains(data.PaymentMethod));
|
|
}
|
|
|
|
return await queryable.ToListAsync();
|
|
}
|
|
|
|
private async Task RemoveProcessor(string id)
|
|
{
|
|
await using var context = _applicationDbContextFactory.CreateContext();
|
|
var item = await context.FindAsync<PayoutProcessorData>(id);
|
|
if (item is not null)
|
|
context.Remove(item);
|
|
await context.SaveChangesAsync();
|
|
await StopProcessor(id, CancellationToken.None);
|
|
}
|
|
|
|
private async Task AddOrUpdateProcessor(PayoutProcessorData data)
|
|
{
|
|
|
|
await using var context = _applicationDbContextFactory.CreateContext();
|
|
if (string.IsNullOrEmpty(data.Id))
|
|
{
|
|
await context.AddAsync(data);
|
|
}
|
|
else
|
|
{
|
|
context.Update(data);
|
|
}
|
|
await context.SaveChangesAsync();
|
|
await StartOrUpdateProcessor(data, CancellationToken.None);
|
|
}
|
|
|
|
protected override void SubscribeToEvents()
|
|
{
|
|
base.SubscribeToEvents();
|
|
Subscribe<PayoutProcessorUpdated>();
|
|
}
|
|
|
|
public override async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
await base.StartAsync(cancellationToken);
|
|
var activeProcessors = await GetProcessors(new PayoutProcessorQuery());
|
|
var tasks = activeProcessors.Select(data => StartOrUpdateProcessor(data, cancellationToken));
|
|
await Task.WhenAll(tasks);
|
|
}
|
|
|
|
private async Task StopProcessor(string id, CancellationToken cancellationToken)
|
|
{
|
|
if (Services.Remove(id, out var currentService))
|
|
{
|
|
await currentService.StopAsync(cancellationToken);
|
|
}
|
|
|
|
}
|
|
|
|
private async Task StartOrUpdateProcessor(PayoutProcessorData data, CancellationToken cancellationToken)
|
|
{
|
|
var matchedProcessor = _payoutProcessorFactories.FirstOrDefault(factory =>
|
|
factory.Processor == data.Processor);
|
|
|
|
if (matchedProcessor is not null)
|
|
{
|
|
await StopProcessor(data.Id, cancellationToken);
|
|
var processor = await matchedProcessor.ConstructProcessor(data);
|
|
await processor.StartAsync(cancellationToken);
|
|
Services.TryAdd(data.Id, processor);
|
|
}
|
|
|
|
}
|
|
|
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
await base.StopAsync(cancellationToken);
|
|
await StopAllService(cancellationToken);
|
|
}
|
|
|
|
private async Task StopAllService(CancellationToken cancellationToken)
|
|
{
|
|
foreach (KeyValuePair<string,IHostedService> service in Services)
|
|
{
|
|
await service.Value.StopAsync(cancellationToken);
|
|
}
|
|
Services.Clear();
|
|
}
|
|
|
|
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
|
|
{
|
|
await base.ProcessEvent(evt, cancellationToken);
|
|
|
|
if (evt is PayoutProcessorUpdated processorUpdated)
|
|
{
|
|
if (processorUpdated.Data is null)
|
|
{
|
|
await RemoveProcessor(processorUpdated.Id);
|
|
}
|
|
else
|
|
{
|
|
await AddOrUpdateProcessor(processorUpdated.Data);
|
|
}
|
|
|
|
processorUpdated.Processed?.SetResult();
|
|
}
|
|
}
|
|
}
|