Skip to content

Commit

Permalink
refactor: populate payment_method_type for card during PaymentAttempt…
Browse files Browse the repository at this point in the history
… insert
  • Loading branch information
kashif-m committed Nov 10, 2024
1 parent bac0f96 commit 4363515
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 138 deletions.
60 changes: 0 additions & 60 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,6 @@ pub enum PaymentAttemptUpdate {
updated_by: String,
connector_metadata: Option<serde_json::Value>,
},
PaymentMethodTypeUpdate {
payment_method_type: Option<storage_enums::PaymentMethodType>,
updated_by: String,
},
}

#[cfg(feature = "v2")]
Expand Down Expand Up @@ -3283,62 +3279,6 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::PaymentMethodTypeUpdate {
payment_method_type,
updated_by,
} => Self {
modified_at: common_utils::date_time::now(),
payment_method_type,
updated_by,
status: None,
error_code: None,
error_message: None,
error_reason: None,
unified_code: None,
unified_message: None,
amount: None,
net_amount: None,
currency: None,
connector_transaction_id: None,
amount_to_capture: None,
connector: None,
authentication_type: None,
payment_method: None,
payment_method_id: None,
cancellation_reason: None,
mandate_id: None,
browser_info: None,
payment_token: None,
connector_metadata: None,
payment_method_data: None,
payment_experience: None,
business_sub_label: None,
straight_through_algorithm: None,
preprocessing_step_id: None,
capture_method: None,
connector_response_reference_id: None,
multiple_capture_count: None,
surcharge_amount: None,
tax_amount: None,
amount_capturable: None,
merchant_connector_id: None,
authentication_data: None,
encoded_data: None,
external_three_ds_authentication_attempted: None,
authentication_connector: None,
authentication_id: None,
fingerprint_id: None,
payment_method_billing_address_id: None,
charge_id: None,
client_source: None,
client_version: None,
customer_acceptance: None,
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,6 @@ pub enum PaymentAttemptUpdate {
updated_by: String,
connector_metadata: Option<serde_json::Value>,
},
PaymentMethodTypeUpdate {
payment_method_type: Option<storage_enums::PaymentMethodType>,
updated_by: String,
},
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -1280,13 +1276,6 @@ impl PaymentAttemptUpdate {
updated_by,
connector_metadata,
},
Self::PaymentMethodTypeUpdate {
payment_method_type,
updated_by,
} => DieselPaymentAttemptUpdate::PaymentMethodTypeUpdate {
payment_method_type,
updated_by,
},
}
}
}
Expand Down
32 changes: 31 additions & 1 deletion crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::marker::PhantomData;
use std::{marker::PhantomData, str::FromStr};

use api_models::{
enums::FrmSuggestion, mandates::RecurringDetails, payment_methods::PaymentMethodsData,
Expand Down Expand Up @@ -1195,6 +1195,36 @@ impl PaymentCreate {
None
};

let payment_method_type =
additional_pm_data
.as_ref()
.and_then(|pm_data| {
if let api_models::payments::AdditionalPaymentData::Card(card_info) = pm_data {
card_info.card_type.as_ref().and_then(|card_type_str| {
enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()).map_err(|err| {
logger::error!(
"Err - {:?}\nInvalid card_type value found in BIN DB - {:?}",
err,
card_type_str,
);
}).ok()
})
} else {
None
}
})
.map_or(payment_method_type, |card_type_in_bin_store| {
if let Some(card_type_in_req) = payment_method_type {
if card_type_in_req != card_type_in_bin_store {
logger::info!(
"Mismatch in card_type\nAPI request - {}; BIN lookup - {}\nOverriding with {}",
card_type_in_req, card_type_in_bin_store, card_type_in_bin_store,
);
}
}
Some(card_type_in_bin_store)
});

Ok((
storage::PaymentAttemptNew {
payment_id: payment_id.to_owned(),
Expand Down
68 changes: 2 additions & 66 deletions crates/router/src/core/payments/operations/payment_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, str::FromStr};
use std::collections::HashMap;

use api_models::payments::{ConnectorMandateReferenceId, MandateReferenceId};
#[cfg(feature = "dynamic_routing")]
Expand Down Expand Up @@ -36,7 +36,7 @@ use crate::{
},
tokenization,
types::MultipleCaptureData,
OperationSessionGetters, PaymentData, PaymentMethodChecker,
PaymentData, PaymentMethodChecker,
},
utils as core_utils,
},
Expand Down Expand Up @@ -1243,67 +1243,6 @@ impl<F: Clone> PostUpdateTracker<F, PaymentData<F>, types::CompleteAuthorizeData
}
}

async fn update_payment_method_type_for_cards<F: Clone>(
state: &SessionState,
storage_scheme: enums::MerchantStorageScheme,
payment_data: &mut PaymentData<F>,
) -> RouterResult<()> {
// Parse Value to AdditionalPaymentData
let additional_payment_method_data: Option<api_models::payments::AdditionalPaymentData> =
payment_data
.get_payment_attempt()
.payment_method_data
.clone()
.map(|data| data.parse_value("AdditionalPaymentData"))
.transpose()
.change_context(errors::ApiErrorResponse::InvalidDataValue {
field_name: "payment_method_data",
})?;

// Fetch card_type from this struct for cards
let payment_method_type = additional_payment_method_data.and_then(|data| match data {
api_models::payments::AdditionalPaymentData::Card(card) => {
card.card_type.as_ref().and_then(|card_type_str| {
match enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()) {
Ok(payment_method_type) => Some(payment_method_type),
// Do not throw errors in case of deserialization errors
Err(e) => {
logger::error!(
"Invalid card_type value in AdditionalPaymentData - {:?}\nErr - {:?}",
card.card_type,
e
);
None
}
}
})
}
_ => None,
});

// If payment_method_type in PaymentAttempt does not match the one in payment_method_data
// update payment_method_type in PaymentAttempt table
if payment_method_type.is_some()
&& payment_data.payment_attempt.payment_method_type.as_ref() != payment_method_type.as_ref()
{
payment_data.payment_attempt.payment_method_type = payment_method_type;
let payment_attempt_update = storage::PaymentAttemptUpdate::PaymentMethodTypeUpdate {
payment_method_type,
updated_by: storage_scheme.to_string(),
};
state
.store
.update_payment_attempt_with_attempt_id(
payment_data.payment_attempt.clone(),
payment_attempt_update,
storage_scheme,
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
}
Ok(())
}

#[cfg(feature = "v1")]
#[instrument(skip_all)]
#[allow(clippy::too_many_arguments)]
Expand All @@ -1319,9 +1258,6 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
>,
#[cfg(all(feature = "v1", feature = "dynamic_routing"))] business_profile: &domain::Profile,
) -> RouterResult<PaymentData<F>> {
// Populate payment_method_type for cards in case it's empty or mismatched
update_payment_method_type_for_cards(state, storage_scheme, &mut payment_data).await?;

// Update additional payment data with the payment method response that we received from connector
let additional_payment_method_data = match payment_data.payment_method_data.clone() {
Some(payment_method_data) => match payment_method_data {
Expand Down

0 comments on commit 4363515

Please sign in to comment.