mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 06:24:24 +01:00
FGreenField: Add DefaultPaymentMethod to Stores API (#2171)
This commit is contained in:
@@ -41,7 +41,8 @@ namespace BTCPayServer.Client.Models
|
|||||||
public bool ShowRecommendedFee { get; set; } = true;
|
public bool ShowRecommendedFee { get; set; } = true;
|
||||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||||
public int RecommendedFeeBlockTarget { get; set; } = 1;
|
public int RecommendedFeeBlockTarget { get; set; } = 1;
|
||||||
|
|
||||||
|
public string DefaultPaymentMethod { get; set; }
|
||||||
|
|
||||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||||
public string DefaultLang { get; set; } = "en";
|
public string DefaultLang { get; set; } = "en";
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using BTCPayServer.Abstractions.Constants;
|
|||||||
using BTCPayServer.Client;
|
using BTCPayServer.Client;
|
||||||
using BTCPayServer.Client.Models;
|
using BTCPayServer.Client.Models;
|
||||||
using BTCPayServer.Data;
|
using BTCPayServer.Data;
|
||||||
|
using BTCPayServer.Payments;
|
||||||
using BTCPayServer.Security;
|
using BTCPayServer.Security;
|
||||||
using BTCPayServer.Services.Stores;
|
using BTCPayServer.Services.Stores;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@@ -22,11 +23,13 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
{
|
{
|
||||||
private readonly StoreRepository _storeRepository;
|
private readonly StoreRepository _storeRepository;
|
||||||
private readonly UserManager<ApplicationUser> _userManager;
|
private readonly UserManager<ApplicationUser> _userManager;
|
||||||
|
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
|
||||||
|
|
||||||
public GreenFieldStoresController(StoreRepository storeRepository, UserManager<ApplicationUser> userManager)
|
public GreenFieldStoresController(StoreRepository storeRepository, UserManager<ApplicationUser> userManager, BTCPayNetworkProvider btcPayNetworkProvider)
|
||||||
{
|
{
|
||||||
_storeRepository = storeRepository;
|
_storeRepository = storeRepository;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
|
_btcPayNetworkProvider = btcPayNetworkProvider;
|
||||||
}
|
}
|
||||||
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||||
[HttpGet("~/api/v1/stores")]
|
[HttpGet("~/api/v1/stores")]
|
||||||
@@ -78,7 +81,9 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
}
|
}
|
||||||
|
|
||||||
var store = new Data.StoreData();
|
var store = new Data.StoreData();
|
||||||
ToModel(request, store);
|
|
||||||
|
PaymentMethodId.TryParse(request.DefaultPaymentMethod, out var defaultPaymnetMethodId);
|
||||||
|
ToModel(request, store, defaultPaymnetMethodId);
|
||||||
await _storeRepository.CreateStore(_userManager.GetUserId(User), store);
|
await _storeRepository.CreateStore(_userManager.GetUserId(User), store);
|
||||||
return Ok(FromModel(store));
|
return Ok(FromModel(store));
|
||||||
}
|
}
|
||||||
@@ -98,12 +103,13 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
return validationResult;
|
return validationResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
ToModel(request, store);
|
PaymentMethodId.TryParse(request.DefaultPaymentMethod, out var defaultPaymnetMethodId);
|
||||||
|
ToModel(request, store, defaultPaymnetMethodId);
|
||||||
await _storeRepository.UpdateStore(store);
|
await _storeRepository.UpdateStore(store);
|
||||||
return Ok(FromModel(store));
|
return Ok(FromModel(store));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Client.Models.StoreData FromModel(Data.StoreData data)
|
private Client.Models.StoreData FromModel(Data.StoreData data)
|
||||||
{
|
{
|
||||||
var storeBlob = data.GetStoreBlob();
|
var storeBlob = data.GetStoreBlob();
|
||||||
return new Client.Models.StoreData()
|
return new Client.Models.StoreData()
|
||||||
@@ -112,13 +118,13 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
Name = data.StoreName,
|
Name = data.StoreName,
|
||||||
Website = data.StoreWebsite,
|
Website = data.StoreWebsite,
|
||||||
SpeedPolicy = data.SpeedPolicy,
|
SpeedPolicy = data.SpeedPolicy,
|
||||||
//we do not include the default payment method in this model and instead opt to set it in the stores/storeid/payment-methods endpoints
|
DefaultPaymentMethod = data.GetDefaultPaymentId(_btcPayNetworkProvider)?.ToStringNormalized(),
|
||||||
//blob
|
//blob
|
||||||
//we do not include DefaultCurrencyPairs,Spread, PreferredExchange, RateScripting, RateScript in this model and instead opt to set it in stores/storeid/rates endpoints
|
//we do not include DefaultCurrencyPairs,Spread, PreferredExchange, RateScripting, RateScript in this model and instead opt to set it in stores/storeid/rates endpoints
|
||||||
//we do not include CoinSwitchSettings in this model and instead opt to set it in stores/storeid/coinswitch endpoints
|
//we do not include CoinSwitchSettings in this model and instead opt to set it in stores/storeid/coinswitch endpoints
|
||||||
//we do not include ExcludedPaymentMethods in this model and instead opt to set it in stores/storeid/payment-methods endpoints
|
//we do not include ExcludedPaymentMethods in this model and instead opt to set it in stores/storeid/payment-methods endpoints
|
||||||
//we do not include EmailSettings in this model and instead opt to set it in stores/storeid/email endpoints
|
//we do not include EmailSettings in this model and instead opt to set it in stores/storeid/email endpoints
|
||||||
//we do not include OnChainMinValue and LightningMaxValue because moving the CurrencyValueJsonConverter to the Client csproj is hard and requires a refactor (#1571 & #1572)
|
//we do not include PaymentMethodCriteria because moving the CurrencyValueJsonConverter to the Client csproj is hard and requires a refactor (#1571 & #1572)
|
||||||
NetworkFeeMode = storeBlob.NetworkFeeMode,
|
NetworkFeeMode = storeBlob.NetworkFeeMode,
|
||||||
RequiresRefundEmail = storeBlob.RequiresRefundEmail,
|
RequiresRefundEmail = storeBlob.RequiresRefundEmail,
|
||||||
LightningAmountInSatoshi = storeBlob.LightningAmountInSatoshi,
|
LightningAmountInSatoshi = storeBlob.LightningAmountInSatoshi,
|
||||||
@@ -140,7 +146,7 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ToModel(StoreBaseData restModel, Data.StoreData model)
|
private static void ToModel(StoreBaseData restModel, Data.StoreData model, PaymentMethodId defaultPaymentMethod)
|
||||||
{
|
{
|
||||||
var blob = model.GetStoreBlob();
|
var blob = model.GetStoreBlob();
|
||||||
|
|
||||||
@@ -148,6 +154,7 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
model.StoreName = restModel.Name;
|
model.StoreName = restModel.Name;
|
||||||
model.StoreWebsite = restModel.Website;
|
model.StoreWebsite = restModel.Website;
|
||||||
model.SpeedPolicy = restModel.SpeedPolicy;
|
model.SpeedPolicy = restModel.SpeedPolicy;
|
||||||
|
model.SetDefaultPaymentId(defaultPaymentMethod);
|
||||||
//we do not include the default payment method in this model and instead opt to set it in the stores/storeid/payment-methods endpoints
|
//we do not include the default payment method in this model and instead opt to set it in the stores/storeid/payment-methods endpoints
|
||||||
//blob
|
//blob
|
||||||
//we do not include DefaultCurrencyPairs;Spread; PreferredExchange; RateScripting; RateScript in this model and instead opt to set it in stores/storeid/rates endpoints
|
//we do not include DefaultCurrencyPairs;Spread; PreferredExchange; RateScripting; RateScript in this model and instead opt to set it in stores/storeid/rates endpoints
|
||||||
@@ -183,6 +190,12 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(request.DefaultPaymentMethod) &&
|
||||||
|
!PaymentMethodId.TryParse(request.DefaultPaymentMethod, out var defaultPaymnetMethodId))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError(nameof(request.Name), "DefaultPaymentMethod is invalid");
|
||||||
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(request.Name))
|
if (string.IsNullOrEmpty(request.Name))
|
||||||
ModelState.AddModelError(nameof(request.Name), "Name is missing");
|
ModelState.AddModelError(nameof(request.Name), "Name is missing");
|
||||||
else if (request.Name.Length < 1 || request.Name.Length > 50)
|
else if (request.Name.Length < 1 || request.Name.Length > 50)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ namespace BTCPayServer.Data
|
|||||||
|
|
||||||
public static void SetDefaultPaymentId(this StoreData storeData, PaymentMethodId defaultPaymentId)
|
public static void SetDefaultPaymentId(this StoreData storeData, PaymentMethodId defaultPaymentId)
|
||||||
{
|
{
|
||||||
storeData.DefaultCrypto = defaultPaymentId.ToString();
|
storeData.DefaultCrypto = defaultPaymentId?.ToString();
|
||||||
}
|
}
|
||||||
#pragma warning restore CS0618
|
#pragma warning restore CS0618
|
||||||
|
|
||||||
|
|||||||
@@ -382,6 +382,11 @@
|
|||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false,
|
"default": false,
|
||||||
"description": "If true, payjoin will be proposed in the checkout page if possible. ([More information](https://docs.btcpayserver.org/Payjoin/))"
|
"description": "If true, payjoin will be proposed in the checkout page if possible. ([More information](https://docs.btcpayserver.org/Payjoin/))"
|
||||||
|
},
|
||||||
|
"defaultPaymentMethod": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "BTC",
|
||||||
|
"description": "The default payment method to load when displaying an invoice. It can be in the format of `BTC_LightningNetwork` to specify Lightning to be the default or `BTC_OnChain`/ `BTC` for on-chain to be the default. "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user