diff --git a/crates/external_services/src/grpc_client/dynamic_routing.rs b/crates/external_services/src/grpc_client/dynamic_routing.rs index 596fe7964163..20239423e189 100644 --- a/crates/external_services/src/grpc_client/dynamic_routing.rs +++ b/crates/external_services/src/grpc_client/dynamic_routing.rs @@ -112,7 +112,6 @@ impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient { params: String, label_input: Vec, ) -> DynamicRoutingResult { - let labels = label_input .into_iter() .map(|conn_choice| conn_choice.to_string()) diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs index 31013cdf6053..12ad81937dd8 100644 --- a/crates/router/src/core/payments.rs +++ b/crates/router/src/core/payments.rs @@ -72,9 +72,9 @@ use super::{ #[cfg(feature = "frm")] use crate::core::fraud_check as frm_core; #[cfg(all(feature = "v1", feature = "dynamic_routing"))] -use crate::types::api::convert_connector_data_to_routable_connectors; -#[cfg(all(feature = "v1", feature = "dynamic_routing"))] use crate::core::routing::helpers as routing_helpers; +#[cfg(all(feature = "v1", feature = "dynamic_routing"))] +use crate::types::api::convert_connector_data_to_routable_connectors; use crate::{ configs::settings::{ApplePayPreDecryptFlow, PaymentMethodTypeTokenFilter}, connector::utils::missing_field_err, @@ -5589,29 +5589,30 @@ where #[cfg(all(feature = "v1", feature = "dynamic_routing"))] let connectors = { if business_profile.dynamic_routing_algorithm.is_some() { - let success_based_routing_config_params_interpolator = routing_helpers::SuccessBasedRoutingConfigParamsInterpolator::new( - payment_data.get_payment_attempt().payment_method, - payment_data.get_payment_attempt().payment_method_type, - payment_data.get_payment_attempt().authentication_type, - payment_data.get_payment_attempt().currency, - payment_data - .get_billing_address() - .unwrap() - .address - .unwrap() - .country, - payment_data - .get_payment_attempt() - .payment_method_data - .as_ref() - .and_then(|data| data.as_object()) - .and_then(|card| card.get("card")) - .and_then(|data| data.as_object()) - .and_then(|card| card.get("card_network")) - .and_then(|network| network.as_str()) - .map(|network| network.to_string()), - None, - ); + let success_based_routing_config_params_interpolator = + routing_helpers::SuccessBasedRoutingConfigParamsInterpolator::new( + payment_data.get_payment_attempt().payment_method, + payment_data.get_payment_attempt().payment_method_type, + payment_data.get_payment_attempt().authentication_type, + payment_data.get_payment_attempt().currency, + payment_data + .get_billing_address() + .unwrap() + .address + .unwrap() + .country, + payment_data + .get_payment_attempt() + .payment_method_data + .as_ref() + .and_then(|data| data.as_object()) + .and_then(|card| card.get("card")) + .and_then(|data| data.as_object()) + .and_then(|card| card.get("card_network")) + .and_then(|network| network.as_str()) + .map(|network| network.to_string()), + None, + ); routing::perform_success_based_routing( state, connectors.clone(), diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index 09a6202e0c14..fd511ac985a9 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -1922,28 +1922,31 @@ async fn payment_response_update_tracker( let state = state.clone(); let business_profile = business_profile.clone(); let payment_attempt = payment_attempt.clone(); - let success_based_routing_config_params_interpolator = routing_helpers::SuccessBasedRoutingConfigParamsInterpolator::new( - payment_attempt.payment_method, - payment_attempt.payment_method_type, - payment_attempt.authentication_type, - payment_attempt.currency, - payment_data.address.get_payment_billing() - .unwrap() - .address - .clone() - .unwrap() - .country, - payment_attempt - .payment_method_data - .as_ref() - .and_then(|data| data.as_object()) - .and_then(|card| card.get("card")) - .and_then(|data| data.as_object()) - .and_then(|card| card.get("card_network")) - .and_then(|network| network.as_str()) - .map(|network| network.to_string()), - None, - ); + let success_based_routing_config_params_interpolator = + routing_helpers::SuccessBasedRoutingConfigParamsInterpolator::new( + payment_attempt.payment_method, + payment_attempt.payment_method_type, + payment_attempt.authentication_type, + payment_attempt.currency, + payment_data + .address + .get_payment_billing() + .unwrap() + .address + .clone() + .unwrap() + .country, + payment_attempt + .payment_method_data + .as_ref() + .and_then(|data| data.as_object()) + .and_then(|card| card.get("card")) + .and_then(|data| data.as_object()) + .and_then(|card| card.get("card_network")) + .and_then(|network| network.as_str()) + .map(|network| network.to_string()), + None, + ); tokio::spawn( async move { routing_helpers::push_metrics_for_success_based_routing( diff --git a/crates/router/src/core/payments/routing.rs b/crates/router/src/core/payments/routing.rs index c6f4f14684f5..8555c96b3d5f 100644 --- a/crates/router/src/core/payments/routing.rs +++ b/crates/router/src/core/payments/routing.rs @@ -1241,8 +1241,7 @@ pub async fn perform_success_based_routing( routable_connectors: Vec, business_profile: &domain::Profile, success_based_routing_config_params_interpolator: routing::helpers::SuccessBasedRoutingConfigParamsInterpolator, -) -> RoutingResult> -{ +) -> RoutingResult> { let success_based_dynamic_routing_algo_ref: api_routing::DynamicRoutingAlgorithmRef = business_profile .dynamic_routing_algorithm @@ -1295,7 +1294,8 @@ pub async fn perform_success_based_routing( .change_context(errors::RoutingError::SuccessBasedRoutingConfigError) .attach_printable("unable to fetch success_rate based dynamic routing configs")?; - let success_based_routing_config_params = success_based_routing_config_params_interpolator.get_string_val(success_based_routing_configs.params.as_ref()); + let success_based_routing_config_params = success_based_routing_config_params_interpolator + .get_string_val(success_based_routing_configs.params.as_ref()); let tenant_business_profile_id = routing::helpers::generate_tenant_business_profile_id( &state.tenant.redis_key_prefix, diff --git a/crates/router/src/core/routing/helpers.rs b/crates/router/src/core/routing/helpers.rs index 05a574722132..773c1ef1d669 100644 --- a/crates/router/src/core/routing/helpers.rs +++ b/crates/router/src/core/routing/helpers.rs @@ -28,8 +28,6 @@ use storage_impl::redis::cache; #[cfg(feature = "v2")] use crate::types::domain::MerchantConnectorAccount; -#[cfg(feature = "v1")] -use crate::{core::metrics as core_metrics, routes::metrics, types::transformers::ForeignInto}; use crate::{ core::errors::{self, RouterResult}, db::StorageInterface, @@ -37,6 +35,8 @@ use crate::{ types::{domain, storage}, utils::StringExt, }; +#[cfg(feature = "v1")] +use crate::{core::metrics as core_metrics, routes::metrics, types::transformers::ForeignInto}; pub const SUCCESS_BASED_DYNAMIC_ROUTING_ALGORITHM: &str = "Success rate based dynamic routing algorithm"; @@ -646,8 +646,7 @@ pub async fn push_metrics_for_success_based_routing( routable_connectors: Vec, business_profile: &domain::Profile, success_based_routing_config_params_interpolator: SuccessBasedRoutingConfigParamsInterpolator, -) -> RouterResult<()> -{ +) -> RouterResult<()> { let success_based_dynamic_routing_algo_ref: routing_types::DynamicRoutingAlgorithmRef = business_profile .dynamic_routing_algorithm @@ -699,7 +698,8 @@ pub async fn push_metrics_for_success_based_routing( business_profile.get_id().get_string_repr(), ); - let success_based_routing_config_params = success_based_routing_config_params_interpolator.get_string_val(success_based_routing_configs.params.as_ref()); + let success_based_routing_config_params = success_based_routing_config_params_interpolator + .get_string_val(success_based_routing_configs.params.as_ref()); let success_based_connectors = client .calculate_success_rate( @@ -976,44 +976,59 @@ pub struct SuccessBasedRoutingConfigParamsInterpolator { impl SuccessBasedRoutingConfigParamsInterpolator { pub fn new( - payment_method: Option, - payment_method_type: Option, - authentication_type: Option, - currency: Option, - country: Option, - card_network: Option, - card_bin: Option, - ) -> Self { - SuccessBasedRoutingConfigParamsInterpolator { - payment_method, - payment_method_type, - authentication_type, - currency, - country, - card_network, - card_bin, - } + payment_method: Option, + payment_method_type: Option, + authentication_type: Option, + currency: Option, + country: Option, + card_network: Option, + card_bin: Option, + ) -> Self { + SuccessBasedRoutingConfigParamsInterpolator { + payment_method, + payment_method_type, + authentication_type, + currency, + country, + card_network, + card_bin, } + } - pub fn get_string_val(&self, params: Option<&Vec>) -> String { + pub fn get_string_val( + &self, + params: Option<&Vec>, + ) -> String { let mut parts: Vec = Vec::new(); if let Some(params) = params { for param in params { let val = match param { - routing_types::SuccessBasedRoutingConfigParams::PaymentMethod => - self.payment_method.as_ref().map_or(String::new(), |pm| pm.to_string()), - routing_types::SuccessBasedRoutingConfigParams::PaymentMethodType => - self.payment_method_type.as_ref().map_or(String::new(), |pmt| pmt.to_string()), - routing_types::SuccessBasedRoutingConfigParams::AuthenticationType => - self.authentication_type.as_ref().map_or(String::new(), |at| at.to_string()), - routing_types::SuccessBasedRoutingConfigParams::Currency => - self.currency.as_ref().map_or(String::new(), |cur| cur.to_string()), - routing_types::SuccessBasedRoutingConfigParams::Country => - self.country.as_ref().map_or(String::new(), |cn| cn.to_string()), - routing_types::SuccessBasedRoutingConfigParams::CardNetwork => - self.card_network.clone().unwrap_or_default(), - routing_types::SuccessBasedRoutingConfigParams::CardBin => - self.card_bin.clone().unwrap_or_default(), + routing_types::SuccessBasedRoutingConfigParams::PaymentMethod => self + .payment_method + .as_ref() + .map_or(String::new(), |pm| pm.to_string()), + routing_types::SuccessBasedRoutingConfigParams::PaymentMethodType => self + .payment_method_type + .as_ref() + .map_or(String::new(), |pmt| pmt.to_string()), + routing_types::SuccessBasedRoutingConfigParams::AuthenticationType => self + .authentication_type + .as_ref() + .map_or(String::new(), |at| at.to_string()), + routing_types::SuccessBasedRoutingConfigParams::Currency => self + .currency + .as_ref() + .map_or(String::new(), |cur| cur.to_string()), + routing_types::SuccessBasedRoutingConfigParams::Country => self + .country + .as_ref() + .map_or(String::new(), |cn| cn.to_string()), + routing_types::SuccessBasedRoutingConfigParams::CardNetwork => { + self.card_network.clone().unwrap_or_default() + } + routing_types::SuccessBasedRoutingConfigParams::CardBin => { + self.card_bin.clone().unwrap_or_default() + } }; if !val.is_empty() { parts.push(val);