Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(router): add `max_amount` validation in payment flows (#4645)

Sai Harsha Vardhandf865d76be1c
diff --git a/crates/router/src/consts.rs b/crates/router/src/consts.rs
index 3a94a4e765..0c9e9443ac 100644
--- a/crates/router/src/consts.rs
+++ b/crates/router/src/consts.rs
@@ -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;
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 5723c7f8b9..24ac32db91 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -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(
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index f5ab92911e..e48a6ce717 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -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)
diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs
index f8ea543823..7ad47deff4 100644
--- a/crates/router/src/core/payments/operations/payment_create.rs
+++ b/crates/router/src/core/payments/operations/payment_create.rs
@@ -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())?;
}
diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rs
index b52611e6f9..0809a81007 100644
--- a/crates/router/src/core/payments/operations/payment_update.rs
+++ b/crates/router/src/core/payments/operations/payment_update.rs
@@ -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())?;
}