1use anyhow::{bail, Result};
4use serde::{Deserialize, Serialize};
5
6use crate::{Amount, ConversionRate, InvoiceState, Strike};
7
8#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct InvoiceRequest {
12 pub correlation_id: Option<String>,
14 pub description: Option<String>,
16 pub amount: Amount,
18}
19
20#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
22#[serde(rename_all = "camelCase")]
23pub struct InvoiceResponse {
24 pub invoice_id: String,
26 pub amount: Amount,
28 pub state: InvoiceState,
30 pub created: String,
32 pub description: Option<String>,
34 pub issuer_id: String,
36 pub receiver_id: String,
38}
39
40#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
42#[serde(rename_all = "camelCase")]
43pub struct InvoiceQuoteResponse {
44 pub quote_id: String,
46 pub description: Option<String>,
48 pub ln_invoice: String,
50 pub onchain_address: Option<String>,
52 pub expiration: String,
54 pub expiration_in_sec: u64,
56 pub source_amount: Amount,
58 pub target_amount: Amount,
60 pub conversion_rate: ConversionRate,
62}
63
64impl Strike {
65 pub async fn create_invoice(&self, invoice_request: InvoiceRequest) -> Result<InvoiceResponse> {
67 let url = self.base_url.join("/v1/invoices")?;
68
69 let res = self
70 .make_post(url, Some(serde_json::to_value(invoice_request)?))
71 .await?;
72
73 match serde_json::from_value(res.clone()) {
74 Ok(res) => Ok(res),
75 Err(_) => {
76 log::error!("Api error response on invoice creation");
77 log::error!("{}", res);
78 bail!("Could not create invoice")
79 }
80 }
81 }
82
83 pub async fn get_incoming_invoice(&self, invoice_id: &str) -> Result<InvoiceResponse> {
85 let url = self.base_url.join("/v1/invoices/")?.join(invoice_id)?;
86
87 let res = self.make_get(url).await?;
88
89 match serde_json::from_value(res.clone()) {
90 Ok(res) => Ok(res),
91 Err(_) => {
92 log::error!("Api error response on find invoice");
93 log::error!("{}", res);
94 bail!("Could not find invoice")
95 }
96 }
97 }
98
99 pub async fn invoice_quote(&self, invoice_id: &str) -> Result<InvoiceQuoteResponse> {
101 let url = self
102 .base_url
103 .join(&format!("/v1/invoices/{invoice_id}/quote"))?;
104
105 let res = self.make_post(url, None::<String>).await?;
106
107 match serde_json::from_value(res.clone()) {
108 Ok(res) => Ok(res),
109 Err(_) => {
110 log::error!("Api error response on invoice quote");
111 log::error!("{}", res);
112 bail!("Could get invoice quote")
113 }
114 }
115 }
116}