use common_utils::{crypto, custom_serde, id_type, pii, types::Description}; use masking::Secret; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; use crate::payments; /// The customer details #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] #[derive(Debug, Default, Clone, Deserialize, Serialize, ToSchema)] pub struct CustomerRequest { /// The identifier for the customer object. If not provided the customer ID will be autogenerated. #[schema(value_type = Option, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub customer_id: Option, /// The identifier for the Merchant Account #[schema(max_length = 255, example = "y3oqhf46pyzuxjbcn2giaqnb44")] #[serde(skip)] pub merchant_id: id_type::MerchantId, /// The customer's name #[schema(max_length = 255, value_type = Option, example = "Jon Test")] pub name: Option>, /// The customer's email address #[schema(value_type = Option, max_length = 255, example = "JonTest@test.com")] pub email: Option, /// The customer's phone number #[schema(value_type = Option, max_length = 255, example = "9123456789")] pub phone: Option>, /// An arbitrary string that you can attach to a customer object. #[schema(max_length = 255, example = "First Customer", value_type = Option)] pub description: Option, /// The country code for the customer phone number #[schema(max_length = 255, example = "+65")] pub phone_country_code: Option, /// The address for the customer #[schema(value_type = Option)] pub address: Option, /// You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 /// characters long. Metadata is useful for storing additional, structured information on an /// object. #[schema(value_type = Option,example = json!({ "city": "NY", "unit": "245" }))] pub metadata: Option, } #[derive(Debug, Default, Clone, Deserialize, Serialize, ToSchema)] pub struct CustomerListRequest { /// Offset #[schema(example = 32)] pub offset: Option, /// Limit #[schema(example = 32)] pub limit: Option, } #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] impl CustomerRequest { pub fn get_merchant_reference_id(&self) -> Option { Some( self.customer_id .to_owned() .unwrap_or_else(common_utils::generate_customer_id_of_default_length), ) } pub fn get_address(&self) -> Option { self.address.clone() } pub fn get_optional_email(&self) -> Option { self.email.clone() } } /// The customer details #[cfg(all(feature = "v2", feature = "customer_v2"))] #[derive(Debug, Default, Clone, Deserialize, Serialize, ToSchema)] pub struct CustomerRequest { /// The merchant identifier for the customer object. #[schema(value_type = Option, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub merchant_reference_id: Option, /// The customer's name #[schema(max_length = 255, value_type = String, example = "Jon Test")] pub name: Secret, /// The customer's email address #[schema(value_type = String, max_length = 255, example = "JonTest@test.com")] pub email: pii::Email, /// The customer's phone number #[schema(value_type = Option, max_length = 255, example = "9123456789")] pub phone: Option>, /// An arbitrary string that you can attach to a customer object. #[schema(max_length = 255, example = "First Customer", value_type = Option)] pub description: Option, /// The country code for the customer phone number #[schema(max_length = 255, example = "+65")] pub phone_country_code: Option, /// The default billing address for the customer #[schema(value_type = Option)] pub default_billing_address: Option, /// The default shipping address for the customer #[schema(value_type = Option)] pub default_shipping_address: Option, /// You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 /// characters long. Metadata is useful for storing additional, structured information on an /// object. #[schema(value_type = Option,example = json!({ "city": "NY", "unit": "245" }))] pub metadata: Option, } #[cfg(all(feature = "v2", feature = "customer_v2"))] impl CustomerRequest { pub fn get_merchant_reference_id(&self) -> Option { self.merchant_reference_id.clone() } pub fn get_default_customer_billing_address(&self) -> Option { self.default_billing_address.clone() } pub fn get_default_customer_shipping_address(&self) -> Option { self.default_shipping_address.clone() } pub fn get_optional_email(&self) -> Option { Some(self.email.clone()) } } #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] #[derive(Debug, Clone, Serialize, ToSchema)] pub struct CustomerResponse { /// The identifier for the customer object #[schema(value_type = String, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub customer_id: id_type::CustomerId, /// The customer's name #[schema(max_length = 255, value_type = Option, example = "Jon Test")] pub name: crypto::OptionalEncryptableName, /// The customer's email address #[schema(value_type = Option,max_length = 255, example = "JonTest@test.com")] pub email: crypto::OptionalEncryptableEmail, /// The customer's phone number #[schema(value_type = Option,max_length = 255, example = "9123456789")] pub phone: crypto::OptionalEncryptablePhone, /// The country code for the customer phone number #[schema(max_length = 255, example = "+65")] pub phone_country_code: Option, /// An arbitrary string that you can attach to a customer object. #[schema(max_length = 255, example = "First Customer", value_type = Option)] pub description: Option, /// The address for the customer #[schema(value_type = Option)] pub address: Option, /// A timestamp (ISO 8601 code) that determines when the customer was created #[schema(value_type = PrimitiveDateTime,example = "2023-01-18T11:04:09.922Z")] #[serde(with = "custom_serde::iso8601")] pub created_at: time::PrimitiveDateTime, /// You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 /// characters long. Metadata is useful for storing additional, structured information on an /// object. #[schema(value_type = Option,example = json!({ "city": "NY", "unit": "245" }))] pub metadata: Option, /// The identifier for the default payment method. #[schema(max_length = 64, example = "pm_djh2837dwduh890123")] pub default_payment_method_id: Option, } #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] impl CustomerResponse { pub fn get_merchant_reference_id(&self) -> Option { Some(self.customer_id.clone()) } } #[cfg(all(feature = "v2", feature = "customer_v2"))] #[derive(Debug, Clone, Serialize, ToSchema)] pub struct CustomerResponse { /// The identifier for the customer object #[schema(value_type = String, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub merchant_reference_id: Option, /// The customer's name #[schema(max_length = 255, value_type = Option, example = "Jon Test")] pub name: crypto::OptionalEncryptableName, /// The customer's email address #[schema(value_type = Option ,max_length = 255, example = "JonTest@test.com")] pub email: crypto::OptionalEncryptableEmail, /// The customer's phone number #[schema(value_type = Option,max_length = 255, example = "9123456789")] pub phone: crypto::OptionalEncryptablePhone, /// The country code for the customer phone number #[schema(max_length = 255, example = "+65")] pub phone_country_code: Option, /// An arbitrary string that you can attach to a customer object. #[schema(max_length = 255, example = "First Customer", value_type = Option)] pub description: Option, /// The default billing address for the customer #[schema(value_type = Option)] pub default_billing_address: Option, /// The default shipping address for the customer #[schema(value_type = Option)] pub default_shipping_address: Option, /// A timestamp (ISO 8601 code) that determines when the customer was created #[schema(value_type = PrimitiveDateTime,example = "2023-01-18T11:04:09.922Z")] #[serde(with = "custom_serde::iso8601")] pub created_at: time::PrimitiveDateTime, /// You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 /// characters long. Metadata is useful for storing additional, structured information on an /// object. #[schema(value_type = Option,example = json!({ "city": "NY", "unit": "245" }))] pub metadata: Option, /// The identifier for the default payment method. #[schema(max_length = 64, example = "pm_djh2837dwduh890123")] pub default_payment_method_id: Option, /// Global id pub id: String, } #[cfg(all(feature = "v2", feature = "customer_v2"))] impl CustomerResponse { pub fn get_merchant_reference_id(&self) -> Option { self.merchant_reference_id.clone() } } #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CustomerId { pub customer_id: id_type::CustomerId, } #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] impl CustomerId { pub fn get_merchant_reference_id(&self) -> id_type::CustomerId { self.customer_id.clone() } pub fn new_customer_id_struct(cust: id_type::CustomerId) -> Self { Self { customer_id: cust } } } #[cfg(all(feature = "v2", feature = "customer_v2"))] #[derive(Clone, Debug, Deserialize, Serialize)] pub struct GlobalId { pub id: String, } #[cfg(all(feature = "v2", feature = "customer_v2"))] impl GlobalId { pub fn new(id: String) -> Self { Self { id } } } #[cfg(all(feature = "v2", feature = "customer_v2"))] #[derive(Clone, Debug, Deserialize, Serialize)] pub struct CustomerId { pub merchant_reference_id: id_type::CustomerId, } #[cfg(all(feature = "v2", feature = "customer_v2"))] impl CustomerId { pub fn get_merchant_reference_id(&self) -> id_type::CustomerId { self.merchant_reference_id.clone() } pub fn new_customer_id_struct(cust: id_type::CustomerId) -> Self { Self { merchant_reference_id: cust, } } } #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] #[derive(Debug, Deserialize, Serialize, ToSchema)] pub struct CustomerDeleteResponse { /// The identifier for the customer object #[schema(value_type = String, max_length = 255, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub customer_id: id_type::CustomerId, /// Whether customer was deleted or not #[schema(example = false)] pub customer_deleted: bool, /// Whether address was deleted or not #[schema(example = false)] pub address_deleted: bool, /// Whether payment methods deleted or not #[schema(example = false)] pub payment_methods_deleted: bool, } #[cfg(all(feature = "v2", feature = "customer_v2"))] #[derive(Debug, Deserialize, Serialize, ToSchema)] pub struct CustomerDeleteResponse { /// The identifier for the customer object #[schema(value_type = String, max_length = 255, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub merchant_reference_id: Option, /// Whether customer was deleted or not #[schema(example = false)] pub customer_deleted: bool, /// Whether address was deleted or not #[schema(example = false)] pub address_deleted: bool, /// Whether payment methods deleted or not #[schema(example = false)] pub payment_methods_deleted: bool, /// Global id pub id: String, } /// The identifier for the customer object. If not provided the customer ID will be autogenerated. #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] #[derive(Debug, Default, Clone, Deserialize, Serialize, ToSchema)] pub struct CustomerUpdateRequest { /// The identifier for the customer object #[schema(value_type = Option, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub customer_id: Option, /// The identifier for the Merchant Account #[schema(max_length = 255, example = "y3oqhf46pyzuxjbcn2giaqnb44")] #[serde(skip)] pub merchant_id: id_type::MerchantId, /// The customer's name #[schema(max_length = 255, value_type = Option, example = "Jon Test")] pub name: Option>, /// The customer's email address #[schema(value_type = Option, max_length = 255, example = "JonTest@test.com")] pub email: Option, /// The customer's phone number #[schema(value_type = Option, max_length = 255, example = "9123456789")] pub phone: Option>, /// An arbitrary string that you can attach to a customer object. #[schema(max_length = 255, example = "First Customer", value_type = Option)] pub description: Option, /// The country code for the customer phone number #[schema(max_length = 255, example = "+65")] pub phone_country_code: Option, /// The address for the customer #[schema(value_type = Option)] pub address: Option, /// You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 /// characters long. Metadata is useful for storing additional, structured information on an /// object. #[schema(value_type = Option,example = json!({ "city": "NY", "unit": "245" }))] pub metadata: Option, } #[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))] impl CustomerUpdateRequest { pub fn get_merchant_reference_id(&self) -> Option { Some( self.customer_id .to_owned() .unwrap_or_else(common_utils::generate_customer_id_of_default_length), ) } pub fn get_address(&self) -> Option { self.address.clone() } } #[cfg(all(feature = "v2", feature = "customer_v2"))] #[derive(Debug, Default, Clone, Deserialize, Serialize, ToSchema)] pub struct CustomerUpdateRequest { /// The merchant identifier for the customer object. #[schema(value_type = Option, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")] pub merchant_reference_id: Option, /// The customer's name #[schema(max_length = 255, value_type = String, example = "Jon Test")] pub name: Option>, /// The customer's email address #[schema(value_type = String, max_length = 255, example = "JonTest@test.com")] pub email: Option, /// The customer's phone number #[schema(value_type = Option, max_length = 255, example = "9123456789")] pub phone: Option>, /// An arbitrary string that you can attach to a customer object. #[schema(max_length = 255, example = "First Customer", value_type = Option)] pub description: Option, /// The country code for the customer phone number #[schema(max_length = 255, example = "+65")] pub phone_country_code: Option, /// The default billing address for the customer #[schema(value_type = Option)] pub default_billing_address: Option, /// The default shipping address for the customer #[schema(value_type = Option)] pub default_shipping_address: Option, /// You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 /// characters long. Metadata is useful for storing additional, structured information on an /// object. #[schema(value_type = Option,example = json!({ "city": "NY", "unit": "245" }))] pub metadata: Option, /// The unique identifier of the payment method #[schema(example = "card_rGK4Vi5iSW70MY7J2mIg")] pub default_payment_method_id: Option, } #[cfg(all(feature = "v2", feature = "customer_v2"))] impl CustomerUpdateRequest { pub fn get_merchant_reference_id(&self) -> Option { self.merchant_reference_id.clone() } pub fn get_default_customer_billing_address(&self) -> Option { self.default_billing_address.clone() } pub fn get_default_customer_shipping_address(&self) -> Option { self.default_shipping_address.clone() } } #[derive(Default, Debug, serde::Deserialize, serde::Serialize, Clone)] pub struct UpdateCustomerId(String); impl UpdateCustomerId { pub fn get_global_id(&self) -> String { self.0.clone() } pub fn new(id: String) -> Self { Self(id) } }