From 2f4e610900c32ad3bf2e079f8bf441705c55940a Mon Sep 17 00:00:00 2001 From: Umar Bolatov Date: Sun, 29 Aug 2021 21:38:57 -0700 Subject: [PATCH] Add UpdateLightningNetworkPaymentMethodRequest --- ...ateLightningNetworkPaymentMethodRequest.cs | 20 +++++++++++++++++++ .../GreenField/LocalBTCPayServerClient.cs | 2 +- ...ightningNetworkPaymentMethodsController.cs | 20 +++++++++---------- ...res-payment-methods.lightning-network.json | 14 ++++++++++++- 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 BTCPayServer.Client/Models/UpdateLightningNetworkPaymentMethodRequest.cs diff --git a/BTCPayServer.Client/Models/UpdateLightningNetworkPaymentMethodRequest.cs b/BTCPayServer.Client/Models/UpdateLightningNetworkPaymentMethodRequest.cs new file mode 100644 index 000000000..0cb2f1b48 --- /dev/null +++ b/BTCPayServer.Client/Models/UpdateLightningNetworkPaymentMethodRequest.cs @@ -0,0 +1,20 @@ +namespace BTCPayServer.Client.Models +{ + public class UpdateLightningNetworkPaymentMethodRequest: LightningNetworkPaymentMethodBaseData + { + /// + /// Whether the payment method is enabled + /// + public bool Enabled { get; set; } + + public UpdateLightningNetworkPaymentMethodRequest() + { + } + + public UpdateLightningNetworkPaymentMethodRequest(string connectionString, bool enabled) + { + Enabled = enabled; + ConnectionString = connectionString; + } + } +} diff --git a/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs b/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs index 374a9ca1b..06017320b 100644 --- a/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs +++ b/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs @@ -771,7 +771,7 @@ namespace BTCPayServer.Controllers.GreenField { return GetFromActionResult(await _storeLightningNetworkPaymentMethodsController.UpdateLightningNetworkPaymentMethod(storeId, cryptoCode, - paymentMethod)); + new UpdateLightningNetworkPaymentMethodRequest(paymentMethod.ConnectionString, paymentMethod.Enabled))); } public override async Task> GetInvoices(string storeId, string[] orderId = null, diff --git a/BTCPayServer/Controllers/GreenField/StoreLightningNetworkPaymentMethodsController.cs b/BTCPayServer/Controllers/GreenField/StoreLightningNetworkPaymentMethodsController.cs index 3dc236985..88f4e878e 100644 --- a/BTCPayServer/Controllers/GreenField/StoreLightningNetworkPaymentMethodsController.cs +++ b/BTCPayServer/Controllers/GreenField/StoreLightningNetworkPaymentMethodsController.cs @@ -114,7 +114,7 @@ namespace BTCPayServer.Controllers.GreenField [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [HttpPut("~/api/v1/stores/{storeId}/payment-methods/LightningNetwork/{cryptoCode}")] public async Task UpdateLightningNetworkPaymentMethod(string storeId, string cryptoCode, - [FromBody] LightningNetworkPaymentMethodData paymentMethodData) + [FromBody] UpdateLightningNetworkPaymentMethodRequest request) { var paymentMethodId = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike); @@ -123,7 +123,7 @@ namespace BTCPayServer.Controllers.GreenField return NotFound(); } - if (string.IsNullOrEmpty(paymentMethodData.ConnectionString)) + if (string.IsNullOrEmpty(request.ConnectionString)) { ModelState.AddModelError(nameof(LightningNetworkPaymentMethodData.ConnectionString), "Missing connectionString"); @@ -133,13 +133,13 @@ namespace BTCPayServer.Controllers.GreenField return this.CreateValidationError(ModelState); LightningSupportedPaymentMethod? paymentMethod = null; - if (!string.IsNullOrEmpty(paymentMethodData!.ConnectionString)) + if (!string.IsNullOrEmpty(request!.ConnectionString)) { - if (paymentMethodData.ConnectionString == LightningSupportedPaymentMethod.InternalNode) + if (request.ConnectionString == LightningSupportedPaymentMethod.InternalNode) { if (!await CanUseInternalLightning()) { - ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), + ModelState.AddModelError(nameof(request.ConnectionString), $"You are not authorized to use the internal lightning node"); return this.CreateValidationError(ModelState); } @@ -152,23 +152,23 @@ namespace BTCPayServer.Controllers.GreenField } else { - if (!LightningConnectionString.TryParse(paymentMethodData.ConnectionString, false, + if (!LightningConnectionString.TryParse(request.ConnectionString, false, out var connectionString, out var error)) { - ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), $"Invalid URL ({error})"); + ModelState.AddModelError(nameof(request.ConnectionString), $"Invalid URL ({error})"); return this.CreateValidationError(ModelState); } if (connectionString.ConnectionType == LightningConnectionType.LndGRPC) { - ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), + ModelState.AddModelError(nameof(request.ConnectionString), $"BTCPay does not support gRPC connections"); return this.CreateValidationError(ModelState); } if (!await CanManageServer() && !connectionString.IsSafe()) { - ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), + ModelState.AddModelError(nameof(request.ConnectionString), $"You do not have 'btcpay.server.canmodifyserversettings' rights, so the connection string should not contain 'cookiefilepath', 'macaroondirectorypath', 'macaroonfilepath', and should not point to a local ip or to a dns name ending with '.internal', '.local', '.lan' or '.'."); return this.CreateValidationError(ModelState); } @@ -184,7 +184,7 @@ namespace BTCPayServer.Controllers.GreenField var store = Store; var storeBlob = store.GetStoreBlob(); store.SetSupportedPaymentMethod(paymentMethodId, paymentMethod); - storeBlob.SetExcluded(paymentMethodId, !paymentMethodData.Enabled); + storeBlob.SetExcluded(paymentMethodId, !request.Enabled); store.SetStoreBlob(storeBlob); await _storeRepository.UpdateStore(store); return Ok(GetExistingLightningLikePaymentMethod(cryptoCode, store)); diff --git a/BTCPayServer/wwwroot/swagger/v1/swagger.template.stores-payment-methods.lightning-network.json b/BTCPayServer/wwwroot/swagger/v1/swagger.template.stores-payment-methods.lightning-network.json index c5a8368fe..33bdce909 100644 --- a/BTCPayServer/wwwroot/swagger/v1/swagger.template.stores-payment-methods.lightning-network.json +++ b/BTCPayServer/wwwroot/swagger/v1/swagger.template.stores-payment-methods.lightning-network.json @@ -136,7 +136,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/LightningNetworkPaymentMethodData" + "$ref": "#/components/schemas/UpdateLightningNetworkPaymentMethodRequest" } } }, @@ -273,6 +273,18 @@ "description": "Crypto code of the payment method" } } + }, + "UpdateLightningNetworkPaymentMethodRequest": { + "type": "object", + "additionalProperties": { + "$ref": "#/components/schemas/LightningNetworkPaymentMethodBaseData" + }, + "properties": { + "enabled": { + "type": "boolean", + "description": "Whether the payment method is enabled" + } + } } } },