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

fix(revenue_recovery): fix the token fetch for adaptive_retry (#13939)

CHALLA NISHANTH BABUcd803385b712
diff --git a/crates/router/src/core/revenue_recovery.rs b/crates/router/src/core/revenue_recovery.rs
index cf670eca91..9d0ca0a797 100644
--- a/crates/router/src/core/revenue_recovery.rs
+++ b/crates/router/src/core/revenue_recovery.rs
@@ -295,21 +295,26 @@ pub async fn perform_execute_payment(
types::Decision::Execute => {
let connector_customer_id = revenue_recovery_metadata.get_connector_customer_id();
- let last_token_used = payment_intent
- .feature_metadata
- .as_ref()
- .and_then(|fm| fm.payment_revenue_recovery_metadata.as_ref())
- .map(|rr| {
- rr.billing_connector_payment_details
- .payment_processor_token
- .clone()
- });
+ let last_token_used =
+ revenue_recovery_workflow::get_invoice_payment_processor_token(payment_intent);
+
+ // Same dimensions calculate resolves the flag on
+ let adaptive_retry_enabled = crate::core::configs::dimension_state::Dimensions::new()
+ .with_processor_merchant_id(payment_intent.merchant_id.clone().into())
+ .with_connector(revenue_recovery_payment_data.billing_mca.connector_name)
+ .get_adaptive_retry_enabled(
+ state.store.as_ref(),
+ state.superposition_service.as_ref(),
+ None,
+ )
+ .await;
let processor_token = storage::revenue_recovery_redis_operation::RedisTokenManager::get_token_based_on_retry_type(
state,
&connector_customer_id,
tracking_data.revenue_recovery_retry,
last_token_used.as_deref(),
+ adaptive_retry_enabled,
)
.await
.change_context(errors::ApiErrorResponse::GenericNotFoundError {
diff --git a/crates/router/src/types/storage/revenue_recovery_redis_operation.rs b/crates/router/src/types/storage/revenue_recovery_redis_operation.rs
index 749acbd097..56fb8482f8 100644
--- a/crates/router/src/types/storage/revenue_recovery_redis_operation.rs
+++ b/crates/router/src/types/storage/revenue_recovery_redis_operation.rs
@@ -935,12 +935,34 @@ impl RedisTokenManager {
Ok(all_hard_declined)
}
+ // Get the token this invoice last used, by id
+ async fn get_invoice_token(
+ state: &SessionState,
+ connector_customer_id: &str,
+ last_token_used: Option<&str>,
+ ) -> CustomResult<Option<PaymentProcessorTokenStatus>, errors::StorageError> {
+ match last_token_used {
+ Some(token_id) => {
+ Self::get_payment_processor_token_using_token_id(
+ state,
+ connector_customer_id,
+ token_id,
+ )
+ .await
+ }
+ None => Ok(None),
+ }
+ }
+
// Get token based on retry type
+ // Adaptive smart decides against the invoice's own token, so it resolves by id rather than
+ // the `scheduled_at` marker, which only the decider writes and is shared across invoices.
pub async fn get_token_based_on_retry_type(
state: &SessionState,
connector_customer_id: &str,
retry_algorithm_type: RevenueRecoveryAlgorithmType,
last_token_used: Option<&str>,
+ adaptive_retry_enabled: bool,
) -> CustomResult<Option<PaymentProcessorTokenStatus>, errors::StorageError> {
let mut token = None;
match retry_algorithm_type {
@@ -949,17 +971,13 @@ impl RedisTokenManager {
}
RevenueRecoveryAlgorithmType::Cascading => {
- token = match last_token_used {
- Some(token_id) => {
- Self::get_payment_processor_token_using_token_id(
- state,
- connector_customer_id,
- token_id,
- )
- .await?
- }
- None => None,
- };
+ token =
+ Self::get_invoice_token(state, connector_customer_id, last_token_used).await?;
+ }
+
+ RevenueRecoveryAlgorithmType::Smart if adaptive_retry_enabled => {
+ token =
+ Self::get_invoice_token(state, connector_customer_id, last_token_used).await?;
}
RevenueRecoveryAlgorithmType::Smart => {
diff --git a/crates/router/src/workflows/revenue_recovery.rs b/crates/router/src/workflows/revenue_recovery.rs
index c18e5d7be1..329b81f002 100644
--- a/crates/router/src/workflows/revenue_recovery.rs
+++ b/crates/router/src/workflows/revenue_recovery.rs
@@ -807,16 +807,11 @@ pub async fn get_token_with_schedule_time_based_on_retry_algorithm_type(
))
}
-/// Check the invoice's payment processor token against a schedule time already decided.
-/// Shared by the cascading and adaptive paths so both gate on the same conditions
#[cfg(feature = "v2")]
-async fn get_token_availability_for_schedule_time(
- state: &SessionState,
- connector_customer_id: &str,
+pub(crate) fn get_invoice_payment_processor_token(
payment_intent: &PaymentIntent,
- scheduled_time: time::PrimitiveDateTime,
-) -> CustomResult<PaymentProcessorTokenResponse, errors::ProcessTrackerError> {
- let payment_processor_token = payment_intent
+) -> Option<String> {
+ payment_intent
.feature_metadata
.as_ref()
.and_then(|metadata| metadata.payment_revenue_recovery_metadata.as_ref())
@@ -825,7 +820,19 @@ async fn get_token_availability_for_schedule_time(
.billing_connector_payment_details
.payment_processor_token
.clone()
- });
+ })
+}
+
+/// Check the invoice's payment processor token against a schedule time already decided.
+/// Shared by the cascading and adaptive paths so both gate on the same conditions
+#[cfg(feature = "v2")]
+async fn get_token_availability_for_schedule_time(
+ state: &SessionState,
+ connector_customer_id: &str,
+ payment_intent: &PaymentIntent,
+ scheduled_time: time::PrimitiveDateTime,
+) -> CustomResult<PaymentProcessorTokenResponse, errors::ProcessTrackerError> {
+ let payment_processor_token = get_invoice_payment_processor_token(payment_intent);
let payment_processor_tokens_details =
RedisTokenManager::get_payment_processor_metadata_for_connector_customer(