mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-03 05:17:02 +08:00
fix: resolve TODO and FIXME in utils module (#220)
This commit is contained in:
@ -583,10 +583,9 @@ pub async fn list_payments(
|
||||
.into_iter()
|
||||
.map(ForeignInto::foreign_into)
|
||||
.collect();
|
||||
utils::when(
|
||||
data.is_empty(),
|
||||
Err(errors::ApiErrorResponse::PaymentNotFound),
|
||||
)?;
|
||||
utils::when(data.is_empty(), || {
|
||||
Err(errors::ApiErrorResponse::PaymentNotFound)
|
||||
})?;
|
||||
Ok(services::BachResponse::Json(api::PaymentListResponse {
|
||||
size: data.len(),
|
||||
data,
|
||||
|
||||
@ -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
|
||||
|
||||
@ -222,10 +222,9 @@ impl<F: Clone + Send> Domain<F, api::PaymentsRequest> for PaymentConfirm {
|
||||
)
|
||||
.await?;
|
||||
|
||||
utils::when(
|
||||
payment_method.is_none(),
|
||||
Err(errors::ApiErrorResponse::PaymentMethodNotFound),
|
||||
)?;
|
||||
utils::when(payment_method.is_none(), || {
|
||||
Err(errors::ApiErrorResponse::PaymentMethodNotFound)
|
||||
})?;
|
||||
|
||||
Ok((op, payment_method, payment_token))
|
||||
}
|
||||
|
||||
@ -251,12 +251,14 @@ async fn get_tracker_for_sync<
|
||||
|
||||
utils::when(
|
||||
request.force_sync && !helpers::can_call_connector(payment_intent.status),
|
||||
Err(ApiErrorResponse::InvalidRequestData {
|
||||
message: format!(
|
||||
"cannot perform force_sync as status: {}",
|
||||
payment_intent.status
|
||||
),
|
||||
}),
|
||||
|| {
|
||||
Err(ApiErrorResponse::InvalidRequestData {
|
||||
message: format!(
|
||||
"cannot perform force_sync as status: {}",
|
||||
payment_intent.status
|
||||
),
|
||||
})
|
||||
},
|
||||
)?;
|
||||
|
||||
let refunds = db
|
||||
|
||||
@ -48,14 +48,13 @@ pub async fn refund_create_core(
|
||||
amount = req.amount.unwrap_or(payment_attempt.amount); // FIXME: Need to that capture amount
|
||||
|
||||
//TODO: Can we change the flow based on some workflow idea
|
||||
utils::when(
|
||||
amount <= 0,
|
||||
utils::when(amount <= 0, || {
|
||||
Err(report!(errors::ApiErrorResponse::InvalidDataFormat {
|
||||
field_name: "amount".to_string(),
|
||||
expected_format: "positive integer".to_string()
|
||||
})
|
||||
.attach_printable("amount less than zero")),
|
||||
)?;
|
||||
.attach_printable("amount less than zero"))
|
||||
})?;
|
||||
|
||||
payment_intent = db
|
||||
.find_payment_intent_by_payment_id_merchant_id(
|
||||
@ -68,8 +67,10 @@ pub async fn refund_create_core(
|
||||
|
||||
utils::when(
|
||||
payment_intent.status != enums::IntentStatus::Succeeded,
|
||||
Err(report!(errors::ApiErrorResponse::PaymentNotSucceeded)
|
||||
.attach_printable("unable to refund for a unsuccessful payment intent")),
|
||||
|| {
|
||||
Err(report!(errors::ApiErrorResponse::PaymentNotSucceeded)
|
||||
.attach_printable("unable to refund for a unsuccessful payment intent"))
|
||||
},
|
||||
)?;
|
||||
|
||||
validate_and_create_refund(
|
||||
@ -363,14 +364,13 @@ pub async fn validate_and_create_refund(
|
||||
.as_ref()
|
||||
.map(|merchant_id| merchant_id != &merchant_account.merchant_id);
|
||||
|
||||
utils::when(
|
||||
predicate.unwrap_or(false),
|
||||
utils::when(predicate.unwrap_or(false), || {
|
||||
Err(report!(errors::ApiErrorResponse::InvalidDataFormat {
|
||||
field_name: "merchant_id".to_string(),
|
||||
expected_format: "merchant_id from merchant account".to_string()
|
||||
})
|
||||
.attach_printable("invalid merchant_id in request")),
|
||||
)?;
|
||||
.attach_printable("invalid merchant_id in request"))
|
||||
})?;
|
||||
|
||||
let refund = match validator::validate_uniqueness_of_refund_id_against_merchant_id(
|
||||
db,
|
||||
|
||||
@ -60,9 +60,11 @@ pub fn validate_refund_amount(
|
||||
|
||||
utils::when(
|
||||
refund_amount > (payment_attempt_amount - total_refunded_amount),
|
||||
Err(report!(
|
||||
RefundValidationError::RefundAmountExceedsPaymentAmount
|
||||
)),
|
||||
|| {
|
||||
Err(report!(
|
||||
RefundValidationError::RefundAmountExceedsPaymentAmount
|
||||
))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@ -74,7 +76,7 @@ pub fn validate_payment_order_age(
|
||||
|
||||
utils::when(
|
||||
(current_time - *created_at).whole_days() > REFUND_MAX_AGE,
|
||||
Err(report!(RefundValidationError::OrderExpired)),
|
||||
|| Err(report!(RefundValidationError::OrderExpired)),
|
||||
)
|
||||
}
|
||||
|
||||
@ -83,10 +85,9 @@ pub fn validate_maximum_refund_against_payment_attempt(
|
||||
all_refunds: &[storage::Refund],
|
||||
) -> CustomResult<(), RefundValidationError> {
|
||||
// TODO: Make this configurable
|
||||
utils::when(
|
||||
all_refunds.len() > REFUND_MAX_ATTEMPTS,
|
||||
Err(report!(RefundValidationError::MaxRefundCountReached)),
|
||||
)
|
||||
utils::when(all_refunds.len() > REFUND_MAX_ATTEMPTS, || {
|
||||
Err(report!(RefundValidationError::MaxRefundCountReached))
|
||||
})
|
||||
}
|
||||
|
||||
#[instrument(skip(db))]
|
||||
|
||||
Reference in New Issue
Block a user