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

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

Sakil Mostakbc549d3693e6
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index f2735f5596..78339c6b4a 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -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,