2022-04-24 05:19:34 +02:00
|
|
|
#nullable enable
|
2023-04-27 10:59:19 +09:00
|
|
|
using System;
|
2022-04-24 05:19:34 +02:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2023-04-27 10:59:19 +09:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2022-04-24 05:19:34 +02:00
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
2023-02-21 15:06:34 +09: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.OnChain;
|
2024-05-01 10:22:07 +09:00
|
|
|
using BTCPayServer.Payouts;
|
2022-04-24 05:19:34 +02:00
|
|
|
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 15:06:34 +09: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 GreenfieldStoreAutomatedOnChainPayoutProcessorsController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly PayoutProcessorService _payoutProcessorService;
|
|
|
|
private readonly EventAggregator _eventAggregator;
|
|
|
|
|
|
|
|
public GreenfieldStoreAutomatedOnChainPayoutProcessorsController(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/OnChainAutomatedPayoutSenderFactory")]
|
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/payout-processors/OnChainAutomatedPayoutSenderFactory/{paymentMethod}")]
|
2022-04-24 05:19:34 +02:00
|
|
|
public async Task<IActionResult> GetStoreOnChainAutomatedPayoutProcessors(
|
|
|
|
string storeId, string? paymentMethod)
|
|
|
|
{
|
2024-05-01 10:22:07 +09:00
|
|
|
var paymentMethodId = !string.IsNullOrEmpty(paymentMethod) ? PayoutMethodId.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[] { OnChainAutomatedPayoutSenderFactory.ProcessorName },
|
2024-05-01 10:22:07 +09:00
|
|
|
PayoutMethodIds = paymentMethodId is null ? null : new[] { paymentMethodId }
|
2022-04-24 05:19:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return Ok(configured.Select(ToModel).ToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static OnChainAutomatedPayoutSettings ToModel(PayoutProcessorData data)
|
|
|
|
{
|
2022-08-23 12:35:20 +02:00
|
|
|
var blob = BaseAutomatedPayoutProcessor<OnChainAutomatedPayoutBlob>.GetBlob(data);
|
2022-04-24 05:19:34 +02:00
|
|
|
return new OnChainAutomatedPayoutSettings()
|
|
|
|
{
|
2022-08-23 12:35:20 +02:00
|
|
|
FeeBlockTarget = blob.FeeTargetBlock,
|
2022-04-24 05:19:34 +02:00
|
|
|
PaymentMethod = data.PaymentMethod,
|
2023-07-20 15:05:14 +02:00
|
|
|
IntervalSeconds = blob.Interval,
|
|
|
|
Threshold = blob.Threshold,
|
|
|
|
ProcessNewPayoutsInstantly = blob.ProcessNewPayoutsInstantly
|
2022-04-24 05:19:34 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:35:20 +02:00
|
|
|
private static OnChainAutomatedPayoutBlob FromModel(OnChainAutomatedPayoutSettings data)
|
2022-04-24 05:19:34 +02:00
|
|
|
{
|
2022-08-23 12:35:20 +02:00
|
|
|
return new OnChainAutomatedPayoutBlob()
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
FeeTargetBlock = data.FeeBlockTarget ?? 1,
|
2023-07-20 15:05:14 +02:00
|
|
|
Interval = data.IntervalSeconds,
|
|
|
|
Threshold = data.Threshold,
|
|
|
|
ProcessNewPayoutsInstantly = data.ProcessNewPayoutsInstantly
|
2022-08-23 12:35:20 +02:00
|
|
|
};
|
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/OnChainAutomatedPayoutSenderFactory/{paymentMethod}")]
|
2022-04-24 05:19:34 +02:00
|
|
|
public async Task<IActionResult> UpdateStoreOnchainAutomatedPayoutProcessor(
|
|
|
|
string storeId, string paymentMethod, OnChainAutomatedPayoutSettings request)
|
|
|
|
{
|
2023-04-27 10:59:19 +09:00
|
|
|
AutomatedPayoutConstants.ValidateInterval(ModelState, request.IntervalSeconds, nameof(request.IntervalSeconds));
|
|
|
|
if (request.FeeBlockTarget is int t && (t < 1 || t > 1000))
|
|
|
|
ModelState.AddModelError(nameof(request.FeeBlockTarget), "The feeBlockTarget should be between 1 and 1000");
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
return this.CreateValidationError(ModelState);
|
2024-05-01 10:22:07 +09:00
|
|
|
var payoutMethodId = PayoutMethodId.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[] { OnChainAutomatedPayoutSenderFactory.ProcessorName },
|
2024-05-01 10:22:07 +09:00
|
|
|
PayoutMethodIds = new[] { payoutMethodId }
|
2022-04-24 05:19:34 +02:00
|
|
|
}))
|
|
|
|
.FirstOrDefault();
|
|
|
|
activeProcessor ??= new PayoutProcessorData();
|
2023-02-21 15:06:34 +09:00
|
|
|
activeProcessor.HasTypedBlob<OnChainAutomatedPayoutBlob>().SetBlob(FromModel(request));
|
2022-04-24 05:19:34 +02:00
|
|
|
activeProcessor.StoreId = storeId;
|
2024-05-01 10:22:07 +09:00
|
|
|
activeProcessor.PaymentMethod = payoutMethodId.ToString();
|
2022-04-24 05:19:34 +02:00
|
|
|
activeProcessor.Processor = OnChainAutomatedPayoutSenderFactory.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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|