mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-03 21:37:41 +08:00
feat(payment_methods): Implement Process tracker workflow for Payment method Status update (#4668)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -8,11 +8,18 @@ use api_models::payments::CardToken;
|
||||
#[cfg(feature = "payouts")]
|
||||
pub use api_models::{enums::PayoutConnectors, payouts as payout_types};
|
||||
use diesel_models::enums;
|
||||
use error_stack::ResultExt;
|
||||
use hyperswitch_domain_models::payments::{payment_attempt::PaymentAttempt, PaymentIntent};
|
||||
use router_env::{instrument, tracing};
|
||||
|
||||
use crate::{
|
||||
core::{errors::RouterResult, payments::helpers, pm_auth as core_pm_auth},
|
||||
consts,
|
||||
core::{
|
||||
errors::{self, RouterResult},
|
||||
payments::helpers,
|
||||
pm_auth as core_pm_auth,
|
||||
},
|
||||
db,
|
||||
routes::SessionState,
|
||||
types::{
|
||||
api::{self, payments},
|
||||
@ -20,6 +27,9 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
const PAYMENT_METHOD_STATUS_UPDATE_TASK: &str = "PAYMENT_METHOD_STATUS_UPDATE";
|
||||
const PAYMENT_METHOD_STATUS_TAG: &str = "PAYMENT_METHOD_STATUS";
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn retrieve_payment_method(
|
||||
pm_data: &Option<payments::PaymentMethodData>,
|
||||
@ -94,6 +104,66 @@ pub async fn retrieve_payment_method(
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_task_id_for_payment_method_status_update_workflow(
|
||||
key_id: &str,
|
||||
runner: &storage::ProcessTrackerRunner,
|
||||
task: &str,
|
||||
) -> String {
|
||||
format!("{runner}_{task}_{key_id}")
|
||||
}
|
||||
|
||||
pub async fn add_payment_method_status_update_task(
|
||||
db: &dyn db::StorageInterface,
|
||||
payment_method: &diesel_models::PaymentMethod,
|
||||
prev_status: enums::PaymentMethodStatus,
|
||||
curr_status: enums::PaymentMethodStatus,
|
||||
merchant_id: &str,
|
||||
) -> Result<(), errors::ProcessTrackerError> {
|
||||
let created_at = payment_method.created_at;
|
||||
let schedule_time =
|
||||
created_at.saturating_add(time::Duration::seconds(consts::DEFAULT_SESSION_EXPIRY));
|
||||
|
||||
let tracking_data = storage::PaymentMethodStatusTrackingData {
|
||||
payment_method_id: payment_method.payment_method_id.clone(),
|
||||
prev_status,
|
||||
curr_status,
|
||||
merchant_id: merchant_id.to_string(),
|
||||
};
|
||||
|
||||
let runner = storage::ProcessTrackerRunner::PaymentMethodStatusUpdateWorkflow;
|
||||
let task = PAYMENT_METHOD_STATUS_UPDATE_TASK;
|
||||
let tag = [PAYMENT_METHOD_STATUS_TAG];
|
||||
|
||||
let process_tracker_id = generate_task_id_for_payment_method_status_update_workflow(
|
||||
payment_method.payment_method_id.as_str(),
|
||||
&runner,
|
||||
task,
|
||||
);
|
||||
let process_tracker_entry = storage::ProcessTrackerNew::new(
|
||||
process_tracker_id,
|
||||
task,
|
||||
runner,
|
||||
tag,
|
||||
tracking_data,
|
||||
schedule_time,
|
||||
)
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)
|
||||
.attach_printable("Failed to construct PAYMENT_METHOD_STATUS_UPDATE process tracker task")?;
|
||||
|
||||
db
|
||||
.insert_process(process_tracker_entry)
|
||||
.await
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)
|
||||
.attach_printable_lazy(|| {
|
||||
format!(
|
||||
"Failed while inserting PAYMENT_METHOD_STATUS_UPDATE reminder to process_tracker for payment_method_id: {}",
|
||||
payment_method.payment_method_id.clone()
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn retrieve_payment_method_with_token(
|
||||
state: &SessionState,
|
||||
|
||||
Reference in New Issue
Block a user