fix: resolve TODO and FIXME in utils module (#220)

This commit is contained in:
Nishant Joshi
2022-12-23 00:56:09 +05:30
committed by GitHub
parent e51c7664e3
commit 87cedc296e
10 changed files with 110 additions and 129 deletions

View File

@ -212,14 +212,13 @@ pub fn validate_merchant_id(
let request_merchant_id = request_merchant_id.unwrap_or(merchant_id);
utils::when(
merchant_id.ne(request_merchant_id),
utils::when(merchant_id.ne(request_merchant_id), || {
Err(report!(errors::ApiErrorResponse::PreconditionFailed {
message: format!(
"Invalid `merchant_id`: {request_merchant_id} not found in merchant account"
)
})),
)
}))
})
}
#[instrument(skip_all)]
@ -235,15 +234,14 @@ pub fn validate_request_amount_and_amount_to_capture(
api::Amount::Value(amount_inner) => {
// If both amount and amount to capture is present
// then amount to be capture should be less than or equal to request amount
utils::when(
!amount_to_capture.le(&amount_inner.get()),
utils::when(!amount_to_capture.le(&amount_inner.get()), || {
Err(report!(errors::ApiErrorResponse::PreconditionFailed {
message: format!(
"amount_to_capture is greater than amount capture_amount: {:?} request_amount: {:?}",
amount_to_capture, amount
)
})),
)
}))
})
}
api::Amount::Zero => {
// If the amount is Null but still amount_to_capture is passed this is invalid and
@ -370,9 +368,11 @@ pub fn verify_mandate_details(
.mandate_amount
.map(|mandate_amount| request_amount > mandate_amount)
.unwrap_or(true),
Err(report!(errors::ApiErrorResponse::MandateValidationFailed {
reason: "request amount is greater than mandate amount".to_string()
})),
|| {
Err(report!(errors::ApiErrorResponse::MandateValidationFailed {
reason: "request amount is greater than mandate amount".to_string()
}))
},
),
storage::enums::MandateType::MultiUse => utils::when(
mandate
@ -381,9 +381,11 @@ pub fn verify_mandate_details(
(mandate.amount_captured.unwrap_or(0) + request_amount) > mandate_amount
})
.unwrap_or(false),
Err(report!(errors::ApiErrorResponse::MandateValidationFailed {
reason: "request amount is greater than mandate amount".to_string()
})),
|| {
Err(report!(errors::ApiErrorResponse::MandateValidationFailed {
reason: "request amount is greater than mandate amount".to_string()
}))
},
),
}?;
utils::when(
@ -391,9 +393,11 @@ pub fn verify_mandate_details(
.mandate_currency
.map(|mandate_currency| mandate_currency.to_string() != request_currency)
.unwrap_or(false),
Err(report!(errors::ApiErrorResponse::MandateValidationFailed {
reason: "cross currency mandates not supported".to_string()
})),
|| {
Err(report!(errors::ApiErrorResponse::MandateValidationFailed {
reason: "cross currency mandates not supported".to_string()
}))
},
)
}
@ -916,12 +920,14 @@ pub(crate) fn validate_capture_method(
) -> RouterResult<()> {
utils::when(
capture_method == storage_enums::CaptureMethod::Automatic,
Err(report!(errors::ApiErrorResponse::PaymentUnexpectedState {
field_name: "capture_method".to_string(),
current_flow: "captured".to_string(),
current_value: capture_method.to_string(),
states: "manual_single, manual_multiple, scheduled".to_string()
})),
|| {
Err(report!(errors::ApiErrorResponse::PaymentUnexpectedState {
field_name: "capture_method".to_string(),
current_flow: "captured".to_string(),
current_value: capture_method.to_string(),
states: "manual_single, manual_multiple, scheduled".to_string()
}))
},
)
}
@ -929,12 +935,14 @@ pub(crate) fn validate_capture_method(
pub(crate) fn validate_status(status: storage_enums::IntentStatus) -> RouterResult<()> {
utils::when(
status != storage_enums::IntentStatus::RequiresCapture,
Err(report!(errors::ApiErrorResponse::PaymentUnexpectedState {
field_name: "payment.status".to_string(),
current_flow: "captured".to_string(),
current_value: status.to_string(),
states: "requires_capture".to_string()
})),
|| {
Err(report!(errors::ApiErrorResponse::PaymentUnexpectedState {
field_name: "payment.status".to_string(),
current_flow: "captured".to_string(),
current_value: status.to_string(),
states: "requires_capture".to_string()
}))
},
)
}
@ -945,9 +953,11 @@ pub(crate) fn validate_amount_to_capture(
) -> RouterResult<()> {
utils::when(
amount_to_capture.is_some() && (Some(amount) < amount_to_capture),
Err(report!(errors::ApiErrorResponse::InvalidRequestData {
message: "amount_to_capture is greater than amount".to_string()
})),
|| {
Err(report!(errors::ApiErrorResponse::InvalidRequestData {
message: "amount_to_capture is greater than amount".to_string()
}))
},
)
}
@ -983,12 +993,11 @@ pub(super) async fn filter_by_constraints(
pub(super) fn validate_payment_list_request(
req: &api::PaymentListConstraints,
) -> CustomResult<(), errors::ApiErrorResponse> {
utils::when(
req.limit > 100 || req.limit < 1,
utils::when(req.limit > 100 || req.limit < 1, || {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "limit should be in between 1 and 100".to_string(),
}),
)?;
})
})?;
Ok(())
}
@ -1236,10 +1245,9 @@ pub(crate) fn authenticate_client_secret(
payment_intent_client_secret: Option<&String>,
) -> Result<(), errors::ApiErrorResponse> {
match (request_client_secret, payment_intent_client_secret) {
(Some(req_cs), Some(pi_cs)) => utils::when(
req_cs.ne(pi_cs),
Err(errors::ApiErrorResponse::ClientSecretInvalid),
),
(Some(req_cs), Some(pi_cs)) => utils::when(req_cs.ne(pi_cs), || {
Err(errors::ApiErrorResponse::ClientSecretInvalid)
}),
_ => Ok(()),
}
}
@ -1248,12 +1256,11 @@ pub(crate) fn validate_pm_or_token_given(
token: &Option<String>,
pm_data: &Option<api::PaymentMethod>,
) -> Result<(), errors::ApiErrorResponse> {
utils::when(
token.is_none() && pm_data.is_none(),
utils::when(token.is_none() && pm_data.is_none(), || {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "A payment token or payment method data is required".to_string(),
}),
)
})
})
}
// A function to perform database lookup and then verify the client secret