Skip to content

Commit

Permalink
fix(router): validate each field for migration request body (#6525)
Browse files Browse the repository at this point in the history
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
prasunna09 and hyperswitch-bot[bot] authored Dec 6, 2024
1 parent 15f873b commit b5d3d49
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use common_utils::{
consts,
crypto::{self, Encryptable},
encryption::Encryption,
ext_traits::{AsyncExt, BytesExt, Encode, StringExt, ValueExt},
ext_traits::{AsyncExt, BytesExt, ConfigExt, Encode, StringExt, ValueExt},
generate_id, id_type,
request::Request,
type_name,
Expand All @@ -55,6 +55,7 @@ use hyperswitch_domain_models::customer::CustomerUpdate;
use kgraph_utils::transformers::IntoDirValue;
use masking::Secret;
use router_env::{instrument, metrics::add_attributes, tracing};
use serde_json::json;
use strum::IntoEnumIterator;

use super::{
Expand Down Expand Up @@ -381,7 +382,7 @@ pub async fn migrate_payment_method(
key_store: &domain::MerchantKeyStore,
) -> errors::RouterResponse<api::PaymentMethodMigrateResponse> {
let mut req = req;
let card_details = req.card.as_ref().get_required_value("card")?;
let card_details = &req.card.get_required_value("card")?;

let card_number_validation_result =
cards::CardNumber::from_str(card_details.card_number.peek());
Expand Down Expand Up @@ -780,17 +781,6 @@ pub async fn skip_locker_call_and_migrate_payment_method(

let network_transaction_id = req.network_transaction_id.clone();

migration_status.network_transaction_id_migrated(network_transaction_id.as_ref().map(|_| true));

migration_status.connector_mandate_details_migrated(
connector_mandate_details
.as_ref()
.map(|_| true)
.or_else(|| req.connector_mandate_details.as_ref().map(|_| false)),
);

migration_status.card_migrated(false);

let payment_method_id = generate_id(consts::ID_LENGTH, "pm");

let current_time = common_utils::date_time::now();
Expand All @@ -810,11 +800,11 @@ pub async fn skip_locker_call_and_migrate_payment_method(
scheme: req.card_network.clone().or(card.scheme.clone()),
metadata: payment_method_metadata.map(Secret::new),
payment_method_data: payment_method_data_encrypted.map(Into::into),
connector_mandate_details,
connector_mandate_details: connector_mandate_details.clone(),
customer_acceptance: None,
client_secret: None,
status: enums::PaymentMethodStatus::Active,
network_transaction_id,
network_transaction_id: network_transaction_id.clone(),
payment_method_issuer_code: None,
accepted_currency: None,
token: None,
Expand Down Expand Up @@ -843,6 +833,21 @@ pub async fn skip_locker_call_and_migrate_payment_method(

logger::debug!("Payment method inserted in db");

migration_status.network_transaction_id_migrated(
network_transaction_id.and_then(|val| (!val.is_empty_after_trim()).then_some(true)),
);

migration_status.connector_mandate_details_migrated(
connector_mandate_details
.clone()
.and_then(|val| if val == json!({}) { None } else { Some(true) })
.or_else(|| {
req.connector_mandate_details
.clone()
.and_then(|val| (!val.0.is_empty()).then_some(false))
}),
);

if customer.default_payment_method_id.is_none() && req.payment_method.is_some() {
let _ = set_default_payment_method(
state,
Expand Down Expand Up @@ -1166,10 +1171,14 @@ pub async fn get_client_secret_or_add_payment_method_for_migration(
migration_status.connector_mandate_details_migrated(
connector_mandate_details
.clone()
.map(|_| true)
.or_else(|| req.connector_mandate_details.clone().map(|_| false)),
.and_then(|val| (val != json!({})).then_some(true))
.or_else(|| {
req.connector_mandate_details
.clone()
.and_then(|val| (!val.0.is_empty()).then_some(false))
}),
);

//card is not migrated in this case
migration_status.card_migrated(false);

if res.status == enums::PaymentMethodStatus::AwaitingData {
Expand Down Expand Up @@ -1722,15 +1731,6 @@ pub async fn save_migration_payment_method(

let network_transaction_id = req.network_transaction_id.clone();

migration_status.network_transaction_id_migrated(network_transaction_id.as_ref().map(|_| true));

migration_status.connector_mandate_details_migrated(
connector_mandate_details
.as_ref()
.map(|_| true)
.or_else(|| req.connector_mandate_details.as_ref().map(|_| false)),
);

let response = match payment_method {
#[cfg(feature = "payouts")]
api_enums::PaymentMethod::BankTransfer => match req.bank_transfer.clone() {
Expand Down Expand Up @@ -1940,8 +1940,8 @@ pub async fn save_migration_payment_method(
pm_metadata.cloned(),
None,
locker_id,
connector_mandate_details,
network_transaction_id,
connector_mandate_details.clone(),
network_transaction_id.clone(),
merchant_account.storage_scheme,
payment_method_billing_address.map(Into::into),
None,
Expand All @@ -1954,6 +1954,20 @@ pub async fn save_migration_payment_method(
}
}

migration_status.card_migrated(true);
migration_status.network_transaction_id_migrated(
network_transaction_id.and_then(|val| (!val.is_empty_after_trim()).then_some(true)),
);

migration_status.connector_mandate_details_migrated(
connector_mandate_details
.and_then(|val| if val == json!({}) { None } else { Some(true) })
.or_else(|| {
req.connector_mandate_details
.and_then(|val| (!val.0.is_empty()).then_some(false))
}),
);

Ok(services::ApplicationResponse::Json(resp))
}

Expand Down

0 comments on commit b5d3d49

Please sign in to comment.