2021-06-01 20:49:58 -07:00
|
|
|
#nullable enable
|
2021-07-23 10:05:15 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-10-01 12:30:00 +02:00
|
|
|
using System.Threading.Tasks;
|
2021-06-01 20:49:58 -07:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
|
|
|
using BTCPayServer.Data;
|
2021-10-01 12:30:00 +02:00
|
|
|
using BTCPayServer.Security;
|
2021-06-01 20:49:58 -07:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using StoreData = BTCPayServer.Data.StoreData;
|
|
|
|
|
2022-01-14 13:05:23 +09:00
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
2021-06-01 20:49:58 -07:00
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2022-01-07 12:17:59 +09:00
|
|
|
public class GreenfieldStorePaymentMethodsController : ControllerBase
|
2021-06-01 20:49:58 -07:00
|
|
|
{
|
|
|
|
private StoreData Store => HttpContext.GetStoreData();
|
|
|
|
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
|
2021-10-01 12:30:00 +02:00
|
|
|
private readonly IAuthorizationService _authorizationService;
|
2021-06-01 20:49:58 -07:00
|
|
|
|
2022-01-07 12:17:59 +09:00
|
|
|
public GreenfieldStorePaymentMethodsController(BTCPayNetworkProvider btcPayNetworkProvider, IAuthorizationService authorizationService)
|
2021-06-01 20:49:58 -07:00
|
|
|
{
|
|
|
|
_btcPayNetworkProvider = btcPayNetworkProvider;
|
2021-10-01 12:30:00 +02:00
|
|
|
_authorizationService = authorizationService;
|
2021-06-01 20:49:58 -07:00
|
|
|
}
|
|
|
|
|
2021-09-22 14:31:00 +02:00
|
|
|
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2021-06-01 20:49:58 -07:00
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/payment-methods")]
|
2021-10-01 12:30:00 +02:00
|
|
|
public async Task<ActionResult<Dictionary<string, GenericPaymentMethodData>>> GetStorePaymentMethods(
|
2021-07-27 14:11:47 +02:00
|
|
|
string storeId,
|
2021-07-26 11:12:44 +02:00
|
|
|
[FromQuery] bool? enabled)
|
2021-06-01 20:49:58 -07:00
|
|
|
{
|
|
|
|
var storeBlob = Store.GetStoreBlob();
|
|
|
|
var excludedPaymentMethods = storeBlob.GetExcludedPaymentMethods();
|
2021-10-01 12:30:00 +02:00
|
|
|
var canModifyStore = (await _authorizationService.AuthorizeAsync(User, null,
|
2021-12-31 16:59:02 +09:00
|
|
|
new PolicyRequirement(Policies.CanModifyStoreSettings))).Succeeded;
|
|
|
|
;
|
2021-07-23 10:05:15 +02:00
|
|
|
return Ok(Store.GetSupportedPaymentMethods(_btcPayNetworkProvider)
|
2021-07-26 11:12:44 +02:00
|
|
|
.Where(method =>
|
|
|
|
enabled is null || (enabled is false && excludedPaymentMethods.Match(method.PaymentId)))
|
2021-07-23 10:05:15 +02:00
|
|
|
.ToDictionary(
|
2021-07-26 11:12:44 +02:00
|
|
|
method => method.PaymentId.ToStringNormalized(),
|
|
|
|
method => new GenericPaymentMethodData()
|
|
|
|
{
|
2021-09-25 07:04:34 +02:00
|
|
|
CryptoCode = method.PaymentId.CryptoCode,
|
2021-07-26 11:12:44 +02:00
|
|
|
Enabled = enabled.GetValueOrDefault(!excludedPaymentMethods.Match(method.PaymentId)),
|
2021-10-01 12:30:00 +02:00
|
|
|
Data = method.PaymentId.PaymentType.GetGreenfieldData(method, canModifyStore)
|
2021-07-26 11:12:44 +02:00
|
|
|
}));
|
2021-06-01 20:49:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|