feat(core): add manual retry cutoff duration (#9330)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Sakil Mostak
2025-09-18 13:24:02 +05:30
committed by GitHub
parent a05827e0ed
commit bc549d3693

View File

@ -4448,6 +4448,21 @@ pub fn get_attempt_type(
match payment_intent.status {
enums::IntentStatus::Failed => {
if matches!(is_manual_retry_enabled, Some(true)) {
// if it is false, don't go ahead with manual retry
fp_utils::when(
!validate_manual_retry_cutoff(
payment_intent.created_at,
payment_intent.session_expiry,
),
|| {
Err(report!(errors::ApiErrorResponse::PreconditionFailed {
message:
format!("You cannot {action} this payment using `manual_retry` because the allowed duration has expired")
}
))
},
)?;
metrics::MANUAL_RETRY_REQUEST_COUNT.add(
1,
router_env::metric_attributes!((
@ -4552,6 +4567,27 @@ pub fn get_attempt_type(
}
}
fn validate_manual_retry_cutoff(
created_at: time::PrimitiveDateTime,
session_expiry: Option<time::PrimitiveDateTime>,
) -> bool {
let utc_current_time = time::OffsetDateTime::now_utc();
let primitive_utc_current_time =
time::PrimitiveDateTime::new(utc_current_time.date(), utc_current_time.time());
let time_difference_from_creation = primitive_utc_current_time - created_at;
// cutoff time is 50% of session duration
let cutoff_limit = match session_expiry {
Some(session_expiry) => {
let duration = session_expiry - created_at;
duration.whole_seconds() / 2
}
None => consts::DEFAULT_SESSION_EXPIRY / 2,
};
time_difference_from_creation.whole_seconds() <= cutoff_limit
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum AttemptType {
New,