Skip to content

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

feat(core): Batch Decrypt PaymentMethod (#13311)

Anurag Thakurc9443bd4ae7e
diff --git a/crates/hyperswitch_domain_models/src/payment_methods.rs b/crates/hyperswitch_domain_models/src/payment_methods.rs
index df8833cfdc..4e84a7356e 100644
--- a/crates/hyperswitch_domain_models/src/payment_methods.rs
+++ b/crates/hyperswitch_domain_models/src/payment_methods.rs
@@ -5,10 +5,10 @@ use api_models::payment_methods::PaymentMethodsData;
use api_models::{customers, payment_methods, payments};
// specific imports because of using the macro
use common_enums::enums::MerchantStorageScheme;
-#[cfg(feature = "v1")]
-use common_utils::crypto::OptionalEncryptableValue;
#[cfg(feature = "v2")]
use common_utils::{crypto::Encryptable, encryption::Encryption, types::keymanager::ToEncryptable};
+#[cfg(feature = "v1")]
+use common_utils::{crypto::Encryptable, encryption::Encryption, types::keymanager::ToEncryptable};
use common_utils::{
errors::{CustomResult, ParsingError, ValidationError},
ext_traits::OptionExt,
@@ -22,14 +22,13 @@ use error_stack::ResultExt;
use hyperswitch_masking::{ExposeInterface, PeekInterface, Secret};
#[cfg(feature = "v1")]
use router_env::logger;
-#[cfg(feature = "v2")]
+#[cfg(feature = "v1")]
use rustc_hash::FxHashMap;
#[cfg(feature = "v2")]
+use rustc_hash::FxHashMap;
use serde_json::Value;
use time::PrimitiveDateTime;
-#[cfg(feature = "v1")]
-use crate::type_encryption::AsyncLift;
#[cfg(feature = "v2")]
use crate::{address::Address, platform::Initiator};
use crate::{
@@ -53,7 +52,7 @@ impl VaultId {
}
}
#[cfg(feature = "v1")]
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, router_derive::ToEncryption)]
pub struct PaymentMethod {
/// Customer_id is made optional to support guest checkout flow when modular pm is enabled
pub customer_id: Option<id_type::CustomerId>,
@@ -76,27 +75,32 @@ pub struct PaymentMethod {
pub payment_method_issuer: Option<String>,
pub payment_method_issuer_code: Option<storage_enums::PaymentMethodIssuerCode>,
pub metadata: Option<pii::SecretSerdeValue>,
- pub payment_method_data: OptionalEncryptableValue,
+ #[encrypt(ty = Value)]
+ pub payment_method_data: Option<Encryptable<Secret<Value>>>,
pub locker_id: Option<String>,
pub last_used_at: PrimitiveDateTime,
- pub connector_mandate_details: Option<serde_json::Value>,
+ pub connector_mandate_details: Option<Value>,
pub customer_acceptance: Option<pii::SecretSerdeValue>,
pub status: storage_enums::PaymentMethodStatus,
pub network_transaction_id: Option<String>,
pub network_transaction_link_id: Option<String>,
pub client_secret: Option<String>,
- pub payment_method_billing_address: OptionalEncryptableValue,
+ #[encrypt(ty = Value)]
+ pub payment_method_billing_address: Option<Encryptable<Secret<Value>>>,
pub updated_by: Option<String>,
pub version: common_enums::ApiVersion,
pub network_token_requestor_reference_id: Option<String>,
pub network_token_locker_id: Option<String>,
- pub network_token_payment_method_data: OptionalEncryptableValue,
+ #[encrypt(ty = Value)]
+ pub network_token_payment_method_data: Option<Encryptable<Secret<Value>>>,
pub vault_source_details: PaymentMethodVaultSourceDetails,
pub created_by: Option<CreatedBy>,
pub last_modified_by: Option<CreatedBy>,
- pub customer_details: OptionalEncryptableValue,
+ #[encrypt(ty = Value)]
+ pub customer_details: Option<Encryptable<Secret<Value>>>,
pub locker_fingerprint_id: Option<String>,
- pub network_tokenization_data: OptionalEncryptableValue,
+ #[encrypt(ty = Value)]
+ pub network_tokenization_data: Option<Encryptable<Secret<Value>>>,
pub storage_type: Option<common_enums::StorageType>,
pub compatibility_updated_at: Option<PrimitiveDateTime>,
}
@@ -520,95 +524,28 @@ impl super::behaviour::Conversion for PaymentMethod {
Self: Sized,
{
// Decrypt encrypted fields first
- let (
- payment_method_data,
- payment_method_billing_address,
- network_token_payment_method_data,
- network_tokenization_data,
- customer_details,
- ) = async {
- let payment_method_data = item
- .payment_method_data
- .async_lift(|inner| async {
- crypto_operation(
- state,
- type_name!(Self::DstType),
- CryptoOperation::DecryptOptional(inner),
- key_manager_identifier.clone(),
- key.peek(),
- )
- .await
- .and_then(|val| val.try_into_optionaloperation())
- })
- .await?;
-
- let payment_method_billing_address = item
- .payment_method_billing_address
- .async_lift(|inner| async {
- crypto_operation(
- state,
- type_name!(Self::DstType),
- CryptoOperation::DecryptOptional(inner),
- key_manager_identifier.clone(),
- key.peek(),
- )
- .await
- .and_then(|val| val.try_into_optionaloperation())
- })
- .await?;
-
- let network_token_payment_method_data = item
- .network_token_payment_method_data
- .async_lift(|inner| async {
- crypto_operation(
- state,
- type_name!(Self::DstType),
- CryptoOperation::DecryptOptional(inner),
- key_manager_identifier.clone(),
- key.peek(),
- )
- .await
- .and_then(|val| val.try_into_optionaloperation())
- })
- .await?;
-
- let network_tokenization_data = item
- .network_tokenization_data
- .async_lift(|inner| async {
- crypto_operation(
- state,
- type_name!(Self::DstType),
- CryptoOperation::DecryptOptional(inner),
- key_manager_identifier.clone(),
- key.peek(),
- )
- .await
- .and_then(|val| val.try_into_optionaloperation())
- })
- .await?;
-
- let customer_details = item
- .customer_details
- .async_lift(|inner| async {
- crypto_operation(
- state,
- type_name!(Self::DstType),
- CryptoOperation::DecryptOptional(inner),
- key_manager_identifier.clone(),
- key.peek(),
- )
- .await
- .and_then(|val| val.try_into_optionaloperation())
- })
- .await?;
+ let data = async {
+ let decrypted_data = crypto_operation(
+ state,
+ type_name!(Self::DstType),
+ CryptoOperation::BatchDecrypt(EncryptedPaymentMethod::to_encryptable(
+ EncryptedPaymentMethod {
+ payment_method_data: item.payment_method_data,
+ payment_method_billing_address: item.payment_method_billing_address,
+ network_token_payment_method_data: item.network_token_payment_method_data,
+ customer_details: item.customer_details,
+ network_tokenization_data: item.network_tokenization_data,
+ },
+ )),
+ key_manager_identifier,
+ key.peek(),
+ )
+ .await
+ .and_then(|val| val.try_into_batchoperation())?;
- Ok::<_, error_stack::Report<common_utils::errors::CryptoError>>((
- payment_method_data,
- payment_method_billing_address,
- network_token_payment_method_data,
- network_tokenization_data,
- customer_details,
- ))
+ EncryptedPaymentMethod::from_encryptable(decrypted_data)
+ .change_context(common_utils::errors::CryptoError::DecodingFailed)
+ .attach_printable("Invalid batch operation data")
}
.await
.change_context(ValidationError::InvalidValue {
@@ -661,7 +598,7 @@ impl super::behaviour::Conversion for PaymentMethod {
payment_method_issuer: item.payment_method_issuer,
payment_method_issuer_code: item.payment_method_issuer_code,
metadata: item.metadata,
- payment_method_data,
+ payment_method_data: data.payment_method_data,
locker_id: item.locker_id,
last_used_at: item.last_used_at,
connector_mandate_details: item.connector_mandate_details,
@@ -670,12 +607,12 @@ impl super::behaviour::Conversion for PaymentMethod {
network_transaction_id: item.network_transaction_id,
network_transaction_link_id: item.network_transaction_link_id,
client_secret: item.client_secret,
- payment_method_billing_address,
+ payment_method_billing_address: data.payment_method_billing_address,
updated_by: item.updated_by,
version: item.version,
network_token_requestor_reference_id: item.network_token_requestor_reference_id,
network_token_locker_id: item.network_token_locker_id,
- network_token_payment_method_data,
+ network_token_payment_method_data: data.network_token_payment_method_data,
vault_source_details,
created_by: item
.created_by
@@ -683,9 +620,9 @@ impl super::behaviour::Conversion for PaymentMethod {
last_modified_by: item
.last_modified_by
.and_then(|last_modified_by| last_modified_by.parse::<CreatedBy>().ok()),
- customer_details,
+ customer_details: data.customer_details,
locker_fingerprint_id: item.locker_fingerprint_id,
- network_tokenization_data,
+ network_tokenization_data: data.network_tokenization_data,
storage_type: None,
compatibility_updated_at: item.compatibility_updated_at,
})
@@ -1743,9 +1680,7 @@ mod tests {
use super::*;
- fn get_payment_method_with_mandate_data(
- mandate_data: Option<serde_json::Value>,
- ) -> PaymentMethod {
+ fn get_payment_method_with_mandate_data(mandate_data: Option<Value>) -> PaymentMethod {
let payment_method = PaymentMethod {
customer_id: Some(id_type::CustomerId::default()),
merchant_id: id_type::MerchantId::default(),