From e978e9d66bcb8ea20837fa0e87aa0b0ffffac622 Mon Sep 17 00:00:00 2001 From: Sai Harsha Vardhan <56996463+sai-harsha-vardhan@users.noreply.github.com> Date: Wed, 5 Jul 2023 17:51:57 +0530 Subject: [PATCH] refactor(router): add psync task to process tracker after building connector request in payments flow (#1603) --- crates/router/src/core/payments.rs | 53 ++++++++++++++----- crates/router/src/core/payments/helpers.rs | 19 +------ crates/router/src/core/payments/operations.rs | 1 + .../operations/payment_complete_authorize.rs | 1 + .../payments/operations/payment_confirm.rs | 3 +- .../payments/operations/payment_create.rs | 1 + .../payments/operations/payment_status.rs | 3 +- .../payments/operations/payment_update.rs | 1 + 8 files changed, 49 insertions(+), 33 deletions(-) diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs index 76c9c4321b..9008b97b97 100644 --- a/crates/router/src/core/payments.rs +++ b/crates/router/src/core/payments.rs @@ -35,7 +35,7 @@ use crate::{ db::StorageInterface, logger, routes::{metrics, AppState}, - scheduler::utils as pt_utils, + scheduler::{utils as pt_utils, workflows::payment_sync}, services::{self, api::Authenticate}, types::{ self, api, domain, @@ -119,6 +119,26 @@ where ) .await?; + let schedule_time = match &connector { + Some(api::ConnectorCallType::Single(connector_data)) => { + if should_add_task_to_process_tracker(&payment_data) { + payment_sync::get_sync_process_schedule_time( + &*state.store, + connector_data.connector.id(), + &merchant_account.merchant_id, + 0, + ) + .await + .into_report() + .change_context(errors::ApiErrorResponse::InternalServerError) + .attach_printable("Failed while getting process schedule time")? + } else { + None + } + } + _ => None, + }; + let (mut payment_data, tokenization_action) = get_connector_tokenization_action(state, &operation, payment_data, &validate_result) .await?; @@ -133,19 +153,6 @@ where .await?; if let Some(connector_details) = connector { - if should_add_task_to_process_tracker(&payment_data) { - operation - .to_domain()? - .add_task_to_process_tracker( - state, - &payment_data.payment_attempt, - validate_result.requeue, - ) - .await - .map_err(|error| logger::error!(process_tracker_error=?error)) - .ok(); - } - payment_data = match connector_details { api::ConnectorCallType::Single(connector) => { let router_data = call_connector_service( @@ -159,6 +166,8 @@ where call_connector_action, tokenization_action, updated_customer, + validate_result.requeue, + schedule_time, ) .await?; @@ -521,6 +530,8 @@ pub async fn call_connector_service( call_connector_action: CallConnectorAction, tokenization_action: TokenizationAction, updated_customer: Option, + requeue: bool, + schedule_time: Option, ) -> RouterResult> where F: Send + Clone + Sync, @@ -592,6 +603,20 @@ where (None, false) }; + if should_add_task_to_process_tracker(payment_data) { + operation + .to_domain()? + .add_task_to_process_tracker( + state, + &payment_data.payment_attempt, + requeue, + schedule_time, + ) + .await + .map_err(|error| logger::error!(process_tracker_error=?error)) + .ok(); + } + // Update the payment trackers just before calling the connector // Since the request is already built in the previous step, // there should be no error in request construction from hyperswitch end diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs index c37354f9c5..a611d195bc 100644 --- a/crates/router/src/core/payments/helpers.rs +++ b/crates/router/src/core/payments/helpers.rs @@ -28,7 +28,7 @@ use crate::{ }, db::StorageInterface, routes::{metrics, AppState}, - scheduler::{metrics as scheduler_metrics, workflows::payment_sync}, + scheduler::metrics as scheduler_metrics, services, types::{ api::{self, admin, enums as api_enums, CustomerAcceptanceExt, MandateValidationFieldsExt}, @@ -764,27 +764,12 @@ pub async fn add_domain_task_to_pt( state: &AppState, payment_attempt: &storage::PaymentAttempt, requeue: bool, + schedule_time: Option, ) -> CustomResult<(), errors::ApiErrorResponse> where Op: std::fmt::Debug, { if check_if_operation_confirm(operation) { - let connector_name = payment_attempt - .connector - .clone() - .ok_or(errors::ApiErrorResponse::InternalServerError)?; - - let schedule_time = payment_sync::get_sync_process_schedule_time( - &*state.store, - &connector_name, - &payment_attempt.merchant_id, - 0, - ) - .await - .into_report() - .change_context(errors::ApiErrorResponse::InternalServerError) - .attach_printable("Failed while getting process schedule time")?; - match schedule_time { Some(stime) => { if !requeue { diff --git a/crates/router/src/core/payments/operations.rs b/crates/router/src/core/payments/operations.rs index b0e362280f..21d4846a7c 100644 --- a/crates/router/src/core/payments/operations.rs +++ b/crates/router/src/core/payments/operations.rs @@ -121,6 +121,7 @@ pub trait Domain: Send + Sync { _db: &'a AppState, _payment_attempt: &storage::PaymentAttempt, _requeue: bool, + _schedule_time: Option, ) -> CustomResult<(), errors::ApiErrorResponse> { Ok(()) } diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rs index 47803dc79d..7ac70ad522 100644 --- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs +++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs @@ -292,6 +292,7 @@ impl Domain for CompleteAuthorize { _state: &'a AppState, _payment_attempt: &storage::PaymentAttempt, _requeue: bool, + _schedule_time: Option, ) -> CustomResult<(), errors::ApiErrorResponse> { Ok(()) } diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs index 6307e4639e..2f9d265304 100644 --- a/crates/router/src/core/payments/operations/payment_confirm.rs +++ b/crates/router/src/core/payments/operations/payment_confirm.rs @@ -344,8 +344,9 @@ impl Domain for PaymentConfirm { state: &'a AppState, payment_attempt: &storage::PaymentAttempt, requeue: bool, + schedule_time: Option, ) -> CustomResult<(), errors::ApiErrorResponse> { - helpers::add_domain_task_to_pt(self, state, payment_attempt, requeue).await + helpers::add_domain_task_to_pt(self, state, payment_attempt, requeue, schedule_time).await } async fn get_connector<'a>( diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs index 24b867003a..6802a0b0f2 100644 --- a/crates/router/src/core/payments/operations/payment_create.rs +++ b/crates/router/src/core/payments/operations/payment_create.rs @@ -311,6 +311,7 @@ impl Domain for PaymentCreate { _state: &'a AppState, _payment_attempt: &storage::PaymentAttempt, _requeue: bool, + _schedule_time: Option, ) -> CustomResult<(), errors::ApiErrorResponse> { Ok(()) } diff --git a/crates/router/src/core/payments/operations/payment_status.rs b/crates/router/src/core/payments/operations/payment_status.rs index 90e655f303..c939e5f7a7 100644 --- a/crates/router/src/core/payments/operations/payment_status.rs +++ b/crates/router/src/core/payments/operations/payment_status.rs @@ -94,8 +94,9 @@ impl Domain for PaymentStatus { state: &'a AppState, payment_attempt: &storage::PaymentAttempt, requeue: bool, + schedule_time: Option, ) -> CustomResult<(), errors::ApiErrorResponse> { - helpers::add_domain_task_to_pt(self, state, payment_attempt, requeue).await + helpers::add_domain_task_to_pt(self, state, payment_attempt, requeue, schedule_time).await } async fn get_connector<'a>( diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rs index 9770a86d72..bcb08249a4 100644 --- a/crates/router/src/core/payments/operations/payment_update.rs +++ b/crates/router/src/core/payments/operations/payment_update.rs @@ -379,6 +379,7 @@ impl Domain for PaymentUpdate { _state: &'a AppState, _payment_attempt: &storage::PaymentAttempt, _requeue: bool, + _schedule_time: Option, ) -> CustomResult<(), errors::ApiErrorResponse> { Ok(()) }