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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(revenue_recovery): support controlled process retriggering (#13572)
CHALLA NISHANTH BABU0ff4f2aa4dcb
diff --git a/crates/api_models/src/process_tracker/revenue_recovery.rs b/crates/api_models/src/process_tracker/revenue_recovery.rsindex 131766e72d..d6c1829c65 100644--- a/crates/api_models/src/process_tracker/revenue_recovery.rs+++ b/crates/api_models/src/process_tracker/revenue_recovery.rs@@ -32,8 +32,13 @@ pub struct RevenueRecoveryRetriggerRequest {#[schema(example = "2022-09-10T10:11:12Z")]#[serde(default, with = "common_utils::custom_serde::iso8601::option")]pub schedule_time: Option<PrimitiveDateTime>,- /// Status of The Process Tracker Task- pub status: enums::ProcessTrackerStatus,- /// Business Status of The Process Tracker Task- pub business_status: String,+ /// Status of The Process Tracker Task. If not provided, the existing+ /// status is retained as is.+ pub status: Option<enums::ProcessTrackerStatus>,+ /// Business Status of The Process Tracker Task. If not provided, the+ /// existing business status is retained as is.+ pub business_status: Option<String>,+ /// Retry count to set on the process tracker task. If not provided, the+ /// existing retry count is retained as is.+ pub retry_count: Option<i32>,}diff --git a/crates/router/src/core/revenue_recovery.rs b/crates/router/src/core/revenue_recovery.rsindex 6f2e065d01..34cf5d633d 100644--- a/crates/router/src/core/revenue_recovery.rs+++ b/crates/router/src/core/revenue_recovery.rs@@ -1160,13 +1160,22 @@ pub async fn resume_revenue_recovery_process_tracker(let pt_update = storage::ProcessTrackerUpdate::Update {name: process_tracker.name.clone(),tracking_data: Some(process_tracker.tracking_data.clone()),- business_status: Some(request_retrigger.business_status.clone()),- status: Some(request_retrigger.status),+ // For business_status and status, use the values from the request+ // if provided, otherwise retain the existing values on the task+ // (None leaves the column unchanged).+ business_status: request_retrigger.business_status.clone(),+ status: request_retrigger.status,updated_at: Some(common_utils::date_time::now()),- retry_count: Some(process_tracker.retry_count + 1),- schedule_time: Some(request_retrigger.schedule_time.unwrap_or(- common_utils::date_time::now().saturating_add(time::Duration::seconds(600)),- )),+ // Use the retry count from the request if provided, otherwise+ // retain the existing retry count on the task.+ retry_count: Some(+ request_retrigger+ .retry_count+ .unwrap_or(process_tracker.retry_count),+ ),+ // Use the schedule time from the request if provided, otherwise+ // retain the existing schedule time on the task.+ schedule_time: request_retrigger.schedule_time,};let updated_pt = db.update_process(process_tracker, pt_update)@@ -1184,8 +1193,91 @@ pub async fn resume_revenue_recovery_process_tracker(};Ok(ApplicationResponse::Json(response))}- IntentStatus::Succeeded- | IntentStatus::Cancelled+ // Payment already charged, but the record-back to the billing connector may have failed+ // (e.g. `site_not_found`). PSYNC only *syncs* the existing attempt (no re-charge) and, on+ // `SuccessfulPayment`, re-runs `record_back_to_billing_connector`. So (re)enqueue a PSYNC+ // task for the executed attempt to retry the record-back.+ IntentStatus::Succeeded => {+ // Sync the intent's *active* (charged) attempt, NOT tracking_data's attempt — the latter+ // is the original failed attempt; the successful retry lives on `active_attempt_id`.+ let payment_intent = db+ .find_payment_intent_by_id(+ &id,+ &revenue_recovery_payment_data.key_store,+ revenue_recovery_payment_data+ .merchant_account+ .storage_scheme,+ )+ .await+ .change_context(errors::ApiErrorResponse::PaymentNotFound)+ .attach_printable("failed to fetch payment intent to resolve the active attempt")?;+ let active_attempt_id = payment_intent.active_attempt_id.clone().ok_or(report!(+ errors::ApiErrorResponse::GenericNotFoundError {+ message: "Succeeded recovery intent has no active attempt to sync".to_owned(),+ }+ ))?;++ let psync_task = PSYNC_WORKFLOW;+ let psync_process_tracker_id =+ active_attempt_id.get_psync_revenue_recovery_id(psync_task, runner);++ let psync_pt = match db+ .find_process_by_id(&psync_process_tracker_id)+ .await+ .change_context(errors::ApiErrorResponse::InternalServerError)+ .attach_printable("error retrieving the psync process tracker id")?+ {+ // PSYNC task already present -> reset it so the consumer re-runs it.+ Some(existing_psync) => {+ let pt_update = storage::ProcessTrackerUpdate::Update {+ name: existing_psync.name.clone(),+ tracking_data: Some(existing_psync.tracking_data.clone()),+ // For business_status and status, use the values from the+ // request if provided, otherwise retain the existing values+ // on the task. Note: to re-run a finished PSYNC task the+ // caller must pass a runnable status explicitly.+ business_status: request_retrigger.business_status.clone(),+ status: request_retrigger.status,+ updated_at: Some(common_utils::date_time::now()),+ retry_count: Some(existing_psync.retry_count + 1),+ // Use the schedule time from the request if provided,+ // otherwise retain the existing schedule time on the task.+ schedule_time: request_retrigger.schedule_time,+ };+ db.update_process(existing_psync, pt_update)+ .await+ .change_context(errors::ApiErrorResponse::GenericNotFoundError {+ message: "Failed to update the psync process tracker".to_owned(),+ })?+ }+ // No PSYNC task (record-back originally failed inside EXECUTE) -> insert one.+ None => {+ insert_psync_pcr_task_to_pt(+ tracking_data.billing_mca_id.clone(),+ db,+ tracking_data.merchant_id.clone(),+ tracking_data.global_payment_id.clone(),+ tracking_data.profile_id.clone(),+ active_attempt_id.clone(),+ runner,+ tracking_data.revenue_recovery_retry,+ state.conf.application_source,+ )+ .await?+ }+ };++ let response = revenue_recovery::RevenueRecoveryResponse {+ id: psync_pt.id,+ name: psync_pt.name,+ schedule_time_for_payment: None,+ schedule_time_for_psync: psync_pt.schedule_time,+ status: psync_pt.status,+ business_status: psync_pt.business_status,+ };+ Ok(ApplicationResponse::Json(response))+ }+ IntentStatus::Cancelled| IntentStatus::CancelledPostCapture| IntentStatus::Processing| IntentStatus::RequiresCustomerAction@@ -1199,7 +1291,6 @@ pub async fn resume_revenue_recovery_process_tracker(| IntentStatus::PartiallyCapturedAndProcessing| IntentStatus::Conflicted| IntentStatus::Expired- | IntentStatus::PartiallyCapturedAndProcessing| IntentStatus::Review => Err(report!(errors::ApiErrorResponse::NotSupported {message: "Invalid Payment Status ".to_owned(),})),