Skip to content

Commit

Permalink
refactor: pass customer object to make_pm_data (#3246)
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
Chethan-rao and hyperswitch-bot[bot] authored Jan 9, 2024
1 parent eba7896 commit 36c32c3
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 21 deletions.
6 changes: 0 additions & 6 deletions crates/common_utils/src/pii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ impl TryFrom<String> for Email {
}
}

impl From<Secret<String, EmailStrategy>> for Email {
fn from(value: Secret<String, EmailStrategy>) -> Self {
Self(value)
}
}

impl ops::Deref for Email {
type Target = Secret<String, EmailStrategy>;

Expand Down
7 changes: 5 additions & 2 deletions crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use common_utils::{
use error_stack::{report, FutureExt, IntoReport, ResultExt};
use futures::future::try_join_all;
use masking::{PeekInterface, Secret};
use pm_auth::connector::plaid::transformers::PlaidAuthType;
use uuid::Uuid;

use crate::{
Expand Down Expand Up @@ -1830,8 +1831,10 @@ pub(crate) fn validate_auth_and_metadata_type(
riskified::transformers::RiskifiedAuthType::try_from(val)?;
Ok(())
}
api_enums::Connector::Plaid => Err(report!(errors::ConnectorError::InvalidConnectorName)
.attach_printable(format!("invalid connector name: {connector_name}"))),
api_enums::Connector::Plaid => {
PlaidAuthType::foreign_try_from(val)?;
Ok(())
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/core/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub trait PaymentMethodRetrieve {
token: &storage::PaymentTokenData,
payment_intent: &PaymentIntent,
card_token_data: Option<&CardToken>,
customer: &Option<domain::Customer>,
) -> RouterResult<Option<(payments::PaymentMethodData, enums::PaymentMethod)>>;
}

Expand Down Expand Up @@ -126,6 +127,7 @@ impl PaymentMethodRetrieve for Oss {
token_data: &storage::PaymentTokenData,
payment_intent: &PaymentIntent,
card_token_data: Option<&CardToken>,
customer: &Option<domain::Customer>,
) -> RouterResult<Option<(payments::PaymentMethodData, enums::PaymentMethod)>> {
match token_data {
storage::PaymentTokenData::TemporaryGeneric(generic_token) => {
Expand Down Expand Up @@ -178,6 +180,7 @@ impl PaymentMethodRetrieve for Oss {
merchant_key_store,
auth_token,
payment_intent,
customer,
)
.await
}
Expand Down
7 changes: 7 additions & 0 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ where
&mut payment_data,
&validate_result,
&key_store,
&customer,
)
.await?;

Expand Down Expand Up @@ -1041,6 +1042,7 @@ where
validate_result,
&merchant_connector_account,
key_store,
customer,
)
.await?;

Expand Down Expand Up @@ -1754,6 +1756,7 @@ pub async fn get_connector_tokenization_action_when_confirm_true<F, Req, Ctx>(
validate_result: &operations::ValidateResult<'_>,
merchant_connector_account: &helpers::MerchantConnectorAccountType,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(PaymentData<F>, TokenizationAction)>
where
F: Send + Clone,
Expand Down Expand Up @@ -1821,6 +1824,7 @@ where
payment_data,
validate_result.storage_scheme,
merchant_key_store,
customer,
)
.await?;
payment_data.payment_method_data = payment_method_data;
Expand All @@ -1836,6 +1840,7 @@ where
payment_data,
validate_result.storage_scheme,
merchant_key_store,
customer,
)
.await?;

Expand Down Expand Up @@ -1873,6 +1878,7 @@ pub async fn tokenize_in_router_when_confirm_false<F, Req, Ctx>(
payment_data: &mut PaymentData<F>,
validate_result: &operations::ValidateResult<'_>,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<PaymentData<F>>
where
F: Send + Clone,
Expand All @@ -1887,6 +1893,7 @@ where
payment_data,
validate_result.storage_scheme,
merchant_key_store,
customer,
)
.await?;
payment_data.payment_method_data = payment_method_data;
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,7 @@ pub async fn make_pm_data<'a, F: Clone, R, Ctx: PaymentMethodRetrieve>(
state: &'a AppState,
payment_data: &mut PaymentData<F>,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, R, Ctx>,
Option<api::PaymentMethodData>,
Expand Down Expand Up @@ -1621,6 +1622,7 @@ pub async fn make_pm_data<'a, F: Clone, R, Ctx: PaymentMethodRetrieve>(
&hyperswitch_token,
&payment_data.payment_intent,
card_token_data.as_ref(),
customer,
)
.await
.attach_printable("in 'make_pm_data'")?;
Expand Down
14 changes: 13 additions & 1 deletion crates/router/src/core/payments/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub trait Domain<F: Clone, R, Ctx: PaymentMethodRetrieve>: Send + Sync {
payment_data: &mut PaymentData<F>,
storage_scheme: enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, R, Ctx>,
Option<api::PaymentMethodData>,
Expand Down Expand Up @@ -251,11 +252,19 @@ where
payment_data: &mut PaymentData<F>,
_storage_scheme: enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRetrieveRequest, Ctx>,
Option<api::PaymentMethodData>,
)> {
helpers::make_pm_data(Box::new(self), state, payment_data, merchant_key_store).await
helpers::make_pm_data(
Box::new(self),
state,
payment_data,
merchant_key_store,
customer,
)
.await
}
}

