fix(router): validate payment method data and type co-presence (#371)

This commit is contained in:
ItsMeShashank
2023-01-14 20:37:27 +05:30
committed by GitHub
parent 491ed871d4
commit d64e5f2df3
4 changed files with 32 additions and 1 deletions

View File

@ -989,6 +989,31 @@ pub(crate) fn validate_amount_to_capture(
)
}
#[instrument(skip_all)]
pub(crate) fn validate_payment_method_fields_present(
req: &api::PaymentsRequest,
) -> RouterResult<()> {
utils::when(
req.payment_method.is_none() && req.payment_method_data.is_some(),
|| {
Err(errors::ApiErrorResponse::MissingRequiredField {
field_name: "payent_method".to_string(),
})
},
)?;
utils::when(
req.payment_method.is_some() && req.payment_method_data.is_none(),
|| {
Err(errors::ApiErrorResponse::MissingRequiredField {
field_name: "payment_method_data".to_string(),
})
},
)?;
Ok(())
}
pub fn can_call_connector(status: &storage_enums::AttemptStatus) -> bool {
!matches!(
status,
@ -1295,7 +1320,7 @@ pub(crate) fn validate_pm_or_token_given(
!matches!(payment_method, Some(api_enums::PaymentMethodType::Paypal))
&& !matches!(mandate_type, Some(api::MandateTxnType::RecurringMandateTxn))
&& token.is_none()
&& payment_method_data.is_none(),
&& (payment_method_data.is_none() || payment_method.is_none()),
|| {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "A payment token or payment method data is required".to_string(),

View File

@ -367,6 +367,8 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentConfir
expected_format: "merchant_id from merchant account".to_string(),
})?;
helpers::validate_payment_method_fields_present(request)?;
let mandate_type = helpers::validate_mandate(request)?;
let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;

View File

@ -367,6 +367,8 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentCreate
expected_format: "amount_to_capture lesser than amount".to_string(),
})?;
helpers::validate_payment_method_fields_present(request)?;
let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;
let mandate_type = helpers::validate_mandate(request)?;

View File

@ -405,6 +405,8 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentUpdate
expected_format: "amount_to_capture lesser than or equal to amount".to_string(),
})?;
helpers::validate_payment_method_fields_present(request)?;
let mandate_type = helpers::validate_mandate(request)?;
let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;