Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix 3DS mandates, for the connector _mandate_details to be stored in the payment_methods table #4323

Merged
merged 9 commits into from
Apr 19, 2024
31 changes: 22 additions & 9 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ pub async fn get_token_pm_type_mandate_details(
mandate_type: Option<api::MandateTransactionType>,
merchant_account: &domain::MerchantAccount,
merchant_key_store: &domain::MerchantKeyStore,
payment_method_id: Option<String>,
) -> RouterResult<MandateGenericData> {
let mandate_data = request.mandate_data.clone().map(MandateData::foreign_from);
let (
Expand Down Expand Up @@ -529,15 +530,27 @@ pub async fn get_token_pm_type_mandate_details(
}
}
}
None => (
request.payment_token.to_owned(),
request.payment_method,
request.payment_method_type,
mandate_data,
None,
None,
None,
),
None => {
let payment_method_info = payment_method_id
.async_map(|payment_method_id| async move {
state
.store
.find_payment_method(&payment_method_id, merchant_account.storage_scheme)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)
})
.await
.transpose()?;
(
request.payment_token.to_owned(),
request.payment_method,
request.payment_method_type,
mandate_data,
None,
None,
payment_method_info,
)
}
};
Ok(MandateGenericData {
token: payment_token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
mandate_type.to_owned(),
merchant_account,
key_store,
payment_attempt.payment_method_id.clone(),
)
.await?;
let token = token.or_else(|| payment_attempt.payment_token.clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
m_mandate_type,
&m_merchant_account,
&m_key_store,
None,
)
.await
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
mandate_type.clone(),
merchant_account,
merchant_key_store,
None,
)
.await?;

Expand Down
32 changes: 21 additions & 11 deletions crates/router/src/core/payments/operations/payment_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
core::{
errors::{self, RouterResult, StorageErrorExt},
mandate,
payment_methods::PaymentMethodRetrieve,
payment_methods::{self, PaymentMethodRetrieve},
payments::{
self,
helpers::{
Expand Down Expand Up @@ -886,6 +886,8 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
.in_current_span(),
);

// When connector requires redirection for mandate creation it can update the connector mandate_id in payment_methods during Psync and CompleteAuthorize

let flow_name = core_utils::get_flow_name::<F>()?;
if flow_name == "PSync" || flow_name == "CompleteAuthorize" {
let connector_mandate_id = match router_data.response.clone() {
Expand All @@ -904,23 +906,31 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
},
Err(_) => None,
};
if let Some(ref payment_method) = payment_data.payment_method_info {
payments::tokenization::update_connector_mandate_details_in_payment_method(
if let Some(payment_method) = payment_data.payment_method_info.clone() {
let connector_mandate_details =
payments::tokenization::update_connector_mandate_details_in_payment_method(
Narayanbhat166 marked this conversation as resolved.
Show resolved Hide resolved
payment_method.clone(),
payment_method.payment_method_type,
Some(payment_data.payment_attempt.amount),
payment_data.payment_attempt.currency,
payment_data.payment_attempt.merchant_connector_id.clone(),
connector_mandate_id,
)?;
payment_methods::cards::update_payment_method_connector_mandate_details(
&*state.store,
payment_method.clone(),
payment_method.payment_method_type,
Some(payment_data.payment_attempt.amount),
payment_data.payment_attempt.currency,
payment_data.payment_attempt.merchant_connector_id.clone(),
connector_mandate_id,
connector_mandate_details,
storage_scheme,
)
.await?;
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to update payment method in db")?
}
}

// When connector requires redirection for mandate creation it can update the connector mandate_id during Psync and CompleteAuthorize
let m_db = state.clone().store;
let m_payment_method_id = payment_data.payment_attempt.payment_method_id.clone();
let m_router_data_merchant_id = router_data.merchant_id.clone();
let m_payment_method_id = payment_data.payment_attempt.payment_method_id.clone();
let m_payment_data_mandate_id =
payment_data
.payment_attempt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
mandate_type.to_owned(),
merchant_account,
key_store,
None,
)
.await?;
helpers::validate_amount_to_capture_and_capture_method(Some(&payment_attempt), request)?;
Expand Down
9 changes: 4 additions & 5 deletions crates/router/src/core/payments/tokenization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ where
currency,
connector.merchant_connector_id.clone(),
connector_mandate_id.clone(),
)
.await?;
)?;

payment_methods::cards::update_payment_method_connector_mandate_details(db, pm, connector_mandate_details, merchant_account.storage_scheme).await.change_context(
errors::ApiErrorResponse::InternalServerError,
Expand Down Expand Up @@ -373,8 +372,7 @@ where
currency,
connector.merchant_connector_id.clone(),
connector_mandate_id.clone(),
)
.await?;
)?;

payment_methods::cards::update_payment_method_connector_mandate_details(db, pm.clone(), connector_mandate_details, merchant_account.storage_scheme).await.change_context(
errors::ApiErrorResponse::InternalServerError,
Expand Down Expand Up @@ -809,7 +807,7 @@ pub fn add_connector_mandate_details_in_payment_method(
}
}

pub async fn update_connector_mandate_details_in_payment_method(
pub fn update_connector_mandate_details_in_payment_method(
payment_method: diesel_models::PaymentMethod,
payment_method_type: Option<storage_enums::PaymentMethodType>,
authorized_amount: Option<i64>,
Expand Down Expand Up @@ -866,5 +864,6 @@ pub async fn update_connector_mandate_details_in_payment_method(
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unable to serialize customer acceptance to value")?;

Ok(connector_mandate_details)
}
Loading