fix: lnbits expired invoice for existing user

This commit is contained in:
thesimplekid 2023-07-01 08:10:59 -04:00 committed by Greg Heartsfield
parent 06fcaad9a1
commit 1131c1986e
2 changed files with 16 additions and 13 deletions

View File

@ -5,10 +5,10 @@ use hyper::Client;
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use nostr::Keys; use nostr::Keys;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
use async_trait::async_trait; use async_trait::async_trait;
use rand::Rng; use rand::Rng;
use tracing::debug;
use std::str::FromStr; use std::str::FromStr;
use url::Url; 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 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(); let uri = Uri::from_str(url.as_str().strip_suffix("/").unwrap_or(url.as_str())).unwrap();
debug!("{uri}");
let req = hyper::Request::builder() let req = hyper::Request::builder()
.method(hyper::Method::POST) .method(hyper::Method::POST)
@ -122,14 +121,10 @@ impl PaymentProcessor for LNBitsPaymentProcessor {
let res = self.client.request(req).await?; let res = self.client.request(req).await?;
debug!("{res:?}");
// Json to Struct of LNbits callback // Json to Struct of LNbits callback
let body = hyper::body::to_bytes(res.into_body()).await?; let body = hyper::body::to_bytes(res.into_body()).await?;
let invoice_response: LNBitsCreateInvoiceResponse = serde_json::from_slice(&body)?; let invoice_response: LNBitsCreateInvoiceResponse = serde_json::from_slice(&body)?;
debug!("{:?}", invoice_response);
Ok(InvoiceInfo { Ok(InvoiceInfo {
pubkey: key.public_key().to_string(), pubkey: key.public_key().to_string(),
payment_hash: invoice_response.payment_hash, payment_hash: invoice_response.payment_hash,
@ -147,7 +142,6 @@ impl PaymentProcessor for LNBitsPaymentProcessor {
.join(APIPATH)? .join(APIPATH)?
.join(payment_hash)?; .join(payment_hash)?;
let uri = Uri::from_str(url.as_str()).unwrap(); let uri = Uri::from_str(url.as_str()).unwrap();
debug!("{uri}");
let req = hyper::Request::builder() let req = hyper::Request::builder()
.method(hyper::Method::GET) .method(hyper::Method::GET)
@ -159,13 +153,18 @@ impl PaymentProcessor for LNBitsPaymentProcessor {
let res = self.client.request(req).await?; let res = self.client.request(req).await?;
// Json to Struct of LNbits callback // Json to Struct of LNbits callback
let body = hyper::body::to_bytes(res.into_body()).await?; let body = hyper::body::to_bytes(res.into_body()).await?;
debug!("check invoice: {body:?}"); let invoice_response: Value = serde_json::from_slice(&body)?;
let invoice_response: LNBitsCheckInvoiceResponse = serde_json::from_slice(&body)?;
let status = if invoice_response.paid { let status = if let Ok(invoice_response) =
serde_json::from_value::<LNBitsCheckInvoiceResponse>(invoice_response)
{
if invoice_response.paid {
InvoiceStatus::Paid InvoiceStatus::Paid
} else { } else {
InvoiceStatus::Unpaid InvoiceStatus::Unpaid
}
} else {
InvoiceStatus::Expired
}; };
Ok(status) Ok(status)

View File

@ -148,7 +148,7 @@ impl Payment {
Ok(PaymentMessage::CheckAccount(pubkey)) => { Ok(PaymentMessage::CheckAccount(pubkey)) => {
let keys = Keys::from_pk_str(&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? { match self.check_invoice_status(&invoice_info.payment_hash).await? {
InvoiceStatus::Paid => { InvoiceStatus::Paid => {
self.repo.admit_account(&keys, self.settings.pay_to_relay.admission_cost).await?; 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(); 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)) => { Ok(PaymentMessage::InvoicePaid(payment_hash)) => {