Skip to content

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

feat(payment-methods): add modular payment method retrieve transformer (#10994)

Gaurav Rawatd2457784e333
diff --git a/Cargo.lock b/Cargo.lock
index 218c998581..b29bbe84f6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6127,6 +6127,7 @@ dependencies = [
"serde_json",
"storage_impl",
"thiserror 1.0.69",
+ "time",
"url",
]
diff --git a/crates/payment_methods/Cargo.toml b/crates/payment_methods/Cargo.toml
index f147af26f0..76996d8a7b 100644
--- a/crates/payment_methods/Cargo.toml
+++ b/crates/payment_methods/Cargo.toml
@@ -19,6 +19,7 @@ serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
thiserror = "1.0.69"
url = { version = "2.5.4", features = ["serde"] }
+time = "0.3.41"
api_models = { version = "0.1.0", path = "../api_models", features = ["errors", "control_center_theme"] }
cards = { version = "0.1.0", path = "../cards" }
diff --git a/crates/payment_methods/src/client.rs b/crates/payment_methods/src/client.rs
index 975dbf6c9d..aa9bb7e243 100644
--- a/crates/payment_methods/src/client.rs
+++ b/crates/payment_methods/src/client.rs
@@ -4,6 +4,7 @@
pub mod create;
/// Delete payment method flow.
pub mod delete;
+#[cfg(feature = "v1")]
/// Retrieve payment method flow.
pub mod retrieve;
/// Update payment method flow.
@@ -13,6 +14,7 @@ use common_utils::request::Headers;
pub use create::{CreatePaymentMethod, CreatePaymentMethodV1Request};
pub use delete::{DeletePaymentMethod, DeletePaymentMethodV1Request};
use hyperswitch_interfaces::micro_service::MicroserviceClient;
+#[cfg(feature = "v1")]
pub use retrieve::{RetrievePaymentMethod, RetrievePaymentMethodV1Request};
use router_env::RequestIdentifier;
pub use update::{UpdatePaymentMethod, UpdatePaymentMethodV1Request};
diff --git a/crates/payment_methods/src/client/retrieve.rs b/crates/payment_methods/src/client/retrieve.rs
index fb9a3862aa..82aa5ded9b 100644
--- a/crates/payment_methods/src/client/retrieve.rs
+++ b/crates/payment_methods/src/client/retrieve.rs
@@ -1,12 +1,14 @@
-//! Retrieve payment method flow types and dummy models.
+//! Retrieve payment method flow types and models.
-use api_models::payment_methods::PaymentMethodId;
+use api_models::payment_methods::{
+ PaymentMethodId, PaymentMethodResponse as RetrievePaymentMethodResponse,
+};
use common_utils::request::Method;
use hyperswitch_interfaces::micro_service::{MicroserviceClientError, MicroserviceClientErrorKind};
-use serde::Deserialize;
-
-const DUMMY_PM_ID: &str = "pm_dummy";
+use crate::types::{
+ ModularPMRetrieveResponse, ModularPMRetrieveResquest, PaymentMethodResponseData,
+};
/// V1-facing retrieve flow type.
#[derive(Debug)]
pub struct RetrievePaymentMethod;
@@ -14,53 +16,47 @@ pub struct RetrievePaymentMethod;
/// V1-facing retrieve request payload.
#[derive(Debug)]
pub struct RetrievePaymentMethodV1Request {
- /// Identifier for the payment method to fetch.
- pub payment_method_id: PaymentMethodId,
-}
-
-/// Dummy modular service request payload.
-#[derive(Clone, Debug)]
-// TODO: replace dummy request types with real v1/modular models.
-pub struct RetrievePaymentMethodV2Request {
- /// Identifier for the payment method to fetch.
pub payment_method_id: PaymentMethodId,
}
-/// Dummy modular service response payload.
-#[derive(Clone, Debug, Deserialize)]
-// TODO: replace dummy response types with real v1/modular models.
-pub struct RetrievePaymentMethodV2Response {
- /// Dummy identifier returned by the modular service.
- pub id: String,
-}
-
-/// V1-facing retrieve response (dummy for now).
-#[derive(Clone, Debug)]
-// TODO: replace dummy response types with real v1/modular models.
-pub struct RetrievePaymentMethodResponse {
- /// V1 payment method identifier.
- pub payment_method_id: String,
- /// Dummy delete marker (unused).
- pub deleted: Option<bool>,
-}
-
-impl TryFrom<&RetrievePaymentMethodV1Request> for RetrievePaymentMethodV2Request {
+impl TryFrom<&RetrievePaymentMethodV1Request> for ModularPMRetrieveResquest {
type Error = MicroserviceClientError;
- fn try_from(value: &RetrievePaymentMethodV1Request) -> Result<Self, Self::Error> {
- Ok(Self {
- payment_method_id: value.payment_method_id.clone(),
- })
+ fn try_from(_value: &RetrievePaymentMethodV1Request) -> Result<Self, Self::Error> {
+ Ok(Self)
}
}
-impl TryFrom<RetrievePaymentMethodV2Response> for RetrievePaymentMethodResponse {
+impl TryFrom<ModularPMRetrieveResponse> for RetrievePaymentMethodResponse {
type Error = MicroserviceClientError;
- fn try_from(_: RetrievePaymentMethodV2Response) -> Result<Self, Self::Error> {
+ fn try_from(v2_resp: ModularPMRetrieveResponse) -> Result<Self, Self::Error> {
+ // Extract payment_method_id from GlobalPaymentMethodId
+ let payment_method_id = v2_resp.id.clone();
+
+ // Convert GlobalCustomerId to CustomerId
+ let customer_id = v2_resp.customer_id;
+
+ // Convert card details from V2 to V1 format
+ let card = v2_resp.payment_method_data.map(|data| match data {
+ PaymentMethodResponseData::Card(card_detail) => card_detail,
+ });
+
Ok(Self {
- payment_method_id: DUMMY_PM_ID.to_string(),
- deleted: None,
+ payment_method_id,
+ merchant_id: v2_resp.merchant_id,
+ customer_id,
+ payment_method: v2_resp.payment_method_type,
+ payment_method_type: v2_resp.payment_method_subtype,
+ card,
+ recurring_enabled: v2_resp.recurring_enabled,
+ created: v2_resp.created,
+ last_used_at: v2_resp.last_used_at,
+ installment_payment_enabled: None,
+ payment_experience: None,
+ metadata: None,
+ bank_transfer: None,
+ client_secret: None,
})
}
}
@@ -99,8 +95,8 @@ hyperswitch_interfaces::impl_microservice_flow!(
method = Method::Get,
path = "/v2/payment-methods/{id}",
v1_request = RetrievePaymentMethodV1Request,
- v2_request = RetrievePaymentMethodV2Request,
- v2_response = RetrievePaymentMethodV2Response,
+ v2_request = ModularPMRetrieveResquest,
+ v2_response = ModularPMRetrieveResponse,
v1_response = RetrievePaymentMethodResponse,
client = crate::client::PaymentMethodClient<'_>,
path_params = RetrievePaymentMethod::build_path_params,
diff --git a/crates/payment_methods/src/lib.rs b/crates/payment_methods/src/lib.rs
index 35e7c75e44..d373a56601 100644
--- a/crates/payment_methods/src/lib.rs
+++ b/crates/payment_methods/src/lib.rs
@@ -4,3 +4,5 @@ pub mod controller;
pub mod core;
pub mod helpers;
pub mod state;
+#[cfg(feature = "v1")]
+pub mod types;
diff --git a/crates/payment_methods/src/types.rs b/crates/payment_methods/src/types.rs
new file mode 100644
index 0000000000..da8f6c5fe6
--- /dev/null
+++ b/crates/payment_methods/src/types.rs
@@ -0,0 +1,68 @@
+use api_models::payment_methods::CardDetailFromLocker;
+use common_enums::{PaymentMethod, PaymentMethodType};
+use common_utils::{id_type, pii};
+use hyperswitch_domain_models::payment_method_data::NetworkTokenDetailsPaymentMethod;
+use serde::Deserialize;
+use time::PrimitiveDateTime;
+
+/// V2 modular service request payload.
+#[derive(Clone, Debug)]
+pub struct ModularPMRetrieveResquest;
+
+/// V2 PaymentMethodResponse as returned by the V2 API.
+/// This is a copy of the V2 PaymentMethodResponse struct from api_models for use in V1-only builds.
+#[derive(Clone, Debug, Deserialize)]
+pub struct ModularPMRetrieveResponse {
+ pub id: String,
+ pub merchant_id: id_type::MerchantId,
+ pub customer_id: Option<id_type::CustomerId>,
+ pub payment_method_type: Option<PaymentMethod>,
+ pub payment_method_subtype: Option<PaymentMethodType>,
+ pub recurring_enabled: Option<bool>,
+ #[serde(default, with = "common_utils::custom_serde::iso8601::option")]
+ pub created: Option<PrimitiveDateTime>,
+ #[serde(default, with = "common_utils::custom_serde::iso8601::option")]
+ pub last_used_at: Option<PrimitiveDateTime>,
+ pub payment_method_data: Option<PaymentMethodResponseData>,
+ pub connector_tokens: Option<Vec<ConnectorTokenDetails>>,
+ pub network_token: Option<NetworkTokenResponse>,
+ pub storage_type: Option<common_enums::StorageType>,
+ pub card_cvc_token_storage: Option<CardCVCTokenStorageDetails>,
+}
+
+/// V2 ConnectorTokenDetails (for deserialization, ignored in transformation)
+#[derive(Clone, Debug, Deserialize)]
+pub struct ConnectorTokenDetails {
+ pub connector_id: id_type::MerchantConnectorAccountId,
+ pub token_type: common_enums::TokenizationType,
+ pub status: common_enums::ConnectorTokenStatus,
+ pub connector_token_request_reference_id: Option<String>,
+ pub original_payment_authorized_amount: Option<common_utils::types::MinorUnit>,
+ pub original_payment_authorized_currency: Option<common_enums::Currency>,
+ pub metadata: Option<pii::SecretSerdeValue>,
+ pub token: masking::Secret<String>,
+}
+
+/// V2 NetworkTokenResponse (for deserialization, ignored in transformation)
+#[derive(Clone, Debug, Deserialize)]
+pub struct NetworkTokenResponse {
+ pub payment_method_data: NetworkTokenDetailsPaymentMethod,
+}
+
+/// V2 CardCVCTokenStorageDetails (for deserialization, ignored in transformation)
+#[derive(Clone, Debug, Deserialize)]
+pub struct CardCVCTokenStorageDetails {
+ pub is_stored: bool,
+
+ #[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),
+}