From 1131c1986e5b3a92fcac9ee61ccb3a529206765f Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 1 Jul 2023 08:10:59 -0400 Subject: [PATCH] fix: `lnbits` expired invoice for existing user --- src/payment/lnbits.rs | 23 +++++++++++------------ src/payment/mod.rs | 6 +++++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/payment/lnbits.rs b/src/payment/lnbits.rs index b9acbcd..055882e 100644 --- a/src/payment/lnbits.rs +++ b/src/payment/lnbits.rs @@ -5,10 +5,10 @@ use hyper::Client; use hyper_tls::HttpsConnector; use nostr::Keys; use serde::{Deserialize, Serialize}; +use serde_json::Value; use async_trait::async_trait; use rand::Rng; -use tracing::debug; use std::str::FromStr; use url::Url; @@ -111,7 +111,6 @@ impl PaymentProcessor for LNBitsPaymentProcessor { }; let url = Url::parse(&self.settings.pay_to_relay.node_url)?.join(APIPATH)?; let uri = Uri::from_str(url.as_str().strip_suffix("/").unwrap_or(url.as_str())).unwrap(); - debug!("{uri}"); let req = hyper::Request::builder() .method(hyper::Method::POST) @@ -122,14 +121,10 @@ impl PaymentProcessor for LNBitsPaymentProcessor { let res = self.client.request(req).await?; - debug!("{res:?}"); - // Json to Struct of LNbits callback let body = hyper::body::to_bytes(res.into_body()).await?; let invoice_response: LNBitsCreateInvoiceResponse = serde_json::from_slice(&body)?; - debug!("{:?}", invoice_response); - Ok(InvoiceInfo { pubkey: key.public_key().to_string(), payment_hash: invoice_response.payment_hash, @@ -147,7 +142,6 @@ impl PaymentProcessor for LNBitsPaymentProcessor { .join(APIPATH)? .join(payment_hash)?; let uri = Uri::from_str(url.as_str()).unwrap(); - debug!("{uri}"); let req = hyper::Request::builder() .method(hyper::Method::GET) @@ -159,13 +153,18 @@ impl PaymentProcessor for LNBitsPaymentProcessor { let res = self.client.request(req).await?; // Json to Struct of LNbits callback let body = hyper::body::to_bytes(res.into_body()).await?; - debug!("check invoice: {body:?}"); - let invoice_response: LNBitsCheckInvoiceResponse = serde_json::from_slice(&body)?; + let invoice_response: Value = serde_json::from_slice(&body)?; - let status = if invoice_response.paid { - InvoiceStatus::Paid + let status = if let Ok(invoice_response) = + serde_json::from_value::(invoice_response) + { + if invoice_response.paid { + InvoiceStatus::Paid + } else { + InvoiceStatus::Unpaid + } } else { - InvoiceStatus::Unpaid + InvoiceStatus::Expired }; Ok(status) diff --git a/src/payment/mod.rs b/src/payment/mod.rs index 8e88933..a6d07b3 100644 --- a/src/payment/mod.rs +++ b/src/payment/mod.rs @@ -148,7 +148,7 @@ impl Payment { Ok(PaymentMessage::CheckAccount(pubkey)) => { let keys = Keys::from_pk_str(&pubkey)?; - if let Some(invoice_info) = self.repo.get_unpaid_invoice(&keys).await? { + if let Ok(Some(invoice_info)) = self.repo.get_unpaid_invoice(&keys).await { match self.check_invoice_status(&invoice_info.payment_hash).await? { InvoiceStatus::Paid => { self.repo.admit_account(&keys, self.settings.pay_to_relay.admission_cost).await?; @@ -158,6 +158,10 @@ impl Payment { self.payment_tx.send(PaymentMessage::Invoice(pubkey, invoice_info)).ok(); } } + } else { + let amount = self.settings.pay_to_relay.admission_cost; + let invoice_info = self.get_invoice_info(&pubkey, amount).await?; + self.payment_tx.send(PaymentMessage::Invoice(pubkey, invoice_info)).ok(); } } Ok(PaymentMessage::InvoicePaid(payment_hash)) => {