mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 09:07:09 +08:00
feat(customer): customer v2 refactor for customer create end point (#5350)
Co-authored-by: Narayan Bhat <narayan.bhat@juspay.in> Co-authored-by: hrithikesh026 <hrithikesh.vm@juspay.in> Co-authored-by: Prajjwal Kumar <prajjwal.kumar@juspay.in> Co-authored-by: Sanchith Hegde <sanchith.hegde@juspay.in> Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -13,6 +13,7 @@ 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.
|
||||
@ -47,6 +48,77 @@ pub struct CustomerRequest {
|
||||
pub metadata: Option<pii::SecretSerdeValue>,
|
||||
}
|
||||
|
||||
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
|
||||
impl CustomerRequest {
|
||||
pub fn get_merchant_reference_id(&self) -> Option<id_type::CustomerId> {
|
||||
Some(
|
||||
self.customer_id
|
||||
.to_owned()
|
||||
.unwrap_or_else(common_utils::generate_customer_id_of_default_length),
|
||||
)
|
||||
}
|
||||
pub fn get_address(&self) -> Option<payments::AddressDetails> {
|
||||
self.address.clone()
|
||||
}
|
||||
pub fn get_optional_email(&self) -> Option<pii::Email> {
|
||||
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<String>, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")]
|
||||
pub merchant_reference_id: Option<id_type::CustomerId>,
|
||||
/// The customer's name
|
||||
#[schema(max_length = 255, value_type = String, example = "Jon Test")]
|
||||
pub name: Secret<String>,
|
||||
/// 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<String>, max_length = 255, example = "9123456789")]
|
||||
pub phone: Option<Secret<String>>,
|
||||
/// An arbitrary string that you can attach to a customer object.
|
||||
#[schema(max_length = 255, example = "First Customer")]
|
||||
pub description: Option<String>,
|
||||
/// The country code for the customer phone number
|
||||
#[schema(max_length = 255, example = "+65")]
|
||||
pub phone_country_code: Option<String>,
|
||||
/// The default billing address for the customer
|
||||
#[schema(value_type = Option<AddressDetails>)]
|
||||
pub default_billing_address: Option<payments::AddressDetails>,
|
||||
/// The default shipping address for the customer
|
||||
#[schema(value_type = Option<AddressDetails>)]
|
||||
pub default_shipping_address: Option<payments::AddressDetails>,
|
||||
/// 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<Object>,example = json!({ "city": "NY", "unit": "245" }))]
|
||||
pub metadata: Option<pii::SecretSerdeValue>,
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "v2", feature = "customer_v2"))]
|
||||
impl CustomerRequest {
|
||||
pub fn get_merchant_reference_id(&self) -> Option<id_type::CustomerId> {
|
||||
self.merchant_reference_id.clone()
|
||||
}
|
||||
|
||||
pub fn get_default_customer_billing_address(&self) -> Option<payments::AddressDetails> {
|
||||
self.default_billing_address.clone()
|
||||
}
|
||||
|
||||
pub fn get_default_customer_shipping_address(&self) -> Option<payments::AddressDetails> {
|
||||
self.default_shipping_address.clone()
|
||||
}
|
||||
|
||||
pub fn get_optional_email(&self) -> Option<pii::Email> {
|
||||
Some(self.email.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CustomerRequestWithEmail {
|
||||
pub name: Option<Secret<String>>,
|
||||
pub email: Option<pii::Email>,
|
||||
@ -126,6 +198,7 @@ impl ToEncryptable<EncryptableCustomer, Secret<String>, Secret<String>>
|
||||
}
|
||||
}
|
||||
|
||||
#[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
|
||||
@ -163,11 +236,97 @@ pub struct CustomerResponse {
|
||||
pub default_payment_method_id: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
|
||||
impl CustomerResponse {
|
||||
pub fn get_merchant_reference_id(&self) -> Option<id_type::CustomerId> {
|
||||
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<id_type::CustomerId>,
|
||||
/// The customer's name
|
||||
#[schema(max_length = 255, value_type = Option<String>, example = "Jon Test")]
|
||||
pub name: crypto::OptionalEncryptableName,
|
||||
/// The customer's email address
|
||||
#[schema(value_type = Option<String> ,max_length = 255, example = "JonTest@test.com")]
|
||||
pub email: crypto::OptionalEncryptableEmail,
|
||||
/// The customer's phone number
|
||||
#[schema(value_type = Option<String>,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<String>,
|
||||
/// An arbitrary string that you can attach to a customer object.
|
||||
#[schema(max_length = 255, example = "First Customer")]
|
||||
pub description: Option<String>,
|
||||
/// The default billing address for the customer
|
||||
#[schema(value_type = Option<AddressDetails>)]
|
||||
pub default_billing_address: Option<payments::AddressDetails>,
|
||||
/// The default shipping address for the customer
|
||||
#[schema(value_type = Option<AddressDetails>)]
|
||||
pub default_shipping_address: Option<payments::AddressDetails>,
|
||||
/// 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<Object>,example = json!({ "city": "NY", "unit": "245" }))]
|
||||
pub metadata: Option<pii::SecretSerdeValue>,
|
||||
/// The identifier for the default payment method.
|
||||
#[schema(max_length = 64, example = "pm_djh2837dwduh890123")]
|
||||
pub default_payment_method_id: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "v2", feature = "customer_v2"))]
|
||||
impl CustomerResponse {
|
||||
pub fn get_merchant_reference_id(&self) -> Option<id_type::CustomerId> {
|
||||
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 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
||||
pub struct CustomerDeleteResponse {
|
||||
/// The identifier for the customer object
|
||||
@ -184,6 +343,7 @@ pub struct CustomerDeleteResponse {
|
||||
pub payment_methods_deleted: bool,
|
||||
}
|
||||
|
||||
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
|
||||
fn unknown_merchant() -> String {
|
||||
String::from("merchant_unknown")
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ impl ApiEventMetric for CustomerDeleteResponse {
|
||||
|
||||
impl ApiEventMetric for CustomerRequest {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
self.customer_id
|
||||
self.get_merchant_reference_id()
|
||||
.clone()
|
||||
.map(|customer_id| ApiEventsType::Customer { customer_id })
|
||||
}
|
||||
@ -20,16 +20,16 @@ impl ApiEventMetric for CustomerRequest {
|
||||
|
||||
impl ApiEventMetric for CustomerResponse {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
Some(ApiEventsType::Customer {
|
||||
customer_id: self.customer_id.clone(),
|
||||
})
|
||||
self.get_merchant_reference_id()
|
||||
.clone()
|
||||
.map(|cid| ApiEventsType::Customer { customer_id: cid })
|
||||
}
|
||||
}
|
||||
|
||||
impl ApiEventMetric for CustomerId {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
Some(ApiEventsType::Customer {
|
||||
customer_id: self.customer_id.clone(),
|
||||
customer_id: self.get_merchant_reference_id().clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,10 +11,12 @@ use masking::PeekInterface;
|
||||
use serde::de;
|
||||
use utoipa::{schema, ToSchema};
|
||||
|
||||
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
|
||||
use crate::customers;
|
||||
#[cfg(feature = "payouts")]
|
||||
use crate::payouts;
|
||||
use crate::{
|
||||
admin, customers, enums as api_enums,
|
||||
admin, enums as api_enums,
|
||||
payments::{self, BankCodeResponse},
|
||||
};
|
||||
|
||||
@ -1481,6 +1483,7 @@ impl From<PaymentMethodRecord> for PaymentMethodMigrate {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
|
||||
impl From<PaymentMethodRecord> for customers::CustomerRequest {
|
||||
fn from(record: PaymentMethodRecord) -> Self {
|
||||
Self {
|
||||
@ -1506,3 +1509,30 @@ impl From<PaymentMethodRecord> for customers::CustomerRequest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(feature = "v2")]
|
||||
// impl From<PaymentMethodRecord> for customers::CustomerRequest {
|
||||
// fn from(record: PaymentMethodRecord) -> Self {
|
||||
// Self {
|
||||
// merchant_reference_id: Some(record.customer_id),
|
||||
// name: record.name.unwrap(),
|
||||
// email: record.email.unwrap(),
|
||||
// phone: record.phone,
|
||||
// description: None,
|
||||
// phone_country_code: record.phone_country_code,
|
||||
// default_billing_address: Some(payments::AddressDetails {
|
||||
// city: Some(record.billing_address_city),
|
||||
// country: record.billing_address_country,
|
||||
// line1: Some(record.billing_address_line1),
|
||||
// line2: record.billing_address_line2,
|
||||
// state: Some(record.billing_address_state),
|
||||
// line3: record.billing_address_line3,
|
||||
// zip: Some(record.billing_address_zip),
|
||||
// first_name: Some(record.billing_address_first_name),
|
||||
// last_name: Some(record.billing_address_last_name),
|
||||
// }),
|
||||
// default_shipping_address: None,
|
||||
// metadata: None,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user