btcpayserver/BTCPayServer/Controllers/GreenField/GreenfieldStorePaymentMethodsController.cs

55 lines
2.4 KiB
C#
Raw Normal View History

#nullable enable
using System.Collections.Generic;
using System.Linq;
2021-10-01 12:30:00 +02:00
using System.Threading.Tasks;
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;
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
{
[ApiController]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
2022-01-07 12:17:59 +09:00
public class GreenfieldStorePaymentMethodsController : ControllerBase
{
private StoreData Store => HttpContext.GetStoreData();
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
2021-10-01 12:30:00 +02:00
private readonly IAuthorizationService _authorizationService;
2022-01-07 12:17:59 +09:00
public GreenfieldStorePaymentMethodsController(BTCPayNetworkProvider btcPayNetworkProvider, IAuthorizationService authorizationService)
{
_btcPayNetworkProvider = btcPayNetworkProvider;
2021-10-01 12:30:00 +02:00
_authorizationService = authorizationService;
}
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/payment-methods")]
2021-10-01 12:30:00 +02:00
public async Task<ActionResult<Dictionary<string, GenericPaymentMethodData>>> GetStorePaymentMethods(
string storeId,
[FromQuery] bool? enabled)
{
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;
;
return Ok(Store.GetSupportedPaymentMethods(_btcPayNetworkProvider)
.Where(method =>
enabled is null || (enabled is false && excludedPaymentMethods.Match(method.PaymentId)))
.ToDictionary(
method => method.PaymentId.ToStringNormalized(),
method => new GenericPaymentMethodData()
{
CryptoCode = method.PaymentId.CryptoCode,
Enabled = enabled.GetValueOrDefault(!excludedPaymentMethods.Match(method.PaymentId)),
2021-10-01 12:30:00 +02:00
Data = method.PaymentId.PaymentType.GetGreenfieldData(method, canModifyStore)
}));
}
}
}