Skip to content

Commit 0eca55f

Browse files
mrudulvajpayee4935Mrudul Vajpayee
andauthored
refactor: check allowed payment method types in enabled options (#7019)
Co-authored-by: Mrudul Vajpayee <[email protected]>
1 parent e35f707 commit 0eca55f

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

crates/router/src/core/payments/helpers.rs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, str::FromStr};
1+
use std::{borrow::Cow, collections::HashSet, str::FromStr};
22

33
#[cfg(feature = "v2")]
44
use api_models::ephemeral_key::EphemeralKeyResponse;
@@ -6301,3 +6301,91 @@ pub async fn is_merchant_eligible_authentication_service(
63016301

63026302
Ok(auth_eligible_array.contains(&merchant_id.get_string_repr().to_owned()))
63036303
}
6304+
6305+
#[cfg(feature = "v1")]
6306+
pub async fn validate_allowed_payment_method_types_request(
6307+
state: &SessionState,
6308+
profile_id: &id_type::ProfileId,
6309+
merchant_account: &domain::MerchantAccount,
6310+
merchant_key_store: &domain::MerchantKeyStore,
6311+
allowed_payment_method_types: Option<Vec<common_enums::PaymentMethodType>>,
6312+
) -> CustomResult<(), errors::ApiErrorResponse> {
6313+
if let Some(allowed_payment_method_types) = allowed_payment_method_types {
6314+
let db = &*state.store;
6315+
let all_connector_accounts = db
6316+
.find_merchant_connector_account_by_merchant_id_and_disabled_list(
6317+
&state.into(),
6318+
merchant_account.get_id(),
6319+
false,
6320+
merchant_key_store,
6321+
)
6322+
.await
6323+
.change_context(errors::ApiErrorResponse::InternalServerError)
6324+
.attach_printable("Failed to fetch merchant connector account for given merchant id")?;
6325+
6326+
let filtered_connector_accounts = filter_mca_based_on_profile_and_connector_type(
6327+
all_connector_accounts,
6328+
profile_id,
6329+
ConnectorType::PaymentProcessor,
6330+
);
6331+
6332+
let supporting_payment_method_types: HashSet<_> = filtered_connector_accounts
6333+
.iter()
6334+
.flat_map(|connector_account| {
6335+
connector_account
6336+
.payment_methods_enabled
6337+
.clone()
6338+
.unwrap_or_default()
6339+
.into_iter()
6340+
.map(|payment_methods_enabled| {
6341+
payment_methods_enabled
6342+
.parse_value::<api_models::admin::PaymentMethodsEnabled>(
6343+
"payment_methods_enabled",
6344+
)
6345+
})
6346+
.filter_map(|parsed_payment_method_result| {
6347+
parsed_payment_method_result
6348+
.inspect_err(|err| {
6349+
logger::error!(
6350+
"Unable to deserialize payment methods enabled: {:?}",
6351+
err
6352+
);
6353+
})
6354+
.ok()
6355+
})
6356+
.flat_map(|parsed_payment_methods_enabled| {
6357+
parsed_payment_methods_enabled
6358+
.payment_method_types
6359+
.unwrap_or_default()
6360+
.into_iter()
6361+
.map(|payment_method_type| payment_method_type.payment_method_type)
6362+
})
6363+
})
6364+
.collect();
6365+
6366+
let unsupported_payment_methods: Vec<_> = allowed_payment_method_types
6367+
.iter()
6368+
.filter(|allowed_pmt| !supporting_payment_method_types.contains(allowed_pmt))
6369+
.collect();
6370+
6371+
if !unsupported_payment_methods.is_empty() {
6372+
metrics::PAYMENT_METHOD_TYPES_MISCONFIGURATION_METRIC.add(
6373+
1,
6374+
router_env::metric_attributes!(("merchant_id", merchant_account.get_id().clone())),
6375+
);
6376+
}
6377+
6378+
fp_utils::when(
6379+
unsupported_payment_methods.len() == allowed_payment_method_types.len(),
6380+
|| {
6381+
Err(errors::ApiErrorResponse::IncorrectPaymentMethodConfiguration)
6382+
.attach_printable(format!(
6383+
"None of the allowed payment method types {:?} are configured for this merchant connector account.",
6384+
allowed_payment_method_types
6385+
))
6386+
},
6387+
)?;
6388+
}
6389+
6390+
Ok(())
6391+
}

crates/router/src/core/payments/operations/payment_create.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
181181
)
182182
.await?;
183183

184+
helpers::validate_allowed_payment_method_types_request(
185+
state,
186+
&profile_id,
187+
merchant_account,
188+
merchant_key_store,
189+
request.allowed_payment_method_types.clone(),
190+
)
191+
.await?;
192+
184193
let customer_details = helpers::get_customer_details_from_request(request);
185194

186195
let shipping_address = helpers::create_or_find_address_for_payment_by_request(

crates/router/src/routes/metrics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,6 @@ histogram_metric_f64!(GENERATE_NETWORK_TOKEN_TIME, GLOBAL_METER);
141141
histogram_metric_f64!(FETCH_NETWORK_TOKEN_TIME, GLOBAL_METER);
142142
histogram_metric_f64!(DELETE_NETWORK_TOKEN_TIME, GLOBAL_METER);
143143
histogram_metric_f64!(CHECK_NETWORK_TOKEN_STATUS_TIME, GLOBAL_METER);
144+
145+
// A counter to indicate allowed payment method types mismatch
146+
counter_metric!(PAYMENT_METHOD_TYPES_MISCONFIGURATION_METRIC, GLOBAL_METER);

0 commit comments

Comments
 (0)