Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions crates/router/src/core/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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;
Expand Down Expand Up @@ -1100,6 +1096,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)
Expand Down Expand Up @@ -1841,11 +1838,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);
Expand Down
43 changes: 29 additions & 14 deletions crates/router/src/core/payment_methods/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1592,10 +1594,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")
}
}
}
Expand All @@ -1605,9 +1612,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()
Expand All @@ -1633,6 +1641,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();

Expand Down Expand Up @@ -1685,21 +1694,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 { .. }
Expand Down Expand Up @@ -1751,13 +1760,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"),
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/router/src/core/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ pub async fn proxy_core(
)
.await?;

let customer_id = req_wrapper
.get_customer_id(
&state,
merchant_context.get_merchant_key_store(),
merchant_context.get_merchant_account().storage_scheme,
)
.await?;
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)
Expand Down
35 changes: 35 additions & 0 deletions crates/router/src/core/proxy/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,41 @@ impl ProxyRequestWrapper {
}
}

pub async fn get_customer_id(
&self,
state: &SessionState,
key_store: &domain::MerchantKeyStore,
storage_scheme: common_enums::enums::MerchantStorageScheme,
) -> RouterResult<id_type::GlobalCustomerId> {
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")?;

Ok(state
.store
.find_payment_method(&((state).into()), key_store, &pm_id, storage_scheme)
.await
.change_context(errors::ApiErrorResponse::PaymentMethodNotFound)?
.customer_id)
}
proxy_api_models::TokenType::TokenizationId => {
Err(report!(errors::ApiErrorResponse::NotImplemented {
message: NotImplementedMessage::Reason(
"Proxy flow using tokenization id".to_string(),
),
}))
}
}
}

pub fn get_headers(&self) -> Vec<(String, masking::Maskable<String>)> {
self.0
.headers
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/tokenization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
),
Expand Down
10 changes: 5 additions & 5 deletions crates/router/src/types/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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,
Expand All @@ -53,7 +53,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>,
}
Expand Down Expand Up @@ -130,7 +130,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,
}

Expand All @@ -143,14 +143,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,
}

Expand Down
Loading