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(router): Handle failures in `apply_three_ds_authentication_strategy` gracefully (#10722)

Sai Harsha Vardhan3305ed6c5433
diff --git a/crates/hyperswitch_domain_models/src/business_profile.rs b/crates/hyperswitch_domain_models/src/business_profile.rs
index 86a39b0d2b..8eded7234d 100644
--- a/crates/hyperswitch_domain_models/src/business_profile.rs
+++ b/crates/hyperswitch_domain_models/src/business_profile.rs
@@ -20,6 +20,7 @@ use diesel_models::business_profile::{
};
use error_stack::ResultExt;
use masking::{ExposeInterface, PeekInterface, Secret};
+use router_env::logger;
use crate::{
behaviour::Conversion,
@@ -1496,6 +1497,31 @@ impl Profile {
.attach_printable("unable to deserialize routing algorithm ref from merchant account")
}
+ #[cfg(feature = "v1")]
+ pub fn get_three_ds_decision_rule_algorithm_id(
+ &self,
+ ) -> Option<common_utils::id_type::RoutingId> {
+ self.three_ds_decision_rule_algorithm
+ .clone()
+ .map(|val| {
+ val.parse_value::<api_models::routing::RoutingAlgorithmRef>("RoutingAlgorithmRef")
+ })
+ .transpose()
+ .change_context(api_error_response::ApiErrorResponse::InternalServerError)
+ .attach_printable(
+ "unable to deserialize three_ds_decision_rule_algorithm ref from profile",
+ )
+ .inspect_err(|err| {
+ logger::error!(
+ "Error while parsing three_ds_decision_rule_algorithm ref from profile {:?}",
+ err
+ )
+ })
+ .ok()
+ .flatten()
+ .and_then(|algorithm| algorithm.algorithm_id)
+ }
+
#[cfg(feature = "v1")]
pub fn get_payout_routing_algorithm(
&self,
diff --git a/crates/router/src/core/metrics.rs b/crates/router/src/core/metrics.rs
index 4110f470ce..411a6d419b 100644
--- a/crates/router/src/core/metrics.rs
+++ b/crates/router/src/core/metrics.rs
@@ -31,6 +31,10 @@ counter_metric!(
GLOBAL_METER
);
+counter_metric!(THREE_DS_EXEMPTION_INCOMING_REQUESTS, GLOBAL_METER); // No. of incoming requests for Three DS Exemption engine in payments flow
+counter_metric!(THREE_DS_EXEMPTION_ALGORITHM_FOUND, GLOBAL_METER); // No. of requests for which Three DS Exemption algorithm is found in business profile
+counter_metric!(THREE_DS_EXEMPTION_DECISION_COMPUTED, GLOBAL_METER); // No. of requests for which Three DS Exemption decision is computed
+
counter_metric!(INCOMING_PAYOUT_WEBHOOK_METRIC, GLOBAL_METER); // No. of incoming payout webhooks
counter_metric!(
INCOMING_PAYOUT_WEBHOOK_SIGNATURE_FAILURE_METRIC,
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index b73f26b4b6..3184e6229a 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -633,6 +633,11 @@ where
payment_data.set_authentication_type_in_attempt(authentication_type);
+ operation
+ .to_domain()?
+ .apply_three_ds_authentication_strategy(state, &mut payment_data, &business_profile)
+ .await;
+
let connector = get_connector_choice(
&operation,
state,
@@ -665,11 +670,6 @@ where
)
.await;
- operation
- .to_domain()?
- .apply_three_ds_authentication_strategy(state, &mut payment_data, &business_profile)
- .await?;
-
let should_add_task_to_process_tracker = should_add_task_to_process_tracker(&payment_data);
let locale = header_payload.locale.clone();
diff --git a/crates/router/src/core/payments/operations.rs b/crates/router/src/core/payments/operations.rs
index 3cb2683851..cc28682e6f 100644
--- a/crates/router/src/core/payments/operations.rs
+++ b/crates/router/src/core/payments/operations.rs
@@ -445,8 +445,7 @@ pub trait Domain<F: Clone, R, D>: Send + Sync {
_state: &SessionState,
_payment_data: &mut D,
_business_profile: &domain::Profile,
- ) -> CustomResult<(), errors::ApiErrorResponse> {
- Ok(())
+ ) {
}
/// Get connector tokenization action
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 7995a176da..2608e53dde 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -34,6 +34,7 @@ use crate::{
card_testing_guard::utils as card_testing_guard_utils,
errors::{self, CustomResult, RouterResult, StorageErrorExt},
mandate::helpers as m_helpers,
+ metrics,
payments::{
self, helpers, operations,
operations::payment_confirm::unified_authentication_service::ThreeDsMetaData,
@@ -1123,19 +1124,24 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
state: &SessionState,
payment_data: &mut PaymentData<F>,
business_profile: &domain::Profile,
- ) -> CustomResult<(), errors::ApiErrorResponse> {
+ ) {
+ metrics::THREE_DS_EXEMPTION_INCOMING_REQUESTS.add(
+ 1,
+ router_env::metric_attributes!(("merchant_id", business_profile.merchant_id.clone()),),
+ );
// If the business profile has a three_ds_decision_rule_algorithm, we will use it to determine the 3DS strategy (authentication_type, exemption_type and force_three_ds_challenge)
- if let Some(three_ds_decision_rule) =
- business_profile.three_ds_decision_rule_algorithm.clone()
- {
- // Parse the three_ds_decision_rule to get the algorithm_id
- let algorithm_id = three_ds_decision_rule
- .parse_value::<api::routing::RoutingAlgorithmRef>("RoutingAlgorithmRef")
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Could not decode profile routing algorithm ref")?
- .algorithm_id
- .ok_or(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("No algorithm_id found in three_ds_decision_rule_algorithm")?;
+ if let Some(algorithm_id) = business_profile.get_three_ds_decision_rule_algorithm_id() {
+ metrics::THREE_DS_EXEMPTION_ALGORITHM_FOUND.add(
+ 1,
+ router_env::metric_attributes!((
+ "merchant_id",
+ business_profile.merchant_id.clone()
+ ),),
+ );
+ logger::info!(
+ "Three DS Decision Rule Algorithm Id {}",
+ algorithm_id.get_string_repr()
+ );
// get additional card info from payment data
let additional_card_info = payment_data
.payment_attempt
@@ -1150,7 +1156,15 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
})
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("unable to parse value into additional_payment_method_data")?
+ .attach_printable("unable to parse value into additional_payment_method_data")
+ .inspect_err(|err| {
+ logger::error!(
+ "Error while parsing additional_payment_method_data {:?}",
+ err
+ )
+ })
+ .ok()
+ .flatten()
.and_then(|additional_payment_method_data| {
additional_payment_method_data.get_additional_card_info()
});
@@ -1161,7 +1175,7 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
.clone()
.and_then(|network| business_profile.get_acquirer_details_from_network(network))
});
- let country = business_profile
+ let acquirer_country = business_profile
.merchant_country_code
.as_ref()
.map(|country_code| {
@@ -1169,7 +1183,15 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
})
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Error while parsing country from merchant country code")?;
+ .attach_printable("Error while parsing country from merchant country code")
+ .inspect_err(|err| {
+ logger::error!(
+ "Error while parsing country from merchant country code {:?}",
+ err
+ )
+ })
+ .ok()
+ .flatten();
// get three_ds_decision_rule_output using algorithm_id and payment data
let decision = three_ds_decision_rule::get_three_ds_decision_rule_output(
state,
@@ -1182,7 +1204,10 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
.payment_intent
.currency
.ok_or(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("currency is not set in payment intent")?,
+ .attach_printable("currency is not set in payment intent")
+ .inspect_err(|err| logger::error!("{:?}", err))
+ .ok()
+ .unwrap_or_default(),
},
payment_method: Some(
api_models::three_ds_decision_rule::PaymentMethodMetaData {
@@ -1197,23 +1222,51 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
.and_then(|info| info.card_issuer.clone()),
country: additional_card_info
.as_ref()
- .map(|info| info.card_issuing_country.clone().parse_enum("Country"))
+ .map(|info| {
+ info.card_issuing_country_code
+ .clone()
+ .parse_enum("CountryAlpha2")
+ })
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable(
- "Error while getting country enum from issuer country",
- )?,
+ "Error while getting country enum from issuer country code",
+ )
+ .inspect_err(|err| {
+ logger::error!(
+ "Error while getting country enum from issuer country {:?}",
+ err
+ )
+ })
+ .ok()
+ .flatten()
+ .map(common_enums::Country::from_alpha2),
}),
customer_device: None,
acquirer: acquirer_config.as_ref().map(|acquirer| {
api_models::three_ds_decision_rule::AcquirerData {
- country,
+ country: acquirer_country,
fraud_rate: Some(acquirer.acquirer_fraud_rate),
}
}),
},
)
- .await?;
+ .await
+ .inspect_err(|err| {
+ logger::error!(
+ "Error while getting three_ds_decision_rule output {:?}",
+ err
+ )
+ })
+ .ok()
+ .unwrap_or_default();
+ metrics::THREE_DS_EXEMPTION_DECISION_COMPUTED.add(
+ 1,
+ router_env::metric_attributes!((
+ "merchant_id",
+ business_profile.merchant_id.clone()
+ ),),
+ );
logger::info!("Three DS Decision Rule Output: {:?}", decision);
// We should update authentication_type from the Three DS Decision if it is not already set
if payment_data.payment_attempt.authentication_type.is_none() {
@@ -1226,7 +1279,6 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
payment_data.payment_intent.force_3ds_challenge =
decision.should_force_3ds_challenge().then_some(true);
}
- Ok(())
}
#[allow(clippy::too_many_arguments)]