use serde::{Deserialize, Serialize};
use crate::client::RazorpayClient;
use crate::error::RazorpayError;
use crate::resources::common::Notes;
use crate::resources::tokens::Token;
use crate::util::pagination::{Collection, ListOptions};
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct Customer {
pub id: String,
#[serde(default)]
pub entity: String,
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub email: Option<String>,
#[serde(default)]
pub contact: Option<String>,
#[serde(default)]
pub gstin: Option<String>,
#[serde(default)]
pub notes: Notes,
#[serde(default)]
pub created_at: i64,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct CustomerParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub contact: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub gstin: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub notes: Option<Notes>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fail_existing: Option<u8>,
}
impl CustomerParams {
pub fn new() -> Self {
Self::default()
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn email(mut self, email: impl Into<String>) -> Self {
self.email = Some(email.into());
self
}
pub fn contact(mut self, contact: impl Into<String>) -> Self {
self.contact = Some(contact.into());
self
}
pub fn gstin(mut self, gstin: impl Into<String>) -> Self {
self.gstin = Some(gstin.into());
self
}
pub fn notes(mut self, notes: Notes) -> Self {
self.notes = Some(notes);
self
}
pub fn reuse_existing(mut self) -> Self {
self.fail_existing = Some(0);
self
}
}
pub struct CustomersClient<'a> {
pub(crate) client: &'a RazorpayClient,
}
impl<'a> CustomersClient<'a> {
pub async fn create(&self, params: CustomerParams) -> Result<Customer, RazorpayError> {
self.client.post("customers", Some(¶ms)).await
}
pub async fn fetch(&self, id: &str) -> Result<Customer, RazorpayError> {
self.client.get::<(), _>(&format!("customers/{id}"), None).await
}
pub async fn all(&self, options: ListOptions) -> Result<Collection<Customer>, RazorpayError> {
self.client.get("customers", Some(&options)).await
}
pub async fn edit(
&self,
id: &str,
params: CustomerParams,
) -> Result<Customer, RazorpayError> {
self.client
.put(&format!("customers/{id}"), Some(¶ms))
.await
}
pub async fn tokens(&self, id: &str) -> Result<Collection<Token>, RazorpayError> {
self.client
.get::<(), _>(&format!("customers/{id}/tokens"), None)
.await
}
pub async fn fetch_token(&self, id: &str, token_id: &str) -> Result<Token, RazorpayError> {
self.client
.get::<(), _>(&format!("customers/{id}/tokens/{token_id}"), None)
.await
}
pub async fn delete_token(&self, id: &str, token_id: &str) -> Result<(), RazorpayError> {
self.client
.delete::<serde::de::IgnoredAny>(&format!("customers/{id}/tokens/{token_id}"))
.await
.map(|_| ())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_params_serialize_to_empty_object() {
assert_eq!(serde_json::to_string(&CustomerParams::new()).unwrap(), "{}");
}
#[test]
fn reuse_existing_sets_fail_existing_zero() {
let json = serde_json::to_string(&CustomerParams::new().reuse_existing()).unwrap();
assert_eq!(json, r#"{"fail_existing":0}"#);
}
}