From 376348028059ef4489e25ea5f401d714fb645579 Mon Sep 17 00:00:00 2001 From: d11n Date: Mon, 8 May 2023 12:11:42 +0200 Subject: [PATCH] NFC: Handle HTTP-related exceptions (#4965) --- BTCPayServer/Plugins/NFC/NFCController.cs | 70 ++++++++++++++--------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/BTCPayServer/Plugins/NFC/NFCController.cs b/BTCPayServer/Plugins/NFC/NFCController.cs index 22b43dbfe..4e1c7e79c 100644 --- a/BTCPayServer/Plugins/NFC/NFCController.cs +++ b/BTCPayServer/Plugins/NFC/NFCController.cs @@ -58,7 +58,7 @@ namespace BTCPayServer.Plugins.NFC if (!methods.TryGetValue(new PaymentMethodId("BTC", PaymentTypes.LNURLPay), out var lnurlPaymentMethod) && !methods.TryGetValue(new PaymentMethodId("BTC", PaymentTypes.LightningLike), out lnPaymentMethod)) { - return BadRequest("Destination for lnurlw was not specified"); + return BadRequest("Destination for LNURL-Withdraw was not specified"); } Uri uri; @@ -81,10 +81,8 @@ namespace BTCPayServer.Plugins.NFC return BadRequest("LNURL was not LNURL-Withdraw"); } - var httpClient = _httpClientFactory.CreateClient(uri.IsOnion() - ? LightningLikePayoutHandler.LightningLikePayoutHandlerOnionNamedClient - : LightningLikePayoutHandler.LightningLikePayoutHandlerClearnetNamedClient); LNURLWithdrawRequest info; + var httpClient = CreateHttpClient(uri); try { info = await LNURL.LNURL.FetchInformation(uri, tag, httpClient) as LNURLWithdrawRequest; @@ -100,10 +98,6 @@ namespace BTCPayServer.Plugins.NFC return BadRequest("Could not fetch info from LNURL-Withdraw"); } - httpClient = _httpClientFactory.CreateClient(info.Callback.IsOnion() - ? LightningLikePayoutHandler.LightningLikePayoutHandlerOnionNamedClient - : LightningLikePayoutHandler.LightningLikePayoutHandlerClearnetNamedClient); - string bolt11 = null; if (lnPaymentMethod is not null) { @@ -155,23 +149,31 @@ namespace BTCPayServer.Plugins.NFC due = lnurlPaymentMethod.Calculate().Due; } - var amount = LightMoney.Satoshis(due.Satoshi); - var actionPath = Url.Action(nameof(UILNURLController.GetLNURLForInvoice), "UILNURL", - new { invoiceId = request.InvoiceId, cryptoCode = "BTC", amount = amount.MilliSatoshi }); - var url = Request.GetAbsoluteUri(actionPath); - var resp = await httpClient.GetAsync(url); - var response = await resp.Content.ReadAsStringAsync(); + try + { + httpClient = CreateHttpClient(info.Callback); + var amount = LightMoney.Satoshis(due.Satoshi); + var actionPath = Url.Action(nameof(UILNURLController.GetLNURLForInvoice), "UILNURL", + new { invoiceId = request.InvoiceId, cryptoCode = "BTC", amount = amount.MilliSatoshi }); + var url = Request.GetAbsoluteUri(actionPath); + var resp = await httpClient.GetAsync(url); + var response = await resp.Content.ReadAsStringAsync(); - if (resp.IsSuccessStatusCode) - { - var res = JObject.Parse(response).ToObject(); - bolt11 = res.Pr; + if (resp.IsSuccessStatusCode) + { + var res = JObject.Parse(response).ToObject(); + bolt11 = res.Pr; + } + else + { + var res = JObject.Parse(response).ToObject(); + return BadRequest($"Could not fetch BOLT11 invoice to pay to: {res.Reason}"); + + } } - else + catch (Exception ex) { - var res = JObject.Parse(response).ToObject(); - return BadRequest( - $"Could not fetch BOLT11 invoice to pay to: {res.Reason}"); + return BadRequest($"Could not fetch BOLT11 invoice to pay to: {ex.Message}"); } } @@ -180,13 +182,27 @@ namespace BTCPayServer.Plugins.NFC return BadRequest("Could not fetch BOLT11 invoice to pay to."); } - var result = await info.SendRequest(bolt11, httpClient); - if (!string.IsNullOrEmpty(result.Status) && result.Status.Equals("ok", StringComparison.InvariantCultureIgnoreCase)) + try { - return Ok(result.Reason); - } + var result = await info.SendRequest(bolt11, httpClient); + if (!string.IsNullOrEmpty(result.Status) && result.Status.Equals("ok", StringComparison.InvariantCultureIgnoreCase)) + { + return Ok(result.Reason); + } - return BadRequest(result.Reason ?? "Unknown error"); + return BadRequest(result.Reason ?? "Unknown error"); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + + private HttpClient CreateHttpClient(Uri uri) + { + return _httpClientFactory.CreateClient(uri.IsOnion() + ? LightningLikePayoutHandler.LightningLikePayoutHandlerOnionNamedClient + : LightningLikePayoutHandler.LightningLikePayoutHandlerClearnetNamedClient); } } }