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

refactor(payments): optimize get_trackers in create flow (#14455)

Kashifea5d1d395cf8
diff --git a/crates/router/src/core/blocklist/batch.rs b/crates/router/src/core/blocklist/batch.rs
index 5ceae987b0..07c957b90f 100644
--- a/crates/router/src/core/blocklist/batch.rs
+++ b/crates/router/src/core/blocklist/batch.rs
@@ -344,15 +344,16 @@ pub async fn initiate_batch_blocklist_upload(
file_name: Option<String>,
) -> RouterResult<api_blocklist::BatchBlocklistUploadResponse> {
let processor_merchant_id = platform.get_processor().get_account().get_id();
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let profile_id = core_utils::get_profile_from_business_details(
None,
None,
platform.get_processor(),
profile_id.as_ref(),
&*state.store,
- true,
)
- .await?;
+ .await?
+ .get_id()
+ .to_owned();
let created_by = platform
.get_initiator()
.and_then(|initiator| initiator.to_created_by())
diff --git a/crates/router/src/core/blocklist/export.rs b/crates/router/src/core/blocklist/export.rs
index 610e320a4f..7083914a43 100644
--- a/crates/router/src/core/blocklist/export.rs
+++ b/crates/router/src/core/blocklist/export.rs
@@ -76,15 +76,16 @@ pub async fn initiate_blocklist_export(
profile_id: Option<id_type::ProfileId>,
) -> RouterResult<api_blocklist::BlocklistExportResponse> {
let processor_merchant_id = platform.get_processor().get_account().get_id();
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let profile_id = core_utils::get_profile_from_business_details(
None,
None,
platform.get_processor(),
profile_id.as_ref(),
&*state.store,
- true,
)
- .await?;
+ .await?
+ .get_id()
+ .to_owned();
let export_id = common_utils::generate_id(crate::consts::ID_LENGTH, "blkexp");
let now = date_time::now();
diff --git a/crates/router/src/core/blocklist/utils.rs b/crates/router/src/core/blocklist/utils.rs
index 77fffc574e..28c8b3f414 100644
--- a/crates/router/src/core/blocklist/utils.rs
+++ b/crates/router/src/core/blocklist/utils.rs
@@ -30,15 +30,16 @@ pub async fn delete_entry_from_blocklist(
request: api_blocklist::DeleteFromBlocklistRequest,
) -> RouterResult<api_blocklist::DeleteFromBlocklistResponse> {
let processor_merchant_id = processor.get_account().get_id();
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let profile_id = core_utils::get_profile_from_business_details(
None,
None,
processor,
profile_id.as_ref(),
&*state.store,
- true,
)
- .await?;
+ .await?
+ .get_id()
+ .to_owned();
let blocklist_entry = match request {
#[allow(deprecated)]
@@ -185,15 +186,16 @@ pub async fn get_blocklist_count(
query: api_blocklist::BlocklistCountQuery,
) -> RouterResult<api_blocklist::BlocklistCountResponse> {
let processor_merchant_id = processor.get_account().get_id();
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let profile_id = core_utils::get_profile_from_business_details(
None,
None,
processor,
profile_id.as_ref(),
&*state.store,
- true,
)
- .await?;
+ .await?
+ .get_id()
+ .to_owned();
let (total_count, counts_by_length) = match query.data_kind {
// Fingerprints are fixed-width hashes, so there is no breakdown worth grouping for.
@@ -261,15 +263,16 @@ pub async fn lookup_blocklist_entry(
query: api_blocklist::BlocklistLookupQuery,
) -> RouterResult<api_blocklist::BlocklistLookupResponse> {
let processor_merchant_id = processor.get_account().get_id();
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let profile_id = core_utils::get_profile_from_business_details(
None,
None,
processor,
profile_id.as_ref(),
&*state.store,
- true,
)
- .await?;
+ .await?
+ .get_id()
+ .to_owned();
let result = state
.store
@@ -303,15 +306,16 @@ pub async fn insert_entry_into_blocklist(
to_block: api_blocklist::AddToBlocklistRequest,
) -> RouterResult<api_blocklist::AddToBlocklistResponse> {
let processor_merchant_id = platform.get_processor().get_account().get_id();
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let profile_id = core_utils::get_profile_from_business_details(
None,
None,
platform.get_processor(),
profile_id.as_ref(),
&*state.store,
- true,
)
- .await?;
+ .await?
+ .get_id()
+ .to_owned();
let blocklist_entry = match &to_block {
#[allow(deprecated)]
diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs
index cd7c6825a2..c38b6bbc96 100644
--- a/crates/router/src/core/payments/operations/payment_create.rs
+++ b/crates/router/src/core/payments/operations/payment_create.rs
@@ -105,48 +105,18 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
platform.get_processor(),
)?;
- // If profile id is not passed, get it from the business_country and business_label
- #[cfg(feature = "v1")]
- let profile_id = core_utils::get_profile_id_from_business_details(
+ // If profile id is not passed, get it from the business_country and business_label.
+ // The lookup is scoped to the merchant, so this also validates that the profile belongs to it.
+ let business_profile = core_utils::get_profile_from_business_details(
request.business_country,
request.business_label.as_ref(),
platform.get_processor(),
request.profile_id.as_ref(),
&*state.store,
- true,
)
.await?;
+ let profile_id = business_profile.get_id().to_owned();
- // Profile id will be mandatory in v2 in the request / headers
- #[cfg(feature = "v2")]
- let profile_id = request
- .profile_id
- .clone()
- .get_required_value("profile_id")
- .attach_printable("Profile id is a mandatory parameter")?;
-
- // TODO: eliminate a redundant db call to fetch the business profile
- // Validate whether profile_id passed in request is valid and is linked to the merchant
- let business_profile = if let Some(business_profile) =
- core_utils::validate_and_get_business_profile(
- db,
- platform.get_processor(),
- Some(&profile_id),
- )
- .await?
- {
- business_profile
- } else {
- platform_wrapper::business_profile::find_business_profile_by_profile_id(
- state.store.as_ref(),
- platform.get_processor(),
- &profile_id,
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::ProfileNotFound {
- id: profile_id.get_string_repr().to_owned(),
- })?
- };
let customer_acceptance = request.customer_acceptance.clone();
let recurring_details = request.recurring_details.clone();
@@ -196,30 +166,6 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
mandate_type.as_ref(),
)?;
- let shipping_address = helpers::create_or_find_address_for_payment_by_request(
- state,
- request.shipping.as_ref(),
- None,
- platform.get_processor().get_account().get_id(),
- customer_details.customer_id.as_ref(),
- platform.get_processor().get_key_store(),
- &payment_id,
- platform.get_processor().get_account().storage_scheme,
- )
- .await?;
-
- let billing_address = helpers::create_or_find_address_for_payment_by_request(
- state,
- request.billing.as_ref(),
- None,
- platform.get_processor().get_account().get_id(),
- customer_details.customer_id.as_ref(),
- platform.get_processor().get_key_store(),
- &payment_id,
- platform.get_processor().get_account().storage_scheme,
- )
- .await?;
-
let payment_method_data_billing = request
.payment_method_data
.as_ref()
@@ -236,7 +182,29 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
})
}));
- let payment_method_billing_address =
+ // The shipping, billing and payment method billing addresses are independent of each
+ // other, so create them concurrently rather than one after another.
+ let (shipping_address, billing_address, payment_method_billing_address) = tokio::try_join!(
+ helpers::create_or_find_address_for_payment_by_request(
+ state,
+ request.shipping.as_ref(),
+ None,
+ platform.get_processor().get_account().get_id(),
+ customer_details.customer_id.as_ref(),
+ platform.get_processor().get_key_store(),
+ &payment_id,
+ platform.get_processor().get_account().storage_scheme,
+ ),
+ helpers::create_or_find_address_for_payment_by_request(
+ state,
+ request.billing.as_ref(),
+ None,
+ platform.get_processor().get_account().get_id(),
+ customer_details.customer_id.as_ref(),
+ platform.get_processor().get_key_store(),
+ &payment_id,
+ platform.get_processor().get_account().storage_scheme,
+ ),
helpers::create_or_find_address_for_payment_by_request(
state,
payment_method_data_billing.as_ref(),
@@ -246,8 +214,8 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
platform.get_processor().get_key_store(),
&payment_id,
platform.get_processor().get_account().storage_scheme,
- )
- .await?;
+ ),
+ )?;
let browser_info = request
.browser_info
diff --git a/crates/router/src/core/payouts.rs b/crates/router/src/core/payouts.rs
index 2cd387576b..fa6186203b 100644
--- a/crates/router/src/core/payouts.rs
+++ b/crates/router/src/core/payouts.rs
@@ -606,19 +606,19 @@ pub async fn payouts_create_core(
.with_processor_merchant_id(platform.get_processor().get_processor_merchant_id())
.with_provider_merchant_id(platform.get_provider().get_provider_merchant_id());
// Validate create request
- let (payout_id, payout_method_data, profile_id, customer, payment_method) = Box::pin(
+ let (payout_id, payout_method_data, business_profile, customer, payment_method) = Box::pin(
validator::validate_create_request(&state, &platform, &req, &dimensions),
)
.await?;
- let dimensions = dimensions.with_profile_id(profile_id.clone());
+ let dimensions = dimensions.with_profile_id(business_profile.get_id().clone());
// Create DB entries
let mut payout_data = Box::pin(payout_create_db_entries(
&state,
&platform,
&req,
&payout_id,
- &profile_id,
+ business_profile,
payout_method_data.as_ref(),
&state.locale,
customer.as_ref(),
@@ -3385,7 +3385,7 @@ pub async fn payout_create_db_entries(
_platform: &domain::Platform,
_req: &payouts::PayoutCreateRequest,
_payout_id: &str,
- _profile_id: &id_type::ProfileId,
+ _business_profile: domain::Profile,
_stored_payout_method_data: Option<&payouts::PayoutMethodData>,
_locale: &str,
_customer: Option<&domain::Customer>,
@@ -3403,7 +3403,7 @@ pub async fn payout_create_db_entries(
platform: &domain::Platform,
req: &payouts::PayoutCreateRequest,
payout_id: &id_type::PayoutId,
- profile_id: &id_type::ProfileId,
+ business_profile: domain::Profile,
stored_payout_method_data: Option<&payouts::PayoutMethodData>,
locale: &str,
customer: Option<&domain::Customer>,
@@ -3413,10 +3413,7 @@ pub async fn payout_create_db_entries(
let db = &*state.store;
let merchant_id = platform.get_processor().get_account().get_id();
let customer_id = customer.map(|cust| cust.get_id().clone());
-
- // Validate whether profile_id passed in request is valid and is linked to the merchant
- let business_profile =
- validate_and_get_business_profile(state, platform.get_processor(), profile_id).await?;
+ let profile_id = business_profile.get_id().to_owned();
let payout_link = match req.payout_link {
Some(true) => Some(
diff --git a/crates/router/src/core/payouts/validator.rs b/crates/router/src/core/payouts/validator.rs
index b62879ceb5..20be0c46bc 100644
--- a/crates/router/src/core/payouts/validator.rs
+++ b/crates/router/src/core/payouts/validator.rs
@@ -66,7 +66,7 @@ pub async fn validate_create_request(
) -> RouterResult<(
String,
Option<payouts::PayoutMethodData>,
- id_type::ProfileId,
+ domain::Profile,
Option<domain::Customer>,
Option<PaymentMethod>,
)> {
@@ -86,7 +86,7 @@ pub async fn validate_create_request(
) -> RouterResult<(
id_type::PayoutId,
Option<payouts::PayoutMethodData>,
- id_type::ProfileId,
+ domain::Profile,
Option<domain::Customer>,
Option<PaymentMethod>,
)> {
@@ -152,26 +152,16 @@ pub async fn validate_create_request(
None
};
- #[cfg(feature = "v1")]
- let profile_id = core_utils::get_profile_id_from_business_details(
+ // Fetch the profile once and hand it back, so payout creation doesn't fetch it again
+ let business_profile = core_utils::get_profile_from_business_details(
req.business_country,
req.business_label.as_ref(),
platform.get_processor(),
req.profile_id.as_ref(),
&*state.store,
- false,
)
.await?;
-
- #[cfg(feature = "v2")]
- // Profile id will be mandatory in v2 in the request / headers
- let profile_id = req
- .profile_id
- .clone()
- .ok_or(errors::ApiErrorResponse::MissingRequiredField {
- field_name: "profile_id".into(),
- })
- .attach_printable("Profile id is a mandatory parameter")?;
+ let profile_id = business_profile.get_id().to_owned();
let payment_method: Option<PaymentMethod> =
match (req.payout_token.as_ref(), req.payout_method_id.clone()) {
@@ -264,7 +254,7 @@ pub async fn validate_create_request(
Ok((
payout_id,
payout_method_data,
- profile_id,
+ business_profile,
customer,
payment_method,
))
diff --git a/crates/router/src/core/unified_authentication_service.rs b/crates/router/src/core/unified_authentication_service.rs
index 8946de0377..fde048dfcb 100644
--- a/crates/router/src/core/unified_authentication_service.rs
+++ b/crates/router/src/core/unified_authentication_service.rs
@@ -718,22 +718,15 @@ pub async fn authentication_create_core(
let processor_merchant_account = platform.get_processor().get_account();
let processor_merchant_id = processor_merchant_account.get_id();
let key_manager_state = (&state).into();
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let business_profile = core_utils::get_profile_from_business_details(
None,
None,
platform.get_processor(),
req.profile_id.as_ref(),
db,
- true,
)
.await?;
-
- let business_profile = db
- .find_business_profile_by_profile_id(platform.get_processor().get_key_store(), &profile_id)
- .await
- .to_not_found_response(ApiErrorResponse::ProfileNotFound {
- id: profile_id.get_string_repr().to_owned(),
- })?;
+ let profile_id = business_profile.get_id().to_owned();
let organization_id = processor_merchant_account.organization_id.clone();
let authentication_id = common_utils::id_type::AuthenticationId::generate_authentication_id(
consts::AUTHENTICATION_ID_PREFIX,
@@ -1075,22 +1068,15 @@ pub async fn authentication_eligibility_core(
ensure_not_terminal_status(authentication.trans_status.clone())?;
- let profile_id = core_utils::get_profile_id_from_business_details(
+ let business_profile = core_utils::get_profile_from_business_details(
None,
None,
platform.get_processor(),
req.profile_id.as_ref(),
db,
- true,
)
.await?;
-
- let business_profile = db
- .find_business_profile_by_profile_id(platform.get_processor().get_key_store(), &profile_id)
- .await
- .to_not_found_response(ApiErrorResponse::ProfileNotFound {
- id: profile_id.get_string_repr().to_owned(),
- })?;
+ let profile_id = business_profile.get_id().to_owned();
let (authentication_connector, three_ds_connector_account) =
auth_utils::get_authentication_connector_data(
diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rs
index f22da7db62..35b4e5a855 100644
--- a/crates/router/src/core/utils.rs
+++ b/crates/router/src/core/utils.rs
@@ -2346,39 +2346,39 @@ pub fn get_connector_label(
#[cfg(feature = "v1")]
/// If profile_id is not passed, use default profile if available, or
/// If business_details (business_country and business_label) are passed, get the business_profile
-/// or return a `MissingRequiredField` error
-#[allow(clippy::too_many_arguments)]
-pub async fn get_profile_id_from_business_details(
+/// or return a `MissingRequiredField` error.
+/// Both lookups are scoped to the merchant, so fetching the profile also validates that it belongs
+/// to the merchant.
+pub async fn get_profile_from_business_details(
business_country: Option<api_models::enums::CountryAlpha2>,
business_label: Option<&String>,
processor: &domain::Processor,
request_profile_id: Option<&common_utils::id_type::ProfileId>,
db: &dyn StorageInterface,
- should_validate: bool,
-) -> RouterResult<common_utils::id_type::ProfileId> {
+) -> RouterResult<domain::Profile> {
match request_profile_id.or(processor.get_account().default_profile.as_ref()) {
- Some(profile_id) => {
- // Check whether this business profile belongs to the merchant
- if should_validate {
- let _ = validate_and_get_business_profile(db, processor, Some(profile_id)).await?;
- }
- Ok(profile_id.clone())
- }
+ Some(profile_id) => db
+ .find_business_profile_by_merchant_id_profile_id(
+ processor.get_key_store(),
+ processor.get_account().get_id(),
+ profile_id,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::ProfileNotFound {
+ id: profile_id.get_string_repr().to_owned(),
+ }),
None => match business_country.zip(business_label) {
Some((business_country, business_label)) => {
let profile_name = format!("{business_country}_{business_label}");
- let business_profile = db
- .find_business_profile_by_profile_name_merchant_id(
- processor.get_key_store(),
- &profile_name,
- processor.get_account().get_id(),
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::ProfileNotFound {
- id: profile_name,
- })?;
-
- Ok(business_profile.get_id().to_owned())
+ db.find_business_profile_by_profile_name_merchant_id(
+ processor.get_key_store(),
+ &profile_name,
+ processor.get_account().get_id(),
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::ProfileNotFound {
+ id: profile_name,
+ })
}
_ => Err(report!(errors::ApiErrorResponse::MissingRequiredField {
field_name: "profile_id or business_country, business_label".into()
diff --git a/crates/router/src/utils/user/sample_data.rs b/crates/router/src/utils/user/sample_data.rs
index 8f41cce09b..9263f34073 100644
--- a/crates/router/src/utils/user/sample_data.rs
+++ b/crates/router/src/utils/user/sample_data.rs
@@ -15,7 +15,10 @@ use time::OffsetDateTime;
use crate::{
consts,
- core::errors::sample_data::{SampleDataError, SampleDataResult},
+ core::{
+ errors::sample_data::{SampleDataError, SampleDataResult},
+ utils as core_utils,
+ },
types::domain,
SessionState,
};
@@ -73,15 +76,15 @@ pub async fn generate_sample_data(
let business_label_default = merchant_parsed_details.first().map(|x| x.business.clone());
- let profile_id = crate::core::utils::get_profile_id_from_business_details(
+ let profile_id = core_utils::get_profile_from_business_details(
business_country_default,
business_label_default.as_ref(),
platform.get_processor(),
req.profile_id.as_ref(),
&*state.store,
- false,
)
- .await;
+ .await
+ .map(|business_profile| business_profile.get_id().to_owned());
(profile_id, business_country_default, business_label_default)
};