Skip to content

Commit

Permalink
refactor: use impls instead of dangling fns
Browse files Browse the repository at this point in the history
  • Loading branch information
kashif-m committed Nov 11, 2024
1 parent 7c9ff20 commit 1396e04
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
33 changes: 0 additions & 33 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6102,36 +6102,3 @@ pub fn validate_platform_fees_for_marketplace(
}
}
}

pub fn get_optional_card_type_from_additional_payment_method_data(
payment_method_type: Option<enums::PaymentMethodType>,
additional_pm_data: Option<&api_models::payments::AdditionalPaymentData>,
) -> Option<enums::PaymentMethodType> {
additional_pm_data
.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| {
api_models::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)
})
}
9 changes: 4 additions & 5 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,11 +644,10 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa

payment_attempt.payment_method = payment_method.or(payment_attempt.payment_method);

let payment_method_type =
helpers::get_optional_card_type_from_additional_payment_method_data(
payment_method_type,
additional_pm_data.as_ref(),
);
let payment_method_type = Option::<api_models::enums::PaymentMethodType>::foreign_from((
payment_method_type,
additional_pm_data.as_ref(),
));

payment_attempt.payment_method_type = payment_method_type
.or(payment_attempt.payment_method_type)
Expand Down
9 changes: 4 additions & 5 deletions crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,11 +1195,10 @@ impl PaymentCreate {
None
};

let payment_method_type =
helpers::get_optional_card_type_from_additional_payment_method_data(
payment_method_type,
additional_pm_data.as_ref(),
);
let payment_method_type = Option::<enums::PaymentMethodType>::foreign_from((
payment_method_type,
additional_pm_data.as_ref(),
));

Ok((
storage::PaymentAttemptNew {
Expand Down
35 changes: 35 additions & 0 deletions crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3357,3 +3357,38 @@ impl ForeignFrom<ConnectorMandateReferenceId> for DieselConnectorMandateReferenc
}
}
}

impl ForeignFrom<(Self, Option<&api_models::payments::AdditionalPaymentData>)>
for Option<enums::PaymentMethodType>
{
fn foreign_from(req: (Self, Option<&api_models::payments::AdditionalPaymentData>)) -> Self {
let (payment_method_type, additional_pm_data) = req;
additional_pm_data
.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| {
api_models::enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()).map_err(|err| {
crate::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 {
crate::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)
})
}
}

0 comments on commit 1396e04

Please sign in to comment.