Skip to content

Commit

Permalink
check allowed payment method types in enabled options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrudul Vajpayee authored and Mrudul Vajpayee committed Jan 10, 2025
1 parent c4d36b5 commit fad712c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/router/src/core/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ counter_metric!(PARTIAL_AUTH_FAILURE, GLOBAL_METER);

counter_metric!(API_KEY_REQUEST_INITIATED, GLOBAL_METER);
counter_metric!(API_KEY_REQUEST_COMPLETED, GLOBAL_METER);
counter_metric!(PAYMENT_METHOD_TYPES_MISCONFIGATION_METRIC, GLOBAL_METER);
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,5 @@ pub trait ShouldCallConnector {
force_sync: Option<bool>,
) -> bool;
}

pub const MERCHANT_ID: &str = "merchant_id";
99 changes: 94 additions & 5 deletions crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::marker::PhantomData;
use std::{collections::HashSet, marker::PhantomData};

use api_models::{
enums::FrmSuggestion, mandates::RecurringDetails, payment_methods::PaymentMethodsData,
payments::GetAddressFromPaymentMethodData,
admin::PaymentMethodsEnabled, enums::FrmSuggestion, mandates::RecurringDetails,
payment_methods::PaymentMethodsData, payments::GetAddressFromPaymentMethodData,
};
use async_trait::async_trait;
use common_utils::{
Expand Down Expand Up @@ -30,13 +30,15 @@ use router_derive::PaymentOperation;
use router_env::{instrument, logger, tracing};
use time::PrimitiveDateTime;

use super::{BoxedOperation, Domain, GetTracker, Operation, UpdateTracker, ValidateRequest};
use super::{
BoxedOperation, Domain, GetTracker, Operation, UpdateTracker, ValidateRequest, MERCHANT_ID,
};
use crate::{
consts,
core::{
errors::{self, CustomResult, RouterResult, StorageErrorExt},
mandate::helpers as m_helpers,
payment_link,
metrics, payment_link,
payment_methods::cards::create_encrypted_data,
payments::{self, helpers, operations, CustomerDetails, PaymentAddress, PaymentData},
utils as core_utils,
Expand Down Expand Up @@ -309,6 +311,93 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
)
.await?;

let allowed_payment_method_types = payment_intent_new
.allowed_payment_method_types
.clone()
.map(|val| {
val.parse_value::<Vec<enums::PaymentMethodType>>("allowed_payment_method_types")
})
.transpose()
.unwrap_or_else(|error| {
logger::error!(
?error,
"Failed to deserialize PaymentIntent allowed_payment_method_types"
);
None
});

if let Some(allowed_payment_method_types) = allowed_payment_method_types {
let all_connector_accounts = db
.find_merchant_connector_account_by_merchant_id_and_disabled_list(
&state.into(),
merchant_account.get_id(),
false,
merchant_key_store,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Database error when querying for merchant connector accounts")?;

let filtered_connector_accounts =
helpers::filter_mca_based_on_profile_and_connector_type(
all_connector_accounts,
&profile_id,
common_enums::ConnectorType::PaymentProcessor,
);

let supporting_payment_method_types: HashSet<_> = filtered_connector_accounts
.iter()
.flat_map(|connector_account| {
connector_account
.payment_methods_enabled
.clone()
.unwrap_or_default()
.into_iter()
.map(|payment_methods_enabled| {
payment_methods_enabled
.parse_value::<PaymentMethodsEnabled>("payment_methods_enabled")
})
.filter_map(|parsed_payment_method_result| {
parsed_payment_method_result
.inspect_err(|err| {
logger::error!(session_token_parsing_error = ?err);
})
.ok()
})
.flat_map(|parsed_payment_methods_enabled| {
parsed_payment_methods_enabled
.payment_method_types
.unwrap_or_default()
.into_iter()
.map(|payment_method_type| payment_method_type.payment_method_type)
})
})
.collect();

let unsupported_payment_methods: Vec<_> = allowed_payment_method_types
.iter()
.filter(|allowed_pmt| !supporting_payment_method_types.contains(allowed_pmt))
.collect();

if !unsupported_payment_methods.is_empty() {
metrics::PAYMENT_METHOD_TYPES_MISCONFIGATION_METRIC.add(
1,
router_env::metric_attributes!((
MERCHANT_ID,
merchant_account.get_id().clone()
)),
);
}

if unsupported_payment_methods.len() == allowed_payment_method_types.len() {
return Err(errors::ApiErrorResponse::IncorrectPaymentMethodConfiguration)
.attach_printable(format!(
"None of the allowed payment method types {:?} are configured for this merchant connector account.",
allowed_payment_method_types
));
}
}

let (payment_attempt_new, additional_payment_data) = Self::make_payment_attempt(
&payment_id,
merchant_id,
Expand Down

0 comments on commit fad712c

Please sign in to comment.