Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwalkumar17 committed Nov 4, 2024
2 parents 99bd296 + 34dbd62 commit 3b19116
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient<Channel> {
params: String,
label_input: Vec<RoutableConnectorChoice>,
) -> DynamicRoutingResult<CalSuccessRateResponse> {

let labels = label_input
.into_iter()
.map(|conn_choice| conn_choice.to_string())
Expand Down
51 changes: 26 additions & 25 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
47 changes: 25 additions & 22 deletions crates/router/src/core/payments/operations/payment_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1922,28 +1922,31 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
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(
Expand Down
6 changes: 3 additions & 3 deletions crates/router/src/core/payments/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,8 +1241,7 @@ pub async fn perform_success_based_routing(
routable_connectors: Vec<api_routing::RoutableConnectorChoice>,
business_profile: &domain::Profile,
success_based_routing_config_params_interpolator: routing::helpers::SuccessBasedRoutingConfigParamsInterpolator,
) -> RoutingResult<Vec<api_routing::RoutableConnectorChoice>>
{
) -> RoutingResult<Vec<api_routing::RoutableConnectorChoice>> {
let success_based_dynamic_routing_algo_ref: api_routing::DynamicRoutingAlgorithmRef =
business_profile
.dynamic_routing_algorithm
Expand Down Expand Up @@ -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,
Expand Down
89 changes: 52 additions & 37 deletions crates/router/src/core/routing/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ 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,
routes::SessionState,
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";

Expand Down Expand Up @@ -646,8 +646,7 @@ pub async fn push_metrics_for_success_based_routing(
routable_connectors: Vec<routing_types::RoutableConnectorChoice>,
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -976,44 +976,59 @@ pub struct SuccessBasedRoutingConfigParamsInterpolator {

impl SuccessBasedRoutingConfigParamsInterpolator {
pub fn new(
payment_method: Option<common_enums::PaymentMethod>,
payment_method_type: Option<common_enums::PaymentMethodType>,
authentication_type: Option<common_enums::AuthenticationType>,
currency: Option<common_enums::Currency>,
country: Option<common_enums::CountryAlpha2>,
card_network: Option<String>,
card_bin: Option<String>,
) -> Self {
SuccessBasedRoutingConfigParamsInterpolator {
payment_method,
payment_method_type,
authentication_type,
currency,
country,
card_network,
card_bin,
}
payment_method: Option<common_enums::PaymentMethod>,
payment_method_type: Option<common_enums::PaymentMethodType>,
authentication_type: Option<common_enums::AuthenticationType>,
currency: Option<common_enums::Currency>,
country: Option<common_enums::CountryAlpha2>,
card_network: Option<String>,
card_bin: Option<String>,
) -> Self {
SuccessBasedRoutingConfigParamsInterpolator {
payment_method,
payment_method_type,
authentication_type,
currency,
country,
card_network,
card_bin,
}
}

pub fn get_string_val(&self, params: Option<&Vec<routing_types::SuccessBasedRoutingConfigParams>>) -> String {
pub fn get_string_val(
&self,
params: Option<&Vec<routing_types::SuccessBasedRoutingConfigParams>>,
) -> String {
let mut parts: Vec<String> = 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);
Expand Down

0 comments on commit 3b19116

Please sign in to comment.