2022-04-24 05:19:34 +02:00
|
|
|
#nullable enable
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2023-04-27 03:59:19 +02:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2022-04-24 05:19:34 +02:00
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
2023-02-21 07:06:34 +01:00
|
|
|
using BTCPayServer.Data;
|
2022-08-17 09:45:51 +02:00
|
|
|
using BTCPayServer.Payments;
|
2022-04-24 05:19:34 +02:00
|
|
|
using BTCPayServer.PayoutProcessors;
|
|
|
|
using BTCPayServer.PayoutProcessors.Lightning;
|
|
|
|
using BTCPayServer.Services.Invoices;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-03-10 15:20:11 +01:00
|
|
|
using Microsoft.AspNetCore.Cors;
|
2022-04-24 05:19:34 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-02-21 07:06:34 +01:00
|
|
|
using PayoutProcessorData = BTCPayServer.Data.PayoutProcessorData;
|
2022-04-24 05:19:34 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-03-10 15:20:11 +01:00
|
|
|
[EnableCors(CorsPolicies.All)]
|
2022-04-24 05:19:34 +02:00
|
|
|
public class GreenfieldStoreAutomatedLightningPayoutProcessorsController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly PayoutProcessorService _payoutProcessorService;
|
|
|
|
private readonly EventAggregator _eventAggregator;
|
|
|
|
|
|
|
|
public GreenfieldStoreAutomatedLightningPayoutProcessorsController(PayoutProcessorService payoutProcessorService,
|
|
|
|
EventAggregator eventAggregator)
|
|
|
|
{
|
|
|
|
_payoutProcessorService = payoutProcessorService;
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-03-10 09:57:33 +01:00
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/payout-processors/LightningAutomatedPayoutSenderFactory")]
|
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/payout-processors/LightningAutomatedPayoutSenderFactory/{paymentMethod}")]
|
2022-04-24 05:19:34 +02:00
|
|
|
public async Task<IActionResult> GetStoreLightningAutomatedPayoutProcessors(
|
|
|
|
string storeId, string? paymentMethod)
|
|
|
|
{
|
2024-04-04 09:31:04 +02:00
|
|
|
var paymentMethodId = !string.IsNullOrEmpty(paymentMethod) ? PaymentMethodId.Parse(paymentMethod) : null;
|
2022-04-24 05:19:34 +02:00
|
|
|
var configured =
|
|
|
|
await _payoutProcessorService.GetProcessors(
|
|
|
|
new PayoutProcessorService.PayoutProcessorQuery()
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
Stores = new[] { storeId },
|
|
|
|
Processors = new[] { LightningAutomatedPayoutSenderFactory.ProcessorName },
|
2024-04-04 09:31:04 +02:00
|
|
|
PaymentMethods = paymentMethodId is null ? null : new[] { paymentMethodId }
|
2022-04-24 05:19:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return Ok(configured.Select(ToModel).ToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static LightningAutomatedPayoutSettings ToModel(PayoutProcessorData data)
|
|
|
|
{
|
2023-07-25 03:50:34 +02:00
|
|
|
var blob = data.HasTypedBlob<LightningAutomatedPayoutBlob>().GetBlob() ?? new LightningAutomatedPayoutBlob();
|
2022-04-24 05:19:34 +02:00
|
|
|
return new LightningAutomatedPayoutSettings()
|
|
|
|
{
|
|
|
|
PaymentMethod = data.PaymentMethod,
|
2023-07-20 15:05:14 +02:00
|
|
|
IntervalSeconds = blob.Interval,
|
|
|
|
CancelPayoutAfterFailures = blob.CancelPayoutAfterFailures,
|
|
|
|
ProcessNewPayoutsInstantly = blob.ProcessNewPayoutsInstantly
|
2022-04-24 05:19:34 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-20 15:05:14 +02:00
|
|
|
private static LightningAutomatedPayoutBlob FromModel(LightningAutomatedPayoutSettings data)
|
2022-04-24 05:19:34 +02:00
|
|
|
{
|
2023-07-20 15:05:14 +02:00
|
|
|
return new LightningAutomatedPayoutBlob() {
|
|
|
|
Interval = data.IntervalSeconds,
|
|
|
|
CancelPayoutAfterFailures = data.CancelPayoutAfterFailures,
|
|
|
|
ProcessNewPayoutsInstantly = data.ProcessNewPayoutsInstantly
|
|
|
|
};
|
2022-04-24 05:19:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-03-10 09:57:33 +01:00
|
|
|
[HttpPut("~/api/v1/stores/{storeId}/payout-processors/LightningAutomatedPayoutSenderFactory/{paymentMethod}")]
|
2022-04-24 05:19:34 +02:00
|
|
|
public async Task<IActionResult> UpdateStoreLightningAutomatedPayoutProcessor(
|
|
|
|
string storeId, string paymentMethod, LightningAutomatedPayoutSettings request)
|
|
|
|
{
|
2023-04-27 03:59:19 +02:00
|
|
|
AutomatedPayoutConstants.ValidateInterval(ModelState, request.IntervalSeconds, nameof(request.IntervalSeconds));
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
return this.CreateValidationError(ModelState);
|
2024-04-04 09:31:04 +02:00
|
|
|
var paymentMethodId = PaymentMethodId.Parse(paymentMethod);
|
2022-04-24 05:19:34 +02:00
|
|
|
var activeProcessor =
|
|
|
|
(await _payoutProcessorService.GetProcessors(
|
|
|
|
new PayoutProcessorService.PayoutProcessorQuery()
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
Stores = new[] { storeId },
|
|
|
|
Processors = new[] { LightningAutomatedPayoutSenderFactory.ProcessorName },
|
2024-04-04 09:31:04 +02:00
|
|
|
PaymentMethods = new[] { paymentMethodId }
|
2022-04-24 05:19:34 +02:00
|
|
|
}))
|
|
|
|
.FirstOrDefault();
|
|
|
|
activeProcessor ??= new PayoutProcessorData();
|
2023-07-20 15:05:14 +02:00
|
|
|
activeProcessor.HasTypedBlob<LightningAutomatedPayoutBlob>().SetBlob(FromModel(request));
|
2022-04-24 05:19:34 +02:00
|
|
|
activeProcessor.StoreId = storeId;
|
2024-04-04 09:31:04 +02:00
|
|
|
activeProcessor.PaymentMethod = paymentMethodId.ToString();
|
2022-04-24 05:19:34 +02:00
|
|
|
activeProcessor.Processor = LightningAutomatedPayoutSenderFactory.ProcessorName;
|
|
|
|
var tcs = new TaskCompletionSource();
|
|
|
|
_eventAggregator.Publish(new PayoutProcessorUpdated()
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
Data = activeProcessor,
|
|
|
|
Id = activeProcessor.Id,
|
|
|
|
Processed = tcs
|
2022-04-24 05:19:34 +02:00
|
|
|
});
|
|
|
|
await tcs.Task;
|
|
|
|
return Ok(ToModel(activeProcessor));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|