|
1 |
| -use std::{borrow::Cow, str::FromStr}; |
| 1 | +use std::{borrow::Cow, collections::HashSet, str::FromStr}; |
2 | 2 |
|
3 | 3 | #[cfg(feature = "v2")]
|
4 | 4 | use api_models::ephemeral_key::EphemeralKeyResponse;
|
@@ -6301,3 +6301,91 @@ pub async fn is_merchant_eligible_authentication_service(
|
6301 | 6301 |
|
6302 | 6302 | Ok(auth_eligible_array.contains(&merchant_id.get_string_repr().to_owned()))
|
6303 | 6303 | }
|
| 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 | +} |
0 commit comments