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

2.7 KB
Raw Blame
//! The recovery request workflow, which records a transaction the merchant reported on a profile
//! whose retries it triggers, decides the next retry of its invoice, and tells the merchant by
//! webhook.

use common_utils::errors::CustomResult;
#[cfg(feature = "v2")]
use common_utils::ext_traits::ValueExt;
#[cfg(feature = "v2")]
use router_env::logger;
#[cfg(feature = "v1")]
use scheduler::consumer;
use scheduler::{consumer::workflows::ProcessTrackerWorkflow, errors};

#[cfg(feature = "v2")]
use crate::{core::revenue_recovery::external_triggers, types::storage::revenue_recovery as pcr};
use crate::{routes::SessionState, types::storage};

pub struct RecoveryRequestWorkflow;

#[async_trait::async_trait]
impl ProcessTrackerWorkflow<SessionState> for RecoveryRequestWorkflow {
    #[cfg(feature = "v1")]
    async fn execute_workflow<'a>(
        &'a self,
        _state: &'a SessionState,
        _process: storage::ProcessTracker,
    ) -> Result<(), errors::ProcessTrackerError> {
        Ok(())
    }

    #[cfg(feature = "v2")]
    async fn execute_workflow<'a>(
        &'a self,
        state: &'a SessionState,
        process: storage::ProcessTracker,
    ) -> Result<(), errors::ProcessTrackerError> {
        let tracking_data = match process
            .tracking_data
            .clone()
            .parse_value::<pcr::RecoveryRequestTrackingData>("RecoveryRequestTrackingData")
        {
            Ok(tracking_data) => tracking_data,
            Err(error) => {
                logger::error!(
                    ?error,
                    process_id = %process.id,
                    "The recovery request's tracking data cannot be read"
                );
                return external_triggers::finish_failed_recovery_request(state, &process).await;
            }
        };
        Box::pin(external_triggers::process_recovery_request(
            state,
            &process,
            tracking_data,
        ))
        .await
    }

    #[cfg(feature = "v1")]
    async fn error_handler<'a>(
        &'a self,
        state: &'a SessionState,
        process: storage::ProcessTracker,
        error: errors::ProcessTrackerError,
    ) -> CustomResult<(), errors::ProcessTrackerError> {
        consumer::consumer_error_handler(state.store.as_scheduler(), process, error).await
    }

    /// The workflow returns an error only for a temporary one, so the recovery request runs again.
    #[cfg(feature = "v2")]
    async fn error_handler<'a>(
        &'a self,
        state: &'a SessionState,
        process: storage::ProcessTracker,
        error: errors::ProcessTrackerError,
    ) -> CustomResult<(), errors::ProcessTrackerError> {
        external_triggers::retry_recovery_request(state, process, error).await
    }
}