Expand Down Expand Up @@ -301,6 +310,7 @@ where
_payment_data: &mut PaymentData<F>,
_storage_scheme: enums::MerchantStorageScheme,
_merchant_key_store: &domain::MerchantKeyStore,
_customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsCaptureRequest, Ctx>,
Option<api::PaymentMethodData>,
Expand Down Expand Up @@ -363,6 +373,7 @@ where
_payment_data: &mut PaymentData<F>,
_storage_scheme: enums::MerchantStorageScheme,
_merchant_key_store: &domain::MerchantKeyStore,
_customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsCancelRequest, Ctx>,
Option<api::PaymentMethodData>,
Expand Down Expand Up @@ -415,6 +426,7 @@ where
_payment_data: &mut PaymentData<F>,
_storage_scheme: enums::MerchantStorageScheme,
_merchant_key_store: &domain::MerchantKeyStore,
_customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRejectRequest, Ctx>,
Option<api::PaymentMethodData>,
Expand Down
11 changes: 9 additions & 2 deletions crates/router/src/core/payments/operations/payment_approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,19 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
payment_data: &mut PaymentData<F>,
_storage_scheme: storage_enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRequest, Ctx>,
Option<api::PaymentMethodData>,
)> {
let (op, payment_method_data) =
helpers::make_pm_data(Box::new(self), state, payment_data, merchant_key_store).await?;
let (op, payment_method_data) = helpers::make_pm_data(
Box::new(self),
state,
payment_data,
merchant_key_store,
customer,
)
.await?;

utils::when(payment_method_data.is_none(), || {
Err(errors::ApiErrorResponse::PaymentMethodNotFound)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,19 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
payment_data: &mut PaymentData<F>,
_storage_scheme: storage_enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRequest, Ctx>,
Option<api::PaymentMethodData>,
)> {
let (op, payment_method_data) =
helpers::make_pm_data(Box::new(self), state, payment_data, merchant_key_store).await?;
let (op, payment_method_data) = helpers::make_pm_data(
Box::new(self),
state,
payment_data,
merchant_key_store,
customer,
)
.await?;
Ok((op, payment_method_data))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,13 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
payment_data: &mut PaymentData<F>,
_storage_scheme: storage_enums::MerchantStorageScheme,
key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRequest, Ctx>,
Option<api::PaymentMethodData>,
)> {
let (op, payment_method_data) =
helpers::make_pm_data(Box::new(self), state, payment_data, key_store).await?;
helpers::make_pm_data(Box::new(self), state, payment_data, key_store, customer).await?;

utils::when(payment_method_data.is_none(), || {
Err(errors::ApiErrorResponse::PaymentMethodNotFound)
Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,19 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
payment_data: &mut PaymentData<F>,
_storage_scheme: enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRequest, Ctx>,
Option<api::PaymentMethodData>,
)> {
helpers::make_pm_data(Box::new(self), state, payment_data, merchant_key_store).await
helpers::make_pm_data(
Box::new(self),
state,
payment_data,
merchant_key_store,
customer,
)
.await
}

#[instrument(skip_all)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ where
_payment_data: &mut PaymentData<F>,
_storage_scheme: storage_enums::MerchantStorageScheme,
_merchant_key_store: &domain::MerchantKeyStore,
_customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'b, F, api::PaymentsSessionRequest, Ctx>,
Option<api::PaymentMethodData>,
Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/core/payments/operations/payment_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ where
payment_data: &mut PaymentData<F>,
_storage_scheme: storage_enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsStartRequest, Ctx>,
Option<api::PaymentMethodData>,
Expand All @@ -289,7 +290,14 @@ where
.map(|connector_name| connector_name == *"bluesnap".to_string())
.unwrap_or(false)
{
helpers::make_pm_data(Box::new(self), state, payment_data, merchant_key_store).await
helpers::make_pm_data(
Box::new(self),
state,
payment_data,
merchant_key_store,
customer,
)
.await
} else {
Ok((Box::new(self), None))
}
Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/core/payments/operations/payment_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,19 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
payment_data: &mut PaymentData<F>,
_storage_scheme: enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRequest, Ctx>,
Option<api::PaymentMethodData>,
)> {
helpers::make_pm_data(Box::new(self), state, payment_data, merchant_key_store).await
helpers::make_pm_data(
Box::new(self),
state,
payment_data,
merchant_key_store,
customer,
)
.await
}

#[instrument(skip_all)]
Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/core/payments/operations/payment_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,19 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
payment_data: &mut PaymentData<F>,
_storage_scheme: storage_enums::MerchantStorageScheme,
merchant_key_store: &domain::MerchantKeyStore,
customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, api::PaymentsRequest, Ctx>,
Option<api::PaymentMethodData>,
)> {
helpers::make_pm_data(Box::new(self), state, payment_data, merchant_key_store).await
helpers::make_pm_data(
Box::new(self),
state,
payment_data,
merchant_key_store,
customer,
)
.await
}

#[instrument(skip_all)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve>
_payment_data: &mut payments::PaymentData<F>,
_storage_scheme: enums::MerchantStorageScheme,
_merchant_key_store: &domain::MerchantKeyStore,
_customer: &Option<domain::Customer>,
) -> RouterResult<(
BoxedOperation<'a, F, PaymentsIncrementalAuthorizationRequest, Ctx>,
Option<api::PaymentMethodData>,
Expand Down
11 changes: 10 additions & 1 deletion crates/router/src/core/pm_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::{
storage,
transformers::ForeignTryFrom,
},
utils::ext_traits::OptionExt,
};

pub async fn create_link_token(
Expand Down Expand Up @@ -618,6 +619,7 @@ pub async fn retrieve_payment_method_from_auth_service(
key_store: &domain::MerchantKeyStore,
auth_token: &payment_methods::BankAccountConnectorDetails,
payment_intent: &PaymentIntent,
customer: &Option<domain::Customer>,
) -> RouterResult<Option<(PaymentMethodData, enums::PaymentMethod)>> {
let db = state.store.as_ref();

Expand Down Expand Up @@ -710,10 +712,17 @@ pub async fn retrieve_payment_method_from_auth_service(
last_name,
}
});

let email = customer
.as_ref()
.and_then(|customer| customer.email.clone())
.map(common_utils::pii::Email::from)
.get_required_value("email")?;

let payment_method_data = PaymentMethodData::BankDebit(BankDebitData::AchBankDebit {
billing_details: BankDebitBilling {
name: name.unwrap_or_default(),
email: common_utils::pii::Email::from(masking::Secret::new("".to_string())),
email,
address: address_details,
},
account_number: masking::Secret::new(bank_account.account_number.clone()),
Expand Down
4 changes: 2 additions & 2 deletions crates/router/tests/connectors/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ pub trait ConnectorActions: Connector {
customer_details: Some(payments::CustomerDetails {
customer_id: core_utils::get_or_generate_id("customer_id", &None, "cust_").ok(),
name: Some(Secret::new("John Doe".to_string())),
email: TryFrom::try_from("john.doe@example".to_string()).ok(),
email: Email::from_str("john.doe@example").ok(),
phone: Some(Secret::new("620874518".to_string())),
phone_country_code: Some("+31".to_string()),
}),
Expand Down Expand Up @@ -996,7 +996,7 @@ impl Default for CustomerType {
let data = types::ConnectorCustomerData {
payment_method_data: types::api::PaymentMethodData::Card(CCardType::default().0),
description: None,
email: Some(Email::from(Secret::new("[email protected]".to_string()))),
email: Email::from_str("[email protected]").ok(),
phone: None,
name: None,
preprocessing_id: None,
Expand Down

0 comments on commit 36c32c3

Please sign in to comment.