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
refactor(storage_impl): Move Conversion impl for MCA to storage_impl crate (#11809)
Anurag Thakur0defa221a393
diff --git a/crates/hyperswitch_domain_models/src/merchant_connector_account.rs b/crates/hyperswitch_domain_models/src/merchant_connector_account.rsindex d915ee6fdc..10d590e9f0 100644--- a/crates/hyperswitch_domain_models/src/merchant_connector_account.rs+++ b/crates/hyperswitch_domain_models/src/merchant_connector_account.rs@@ -3,12 +3,11 @@ use std::collections::HashMap;use common_utils::{crypto::Encryptable,- date_time,encryption::Encryption,errors::{CustomResult, ValidationError},ext_traits::{StringExt, ValueExt},- id_type, pii, type_name,- types::keymanager::{Identifier, KeyManagerState, ToEncryptable},+ id_type, pii,+ types::keymanager::ToEncryptable,};#[cfg(feature = "v2")]use diesel_models::merchant_connector_account::{@@ -17,23 +16,19 @@ use diesel_models::merchant_connector_account::{RevenueRecoveryMetadata as DieselRevenueRecoveryMetadata,};use diesel_models::{- enums,- merchant_connector_account::{self as storage, MerchantConnectorAccountUpdateInternal},+ enums, merchant_connector_account as storage,+ merchant_connector_account::MerchantConnectorAccountUpdateInternal,};use error_stack::ResultExt;-use hyperswitch_masking::{PeekInterface, Secret};+#[cfg(feature = "v2")]+use hyperswitch_masking::PeekInterface;+use hyperswitch_masking::Secret;use rustc_hash::FxHashMap;use serde_json::Value;-use super::behaviour;#[cfg(feature = "v2")]use crate::errors::api_error_response;-use crate::{- mandates::CommonMandateReference,- merchant_key_store::MerchantKeyStore,- router_data,- type_encryption::{crypto_operation, CryptoOperation},-};+use crate::{mandates::CommonMandateReference, merchant_key_store::MerchantKeyStore, router_data};#[cfg(feature = "v1")]#[derive(Clone, Debug, router_derive::ToEncryption, serde::Serialize, serde::Deserialize)]@@ -564,481 +559,6 @@ pub enum MerchantConnectorAccountUpdate {},}-#[cfg(feature = "v1")]-#[async_trait::async_trait]-impl behaviour::Conversion for MerchantConnectorAccount {- type DstType = storage::MerchantConnectorAccount;- type NewDstType = storage::MerchantConnectorAccountNew;-- async fn convert(self) -> CustomResult<Self::DstType, ValidationError> {- Ok(storage::MerchantConnectorAccount {- merchant_id: self.merchant_id,- connector_name: self.connector_name,- connector_account_details: self.connector_account_details.into(),- test_mode: self.test_mode,- disabled: self.disabled,- merchant_connector_id: self.merchant_connector_id.clone(),- id: Some(self.merchant_connector_id),- payment_methods_enabled: self.payment_methods_enabled,- connector_type: self.connector_type,- metadata: self.metadata,- frm_configs: None,- frm_config: self.frm_configs,- business_country: self.business_country,- business_label: self.business_label,- connector_label: self.connector_label,- business_sub_label: self.business_sub_label,- created_at: self.created_at,- modified_at: self.modified_at,- connector_webhook_details: self.connector_webhook_details,- profile_id: Some(self.profile_id),- applepay_verified_domains: self.applepay_verified_domains,- pm_auth_config: self.pm_auth_config,- status: self.status,- connector_wallets_details: self.connector_wallets_details.map(Encryption::from),- additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),- version: self.version,- connector_webhook_registration_details: self.connector_webhook_registration_details,- apple_pay_certificates: self.apple_pay_certificates,- apple_pay_certificates_encrypted: self- .apple_pay_certificates_encrypted- .map(|data| data.into()),- })- }-- async fn convert_back(- state: &KeyManagerState,- other: Self::DstType,- key: &Secret<Vec<u8>>,- _key_manager_identifier: Identifier,- ) -> CustomResult<Self, ValidationError> {- let identifier = Identifier::Merchant(other.merchant_id.clone());- let decrypted_data = crypto_operation(- state,- type_name!(Self::DstType),- CryptoOperation::BatchDecrypt(EncryptedMerchantConnectorAccount::to_encryptable(- EncryptedMerchantConnectorAccount {- connector_account_details: other.connector_account_details,- additional_merchant_data: other.additional_merchant_data,- connector_wallets_details: other.connector_wallets_details,- apple_pay_certificates_encrypted: other.apple_pay_certificates_encrypted,- },- )),- identifier.clone(),- key.peek(),- )- .await- .and_then(|val| val.try_into_batchoperation())- .change_context(ValidationError::InvalidValue {- message: "Failed while decrypting connector account details".to_string(),- })?;-- let decrypted_data = EncryptedMerchantConnectorAccount::from_encryptable(decrypted_data)- .change_context(ValidationError::InvalidValue {- message: "Failed while decrypting connector account details".to_string(),- })?;-- Ok(Self {- merchant_id: other.merchant_id,- connector_name: other.connector_name,- connector_account_details: decrypted_data.connector_account_details,- test_mode: other.test_mode,- disabled: other.disabled,- merchant_connector_id: other.merchant_connector_id,- payment_methods_enabled: other.payment_methods_enabled,- connector_type: other.connector_type,- metadata: other.metadata,-- frm_configs: other.frm_config,- business_country: other.business_country,- business_label: other.business_label,- connector_label: other.connector_label,- business_sub_label: other.business_sub_label,- created_at: other.created_at,- modified_at: other.modified_at,- connector_webhook_details: other.connector_webhook_details,- profile_id: other- .profile_id- .ok_or(ValidationError::MissingRequiredField {- field_name: "profile_id".into(),- })?,- applepay_verified_domains: other.applepay_verified_domains,- pm_auth_config: other.pm_auth_config,- status: other.status,- connector_wallets_details: decrypted_data.connector_wallets_details,- additional_merchant_data: decrypted_data.additional_merchant_data,- version: other.version,- connector_webhook_registration_details: other.connector_webhook_registration_details,- apple_pay_certificates: other.apple_pay_certificates,- apple_pay_certificates_encrypted: decrypted_data.apple_pay_certificates_encrypted,- })- }-- async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {- let now = date_time::now();- Ok(Self::NewDstType {- merchant_id: Some(self.merchant_id),- connector_name: Some(self.connector_name),- connector_account_details: Some(self.connector_account_details.into()),- test_mode: self.test_mode,- disabled: self.disabled,- merchant_connector_id: self.merchant_connector_id.clone(),- id: Some(self.merchant_connector_id),- payment_methods_enabled: self.payment_methods_enabled,- connector_type: Some(self.connector_type),- metadata: self.metadata,- frm_configs: None,- frm_config: self.frm_configs,- business_country: self.business_country,- business_label: self.business_label,- connector_label: self.connector_label,- business_sub_label: self.business_sub_label,- created_at: now,- modified_at: now,- connector_webhook_details: self.connector_webhook_details,- profile_id: Some(self.profile_id),- applepay_verified_domains: self.applepay_verified_domains,- pm_auth_config: self.pm_auth_config,- status: self.status,- connector_wallets_details: self.connector_wallets_details.map(Encryption::from),- additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),- version: self.version,- })- }-}--#[cfg(feature = "v2")]-#[async_trait::async_trait]-impl behaviour::Conversion for MerchantConnectorAccount {- type DstType = storage::MerchantConnectorAccount;- type NewDstType = storage::MerchantConnectorAccountNew;-- async fn convert(self) -> CustomResult<Self::DstType, ValidationError> {- Ok(storage::MerchantConnectorAccount {- id: self.id,- merchant_id: self.merchant_id,- connector_name: self.connector_name,- connector_account_details: self.connector_account_details.into(),- disabled: self.disabled,- payment_methods_enabled: self.payment_methods_enabled,- connector_type: self.connector_type,- metadata: self.metadata,- frm_config: self.frm_configs,- connector_label: self.connector_label,- created_at: self.created_at,- modified_at: self.modified_at,- connector_webhook_details: self.connector_webhook_details,- profile_id: self.profile_id,- applepay_verified_domains: self.applepay_verified_domains,- pm_auth_config: self.pm_auth_config,- status: self.status,- connector_wallets_details: self.connector_wallets_details.map(Encryption::from),- additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),- version: self.version,- feature_metadata: self.feature_metadata.map(From::from),- connector_webhook_registration_details: None,- apple_pay_certificates: None,- apple_pay_certificates_encrypted: None,- })- }-- async fn convert_back(- state: &KeyManagerState,- other: Self::DstType,- key: &Secret<Vec<u8>>,- _key_manager_identifier: Identifier,- ) -> CustomResult<Self, ValidationError> {- let identifier = Identifier::Merchant(other.merchant_id.clone());-- let decrypted_data = crypto_operation(- state,- type_name!(Self::DstType),- CryptoOperation::BatchDecrypt(EncryptedMerchantConnectorAccount::to_encryptable(- EncryptedMerchantConnectorAccount {- connector_account_details: other.connector_account_details,- additional_merchant_data: other.additional_merchant_data,- connector_wallets_details: other.connector_wallets_details,- },- )),- identifier.clone(),- key.peek(),- )- .await- .and_then(|val| val.try_into_batchoperation())- .change_context(ValidationError::InvalidValue {- message: "Failed while decrypting connector account details".to_string(),- })?;-- let decrypted_data = EncryptedMerchantConnectorAccount::from_encryptable(decrypted_data)- .change_context(ValidationError::InvalidValue {- message: "Failed while decrypting connector account details".to_string(),- })?;-- Ok(Self {- id: other.id,- merchant_id: other.merchant_id,- connector_name: other.connector_name,- connector_account_details: decrypted_data.connector_account_details,- disabled: other.disabled,- payment_methods_enabled: other.payment_methods_enabled,- connector_type: other.connector_type,- metadata: other.metadata,-- frm_configs: other.frm_config,- connector_label: other.connector_label,- created_at: other.created_at,- modified_at: other.modified_at,- connector_webhook_details: other.connector_webhook_details,- profile_id: other.profile_id,- applepay_verified_domains: other.applepay_verified_domains,- pm_auth_config: other.pm_auth_config,- status: other.status,- connector_wallets_details: decrypted_data.connector_wallets_details,- additional_merchant_data: decrypted_data.additional_merchant_data,- version: other.version,- feature_metadata: other.feature_metadata.map(From::from),- })- }-- async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {- let now = date_time::now();- Ok(Self::NewDstType {- id: self.id,- merchant_id: Some(self.merchant_id),- connector_name: Some(self.connector_name),- connector_account_details: Some(self.connector_account_details.into()),- disabled: self.disabled,- payment_methods_enabled: self.payment_methods_enabled,- connector_type: Some(self.connector_type),- metadata: self.metadata,- frm_config: self.frm_configs,- connector_label: self.connector_label,- created_at: now,- modified_at: now,- connector_webhook_details: self.connector_webhook_details,- profile_id: self.profile_id,- applepay_verified_domains: self.applepay_verified_domains,- pm_auth_config: self.pm_auth_config,- status: self.status,- connector_wallets_details: self.connector_wallets_details.map(Encryption::from),- additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),- version: self.version,- feature_metadata: self.feature_metadata.map(From::from),- })- }-}--#[cfg(feature = "v1")]-impl From<MerchantConnectorAccountUpdate> for MerchantConnectorAccountUpdateInternal {- fn from(merchant_connector_account_update: MerchantConnectorAccountUpdate) -> Self {- match merchant_connector_account_update {- MerchantConnectorAccountUpdate::Update {- connector_type,- connector_name,- connector_account_details,- test_mode,- disabled,- merchant_connector_id,- payment_methods_enabled,- metadata,- frm_configs,- connector_webhook_details,- applepay_verified_domains,- pm_auth_config,- connector_label,- status,- connector_wallets_details,- additional_merchant_data,- } => Self {- connector_type,- connector_name,- connector_account_details: connector_account_details.map(Encryption::from),- test_mode,- disabled,- merchant_connector_id,- payment_methods_enabled,- metadata,- frm_configs: None,- frm_config: frm_configs,- modified_at: Some(date_time::now()),- connector_webhook_details: *connector_webhook_details,- applepay_verified_domains,- pm_auth_config: *pm_auth_config,- connector_label,- status,- connector_wallets_details: connector_wallets_details.map(Encryption::from),- additional_merchant_data: additional_merchant_data.map(Encryption::from),- connector_webhook_registration_details: None,- apple_pay_certificates: None,- apple_pay_certificates_encrypted: None,- },- MerchantConnectorAccountUpdate::ConnectorWalletDetailsUpdate {- connector_wallets_details,- } => Self {- connector_wallets_details: Some(Encryption::from(connector_wallets_details)),- connector_type: None,- connector_name: None,- connector_account_details: None,- connector_label: None,- test_mode: None,- disabled: None,- merchant_connector_id: None,- payment_methods_enabled: None,- frm_configs: None,- metadata: None,- modified_at: None,- connector_webhook_details: None,- frm_config: None,- applepay_verified_domains: None,- pm_auth_config: None,- status: None,- additional_merchant_data: None,- connector_webhook_registration_details: None,- apple_pay_certificates: None,- apple_pay_certificates_encrypted: None,- },- MerchantConnectorAccountUpdate::ConnectorWebhookRegisterationUpdate {- connector_webhook_registration_details,- connector_webhook_details,- metadata,- } => Self {- connector_type: None,- connector_name: None,- connector_account_details: None,- connector_label: None,- test_mode: None,- disabled: None,- merchant_connector_id: None,- payment_methods_enabled: None,- frm_configs: None,- metadata,- modified_at: None,- connector_webhook_details,- frm_config: None,- applepay_verified_domains: None,- pm_auth_config: None,- status: None,- connector_wallets_details: None,- additional_merchant_data: None,- connector_webhook_registration_details,- apple_pay_certificates: None,- apple_pay_certificates_encrypted: None,- },- MerchantConnectorAccountUpdate::ApplePayCertificateCacheUpdate {- apple_pay_certificates,- apple_pay_certificates_encrypted,- } => Self {- connector_type: None,- connector_name: None,- connector_account_details: None,- connector_label: None,- test_mode: None,- disabled: None,- merchant_connector_id: None,- payment_methods_enabled: None,- frm_configs: None,- metadata: None,- modified_at: Some(date_time::now()),- connector_webhook_details: None,- frm_config: None,- applepay_verified_domains: None,- pm_auth_config: None,- status: None,- connector_wallets_details: None,- additional_merchant_data: None,- connector_webhook_registration_details: None,- apple_pay_certificates,- apple_pay_certificates_encrypted,- },- }- }-}--#[cfg(feature = "v2")]-impl From<MerchantConnectorAccountUpdate> for MerchantConnectorAccountUpdateInternal {- fn from(merchant_connector_account_update: MerchantConnectorAccountUpdate) -> Self {- match merchant_connector_account_update {- MerchantConnectorAccountUpdate::Update {- connector_type,- connector_account_details,- disabled,- payment_methods_enabled,- metadata,- frm_configs,- connector_webhook_details,- applepay_verified_domains,- pm_auth_config,- connector_label,- status,- connector_wallets_details,- additional_merchant_data,- feature_metadata,- } => Self {- connector_type,- connector_account_details: connector_account_details.map(Encryption::from),- disabled,- payment_methods_enabled,- metadata,- frm_config: frm_configs,- modified_at: Some(date_time::now()),- connector_webhook_details: *connector_webhook_details,- applepay_verified_domains,- pm_auth_config: *pm_auth_config,- connector_label,- status,- connector_wallets_details: connector_wallets_details.map(Encryption::from),- additional_merchant_data: additional_merchant_data.map(Encryption::from),- feature_metadata: feature_metadata.map(From::from),- apple_pay_certificates: None,- apple_pay_certificates_encrypted: None,- },- MerchantConnectorAccountUpdate::ConnectorWalletDetailsUpdate {- connector_wallets_details,- } => Self {- connector_wallets_details: Some(Encryption::from(connector_wallets_details)),- connector_type: None,- connector_account_details: None,- connector_label: None,- disabled: None,- payment_methods_enabled: None,- metadata: None,- modified_at: None,- connector_webhook_details: None,- frm_config: None,- applepay_verified_domains: None,- pm_auth_config: None,- status: None,- additional_merchant_data: None,- feature_metadata: None,- apple_pay_certificates: None,- apple_pay_certificates_encrypted: None,- },- MerchantConnectorAccountUpdate::ApplePayCertificateCacheUpdate {- apple_pay_certificates,- apple_pay_certificates_encrypted,- } => Self {- connector_type: None,- connector_account_details: None,- connector_label: None,- disabled: None,- payment_methods_enabled: None,- metadata: None,- modified_at: Some(date_time::now()),- connector_webhook_details: None,- frm_config: None,- applepay_verified_domains: None,- pm_auth_config: None,- status: None,- connector_wallets_details: None,- additional_merchant_data: None,- feature_metadata: None,- apple_pay_certificates,- apple_pay_certificates_encrypted,- },- }- }-}-common_utils::create_list_wrapper!(MerchantConnectorAccounts,MerchantConnectorAccount,@@ -1352,13 +872,7 @@ impl From<DieselMerchantConnectorAccountFeatureMetadata>}#[async_trait::async_trait]-pub trait MerchantConnectorAccountInterface-where- MerchantConnectorAccount: behaviour::Conversion<- DstType = storage::MerchantConnectorAccount,- NewDstType = storage::MerchantConnectorAccountNew,- >,-{+pub trait MerchantConnectorAccountInterface {type Error;#[cfg(feature = "v1")]async fn find_merchant_connector_account_by_merchant_id_connector_label(diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rsindex 97ce11d60b..07d39630a0 100644--- a/crates/router/src/core/admin.rs+++ b/crates/router/src/core/admin.rs@@ -21,6 +21,7 @@ use hyperswitch_domain_models::merchant_connector_account::{};use hyperswitch_masking::{ExposeInterface, PeekInterface, Secret};use pm_auth::types as pm_auth_types;+use storage_impl::behaviour;#[cfg(feature = "olap")]use {base64::Engine,@@ -3089,15 +3090,19 @@ pub async fn update_connector(let request_connector_label = req.connector_label;- let updated_mca =- db.update_merchant_connector_account(mca.clone(), payment_connector.into(), &key_store)- .await- .change_context(- errors::ApiErrorResponse::DuplicateMerchantConnectorAccount {- profile_id: profile_id.get_string_repr().to_owned(),- connector_label: request_connector_label.unwrap_or_default(),- },- )+ let updated_mca = db+ .update_merchant_connector_account(+ mca.clone(),+ behaviour::ForeignInto::foreign_into(payment_connector),+ &key_store,+ )+ .await+ .change_context(+ errors::ApiErrorResponse::DuplicateMerchantConnectorAccount {+ profile_id: profile_id.get_string_repr().to_owned(),+ connector_label: request_connector_label.unwrap_or_default(),+ },+ ).attach_printable_lazy(|| {format!("Failed while updating MerchantConnectorAccount: id: {merchant_connector_id:?}",diff --git a/crates/router/src/core/apple_pay_certificates_migration.rs b/crates/router/src/core/apple_pay_certificates_migration.rsindex adb804767b..b825a610a2 100644--- a/crates/router/src/core/apple_pay_certificates_migration.rs+++ b/crates/router/src/core/apple_pay_certificates_migration.rs@@ -2,6 +2,7 @@ use api_models::apple_pay_certificates_migration;use common_utils::{errors::CustomResult, type_name, types::keymanager::Identifier};use error_stack::ResultExt;use hyperswitch_masking::{PeekInterface, Secret};+use storage_impl::behaviour::ForeignInto;use super::{errors::{self, StorageErrorExt},@@ -83,7 +84,7 @@ pub async fn apple_pay_certificates_migration(connector_wallets_details: encrypted_apple_pay_metadata,};- mca_to_update.push((connector_account, updated_mca.into()));+ mca_to_update.push((connector_account, updated_mca.foreign_into()));}}diff --git a/crates/router/src/core/hierarchical_resources.rs b/crates/router/src/core/hierarchical_resources.rsindex 2de2103699..17060d90e5 100644--- a/crates/router/src/core/hierarchical_resources.rs+++ b/crates/router/src/core/hierarchical_resources.rs@@ -13,6 +13,7 @@ use openssl::{pkey::PKey,x509::{X509NameBuilder, X509Req, X509},};+use storage_impl::behaviour::ForeignInto;use crate::{core::errors::{self, RouterResponse, RouterResult, StorageErrorExt},@@ -553,7 +554,7 @@ async fn link_hierarchical_resource_data_to_scope(RequestorAccount::MerchantConnectorAccount(mca) => {db.update_merchant_connector_account(*mca,- update.for_merchant_connector_account().into(),+ update.for_merchant_connector_account().foreign_into(),key_store,).awaitdiff --git a/crates/router/src/core/merchant_connector_webhook_management.rs b/crates/router/src/core/merchant_connector_webhook_management.rsindex 8102f809b5..acaf4736c2 100644--- a/crates/router/src/core/merchant_connector_webhook_management.rs+++ b/crates/router/src/core/merchant_connector_webhook_management.rs@@ -21,6 +21,7 @@ use hyperswitch_domain_models::{},};use hyperswitch_interfaces::api::{ConnectorSpecifications, ConnectorValidation};+use storage_impl::behaviour::ForeignInto;use transformers as configure_connector_webhook_flow;use crate::{@@ -498,7 +499,7 @@ pub async fn register_connector_webhook();if should_update_db {- db.update_merchant_connector_account(mca.clone(), mca_update.into(), &key_store)+ db.update_merchant_connector_account(mca.clone(), mca_update.foreign_into(), &key_store).await.change_context(errors::ApiErrorResponse::DuplicateMerchantConnectorAccount {diff --git a/crates/router/src/core/verification/utils.rs b/crates/router/src/core/verification/utils.rsindex eb3682a98f..1569d94755 100644--- a/crates/router/src/core/verification/utils.rs+++ b/crates/router/src/core/verification/utils.rs@@ -3,6 +3,7 @@ use common_utils::{id_type::{PaymentId, PayoutId},};use error_stack::{Report, ResultExt};+use storage_impl::behaviour::ForeignInto;use crate::{core::{@@ -102,7 +103,7 @@ pub async fn check_existence_and_add_domain_to_db(.store.update_merchant_connector_account(merchant_connector_account,- updated_mca.into(),+ updated_mca.foreign_into(),&key_store,).awaitdiff --git a/crates/router/src/db/merchant_connector_account.rs b/crates/router/src/db/merchant_connector_account.rsindex 4b0114c21c..cc38cc586e 100644--- a/crates/router/src/db/merchant_connector_account.rs+++ b/crates/router/src/db/merchant_connector_account.rs@@ -101,10 +101,13 @@ mod merchant_connector_account_cache_tests {use error_stack::ResultExt;use hyperswitch_domain_models::master_key::MasterKeyInterface;use hyperswitch_masking::PeekInterface;- use storage_impl::redis::{- cache::{self, CacheKey, CacheKind, ACCOUNTS_CACHE},- kv_store::RedisConnInterface,- pub_sub::PubSubInterface,+ use storage_impl::{+ behaviour::Conversion,+ redis::{+ cache::{self, CacheKey, CacheKind, ACCOUNTS_CACHE},+ kv_store::RedisConnInterface,+ pub_sub::PubSubInterface,+ },};use time::macros::datetime;use tokio::sync::oneshot;@@ -121,7 +124,7 @@ mod merchant_connector_account_cache_tests {},services,types::{- domain::{self, behaviour::Conversion},+ domain::{self},storage,},};diff --git a/crates/storage_impl/src/merchant_connector_account.rs b/crates/storage_impl/src/merchant_connector_account.rsindex 40969e33da..d4d6f20897 100644--- a/crates/storage_impl/src/merchant_connector_account.rs+++ b/crates/storage_impl/src/merchant_connector_account.rs@@ -3,7 +3,6 @@ use common_utils::{encryption::Encryption, ext_traits::AsyncExt};use diesel_models::merchant_connector_account as storage;use error_stack::{report, ResultExt};use hyperswitch_domain_models::{- behaviour::{Conversion, ReverseConversion},merchant_connector_account::{self as domain, MerchantConnectorAccountInterface},merchant_key_store::MerchantKeyStore,};@@ -12,6 +11,7 @@ use router_env::{instrument, tracing};#[cfg(feature = "accounts_cache")]use crate::redis::cache;use crate::{+ behaviour::{Conversion, ForeignFrom, ReverseConversion},kv_router_store,utils::{pg_accounts_connection_read, pg_accounts_connection_write},CustomResult, DatabaseStore, MockDb, RouterStore, StorageError,@@ -645,9 +645,7 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {self.get_keymanager_state().attach_printable("Missing KeyManagerState")?,key_store.key.get_inner(),- common_utils::types::keymanager::Identifier::Merchant(- key_store.merchant_id.clone(),- ),+ keymanager::Identifier::Merchant(key_store.merchant_id.clone()),).await.change_context(Self::Error::DecryptionError)@@ -1046,9 +1044,7 @@ impl<T: DatabaseStore> MerchantConnectorAccountInterface for RouterStore<T> {self.get_keymanager_state().attach_printable("Missing KeyManagerState")?,key_store.key.get_inner(),- common_utils::types::keymanager::Identifier::Merchant(- key_store.merchant_id.clone(),- ),+ keymanager::Identifier::Merchant(key_store.merchant_id.clone()),).await.change_context(Self::Error::DecryptionError)@@ -1445,9 +1441,7 @@ impl MerchantConnectorAccountInterface for MockDb {self.get_keymanager_state().attach_printable("Missing KeyManagerState")?,key_store.key.get_inner(),- common_utils::types::keymanager::Identifier::Merchant(- key_store.merchant_id.clone(),- ),+ keymanager::Identifier::Merchant(key_store.merchant_id.clone()),).await.change_context(StorageError::DecryptionError)@@ -1553,9 +1547,7 @@ impl MerchantConnectorAccountInterface for MockDb {self.get_keymanager_state().attach_printable("Missing KeyManagerState")?,key_store.key.get_inner(),- common_utils::types::keymanager::Identifier::Merchant(- key_store.merchant_id.clone(),- ),+ keymanager::Identifier::Merchant(key_store.merchant_id.clone()),).await.change_context(StorageError::DecryptionError)@@ -1798,9 +1790,7 @@ impl MerchantConnectorAccountInterface for MockDb {self.get_keymanager_state().attach_printable("Missing KeyManagerState")?,key_store.key.get_inner(),- common_utils::types::keymanager::Identifier::Merchant(- key_store.merchant_id.clone(),- ),+ keymanager::Identifier::Merchant(key_store.merchant_id.clone()),).await.change_context(StorageError::DecryptionError)@@ -1863,6 +1853,514 @@ impl MerchantConnectorAccountInterface for MockDb {}}+#[cfg(feature = "v2")]+use std::collections::HashMap;++use common_utils::{+ errors::ValidationError,+ type_name,+ types::keymanager::{self, KeyManagerState, ToEncryptable},+};+#[cfg(feature = "v2")]+use diesel_models::merchant_connector_account::{+ BillingAccountReference as DieselBillingAccountReference,+ MerchantConnectorAccountFeatureMetadata as DieselMerchantConnectorAccountFeatureMetadata,+ RevenueRecoveryMetadata as DieselRevenueRecoveryMetadata,+};+#[cfg(feature = "v2")]+use hyperswitch_domain_models::errors::api_error_response;+use hyperswitch_domain_models::type_encryption::{crypto_operation, CryptoOperation};+use hyperswitch_masking::{PeekInterface, Secret};++#[cfg(feature = "v1")]+#[async_trait::async_trait]+impl Conversion for domain::MerchantConnectorAccount {+ type DstType = storage::MerchantConnectorAccount;+ type NewDstType = storage::MerchantConnectorAccountNew;++ async fn convert(self) -> CustomResult<Self::DstType, ValidationError> {+ Ok(storage::MerchantConnectorAccount {+ merchant_id: self.merchant_id,+ connector_name: self.connector_name,+ connector_account_details: self.connector_account_details.into(),+ test_mode: self.test_mode,+ disabled: self.disabled,+ merchant_connector_id: self.merchant_connector_id.clone(),+ id: Some(self.merchant_connector_id),+ payment_methods_enabled: self.payment_methods_enabled,+ connector_type: self.connector_type,+ metadata: self.metadata,+ frm_configs: None,+ frm_config: self.frm_configs,+ business_country: self.business_country,+ business_label: self.business_label,+ connector_label: self.connector_label,+ business_sub_label: self.business_sub_label,+ created_at: self.created_at,+ modified_at: self.modified_at,+ connector_webhook_details: self.connector_webhook_details,+ profile_id: Some(self.profile_id),+ applepay_verified_domains: self.applepay_verified_domains,+ pm_auth_config: self.pm_auth_config,+ status: self.status,+ connector_wallets_details: self.connector_wallets_details.map(Encryption::from),+ additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),+ version: self.version,+ connector_webhook_registration_details: self.connector_webhook_registration_details,+ apple_pay_certificates: self.apple_pay_certificates,+ apple_pay_certificates_encrypted: self+ .apple_pay_certificates_encrypted+ .map(|data| data.into()),+ })+ }++ async fn convert_back(+ state: &KeyManagerState,+ other: Self::DstType,+ key: &Secret<Vec<u8>>,+ _key_manager_identifier: keymanager::Identifier,+ ) -> CustomResult<Self, ValidationError> {+ let identifier = keymanager::Identifier::Merchant(other.merchant_id.clone());+ let decrypted_data = crypto_operation(+ state,+ type_name!(Self::DstType),+ CryptoOperation::BatchDecrypt(+ domain::EncryptedMerchantConnectorAccount::to_encryptable(+ domain::EncryptedMerchantConnectorAccount {+ connector_account_details: other.connector_account_details,+ additional_merchant_data: other.additional_merchant_data,+ connector_wallets_details: other.connector_wallets_details,+ apple_pay_certificates_encrypted: other.apple_pay_certificates_encrypted,+ },+ ),+ ),+ identifier.clone(),+ key.peek(),+ )+ .await+ .and_then(|val| val.try_into_batchoperation())+ .change_context(ValidationError::InvalidValue {+ message: "Failed while decrypting connector account details".to_string(),+ })?;++ let decrypted_data =+ domain::EncryptedMerchantConnectorAccount::from_encryptable(decrypted_data)+ .change_context(ValidationError::InvalidValue {+ message: "Failed while decrypting connector account details".to_string(),+ })?;++ Ok(Self {+ merchant_id: other.merchant_id,+ connector_name: other.connector_name,+ connector_account_details: decrypted_data.connector_account_details,+ test_mode: other.test_mode,+ disabled: other.disabled,+ merchant_connector_id: other.merchant_connector_id,+ payment_methods_enabled: other.payment_methods_enabled,+ connector_type: other.connector_type,+ metadata: other.metadata,++ frm_configs: other.frm_config,+ business_country: other.business_country,+ business_label: other.business_label,+ connector_label: other.connector_label,+ business_sub_label: other.business_sub_label,+ created_at: other.created_at,+ modified_at: other.modified_at,+ connector_webhook_details: other.connector_webhook_details,+ profile_id: other+ .profile_id+ .ok_or(ValidationError::MissingRequiredField {+ field_name: "profile_id".to_string(),+ })?,+ applepay_verified_domains: other.applepay_verified_domains,+ pm_auth_config: other.pm_auth_config,+ status: other.status,+ connector_wallets_details: decrypted_data.connector_wallets_details,+ additional_merchant_data: decrypted_data.additional_merchant_data,+ version: other.version,+ connector_webhook_registration_details: other.connector_webhook_registration_details,+ apple_pay_certificates: other.apple_pay_certificates,+ apple_pay_certificates_encrypted: decrypted_data.apple_pay_certificates_encrypted,+ })+ }++ async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {+ let now = common_utils::date_time::now();+ Ok(Self::NewDstType {+ merchant_id: Some(self.merchant_id),+ connector_name: Some(self.connector_name),+ connector_account_details: Some(self.connector_account_details.into()),+ test_mode: self.test_mode,+ disabled: self.disabled,+ merchant_connector_id: self.merchant_connector_id.clone(),+ id: Some(self.merchant_connector_id),+ payment_methods_enabled: self.payment_methods_enabled,+ connector_type: Some(self.connector_type),+ metadata: self.metadata,+ frm_configs: None,+ frm_config: self.frm_configs,+ business_country: self.business_country,+ business_label: self.business_label,+ connector_label: self.connector_label,+ business_sub_label: self.business_sub_label,+ created_at: now,+ modified_at: now,+ connector_webhook_details: self.connector_webhook_details,+ profile_id: Some(self.profile_id),+ applepay_verified_domains: self.applepay_verified_domains,+ pm_auth_config: self.pm_auth_config,+ status: self.status,+ connector_wallets_details: self.connector_wallets_details.map(Encryption::from),+ additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),+ version: self.version,+ })+ }+}++#[cfg(feature = "v2")]+#[async_trait::async_trait]+impl Conversion for domain::MerchantConnectorAccount {+ type DstType = storage::MerchantConnectorAccount;+ type NewDstType = storage::MerchantConnectorAccountNew;++ async fn convert(self) -> CustomResult<Self::DstType, ValidationError> {+ Ok(storage::MerchantConnectorAccount {+ id: self.id,+ merchant_id: self.merchant_id,+ connector_name: self.connector_name,+ connector_account_details: self.connector_account_details.into(),+ disabled: self.disabled,+ payment_methods_enabled: self.payment_methods_enabled,+ connector_type: self.connector_type,+ metadata: self.metadata,+ frm_config: self.frm_configs,+ connector_label: self.connector_label,+ created_at: self.created_at,+ modified_at: self.modified_at,+ connector_webhook_details: self.connector_webhook_details,+ profile_id: self.profile_id,+ applepay_verified_domains: self.applepay_verified_domains,+ pm_auth_config: self.pm_auth_config,+ status: self.status,+ connector_wallets_details: self.connector_wallets_details.map(Encryption::from),+ additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),+ version: self.version,+ feature_metadata: self.feature_metadata.map(From::from),+ connector_webhook_registration_details: None,+ apple_pay_certificates: None,+ apple_pay_certificates_encrypted: None,+ })+ }++ async fn convert_back(+ state: &KeyManagerState,+ other: Self::DstType,+ key: &Secret<Vec<u8>>,+ _key_manager_identifier: keymanager::Identifier,+ ) -> CustomResult<Self, ValidationError> {+ let identifier = keymanager::Identifier::Merchant(other.merchant_id.clone());++ let decrypted_data = crypto_operation(+ state,+ type_name!(Self::DstType),+ CryptoOperation::BatchDecrypt(+ domain::EncryptedMerchantConnectorAccount::to_encryptable(+ domain::EncryptedMerchantConnectorAccount {+ connector_account_details: other.connector_account_details,+ additional_merchant_data: other.additional_merchant_data,+ connector_wallets_details: other.connector_wallets_details,+ },+ ),+ ),+ identifier.clone(),+ key.peek(),+ )+ .await+ .and_then(|val| val.try_into_batchoperation())+ .change_context(ValidationError::InvalidValue {+ message: "Failed while decrypting connector account details".to_string(),+ })?;++ let decrypted_data =+ domain::EncryptedMerchantConnectorAccount::from_encryptable(decrypted_data)+ .change_context(ValidationError::InvalidValue {+ message: "Failed while decrypting connector account details".to_string(),+ })?;++ Ok(Self {+ id: other.id,+ merchant_id: other.merchant_id,+ connector_name: other.connector_name,+ connector_account_details: decrypted_data.connector_account_details,+ disabled: other.disabled,+ payment_methods_enabled: other.payment_methods_enabled,+ connector_type: other.connector_type,+ metadata: other.metadata,++ frm_configs: other.frm_config,+ connector_label: other.connector_label,+ created_at: other.created_at,+ modified_at: other.modified_at,+ connector_webhook_details: other.connector_webhook_details,+ profile_id: other.profile_id,+ applepay_verified_domains: other.applepay_verified_domains,+ pm_auth_config: other.pm_auth_config,+ status: other.status,+ connector_wallets_details: decrypted_data.connector_wallets_details,+ additional_merchant_data: decrypted_data.additional_merchant_data,+ version: other.version,+ feature_metadata: other.feature_metadata.map(From::from),+ })+ }++ async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {+ let now = common_utils::date_time::now();+ Ok(Self::NewDstType {+ id: self.id,+ merchant_id: Some(self.merchant_id),+ connector_name: Some(self.connector_name),+ connector_account_details: Some(self.connector_account_details.into()),+ disabled: self.disabled,+ payment_methods_enabled: self.payment_methods_enabled,+ connector_type: Some(self.connector_type),+ metadata: self.metadata,+ frm_config: self.frm_configs,+ connector_label: self.connector_label,+ created_at: now,+ modified_at: now,+ connector_webhook_details: self.connector_webhook_details,+ profile_id: self.profile_id,+ applepay_verified_domains: self.applepay_verified_domains,+ pm_auth_config: self.pm_auth_config,+ status: self.status,+ connector_wallets_details: self.connector_wallets_details.map(Encryption::from),+ additional_merchant_data: self.additional_merchant_data.map(|data| data.into()),+ version: self.version,+ feature_metadata: self.feature_metadata.map(From::from),+ })+ }+}++#[cfg(feature = "v1")]+impl ForeignFrom<domain::MerchantConnectorAccountUpdate>+ for storage::MerchantConnectorAccountUpdateInternal+{+ fn foreign_from(+ merchant_connector_account_update: domain::MerchantConnectorAccountUpdate,+ ) -> Self {+ match merchant_connector_account_update {+ domain::MerchantConnectorAccountUpdate::Update {+ connector_type,+ connector_name,+ connector_account_details,+ test_mode,+ disabled,+ merchant_connector_id,+ payment_methods_enabled,+ metadata,+ frm_configs,+ connector_webhook_details,+ applepay_verified_domains,+ pm_auth_config,+ connector_label,+ status,+ connector_wallets_details,+ additional_merchant_data,+ } => Self {+ connector_type,+ connector_name,+ connector_account_details: connector_account_details.map(Encryption::from),+ test_mode,+ disabled,+ merchant_connector_id,+ payment_methods_enabled,+ metadata,+ frm_configs: None,+ frm_config: frm_configs,+ modified_at: Some(common_utils::date_time::now()),+ connector_webhook_details: *connector_webhook_details,+ applepay_verified_domains,+ pm_auth_config: *pm_auth_config,+ connector_label,+ status,+ connector_wallets_details: connector_wallets_details.map(Encryption::from),+ additional_merchant_data: additional_merchant_data.map(Encryption::from),+ connector_webhook_registration_details: None,+ apple_pay_certificates: None,+ apple_pay_certificates_encrypted: None,+ },+ domain::MerchantConnectorAccountUpdate::ConnectorWalletDetailsUpdate {+ connector_wallets_details,+ } => Self {+ connector_wallets_details: Some(Encryption::from(connector_wallets_details)),+ connector_type: None,+ connector_name: None,+ connector_account_details: None,+ connector_label: None,+ test_mode: None,+ disabled: None,+ merchant_connector_id: None,+ payment_methods_enabled: None,+ frm_configs: None,+ metadata: None,+ modified_at: None,+ connector_webhook_details: None,+ frm_config: None,+ applepay_verified_domains: None,+ pm_auth_config: None,+ status: None,+ additional_merchant_data: None,+ connector_webhook_registration_details: None,+ apple_pay_certificates: None,+ apple_pay_certificates_encrypted: None,+ },+ domain::MerchantConnectorAccountUpdate::ConnectorWebhookRegisterationUpdate {+ connector_webhook_registration_details,+ connector_webhook_details,+ metadata,+ } => Self {+ connector_type: None,+ connector_name: None,+ connector_account_details: None,+ connector_label: None,+ test_mode: None,+ disabled: None,+ merchant_connector_id: None,+ payment_methods_enabled: None,+ frm_configs: None,+ metadata,+ modified_at: None,+ connector_webhook_details,+ frm_config: None,+ applepay_verified_domains: None,+ pm_auth_config: None,+ status: None,+ connector_wallets_details: None,+ additional_merchant_data: None,+ connector_webhook_registration_details,+ apple_pay_certificates: None,+ apple_pay_certificates_encrypted: None,+ },+ domain::MerchantConnectorAccountUpdate::ApplePayCertificateCacheUpdate {+ apple_pay_certificates,+ apple_pay_certificates_encrypted,+ } => Self {+ connector_type: None,+ connector_name: None,+ connector_account_details: None,+ connector_label: None,+ test_mode: None,+ disabled: None,+ merchant_connector_id: None,+ payment_methods_enabled: None,+ frm_configs: None,+ metadata: None,+ modified_at: Some(common_utils::date_time::now()),+ connector_webhook_details: None,+ frm_config: None,+ applepay_verified_domains: None,+ pm_auth_config: None,+ status: None,+ connector_wallets_details: None,+ additional_merchant_data: None,+ connector_webhook_registration_details: None,+ apple_pay_certificates,+ apple_pay_certificates_encrypted,+ },+ }+ }+}++#[cfg(feature = "v2")]+impl ForeignFrom<domain::MerchantConnectorAccountUpdate>+ for storage::MerchantConnectorAccountUpdateInternal+{+ fn foreign_from(+ merchant_connector_account_update: domain::MerchantConnectorAccountUpdate,+ ) -> Self {+ match merchant_connector_account_update {+ domain::MerchantConnectorAccountUpdate::Update {+ connector_type,+ connector_account_details,+ disabled,+ payment_methods_enabled,+ metadata,+ frm_configs,+ connector_webhook_details,+ applepay_verified_domains,+ pm_auth_config,+ connector_label,+ status,+ connector_wallets_details,+ additional_merchant_data,+ feature_metadata,+ } => Self {+ connector_type,+ connector_account_details: connector_account_details.map(Encryption::from),+ disabled,+ payment_methods_enabled,+ metadata,+ frm_config: frm_configs,+ modified_at: Some(common_utils::date_time::now()),+ connector_webhook_details: *connector_webhook_details,+ applepay_verified_domains,+ pm_auth_config: *pm_auth_config,+ connector_label,+ status,+ connector_wallets_details: connector_wallets_details.map(Encryption::from),+ additional_merchant_data: additional_merchant_data.map(Encryption::from),+ feature_metadata: feature_metadata.map(From::from),+ apple_pay_certificates: None,+ apple_pay_certificates_encrypted: None,+ },+ domain::MerchantConnectorAccountUpdate::ConnectorWalletDetailsUpdate {+ connector_wallets_details,+ } => Self {+ connector_wallets_details: Some(Encryption::from(connector_wallets_details)),+ connector_type: None,+ connector_account_details: None,+ connector_label: None,+ disabled: None,+ payment_methods_enabled: None,+ metadata: None,+ modified_at: None,+ connector_webhook_details: None,+ frm_config: None,+ applepay_verified_domains: None,+ pm_auth_config: None,+ status: None,+ additional_merchant_data: None,+ feature_metadata: None,+ apple_pay_certificates: None,+ apple_pay_certificates_encrypted: None,+ },+ domain::MerchantConnectorAccountUpdate::ApplePayCertificateCacheUpdate {+ apple_pay_certificates,+ apple_pay_certificates_encrypted,+ } => Self {+ connector_type: None,+ connector_account_details: None,+ connector_label: None,+ disabled: None,+ payment_methods_enabled: None,+ metadata: None,+ modified_at: Some(common_utils::date_time::now()),+ connector_webhook_details: None,+ frm_config: None,+ applepay_verified_domains: None,+ pm_auth_config: None,+ status: None,+ connector_wallets_details: None,+ additional_merchant_data: None,+ feature_metadata: None,+ apple_pay_certificates,+ apple_pay_certificates_encrypted,+ },+ }+ }+}+#[cfg(test)]mod tests {use super::*;