mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-20 22:24:54 +01:00
feat: payment processor
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#![warn(rustdoc::bare_urls)]
|
||||
|
||||
use std::pin::Pin;
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -11,11 +12,12 @@ use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use axum::Router;
|
||||
use cdk::amount::{to_unit, Amount, MSAT_IN_SAT};
|
||||
use cdk::cdk_lightning::{
|
||||
self, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings,
|
||||
use cdk::cdk_payment::{
|
||||
self, Bolt11Settings, CreateIncomingPaymentResponse, MakePaymentResponse, MintPayment,
|
||||
PaymentQuoteResponse,
|
||||
};
|
||||
use cdk::mint::FeeReserve;
|
||||
use cdk::nuts::{CurrencyUnit, MeltQuoteBolt11Request, MeltQuoteState, MintQuoteState};
|
||||
use cdk::nuts::{CurrencyUnit, MeltOptions, MeltQuoteState, MintQuoteState};
|
||||
use cdk::types::FeeReserve;
|
||||
use cdk::util::unix_time;
|
||||
use cdk::{mint, Bolt11Invoice};
|
||||
use error::Error;
|
||||
@@ -23,6 +25,7 @@ use futures::stream::StreamExt;
|
||||
use futures::Stream;
|
||||
use lnbits_rs::api::invoice::CreateInvoiceRequest;
|
||||
use lnbits_rs::LNBitsClient;
|
||||
use serde_json::Value;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
@@ -37,6 +40,7 @@ pub struct LNbits {
|
||||
webhook_url: String,
|
||||
wait_invoice_cancel_token: CancellationToken,
|
||||
wait_invoice_is_active: Arc<AtomicBool>,
|
||||
settings: Bolt11Settings,
|
||||
}
|
||||
|
||||
impl LNbits {
|
||||
@@ -59,20 +63,21 @@ impl LNbits {
|
||||
webhook_url,
|
||||
wait_invoice_cancel_token: CancellationToken::new(),
|
||||
wait_invoice_is_active: Arc::new(AtomicBool::new(false)),
|
||||
settings: Bolt11Settings {
|
||||
mpp: false,
|
||||
unit: CurrencyUnit::Sat,
|
||||
invoice_description: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MintLightning for LNbits {
|
||||
type Err = cdk_lightning::Error;
|
||||
impl MintPayment for LNbits {
|
||||
type Err = cdk_payment::Error;
|
||||
|
||||
fn get_settings(&self) -> Settings {
|
||||
Settings {
|
||||
mpp: false,
|
||||
unit: CurrencyUnit::Sat,
|
||||
invoice_description: true,
|
||||
}
|
||||
async fn get_settings(&self) -> Result<Value, Self::Err> {
|
||||
Ok(serde_json::to_value(&self.settings)?)
|
||||
}
|
||||
|
||||
fn is_wait_invoice_active(&self) -> bool {
|
||||
@@ -83,7 +88,7 @@ impl MintLightning for LNbits {
|
||||
self.wait_invoice_cancel_token.cancel()
|
||||
}
|
||||
|
||||
async fn wait_any_invoice(
|
||||
async fn wait_any_incoming_payment(
|
||||
&self,
|
||||
) -> Result<Pin<Box<dyn Stream<Item = String> + Send>>, Self::Err> {
|
||||
let receiver = self
|
||||
@@ -145,15 +150,30 @@ impl MintLightning for LNbits {
|
||||
|
||||
async fn get_payment_quote(
|
||||
&self,
|
||||
melt_quote_request: &MeltQuoteBolt11Request,
|
||||
request: &str,
|
||||
unit: &CurrencyUnit,
|
||||
options: Option<MeltOptions>,
|
||||
) -> Result<PaymentQuoteResponse, Self::Err> {
|
||||
if melt_quote_request.unit != CurrencyUnit::Sat {
|
||||
if unit != &CurrencyUnit::Sat {
|
||||
return Err(Self::Err::Anyhow(anyhow!("Unsupported unit")));
|
||||
}
|
||||
|
||||
let amount = melt_quote_request.amount_msat()?;
|
||||
let bolt11 = Bolt11Invoice::from_str(request)?;
|
||||
|
||||
let amount = amount / MSAT_IN_SAT.into();
|
||||
let amount_msat = match options {
|
||||
Some(amount) => {
|
||||
if matches!(amount, MeltOptions::Mpp { mpp: _ }) {
|
||||
return Err(cdk_payment::Error::UnsupportedPaymentOption);
|
||||
}
|
||||
amount.amount_msat()
|
||||
}
|
||||
None => bolt11
|
||||
.amount_milli_satoshis()
|
||||
.ok_or(Error::UnknownInvoiceAmount)?
|
||||
.into(),
|
||||
};
|
||||
|
||||
let amount = amount_msat / MSAT_IN_SAT.into();
|
||||
|
||||
let relative_fee_reserve =
|
||||
(self.fee_reserve.percent_fee_reserve * u64::from(amount) as f32) as u64;
|
||||
@@ -166,19 +186,19 @@ impl MintLightning for LNbits {
|
||||
};
|
||||
|
||||
Ok(PaymentQuoteResponse {
|
||||
request_lookup_id: melt_quote_request.request.payment_hash().to_string(),
|
||||
request_lookup_id: bolt11.payment_hash().to_string(),
|
||||
amount,
|
||||
fee: fee.into(),
|
||||
state: MeltQuoteState::Unpaid,
|
||||
})
|
||||
}
|
||||
|
||||
async fn pay_invoice(
|
||||
async fn make_payment(
|
||||
&self,
|
||||
melt_quote: mint::MeltQuote,
|
||||
_partial_msats: Option<Amount>,
|
||||
_max_fee_msats: Option<Amount>,
|
||||
) -> Result<PayInvoiceResponse, Self::Err> {
|
||||
) -> Result<MakePaymentResponse, Self::Err> {
|
||||
let pay_response = self
|
||||
.lnbits_api
|
||||
.pay_invoice(&melt_quote.request, None)
|
||||
@@ -212,36 +232,35 @@ impl MintLightning for LNbits {
|
||||
.unsigned_abs(),
|
||||
);
|
||||
|
||||
Ok(PayInvoiceResponse {
|
||||
Ok(MakePaymentResponse {
|
||||
payment_lookup_id: pay_response.payment_hash,
|
||||
payment_preimage: Some(invoice_info.payment_hash),
|
||||
payment_proof: Some(invoice_info.payment_hash),
|
||||
status,
|
||||
total_spent,
|
||||
unit: CurrencyUnit::Sat,
|
||||
})
|
||||
}
|
||||
|
||||
async fn create_invoice(
|
||||
async fn create_incoming_payment_request(
|
||||
&self,
|
||||
amount: Amount,
|
||||
unit: &CurrencyUnit,
|
||||
description: String,
|
||||
unix_expiry: u64,
|
||||
) -> Result<CreateInvoiceResponse, Self::Err> {
|
||||
unix_expiry: Option<u64>,
|
||||
) -> Result<CreateIncomingPaymentResponse, Self::Err> {
|
||||
if unit != &CurrencyUnit::Sat {
|
||||
return Err(Self::Err::Anyhow(anyhow!("Unsupported unit")));
|
||||
}
|
||||
|
||||
let time_now = unix_time();
|
||||
assert!(unix_expiry > time_now);
|
||||
|
||||
let expiry = unix_expiry - time_now;
|
||||
let expiry = unix_expiry.map(|t| t - time_now);
|
||||
|
||||
let invoice_request = CreateInvoiceRequest {
|
||||
amount: to_unit(amount, unit, &CurrencyUnit::Sat)?.into(),
|
||||
memo: Some(description),
|
||||
unit: unit.to_string(),
|
||||
expiry: Some(expiry),
|
||||
expiry,
|
||||
webhook: Some(self.webhook_url.clone()),
|
||||
internal: None,
|
||||
out: false,
|
||||
@@ -260,14 +279,14 @@ impl MintLightning for LNbits {
|
||||
let request: Bolt11Invoice = create_invoice_response.payment_request.parse()?;
|
||||
let expiry = request.expires_at().map(|t| t.as_secs());
|
||||
|
||||
Ok(CreateInvoiceResponse {
|
||||
Ok(CreateIncomingPaymentResponse {
|
||||
request_lookup_id: create_invoice_response.payment_hash,
|
||||
request,
|
||||
request: request.to_string(),
|
||||
expiry,
|
||||
})
|
||||
}
|
||||
|
||||
async fn check_incoming_invoice_status(
|
||||
async fn check_incoming_payment_status(
|
||||
&self,
|
||||
payment_hash: &str,
|
||||
) -> Result<MintQuoteState, Self::Err> {
|
||||
@@ -292,7 +311,7 @@ impl MintLightning for LNbits {
|
||||
async fn check_outgoing_payment(
|
||||
&self,
|
||||
payment_hash: &str,
|
||||
) -> Result<PayInvoiceResponse, Self::Err> {
|
||||
) -> Result<MakePaymentResponse, Self::Err> {
|
||||
let payment = self
|
||||
.lnbits_api
|
||||
.get_payment_info(payment_hash)
|
||||
@@ -303,15 +322,15 @@ impl MintLightning for LNbits {
|
||||
Self::Err::Anyhow(anyhow!("Could not check invoice status"))
|
||||
})?;
|
||||
|
||||
let pay_response = PayInvoiceResponse {
|
||||
let pay_response = MakePaymentResponse {
|
||||
payment_lookup_id: payment.details.payment_hash,
|
||||
payment_preimage: Some(payment.preimage),
|
||||
payment_proof: Some(payment.preimage),
|
||||
status: lnbits_to_melt_status(&payment.details.status, payment.details.pending),
|
||||
total_spent: Amount::from(
|
||||
payment.details.amount.unsigned_abs()
|
||||
+ payment.details.fee.unsigned_abs() / MSAT_IN_SAT,
|
||||
),
|
||||
unit: self.get_settings().unit,
|
||||
unit: self.settings.unit.clone(),
|
||||
};
|
||||
|
||||
Ok(pay_response)
|
||||
|
||||
Reference in New Issue
Block a user