From 90e43929a0c05e39feac4f13d75b2eea60b858a0 Mon Sep 17 00:00:00 2001 From: BallaNitesh <126162378+BallaNitesh@users.noreply.github.com> Date: Fri, 22 Sep 2023 14:45:33 +0530 Subject: [PATCH] refactor(core): eliminate business profile database queries in payments confirm flow (#2190) --- crates/router/src/core/admin.rs | 1 + .../router/src/core/payment_methods/cards.rs | 1 + crates/router/src/core/payments.rs | 18 ++++++++--------- .../payments/operations/payment_create.rs | 1 + .../payments/operations/payment_session.rs | 1 + crates/router/src/core/payouts.rs | 1 + crates/router/src/core/utils.rs | 20 +++++++++++++------ crates/router/src/utils.rs | 1 + 8 files changed, 28 insertions(+), 16 deletions(-) diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs index db1af73e62..a6847968c2 100644 --- a/crates/router/src/core/admin.rs +++ b/crates/router/src/core/admin.rs @@ -636,6 +636,7 @@ pub async fn create_payment_connector( &merchant_account, req.profile_id.as_ref(), &*state.store, + true, ) .await?; diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs index ee790c9511..7cc4eacbe3 100644 --- a/crates/router/src/core/payment_methods/cards.rs +++ b/crates/router/src/core/payment_methods/cards.rs @@ -879,6 +879,7 @@ pub async fn list_payment_methods( &merchant_account, payment_intent.profile_id.as_ref(), db, + false, ) .await .attach_printable("Could not find profile id from business details") diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs index 4f8fa4a7b5..d2bd5beae7 100644 --- a/crates/router/src/core/payments.rs +++ b/crates/router/src/core/payments.rs @@ -591,6 +591,7 @@ where payment_data, connector_name, key_store, + false, ) .await?; @@ -610,6 +611,7 @@ where customer, merchant_account, key_store, + &merchant_connector_account, payment_data, ) .await?; @@ -799,6 +801,7 @@ where &mut payment_data, &session_connector_data.connector.connector_name.to_string(), key_store, + false, ) .await?; @@ -870,6 +873,7 @@ pub async fn call_create_connector_customer_if_required( customer: &Option, merchant_account: &domain::MerchantAccount, key_store: &domain::MerchantKeyStore, + merchant_connector_account: &helpers::MerchantConnectorAccountType, payment_data: &mut PaymentData, ) -> RouterResult> where @@ -888,15 +892,6 @@ where match connector_name { Some(connector_name) => { - let merchant_connector_account = construct_profile_id_and_get_mca( - state, - merchant_account, - payment_data, - &connector_name, - key_store, - ) - .await?; - let connector = api::ConnectorData::get_connector_by_name( &state.conf.connectors, &connector_name, @@ -919,6 +914,7 @@ where merchant_account, payment_data.payment_intent.profile_id.as_ref(), &*state.store, + false, ) .await .attach_printable("Could not find profile id from business details")?; @@ -943,7 +939,7 @@ where merchant_account, key_store, customer, - &merchant_connector_account, + merchant_connector_account, ) .await?; @@ -1057,6 +1053,7 @@ pub async fn construct_profile_id_and_get_mca<'a, F>( payment_data: &mut PaymentData, connector_id: &str, key_store: &domain::MerchantKeyStore, + should_validate: bool, ) -> RouterResult where F: Clone, @@ -1067,6 +1064,7 @@ where merchant_account, payment_data.payment_intent.profile_id.as_ref(), &*state.store, + should_validate, ) .await .change_context(errors::ApiErrorResponse::InternalServerError) diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs index 831090c14c..5ebd0995f7 100644 --- a/crates/router/src/core/payments/operations/payment_create.rs +++ b/crates/router/src/core/payments/operations/payment_create.rs @@ -615,6 +615,7 @@ impl PaymentCreate { merchant_account, request.profile_id.as_ref(), &*state.store, + true, ) .await?; diff --git a/crates/router/src/core/payments/operations/payment_session.rs b/crates/router/src/core/payments/operations/payment_session.rs index fb65a5507d..938441962b 100644 --- a/crates/router/src/core/payments/operations/payment_session.rs +++ b/crates/router/src/core/payments/operations/payment_session.rs @@ -340,6 +340,7 @@ where merchant_account, payment_intent.profile_id.as_ref(), &*state.store, + false, ) .await .attach_printable("Could not find profile id from business details")?; diff --git a/crates/router/src/core/payouts.rs b/crates/router/src/core/payouts.rs index a487700a36..fcb7fc571d 100644 --- a/crates/router/src/core/payouts.rs +++ b/crates/router/src/core/payouts.rs @@ -565,6 +565,7 @@ pub async fn create_recipient( merchant_account, payout_data.payout_attempt.profile_id.as_ref(), &*state.store, + false, ) .await?; diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rs index 14a3b77714..bf34c13296 100644 --- a/crates/router/src/core/utils.rs +++ b/crates/router/src/core/utils.rs @@ -211,6 +211,7 @@ pub async fn construct_refund_router_data<'a, F>( merchant_account, payment_intent.profile_id.as_ref(), &*state.store, + false, ) .await .change_context(errors::ApiErrorResponse::InternalServerError) @@ -484,6 +485,7 @@ pub async fn construct_accept_dispute_router_data<'a>( merchant_account, payment_intent.profile_id.as_ref(), &*state.store, + false, ) .await .change_context(errors::ApiErrorResponse::InternalServerError) @@ -570,6 +572,7 @@ pub async fn construct_submit_evidence_router_data<'a>( merchant_account, payment_intent.profile_id.as_ref(), &*state.store, + false, ) .await .change_context(errors::ApiErrorResponse::InternalServerError) @@ -654,6 +657,7 @@ pub async fn construct_upload_file_router_data<'a>( merchant_account, payment_intent.profile_id.as_ref(), &*state.store, + false, ) .await .change_context(errors::ApiErrorResponse::InternalServerError) @@ -741,6 +745,7 @@ pub async fn construct_defend_dispute_router_data<'a>( merchant_account, payment_intent.profile_id.as_ref(), &*state.store, + false, ) .await .change_context(errors::ApiErrorResponse::InternalServerError) @@ -992,16 +997,19 @@ pub async fn get_profile_id_from_business_details( merchant_account: &domain::MerchantAccount, request_profile_id: Option<&String>, db: &dyn StorageInterface, + should_validate: bool, ) -> RouterResult { match request_profile_id.or(merchant_account.default_profile.as_ref()) { Some(profile_id) => { // Check whether this business profile belongs to the merchant - let _ = validate_and_get_business_profile( - db, - Some(profile_id), - &merchant_account.merchant_id, - ) - .await?; + if should_validate { + let _ = validate_and_get_business_profile( + db, + Some(profile_id), + &merchant_account.merchant_id, + ) + .await?; + } Ok(profile_id.clone()) } None => match business_country.zip(business_label) { diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rs index 27c8acb1fa..72fce16bdd 100644 --- a/crates/router/src/utils.rs +++ b/crates/router/src/utils.rs @@ -320,6 +320,7 @@ pub async fn get_profile_id_using_object_reference_id( merchant_account, payment_intent.profile_id.as_ref(), db, + false, ) .await .change_context(errors::ApiErrorResponse::InternalServerError)