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

fix(payment_method): update entity id used for Vault to global customer id (#8380)

Prasunna Soppacf96c826cad0
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index afd3ab1d57..4a23f04cda 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -38,8 +38,6 @@ use error_stack::{report, ResultExt};
use futures::TryStreamExt;
#[cfg(feature = "v1")]
use hyperswitch_domain_models::api::{GenericLinks, GenericLinksData};
-#[cfg(feature = "v2")]
-use hyperswitch_domain_models::mandates::CommonMandateReference;
use hyperswitch_domain_models::payments::{
payment_attempt::PaymentAttempt, PaymentIntent, VaultData,
};
@@ -49,8 +47,6 @@ use hyperswitch_domain_models::{
router_data_v2::flow_common_types::VaultConnectorFlowData,
router_flow_types::ExternalVaultInsertFlow, types::VaultRouterData,
};
-#[cfg(feature = "v2")]
-use masking::ExposeOptionInterface;
use masking::{PeekInterface, Secret};
use router_env::{instrument, tracing};
use time::Duration;
@@ -1102,6 +1098,7 @@ pub async fn network_tokenize_and_vault_the_pmd(
merchant_context,
&network_token_vaulting_data,
None,
+ customer_id,
)
.await
.change_context(errors::NetworkTokenizationError::SaveNetworkTokenFailed)
@@ -1842,11 +1839,16 @@ pub async fn vault_payment_method_internal(
},
)?;
- let mut resp_from_vault =
- vault::add_payment_method_to_vault(state, merchant_context, pmd, existing_vault_id)
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to add payment method in vault")?;
+ let mut resp_from_vault = vault::add_payment_method_to_vault(
+ state,
+ merchant_context,
+ pmd,
+ existing_vault_id,
+ customer_id,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to add payment method in vault")?;
// add fingerprint_id to the response
resp_from_vault.fingerprint_id = Some(fingerprint_id_from_vault);
diff --git a/crates/router/src/core/payment_methods/vault.rs b/crates/router/src/core/payment_methods/vault.rs
index 5d82c8ded4..c59a2f84c3 100644
--- a/crates/router/src/core/payment_methods/vault.rs
+++ b/crates/router/src/core/payment_methods/vault.rs
@@ -1304,9 +1304,10 @@ pub async fn add_payment_method_to_vault(
merchant_context: &domain::MerchantContext,
pmd: &domain::PaymentMethodVaultingData,
existing_vault_id: Option<domain::VaultId>,
+ customer_id: &id_type::GlobalCustomerId,
) -> CustomResult<pm_types::AddVaultResponse, errors::VaultError> {
let payload = pm_types::AddVaultRequest {
- entity_id: merchant_context.get_merchant_account().get_id().to_owned(),
+ entity_id: customer_id.to_owned(),
vault_id: existing_vault_id
.unwrap_or(domain::VaultId::generate(uuid::Uuid::now_v7().to_string())),
data: pmd,
@@ -1335,9 +1336,10 @@ pub async fn retrieve_payment_method_from_vault_internal(
state: &routes::SessionState,
merchant_context: &domain::MerchantContext,
vault_id: &domain::VaultId,
+ customer_id: &id_type::GlobalCustomerId,
) -> CustomResult<pm_types::VaultRetrieveResponse, errors::VaultError> {
let payload = pm_types::VaultRetrieveRequest {
- entity_id: merchant_context.get_merchant_account().get_id().to_owned(),
+ entity_id: customer_id.to_owned(),
vault_id: vault_id.to_owned(),
}
.encode_to_vec()
@@ -1697,10 +1699,15 @@ pub async fn retrieve_payment_method_from_vault(
})
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Missing locker_id for VaultRetrieveRequest")?;
- retrieve_payment_method_from_vault_internal(state, merchant_context, &vault_id)
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to retrieve payment method from vault")
+ retrieve_payment_method_from_vault_internal(
+ state,
+ merchant_context,
+ &vault_id,
+ &pm.customer_id,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to retrieve payment method from vault")
}
}
}
@@ -1710,9 +1717,10 @@ pub async fn delete_payment_method_data_from_vault_internal(
state: &routes::SessionState,
merchant_context: &domain::MerchantContext,
vault_id: domain::VaultId,
+ customer_id: &id_type::GlobalCustomerId,
) -> CustomResult<pm_types::VaultDeleteResponse, errors::VaultError> {
let payload = pm_types::VaultDeleteRequest {
- entity_id: merchant_context.get_merchant_account().get_id().to_owned(),
+ entity_id: customer_id.to_owned(),
vault_id,
}
.encode_to_vec()
@@ -1738,6 +1746,7 @@ pub async fn delete_payment_method_data_from_vault_external(
merchant_account: &domain::MerchantAccount,
merchant_connector_account: domain::MerchantConnectorAccountTypeDetails,
vault_id: domain::VaultId,
+ customer_id: &id_type::GlobalCustomerId,
) -> RouterResult<pm_types::VaultDeleteResponse> {
let connector_vault_id = vault_id.get_string_repr().to_owned();
@@ -1790,21 +1799,21 @@ pub async fn delete_payment_method_data_from_vault_external(
get_vault_response_for_delete_payment_method_data::<ExternalVaultDeleteFlow>(
router_data_resp,
- merchant_account.get_id().to_owned(),
+ customer_id.to_owned(),
)
}
#[cfg(feature = "v2")]
pub fn get_vault_response_for_delete_payment_method_data<F>(
router_data: VaultRouterData<F>,
- merchant_id: id_type::MerchantId,
+ customer_id: id_type::GlobalCustomerId,
) -> RouterResult<pm_types::VaultDeleteResponse> {
match router_data.response {
Ok(response) => match response {
types::VaultResponseData::ExternalVaultDeleteResponse { connector_vault_id } => {
Ok(pm_types::VaultDeleteResponse {
vault_id: domain::VaultId::generate(connector_vault_id), // converted to VaultId type
- entity_id: merchant_id,
+ entity_id: customer_id,
})
}
types::VaultResponseData::ExternalVaultInsertResponse { .. }
@@ -1856,13 +1865,19 @@ pub async fn delete_payment_method_data_from_vault(
merchant_context.get_merchant_account(),
merchant_connector_account,
vault_id.clone(),
+ &pm.customer_id,
)
.await
}
- false => delete_payment_method_data_from_vault_internal(state, merchant_context, vault_id)
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to delete payment method from vault"),
+ false => delete_payment_method_data_from_vault_internal(
+ state,
+ merchant_context,
+ vault_id,
+ &pm.customer_id,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to delete payment method from vault"),
}
}
diff --git a/crates/router/src/core/proxy.rs b/crates/router/src/core/proxy.rs
index afea3fb6ac..b6114a3e1c 100644
--- a/crates/router/src/core/proxy.rs
+++ b/crates/router/src/core/proxy.rs
@@ -16,19 +16,23 @@ pub async fn proxy_core(
req: proxy_api_models::ProxyRequest,
) -> RouterResponse<proxy_api_models::ProxyResponse> {
let req_wrapper = utils::ProxyRequestWrapper(req.clone());
- let vault_id = req_wrapper
- .get_vault_id(
+ let proxy_record = req_wrapper
+ .get_proxy_record(
&state,
merchant_context.get_merchant_key_store(),
merchant_context.get_merchant_account().storage_scheme,
)
.await?;
+ let vault_id = proxy_record.get_vault_id()?;
+ let customer_id = proxy_record.get_customer_id()?;
+
let vault_response =
super::payment_methods::vault::retrieve_payment_method_from_vault_internal(
&state,
&merchant_context,
&vault_id,
+ &customer_id,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
diff --git a/crates/router/src/core/proxy/utils.rs b/crates/router/src/core/proxy/utils.rs
index e2a6c94641..3bf5327aa5 100644
--- a/crates/router/src/core/proxy/utils.rs
+++ b/crates/router/src/core/proxy/utils.rs
@@ -19,14 +19,55 @@ use crate::{
};
pub struct ProxyRequestWrapper(pub proxy_api_models::ProxyRequest);
+pub enum ProxyRecord {
+ PaymentMethodRecord(Box<domain::PaymentMethod>),
+ TokenizationRecord(Box<domain::Tokenization>),
+}
impl ProxyRequestWrapper {
- pub async fn get_vault_id(
+ pub async fn get_proxy_record(
+ &self,
+ state: &SessionState,
+ key_store: &domain::MerchantKeyStore,
+ storage_scheme: common_enums::enums::MerchantStorageScheme,
+ ) -> RouterResult<ProxyRecord> {
+ let token = &self.0.token;
+
+ match self.0.token_type {
+ proxy_api_models::TokenType::PaymentMethodId => {
+ let pm_id = PaymentMethodId {
+ payment_method_id: token.clone(),
+ };
+ let pm_id =
+ id_type::GlobalPaymentMethodId::generate_from_string(pm_id.payment_method_id)
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Unable to generate GlobalPaymentMethodId")?;
+
+ let payment_method_record = state
+ .store
+ .find_payment_method(&((state).into()), key_store, &pm_id, storage_scheme)
+ .await
+ .change_context(errors::ApiErrorResponse::PaymentMethodNotFound)?;
+ Ok(ProxyRecord::PaymentMethodRecord(Box::new(
+ payment_method_record,
+ )))
+ }
+ proxy_api_models::TokenType::TokenizationId => {
+ Err(report!(errors::ApiErrorResponse::NotImplemented {
+ message: NotImplementedMessage::Reason(
+ "Proxy flow using tokenization id".to_string(),
+ ),
+ }))
+ }
+ }
+ }
+
+ pub async fn get_customer_id(
&self,
state: &SessionState,
key_store: &domain::MerchantKeyStore,
storage_scheme: common_enums::enums::MerchantStorageScheme,
- ) -> RouterResult<payment_methods::VaultId> {
+ ) -> RouterResult<id_type::GlobalCustomerId> {
let token = &self.0.token;
match self.0.token_type {
@@ -39,15 +80,12 @@ impl ProxyRequestWrapper {
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unable to generate GlobalPaymentMethodId")?;
- state
+ Ok(state
.store
.find_payment_method(&((state).into()), key_store, &pm_id, storage_scheme)
.await
.change_context(errors::ApiErrorResponse::PaymentMethodNotFound)?
- .locker_id
- .get_required_value("vault_id")
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Locker id not present in Payment Method Entry")
+ .customer_id)
}
proxy_api_models::TokenType::TokenizationId => {
Err(report!(errors::ApiErrorResponse::NotImplemented {
@@ -77,6 +115,35 @@ impl ProxyRequestWrapper {
}
}
+impl ProxyRecord {
+ pub fn get_vault_id(&self) -> RouterResult<payment_methods::VaultId> {
+ match self {
+ Self::PaymentMethodRecord(payment_method) => payment_method
+ .locker_id
+ .clone()
+ .get_required_value("vault_id")
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Locker id not present in Payment Method Entry"),
+ Self::TokenizationRecord(_) => Err(report!(errors::ApiErrorResponse::NotImplemented {
+ message: NotImplementedMessage::Reason(
+ "Proxy flow using tokenization id".to_string(),
+ ),
+ })),
+ }
+ }
+
+ pub fn get_customer_id(&self) -> RouterResult<id_type::GlobalCustomerId> {
+ match self {
+ Self::PaymentMethodRecord(payment_method) => Ok(payment_method.customer_id.clone()),
+ Self::TokenizationRecord(_) => Err(report!(errors::ApiErrorResponse::NotImplemented {
+ message: NotImplementedMessage::Reason(
+ "Proxy flow using tokenization id".to_string(),
+ ),
+ })),
+ }
+ }
+}
+
#[derive(Debug)]
pub struct TokenReference {
pub field: String,
diff --git a/crates/router/src/core/tokenization.rs b/crates/router/src/core/tokenization.rs
index 16df3089ce..87d94dd9ee 100644
--- a/crates/router/src/core/tokenization.rs
+++ b/crates/router/src/core/tokenization.rs
@@ -52,7 +52,7 @@ pub async fn create_vault_token_core(
let customer_id = req.customer_id.clone();
// Create vault request
let payload = pm_types::AddVaultRequest {
- entity_id: merchant_account.get_id().to_owned(),
+ entity_id: req.customer_id.to_owned(),
vault_id: vault_id.clone(),
data: req.token_request.clone(),
ttl: state.conf.locker.ttl_for_storage_in_secs,
@@ -135,7 +135,7 @@ pub async fn get_token_vault_core(
}
let vault_request = pm_types::VaultRetrieveRequest {
- entity_id: tokenization_record.merchant_id.clone(),
+ entity_id: tokenization_record.customer_id.clone(),
vault_id: hyperswitch_domain_models::payment_methods::VaultId::generate(
tokenization_record.locker_id.clone(),
),
diff --git a/crates/router/src/types/domain.rs b/crates/router/src/types/domain.rs
index 63c27e4300..a825853620 100644
--- a/crates/router/src/types/domain.rs
+++ b/crates/router/src/types/domain.rs
@@ -56,6 +56,11 @@ pub mod vault {
pub use hyperswitch_domain_models::vault::*;
}
+#[cfg(feature = "v2")]
+pub mod tokenization {
+ pub use hyperswitch_domain_models::tokenization::*;
+}
+
mod routing {
pub use hyperswitch_domain_models::routing::*;
}
@@ -78,6 +83,8 @@ pub use network_tokenization::*;
pub use payment_method_data::*;
pub use payment_methods::*;
pub use routing::*;
+#[cfg(feature = "v2")]
+pub use tokenization::*;
#[cfg(feature = "olap")]
pub use user::*;
pub use user_key_store::*;
diff --git a/crates/router/src/types/payment_methods.rs b/crates/router/src/types/payment_methods.rs
index 0d205f33e8..88d60e7b3d 100644
--- a/crates/router/src/types/payment_methods.rs
+++ b/crates/router/src/types/payment_methods.rs
@@ -45,7 +45,7 @@ pub struct VaultFingerprintResponse {
#[cfg(any(feature = "v2", feature = "tokenization_v2"))]
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct AddVaultRequest<D> {
- pub entity_id: id_type::MerchantId,
+ pub entity_id: id_type::GlobalCustomerId,
pub vault_id: domain::VaultId,
pub data: D,
pub ttl: i64,
@@ -54,7 +54,7 @@ pub struct AddVaultRequest<D> {
#[cfg(feature = "v2")]
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct AddVaultResponse {
- pub entity_id: Option<id_type::MerchantId>,
+ pub entity_id: Option<id_type::GlobalCustomerId>,
pub vault_id: domain::VaultId,
pub fingerprint_id: Option<String>,
}
@@ -131,7 +131,7 @@ pub struct SavedPMLPaymentsInfo {
#[cfg(feature = "v2")]
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct VaultRetrieveRequest {
- pub entity_id: id_type::MerchantId,
+ pub entity_id: id_type::GlobalCustomerId,
pub vault_id: domain::VaultId,
}
@@ -144,14 +144,14 @@ pub struct VaultRetrieveResponse {
#[cfg(feature = "v2")]
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct VaultDeleteRequest {
- pub entity_id: id_type::MerchantId,
+ pub entity_id: id_type::GlobalCustomerId,
pub vault_id: domain::VaultId,
}
#[cfg(feature = "v2")]
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct VaultDeleteResponse {
- pub entity_id: id_type::MerchantId,
+ pub entity_id: id_type::GlobalCustomerId,
pub vault_id: domain::VaultId,
}