Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(payment-methods): add modular customer pml transformer (#11064)
Gaurav Rawat42ea90cb88fe
diff --git a/crates/payment_methods/src/client.rs b/crates/payment_methods/src/client.rsindex 881af3477b..c111e02df9 100644--- a/crates/payment_methods/src/client.rs+++ b/crates/payment_methods/src/client.rs@@ -5,6 +5,9 @@pub mod create;/// Delete payment method flow.pub mod delete;+/// List customer payment methods flow.+#[cfg(feature = "v1")]+pub mod list;#[cfg(feature = "v1")]/// Retrieve payment method flow.pub mod retrieve;diff --git a/crates/payment_methods/src/client/list.rs b/crates/payment_methods/src/client/list.rsnew file mode 100644index 0000000000..615f452b7e--- /dev/null+++ b/crates/payment_methods/src/client/list.rs@@ -0,0 +1,159 @@+use api_models::payment_methods::{+ CustomerPaymentMethod,+ CustomerPaymentMethodsListResponse as ListCustomerPaymentMethodsV1Response,+ PaymentMethodListRequest,+};+use common_utils::{id_type, request::Method};+use hyperswitch_interfaces::micro_service::{MicroserviceClientError, MicroserviceClientErrorKind};++use crate::types::{+ ModularListCustomerPaymentMethodsRequest, ModularListCustomerPaymentMethodsResponse,+ PaymentMethodResponseData,+};++/// V1-facing retrieve flow type.+#[derive(Debug)]+pub struct ListCustomerPaymentMethods;++/// V1-facing retrieve request payload.+#[derive(Debug)]+pub struct ListCustomerPaymentMethodsV1Request {+ pub customer_id: id_type::CustomerId,+ pub query_params: PaymentMethodListRequest,+}++impl TryFrom<&ListCustomerPaymentMethodsV1Request> for ModularListCustomerPaymentMethodsRequest {+ type Error = MicroserviceClientError;++ fn try_from(_value: &ListCustomerPaymentMethodsV1Request) -> Result<Self, Self::Error> {+ Ok(Self)+ }+}++impl TryFrom<ModularListCustomerPaymentMethodsResponse> for ListCustomerPaymentMethodsV1Response {+ type Error = MicroserviceClientError;++ fn try_from(+ v2_response: ModularListCustomerPaymentMethodsResponse,+ ) -> Result<Self, Self::Error> {+ let customer_payment_methods = v2_response+ .customer_payment_methods+ .into_iter()+ .map(|pm| CustomerPaymentMethod {+ payment_token: pm.id.clone(),+ payment_method_id: pm.id,+ customer_id: pm.customer_id,+ payment_method: pm.payment_method_type,+ payment_method_type: Some(pm.payment_method_subtype),+ payment_method_issuer: None,+ payment_method_issuer_code: None,+ recurring_enabled: pm.recurring_enabled,+ installment_payment_enabled: None,+ payment_experience: None,+ card: pm.payment_method_data.map(|data| match data {+ PaymentMethodResponseData::Card(card_detail) => card_detail,+ }),+ metadata: None,+ created: Some(pm.created),+ bank_transfer: None,+ bank: pm.bank,+ surcharge_details: None,+ requires_cvv: pm.requires_cvv,+ last_used_at: Some(pm.last_used_at),+ default_payment_method_set: pm.is_default,+ billing: pm.billing,+ })+ .collect();++ Ok(Self {+ customer_payment_methods,+ is_guest_customer: None,+ })+ }+}++impl ListCustomerPaymentMethods {+ fn validate_request(+ &self,+ request: &ListCustomerPaymentMethodsV1Request,+ ) -> Result<(), MicroserviceClientError> {+ if request.customer_id.get_string_repr().trim().is_empty() {+ return Err(MicroserviceClientError {+ operation: std::any::type_name::<Self>().to_string(),+ kind: MicroserviceClientErrorKind::InvalidRequest(+ "Customer ID cannot be empty".to_string(),+ ),+ });+ }+ Ok(())+ }++ fn build_path_params(+ &self,+ request: &ListCustomerPaymentMethodsV1Request,+ ) -> Vec<(&'static str, String)> {+ vec![(+ "customer_id",+ request.customer_id.get_string_repr().to_string(),+ )]+ }++ fn query_params(+ &self,+ request: &ListCustomerPaymentMethodsV1Request,+ ) -> Vec<(&'static str, String)> {+ let mut params = Vec::new();++ let qp = &request.query_params;++ if let Some(secret) = &qp.client_secret {+ params.push(("client_secret", secret.clone()));+ }++ if let Some(amount) = qp.amount {+ params.push(("amount", amount.to_string()));+ }++ if let Some(recurring) = qp.recurring_enabled {+ params.push(("recurring_enabled", recurring.to_string()));+ }++ if let Some(limit) = qp.limit {+ params.push(("limit", limit.to_string()));+ }++ if let Some(countries) = &qp.accepted_countries {+ if let Ok(serialized) = serde_json::to_string(countries) {+ params.push(("accepted_countries", serialized));+ }+ }++ if let Some(currencies) = &qp.accepted_currencies {+ if let Ok(serialized) = serde_json::to_string(currencies) {+ params.push(("accepted_currencies", serialized));+ }+ }++ if let Some(networks) = &qp.card_networks {+ if let Ok(serialized) = serde_json::to_string(networks) {+ params.push(("card_networks", serialized));+ }+ }++ params+ }+}++hyperswitch_interfaces::impl_microservice_flow!(+ ListCustomerPaymentMethods,+ method = Method::Get,+ path = "/v2/customers/{customer_id}/saved-payment-methods",+ v1_request = ListCustomerPaymentMethodsV1Request,+ v2_request = ModularListCustomerPaymentMethodsRequest,+ v2_response = ModularListCustomerPaymentMethodsResponse,+ v1_response = ListCustomerPaymentMethodsV1Response,+ client = crate::client::PaymentMethodClient<'_>,+ path_params = ListCustomerPaymentMethods::build_path_params,+ query_params = ListCustomerPaymentMethods::query_params,+ validate = ListCustomerPaymentMethods::validate_request+);diff --git a/crates/payment_methods/src/types.rs b/crates/payment_methods/src/types.rsindex da8f6c5fe6..e2ea74a9a8 100644--- a/crates/payment_methods/src/types.rs+++ b/crates/payment_methods/src/types.rs@@ -4,6 +4,42 @@ use common_utils::{id_type, pii};use hyperswitch_domain_models::payment_method_data::NetworkTokenDetailsPaymentMethod;use serde::Deserialize;use time::PrimitiveDateTime;+#[derive(Clone, Debug)]+pub struct ModularListCustomerPaymentMethodsRequest;++/// Dummy modular service response payload.+#[derive(Debug, Deserialize)]+// TODO: replace dummy response types with real v1/modular models.+pub struct ModularListCustomerPaymentMethodsResponse {+ pub customer_payment_methods: Vec<PaymentMethodResponseItem>,+}++#[derive(Debug, Deserialize)]+pub struct PaymentMethodResponseItem {+ pub id: String,+ pub customer_id: id_type::CustomerId,+ pub payment_method_type: PaymentMethod,+ pub payment_method_subtype: PaymentMethodType,+ pub recurring_enabled: Option<bool>,+ pub payment_method_data: Option<PaymentMethodResponseData>,+ pub bank: Option<api_models::payment_methods::MaskedBankDetails>,+ #[serde(with = "common_utils::custom_serde::iso8601")]+ pub created: PrimitiveDateTime,+ pub requires_cvv: bool,+ #[serde(with = "common_utils::custom_serde::iso8601")]+ pub last_used_at: PrimitiveDateTime,+ pub is_default: bool,+ pub billing: Option<api_models::payments::Address>,+ pub network_tokenization: Option<NetworkTokenResponse>,+ pub psp_tokenization_enabled: bool,+}+/// V2 PaymentMethodResponseData enum+#[derive(Clone, Debug, Deserialize)]+#[serde(deny_unknown_fields)]+#[serde(rename_all = "snake_case")]+pub enum PaymentMethodResponseData {+ Card(CardDetailFromLocker),+}/// V2 modular service request payload.#[derive(Clone, Debug)]@@ -57,12 +93,3 @@ pub struct CardCVCTokenStorageDetails {#[serde(default, with = "common_utils::custom_serde::iso8601::option")]pub expires_at: Option<PrimitiveDateTime>,}--/// V2 PaymentMethodResponseData enum-#[derive(Clone, Debug, Deserialize)]-#[serde(deny_unknown_fields)]-#[serde(rename_all = "snake_case")]-#[serde(rename = "payment_method_data")]-pub enum PaymentMethodResponseData {- Card(CardDetailFromLocker),-}