Skip to content

Commit

Permalink
fix(router): add max_amount validation in payment flows (#4645)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-harsha-vardhan authored May 15, 2024
1 parent 1a27ba5 commit df865d7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/router/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,6 @@ pub const DEFAULT_POLL_DELAY_IN_SECS: i8 = 2;
pub const DEFAULT_POLL_FREQUENCY: i8 = 5;

pub const CONNECTOR_CREDS_TOKEN_TTL: i64 = 900;

//max_amount allowed is 999999999 in minor units
pub const MAX_ALLOWED_AMOUNT: i64 = 999999999;
18 changes: 18 additions & 0 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,24 @@ fn validate_options_for_inequality<T: PartialEq>(
)
}

pub fn validate_max_amount(
amount: api_models::payments::Amount,
) -> CustomResult<(), errors::ApiErrorResponse> {
match amount {
api_models::payments::Amount::Value(value) => {
utils::when(value.get() > consts::MAX_ALLOWED_AMOUNT, || {
Err(report!(errors::ApiErrorResponse::PreconditionFailed {
message: format!(
"amount should not be more than {}",
consts::MAX_ALLOWED_AMOUNT
)
}))
})
}
api_models::payments::Amount::Zero => Ok(()),
}
}

// Checks if the customer details are passed in both places
// If so, raise an error
pub fn validate_customer_details_in_request(
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,9 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentConfir
operations::ValidateResult<'a>,
)> {
helpers::validate_customer_details_in_request(request)?;
if let Some(amount) = request.amount {
helpers::validate_max_amount(amount)?;
}

let request_merchant_id = request.merchant_id.as_deref();
helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id)
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,9 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentCreate
operations::ValidateResult<'a>,
)> {
helpers::validate_customer_details_in_request(request)?;
if let Some(amount) = request.amount {
helpers::validate_max_amount(amount)?;
}
if let Some(session_expiry) = &request.session_expiry {
helpers::validate_session_expiry(session_expiry.to_owned())?;
}
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/core/payments/operations/payment_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,9 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentUpdate
operations::ValidateResult<'a>,
)> {
helpers::validate_customer_details_in_request(request)?;
if let Some(amount) = request.amount {
helpers::validate_max_amount(amount)?;
}
if let Some(session_expiry) = &request.session_expiry {
helpers::validate_session_expiry(session_expiry.to_owned())?;
}
Expand Down

0 comments on commit df865d7

Please sign in to comment.