Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): added validation to check total orderDetails amount equal to amount in request #2965

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3685,3 +3685,27 @@ pub async fn get_gsm_record(
})
.ok()
}

pub fn validate_order_details_amount(
order_details: Vec<api_models::payments::OrderDetailsWithAmount>,
amount: Option<api_models::payments::Amount>,
) -> Result<(), errors::ApiErrorResponse> {
let total_order_details_amount: i64 = order_details.iter().map(|order| order.amount).sum();
let amount: i64 = match amount {
Some(value) => value.into(),
None => {
return Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Please provide amount if order details are provided".to_string(),
});
}
};

if total_order_details_amount != amount {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Total sum of order details doesn't match amount in payment request"
.to_string(),
})
} else {
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,10 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
)> {
helpers::validate_customer_details_in_request(request)?;

if let Some(order_details) = &request.order_details {
helpers::validate_order_details_amount(order_details.to_owned(), request.amount)?;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the amount is mandated in confirm flow, this should not be the case


let request_merchant_id = request.merchant_id.as_deref();
helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id)
.change_context(errors::ApiErrorResponse::InvalidDataFormat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
)?;
}

if let Some(order_details) = &request.order_details {
helpers::validate_order_details_amount(order_details.to_owned(), request.amount)?;
}

let payment_id = request.payment_id.clone().ok_or(error_stack::report!(
errors::ApiErrorResponse::PaymentNotFound
))?;
Expand Down
Loading