Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(pm_auth): Support different pm types in PM auth (#3114)
Sarthak Soni290c456a2350
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rsindex 35193b958f..9873f62bfe 100644--- a/crates/api_models/src/payment_methods.rs+++ b/crates/api_models/src/payment_methods.rs@@ -222,17 +222,24 @@ pub struct PaymentMethodDataBankCreds {pub connector_details: Vec<BankAccountConnectorDetails>,}+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]+pub struct BankAccountTokenData {+ pub payment_method_type: api_enums::PaymentMethodType,+ pub payment_method: api_enums::PaymentMethod,+ pub connector_details: BankAccountConnectorDetails,+}+#[derive(Debug, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]pub struct BankAccountConnectorDetails {pub connector: String,- pub account_id: String,+ pub account_id: masking::Secret<String>,pub mca_id: String,pub access_token: BankAccountAccessCreds,}#[derive(Debug, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]pub enum BankAccountAccessCreds {- AccessToken(String),+ AccessToken(masking::Secret<String>),}#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)]pub struct CardDetailFromLocker {diff --git a/crates/pm_auth/src/connector/plaid/transformers.rs b/crates/pm_auth/src/connector/plaid/transformers.rsindex 5e1ad67aea..f163875958 100644--- a/crates/pm_auth/src/connector/plaid/transformers.rs+++ b/crates/pm_auth/src/connector/plaid/transformers.rs@@ -1,7 +1,7 @@use std::collections::HashMap;-use common_enums::PaymentMethodType;-use masking::Secret;+use common_enums::{PaymentMethod, PaymentMethodType};+use masking::{PeekInterface, Secret};use serde::{Deserialize, Serialize};use crate::{core::errors, types};@@ -200,13 +200,19 @@ pub struct PlaidBankAccountCredentialsBacs {impl TryFrom<&types::BankDetailsRouterData> for PlaidBankAccountCredentialsRequest {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(item: &types::BankDetailsRouterData) -> Result<Self, Self::Error> {+ let options = item.request.optional_ids.as_ref().map(|bank_account_ids| {+ let ids = bank_account_ids+ .ids+ .iter()+ .map(|id| id.peek().to_string())+ .collect::<Vec<_>>();++ BankAccountCredentialsOptions { account_ids: ids }+ });+Ok(Self {- access_token: item.request.access_token.clone(),- options: item.request.optional_ids.as_ref().map(|bank_account_ids| {- BankAccountCredentialsOptions {- account_ids: bank_account_ids.ids.clone(),- }- }),+ access_token: item.request.access_token.peek().to_string(),+ options,})}}@@ -232,26 +238,84 @@ impl<F, T>) -> Result<Self, Self::Error> {let (account_numbers, accounts_info) = (item.response.numbers, item.response.accounts);let mut bank_account_vec = Vec::new();- let mut id_to_suptype = HashMap::new();+ let mut id_to_subtype = HashMap::new();accounts_info.into_iter().for_each(|acc| {- id_to_suptype.insert(acc.account_id, (acc.subtype, acc.name));+ id_to_subtype.insert(acc.account_id, (acc.subtype, acc.name));});account_numbers.ach.into_iter().for_each(|ach| {let (acc_type, acc_name) =- if let Some((_type, name)) = id_to_suptype.get(&ach.account_id) {+ if let Some((_type, name)) = id_to_subtype.get(&ach.account_id) {(_type.to_owned(), Some(name.clone()))} else {(None, None)};+ let account_details =+ types::PaymentMethodTypeDetails::Ach(types::BankAccountDetailsAch {+ account_number: Secret::new(ach.account),+ routing_number: Secret::new(ach.routing),+ });+let bank_details_new = types::BankAccountDetails {account_name: acc_name,- account_number: ach.account,- routing_number: ach.routing,+ account_details,payment_method_type: PaymentMethodType::Ach,- account_id: ach.account_id,+ payment_method: PaymentMethod::BankDebit,+ account_id: ach.account_id.into(),+ account_type: acc_type,+ };++ bank_account_vec.push(bank_details_new);+ });++ account_numbers.bacs.into_iter().for_each(|bacs| {+ let (acc_type, acc_name) =+ if let Some((_type, name)) = id_to_subtype.get(&bacs.account_id) {+ (_type.to_owned(), Some(name.clone()))+ } else {+ (None, None)+ };++ let account_details =+ types::PaymentMethodTypeDetails::Bacs(types::BankAccountDetailsBacs {+ account_number: Secret::new(bacs.account),+ sort_code: Secret::new(bacs.sort_code),+ });++ let bank_details_new = types::BankAccountDetails {+ account_name: acc_name,+ account_details,+ payment_method_type: PaymentMethodType::Bacs,+ payment_method: PaymentMethod::BankDebit,+ account_id: bacs.account_id.into(),+ account_type: acc_type,+ };++ bank_account_vec.push(bank_details_new);+ });++ account_numbers.international.into_iter().for_each(|sepa| {+ let (acc_type, acc_name) =+ if let Some((_type, name)) = id_to_subtype.get(&sepa.account_id) {+ (_type.to_owned(), Some(name.clone()))+ } else {+ (None, None)+ };++ let account_details =+ types::PaymentMethodTypeDetails::Sepa(types::BankAccountDetailsSepa {+ iban: Secret::new(sepa.iban),+ bic: Secret::new(sepa.bic),+ });++ let bank_details_new = types::BankAccountDetails {+ account_name: acc_name,+ account_details,+ payment_method_type: PaymentMethodType::Sepa,+ payment_method: PaymentMethod::BankDebit,+ account_id: sepa.account_id.into(),account_type: acc_type,};diff --git a/crates/pm_auth/src/types.rs b/crates/pm_auth/src/types.rsindex 6f5875247f..51fd796072 100644--- a/crates/pm_auth/src/types.rs+++ b/crates/pm_auth/src/types.rs@@ -3,7 +3,7 @@ pub mod api;use std::marker::PhantomData;use api::auth_service::{BankAccountCredentials, ExchangeToken, LinkToken};-use common_enums::PaymentMethodType;+use common_enums::{PaymentMethod, PaymentMethodType};use masking::Secret;#[derive(Debug, Clone)]pub struct PaymentAuthRouterData<F, Request, Response> {@@ -55,13 +55,13 @@ pub type ExchangeTokenRouterData =#[derive(Debug, Clone)]pub struct BankAccountCredentialsRequest {- pub access_token: String,+ pub access_token: Secret<String>,pub optional_ids: Option<BankAccountOptionalIDs>,}#[derive(Debug, Clone)]pub struct BankAccountOptionalIDs {- pub ids: Vec<String>,+ pub ids: Vec<Secret<String>>,}#[derive(Debug, Clone)]@@ -72,13 +72,37 @@ pub struct BankAccountCredentialsResponse {#[derive(Debug, Clone)]pub struct BankAccountDetails {pub account_name: Option<String>,- pub account_number: String,- pub routing_number: String,+ pub account_details: PaymentMethodTypeDetails,pub payment_method_type: PaymentMethodType,- pub account_id: String,+ pub payment_method: PaymentMethod,+ pub account_id: Secret<String>,pub account_type: Option<String>,}+#[derive(Debug, Clone)]+pub enum PaymentMethodTypeDetails {+ Ach(BankAccountDetailsAch),+ Bacs(BankAccountDetailsBacs),+ Sepa(BankAccountDetailsSepa),+}+#[derive(Debug, Clone)]+pub struct BankAccountDetailsAch {+ pub account_number: Secret<String>,+ pub routing_number: Secret<String>,+}++#[derive(Debug, Clone)]+pub struct BankAccountDetailsBacs {+ pub account_number: Secret<String>,+ pub sort_code: Secret<String>,+}++#[derive(Debug, Clone)]+pub struct BankAccountDetailsSepa {+ pub iban: Secret<String>,+ pub bic: Secret<String>,+}+pub type BankDetailsRouterData = PaymentAuthRouterData<BankAccountCredentials,BankAccountCredentialsRequest,diff --git a/crates/router/src/configs/defaults.rs b/crates/router/src/configs/defaults.rsindex f9d98647d1..7ade016856 100644--- a/crates/router/src/configs/defaults.rs+++ b/crates/router/src/configs/defaults.rs@@ -6453,7 +6453,15 @@ impl Default for super::settings::RequiredFields {RequiredFieldFinal {mandate: HashMap::new(),non_mandate: HashMap::new(),- common: HashMap::new(),+ common: HashMap::from([ (+ "billing.address.first_name".to_string(),+ RequiredFieldInfo {+ required_field: "billing.address.first_name".to_string(),+ display_name: "billing_first_name".to_string(),+ field_type: enums::FieldType::UserBillingName,+ value: None,+ }+ )]),})])}),@@ -6466,7 +6474,15 @@ impl Default for super::settings::RequiredFields {RequiredFieldFinal {mandate: HashMap::new(),non_mandate: HashMap::new(),- common: HashMap::new(),+ common: HashMap::from([ (+ "billing.address.first_name".to_string(),+ RequiredFieldInfo {+ required_field: "billing.address.first_name".to_string(),+ display_name: "billing_first_name".to_string(),+ field_type: enums::FieldType::UserBillingName,+ value: None,+ }+ )]),}),]),@@ -6481,7 +6497,15 @@ impl Default for super::settings::RequiredFields {RequiredFieldFinal {mandate: HashMap::new(),non_mandate: HashMap::new(),- common: HashMap::new(),+ common: HashMap::from([ (+ "billing.address.first_name".to_string(),+ RequiredFieldInfo {+ required_field: "billing.address.first_name".to_string(),+ display_name: "billing_first_name".to_string(),+ field_type: enums::FieldType::UserBillingName,+ value: None,+ }+ )]),}),]),diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rsindex 75f192f606..593e7438ee 100644--- a/crates/router/src/core/payment_methods/cards.rs+++ b/crates/router/src/core/payment_methods/cards.rs@@ -7,7 +7,7 @@ use api_models::{admin::{self, PaymentMethodsEnabled},enums::{self as api_enums},payment_methods::{- BankAccountConnectorDetails, CardDetailsPaymentMethod, CardNetworkTypes,+ BankAccountTokenData, CardDetailsPaymentMethod, CardNetworkTypes,CustomerDefaultPaymentMethodResponse, MaskedBankDetails, PaymentExperienceTypes,PaymentMethodsData, RequestPaymentMethodTypes, RequiredFieldInfo,ResponsePaymentMethodIntermediate, ResponsePaymentMethodTypes,@@ -2836,14 +2836,14 @@ pub async fn list_customer_payment_method(enums::PaymentMethod::BankDebit => {// Retrieve the pm_auth connector details so that it can be tokenized- let bank_account_connector_details = get_bank_account_connector_details(&pm, key)+ let bank_account_token_data = get_bank_account_connector_details(&pm, key).await.unwrap_or_else(|err| {logger::error!(error=?err);None});- if let Some(connector_details) = bank_account_connector_details {- let token_data = PaymentTokenData::AuthBankDebit(connector_details);+ if let Some(data) = bank_account_token_data {+ let token_data = PaymentTokenData::AuthBankDebit(data);(None, None, token_data)} else {continue;@@ -3145,7 +3145,7 @@ async fn get_masked_bank_details(async fn get_bank_account_connector_details(pm: &payment_method::PaymentMethod,key: &[u8],-) -> errors::RouterResult<Option<BankAccountConnectorDetails>> {+) -> errors::RouterResult<Option<BankAccountTokenData>> {let payment_method_data =decrypt::<serde_json::Value, masking::WithType>(pm.payment_method_data.clone(), key).await@@ -3174,12 +3174,19 @@ async fn get_bank_account_connector_details(.connector_details.first().ok_or(errors::ApiErrorResponse::InternalServerError)?;- Ok(Some(BankAccountConnectorDetails {- connector: connector_details.connector.clone(),- account_id: connector_details.account_id.clone(),- mca_id: connector_details.mca_id.clone(),- access_token: connector_details.access_token.clone(),- }))++ let pm_type = pm+ .payment_method_type+ .get_required_value("payment_method_type")+ .attach_printable("PaymentMethodType not found")?;++ let token_data = BankAccountTokenData {+ payment_method_type: pm_type,+ payment_method: pm.payment_method,+ connector_details: connector_details.clone(),+ };++ Ok(Some(token_data))}},None => Ok(None),diff --git a/crates/router/src/core/pm_auth.rs b/crates/router/src/core/pm_auth.rsindex 21ba27eac1..20db03a26a 100644--- a/crates/router/src/core/pm_auth.rs+++ b/crates/router/src/core/pm_auth.rs@@ -5,6 +5,7 @@ use api_models::{payment_methods::{self, BankAccountAccessCreds},payments::{AddressDetails, BankDebitBilling, BankDebitData, PaymentMethodData},};+use common_enums::PaymentMethodType;use hex;pub mod helpers;pub mod transformers;@@ -18,7 +19,7 @@ use common_utils::{use data_models::payments::PaymentIntent;use error_stack::{IntoReport, ResultExt};use helpers::PaymentAuthConnectorDataExt;-use masking::{ExposeInterface, PeekInterface};+use masking::{ExposeInterface, PeekInterface, Secret};use pm_auth::{connector::plaid::transformers::PlaidAuthType,types::{@@ -273,7 +274,7 @@ async fn store_bank_details_in_payment_methods(merchant_account: domain::MerchantAccount,state: AppState,bank_account_details_resp: pm_auth_types::BankAccountCredentialsResponse,- connector_details: (&str, String),+ connector_details: (&str, Secret<String>),mca_id: String,) -> RouterResult<()> {let key = key_store.key.get_inner().peek();@@ -357,7 +358,31 @@ async fn store_bank_details_in_payment_methods(let mut new_entries: Vec<storage::PaymentMethodNew> = Vec::new();for creds in bank_account_details_resp.credentials {- let hash_string = format!("{}-{}", creds.account_number, creds.routing_number);+ let (account_number, hash_string) = match creds.account_details {+ pm_auth_types::PaymentMethodTypeDetails::Ach(ach) => (+ ach.account_number.clone(),+ format!(+ "{}-{}-{}",+ ach.account_number.peek(),+ ach.routing_number.peek(),+ PaymentMethodType::Ach,+ ),+ ),+ pm_auth_types::PaymentMethodTypeDetails::Bacs(bacs) => (+ bacs.account_number.clone(),+ format!(+ "{}-{}-{}",+ bacs.account_number.peek(),+ bacs.sort_code.peek(),+ PaymentMethodType::Bacs+ ),+ ),+ pm_auth_types::PaymentMethodTypeDetails::Sepa(sepa) => (+ sepa.iban.clone(),+ format!("{}-{}", sepa.iban.expose(), PaymentMethodType::Sepa),+ ),+ };+let generated_hash = hex::encode(HmacSha256::sign_message(&HmacSha256, pm_auth_key.as_bytes(), hash_string.as_bytes()).change_context(ApiErrorResponse::InternalServerError)@@ -366,8 +391,8 @@ async fn store_bank_details_in_payment_methods(let contains_account = hash_to_payment_method.get(&generated_hash);let mut pmd = payment_methods::PaymentMethodDataBankCreds {- mask: creds- .account_number+ mask: account_number+ .peek().chars().rev().take(4)@@ -472,10 +497,10 @@ pub async fn get_bank_account_creds(connector: PaymentAuthConnectorData,merchant_account: &domain::MerchantAccount,connector_name: &str,- access_token: &str,+ access_token: &Secret<String>,auth_type: pm_auth_types::ConnectorAuthType,state: &AppState,- bank_account_id: Option<String>,+ bank_account_id: Option<Secret<String>>,) -> RouterResult<pm_auth_types::BankAccountCredentialsResponse> {let connector_integration_bank_details: BoxedConnectorIntegration<'_,@@ -489,7 +514,7 @@ pub async fn get_bank_account_creds(merchant_id: Some(merchant_account.merchant_id.clone()),connector: Some(connector_name.to_string()),request: pm_auth_types::BankAccountCredentialsRequest {- access_token: access_token.to_string(),+ access_token: access_token.clone(),optional_ids: bank_account_id.map(|id| pm_auth_types::BankAccountOptionalIDs { ids: vec![id] }),},@@ -530,7 +555,7 @@ async fn get_access_token_from_exchange_api(payload: &api_models::pm_auth::ExchangeTokenCreateRequest,auth_type: &pm_auth_types::ConnectorAuthType,state: &AppState,-) -> RouterResult<String> {+) -> RouterResult<Secret<String>> {let connector_integration: BoxedConnectorIntegration<'_,ExchangeToken,@@ -573,7 +598,7 @@ async fn get_access_token_from_exchange_api(})?;let access_token = exchange_token_resp.access_token;- Ok(access_token)+ Ok(Secret::new(access_token))}async fn get_selected_config_from_redis(@@ -593,7 +618,9 @@ async fn get_selected_config_from_redis("Vec<PaymentMethodAuthConnectorChoice>",).await- .change_context(errors::ApiErrorResponse::InternalServerError)+ .change_context(errors::ApiErrorResponse::GenericNotFoundError {+ message: "payment method auth connector name not found".to_string(),+ }).attach_printable("Failed to get payment method auth choices from redis")?;let selected_config = pm_auth_configs@@ -603,7 +630,7 @@ async fn get_selected_config_from_redis(&& conf.payment_method_type == payload.payment_method_type}).ok_or(ApiErrorResponse::GenericNotFoundError {- message: "connector name not found".to_string(),+ message: "payment method auth connector name not found".to_string(),}).into_report()?.clone();@@ -614,14 +641,14 @@ async fn get_selected_config_from_redis(pub async fn retrieve_payment_method_from_auth_service(state: &AppState,key_store: &domain::MerchantKeyStore,- auth_token: &payment_methods::BankAccountConnectorDetails,+ auth_token: &payment_methods::BankAccountTokenData,payment_intent: &PaymentIntent,customer: &Option<domain::Customer>,) -> RouterResult<Option<(PaymentMethodData, enums::PaymentMethod)>> {let db = state.store.as_ref();let connector = pm_auth_types::api::PaymentAuthConnectorData::get_connector_by_name(- auth_token.connector.as_str(),+ auth_token.connector_details.connector.as_str(),)?;let merchant_account = db@@ -632,12 +659,12 @@ pub async fn retrieve_payment_method_from_auth_service(let mca = db.find_by_merchant_connector_account_merchant_id_merchant_connector_id(&payment_intent.merchant_id,- &auth_token.mca_id,+ &auth_token.connector_details.mca_id,key_store,).await.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {- id: auth_token.mca_id.clone(),+ id: auth_token.connector_details.mca_id.clone(),}).attach_printable("error while fetching merchant_connector_account from merchant_id and connector name",@@ -645,24 +672,27 @@ pub async fn retrieve_payment_method_from_auth_service(let auth_type = pm_auth_helpers::get_connector_auth_type(mca)?;- let BankAccountAccessCreds::AccessToken(access_token) = &auth_token.access_token;+ let BankAccountAccessCreds::AccessToken(access_token) =+ &auth_token.connector_details.access_token;let bank_account_creds = get_bank_account_creds(connector,&merchant_account,- &auth_token.connector,+ &auth_token.connector_details.connector,access_token,auth_type,state,- Some(auth_token.account_id.clone()),+ Some(auth_token.connector_details.account_id.clone()),).await?;- logger::debug!("bank_creds: {:?}", bank_account_creds);-let bank_account = bank_account_creds.credentials- .first()+ .iter()+ .find(|acc| {+ acc.payment_method_type == auth_token.payment_method_type+ && acc.payment_method == auth_token.payment_method+ }).ok_or(errors::ApiErrorResponse::InternalServerError).into_report().attach_printable("Bank account details not found")?;@@ -686,7 +716,12 @@ pub async fn retrieve_payment_method_from_auth_service(let name = address.as_ref()- .and_then(|addr| addr.first_name.clone().map(|name| name.into_inner()));+ .and_then(|addr| addr.first_name.clone().map(|name| name.into_inner()))+ .ok_or(errors::ApiErrorResponse::GenericNotFoundError {+ message: "billing_first_name not found".to_string(),+ })+ .into_report()+ .attach_printable("billing_first_name not found")?;let address_details = address.clone().map(|addr| {let line1 = addr.line1.map(|line1| line1.into_inner());@@ -716,20 +751,41 @@ pub async fn retrieve_payment_method_from_auth_service(.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,- address: address_details,- },- account_number: masking::Secret::new(bank_account.account_number.clone()),- routing_number: masking::Secret::new(bank_account.routing_number.clone()),- card_holder_name: None,- bank_account_holder_name: None,- bank_name: None,- bank_type,- bank_holder_type: None,- });+ let billing_details = BankDebitBilling {+ name,+ email,+ address: address_details,+ };++ let payment_method_data = match &bank_account.account_details {+ pm_auth_types::PaymentMethodTypeDetails::Ach(ach) => {+ PaymentMethodData::BankDebit(BankDebitData::AchBankDebit {+ billing_details,+ account_number: ach.account_number.clone(),+ routing_number: ach.routing_number.clone(),+ card_holder_name: None,+ bank_account_holder_name: None,+ bank_name: None,+ bank_type,+ bank_holder_type: None,+ })+ }+ pm_auth_types::PaymentMethodTypeDetails::Bacs(bacs) => {+ PaymentMethodData::BankDebit(BankDebitData::BacsBankDebit {+ billing_details,+ account_number: bacs.account_number.clone(),+ sort_code: bacs.sort_code.clone(),+ bank_account_holder_name: None,+ })+ }+ pm_auth_types::PaymentMethodTypeDetails::Sepa(sepa) => {+ PaymentMethodData::BankDebit(BankDebitData::SepaBankDebit {+ billing_details,+ iban: sepa.iban.clone(),+ bank_account_holder_name: None,+ })+ }+ };Ok(Some((payment_method_data, enums::PaymentMethod::BankDebit)))}diff --git a/crates/router/src/types/storage/payment_method.rs b/crates/router/src/types/storage/payment_method.rsindex d3339b7c4b..e1d74c7008 100644--- a/crates/router/src/types/storage/payment_method.rs+++ b/crates/router/src/types/storage/payment_method.rs@@ -52,7 +52,7 @@ pub enum PaymentTokenData {TemporaryGeneric(GenericTokenData),Permanent(CardTokenData),PermanentCard(CardTokenData),- AuthBankDebit(payment_methods::BankAccountConnectorDetails),+ AuthBankDebit(payment_methods::BankAccountTokenData),WalletToken(WalletTokenData),}