Skip to content

Commit

Permalink
refactor(core): locker call made synchronous for updation of pm_id (#…
Browse files Browse the repository at this point in the history
…4289)

Co-authored-by: Narayan Bhat <[email protected]>
  • Loading branch information
prajjwalkumar17 and Narayanbhat166 authored Apr 3, 2024
1 parent 2a691a5 commit 6e94a56
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 45 deletions.
6 changes: 3 additions & 3 deletions crates/data_models/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ pub enum PaymentAttemptUpdate {
connector: Option<String>,
connector_transaction_id: Option<String>,
authentication_type: Option<storage_enums::AuthenticationType>,
payment_method_id: Option<Option<String>>,
payment_method_id: Option<String>,
mandate_id: Option<String>,
connector_metadata: Option<serde_json::Value>,
payment_token: Option<String>,
Expand All @@ -367,7 +367,7 @@ pub enum PaymentAttemptUpdate {
status: storage_enums::AttemptStatus,
connector: Option<String>,
connector_transaction_id: Option<String>,
payment_method_id: Option<Option<String>>,
payment_method_id: Option<String>,
error_code: Option<Option<String>>,
error_message: Option<Option<String>>,
error_reason: Option<Option<String>>,
Expand Down Expand Up @@ -403,7 +403,7 @@ pub enum PaymentAttemptUpdate {
},
PreprocessingUpdate {
status: storage_enums::AttemptStatus,
payment_method_id: Option<Option<String>>,
payment_method_id: Option<String>,
connector_metadata: Option<serde_json::Value>,
preprocessing_step_id: Option<String>,
connector_transaction_id: Option<String>,
Expand Down
12 changes: 6 additions & 6 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub enum PaymentAttemptUpdate {
connector: Option<String>,
connector_transaction_id: Option<String>,
authentication_type: Option<storage_enums::AuthenticationType>,
payment_method_id: Option<Option<String>>,
payment_method_id: Option<String>,
mandate_id: Option<String>,
connector_metadata: Option<serde_json::Value>,
payment_token: Option<String>,
Expand All @@ -274,7 +274,7 @@ pub enum PaymentAttemptUpdate {
status: storage_enums::AttemptStatus,
connector: Option<String>,
connector_transaction_id: Option<String>,
payment_method_id: Option<Option<String>>,
payment_method_id: Option<String>,
error_code: Option<Option<String>>,
error_message: Option<Option<String>>,
error_reason: Option<Option<String>>,
Expand Down Expand Up @@ -310,7 +310,7 @@ pub enum PaymentAttemptUpdate {
},
PreprocessingUpdate {
status: storage_enums::AttemptStatus,
payment_method_id: Option<Option<String>>,
payment_method_id: Option<String>,
connector_metadata: Option<serde_json::Value>,
preprocessing_step_id: Option<String>,
connector_transaction_id: Option<String>,
Expand Down Expand Up @@ -350,7 +350,7 @@ pub struct PaymentAttemptUpdateInternal {
authentication_type: Option<storage_enums::AuthenticationType>,
payment_method: Option<storage_enums::PaymentMethod>,
error_message: Option<Option<String>>,
payment_method_id: Option<Option<String>>,
payment_method_id: Option<String>,
cancellation_reason: Option<String>,
modified_at: Option<PrimitiveDateTime>,
mandate_id: Option<String>,
Expand Down Expand Up @@ -459,7 +459,7 @@ impl PaymentAttemptUpdate {
authentication_type: authentication_type.or(source.authentication_type),
payment_method: payment_method.or(source.payment_method),
error_message: error_message.unwrap_or(source.error_message),
payment_method_id: payment_method_id.unwrap_or(source.payment_method_id),
payment_method_id: payment_method_id.or(source.payment_method_id),
cancellation_reason: cancellation_reason.or(source.cancellation_reason),
modified_at: common_utils::date_time::now(),
mandate_id: mandate_id.or(source.mandate_id),
Expand Down Expand Up @@ -605,7 +605,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
authentication_id,
payment_method_billing_address_id,
fingerprint_id,
payment_method_id: payment_method_id.map(Some),
payment_method_id,
..Default::default()
},
PaymentAttemptUpdate::VoidUpdate {
Expand Down
4 changes: 3 additions & 1 deletion crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2136,7 +2136,9 @@ where
)
.await?;
payment_data.payment_method_data = payment_method_data;
payment_data.payment_attempt.payment_method_id = pm_id;
if let Some(payment_method_id) = pm_id {
payment_data.payment_attempt.payment_method_id = Some(payment_method_id);
}
payment_data
} else {
payment_data
Expand Down
84 changes: 53 additions & 31 deletions crates/router/src/core/payments/flows/authorize_flow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use error_stack;
use router_env::tracing::Instrument;

// use router_env::tracing::Instrument;
use super::{ConstructFlowSpecificData, Feature};
use crate::{
core::{
Expand Down Expand Up @@ -118,43 +118,65 @@ impl Feature<api::Authorize, types::PaymentsAuthorizeData> for types::PaymentsAu
)
.await?)
} else {
let connector = connector.clone();
let response = resp.clone();
let maybe_customer = maybe_customer.clone();
let merchant_account = merchant_account.clone();
let key_store = key_store.clone();
let state = state.clone();

logger::info!("Call to save_payment_method in locker");
let _task_handle = tokio::spawn(
async move {
logger::info!("Starting async call to save_payment_method in locker");

let result = Box::pin(tokenization::save_payment_method(
&state,
&connector,
response,
&maybe_customer,
&merchant_account,
self.request.payment_method_type,
&key_store,
Some(resp.request.amount),
Some(resp.request.currency),
))
.await;

if let Err(err) = result {
logger::error!(
"Asynchronously saving card in locker failed : {:?}",
err
);
}

let pm = Box::pin(tokenization::save_payment_method(
state,
connector,
response,
maybe_customer,
merchant_account,
self.request.payment_method_type,
key_store,
Some(resp.request.amount),
Some(resp.request.currency),
))
.await;

match pm {
Ok((payment_method_id, payment_method_status)) => {
resp.payment_method_id = payment_method_id.clone();
resp.payment_method_status = payment_method_status;
}
.in_current_span(),
);
Err(_) => logger::error!("Save pm to locker failed"),
}

Ok(resp)
}

// Async locker code (Commenting out the code for near future refactors)
// logger::info!("Call to save_payment_method in locker");
// let _task_handle = tokio::spawn(
// async move {
// logger::info!("Starting async call to save_payment_method in locker");
//
// let result = Box::pin(tokenization::save_payment_method(
// &state,
// &connector,
// response,
// &maybe_customer,
// &merchant_account,
// self.request.payment_method_type,
// &key_store,
// Some(resp.request.amount),
// Some(resp.request.currency),
// ))
// .await;
//
// if let Err(err) = result {
// logger::error!(
// "Asynchronously saving card in locker failed : {:?}",
// err
// );
// }
// }
// .in_current_span(),
// );
//
// Ok(resp)
// }
} else {
Ok(self.clone())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
let payment_attempt_update =
storage::PaymentAttemptUpdate::PreprocessingUpdate {
status: updated_attempt_status,
payment_method_id: Some(router_data.payment_method_id),
payment_method_id: router_data.payment_method_id,
connector_metadata,
preprocessing_step_id,
connector_transaction_id,
Expand Down Expand Up @@ -676,7 +676,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
amount_capturable: router_data
.request
.get_amount_capturable(&payment_data, updated_attempt_status),
payment_method_id: Some(payment_method_id),
payment_method_id,
mandate_id: payment_data
.mandate_id
.clone()
Expand Down Expand Up @@ -715,7 +715,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
status: updated_attempt_status,
connector: None,
connector_transaction_id,
payment_method_id: Some(router_data.payment_method_id),
payment_method_id: router_data.payment_method_id,
error_code: Some(reason.clone().map(|cd| cd.code)),
error_message: Some(reason.clone().map(|cd| cd.message)),
error_reason: Some(reason.map(|cd| cd.message)),
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/core/payments/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ where
.connector_response_reference_id
.clone(),
authentication_type: None,
payment_method_id: Some(router_data.payment_method_id),
payment_method_id: router_data.payment_method_id,
mandate_id: payment_data
.mandate_id
.clone()
Expand Down

0 comments on commit 6e94a56

Please sign in to comment.