use crate::{FilesClient, PaginationInfo, Result};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentLineItemEntity {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub invoice_id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_id: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentEntity {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub balance: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub download_uri: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_line_items: Option<Vec<PaymentLineItemEntity>>,
}
pub struct PaymentHandler {
client: FilesClient,
}
impl PaymentHandler {
pub fn new(client: FilesClient) -> Self {
Self { client }
}
pub async fn list(
&self,
cursor: Option<&str>,
per_page: Option<i64>,
) -> Result<(Vec<PaymentEntity>, PaginationInfo)> {
let mut params = vec![];
if let Some(c) = cursor {
params.push(("cursor", c.to_string()));
}
if let Some(pp) = per_page {
params.push(("per_page", pp.to_string()));
}
let query = if params.is_empty() {
String::new()
} else {
format!(
"?{}",
params
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join("&")
)
};
let response = self.client.get_raw(&format!("/payments{}", query)).await?;
let payments: Vec<PaymentEntity> = serde_json::from_value(response)?;
let pagination = PaginationInfo {
cursor_next: None,
cursor_prev: None,
};
Ok((payments, pagination))
}
pub async fn get(&self, id: i64) -> Result<PaymentEntity> {
let response = self.client.get_raw(&format!("/payments/{}", id)).await?;
Ok(serde_json::from_value(response)?)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_handler_creation() {
let client = FilesClient::builder().api_key("test-key").build().unwrap();
let _handler = PaymentHandler::new(client);
}
}