mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 18:17:13 +08:00 
			
		
		
		
	chore: making of function create_encrypted_data (#5251)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
		| @ -46,6 +46,8 @@ use super::surcharge_decision_configs::{ | |||||||
| }; | }; | ||||||
| #[cfg(not(feature = "connector_choice_mca_id"))] | #[cfg(not(feature = "connector_choice_mca_id"))] | ||||||
| use crate::core::utils::get_connector_label; | use crate::core::utils::get_connector_label; | ||||||
|  | #[cfg(feature = "payouts")] | ||||||
|  | use crate::types::domain::types::AsyncLift; | ||||||
| use crate::{ | use crate::{ | ||||||
|     configs::settings, |     configs::settings, | ||||||
|     core::{ |     core::{ | ||||||
| @ -67,10 +69,7 @@ use crate::{ | |||||||
|     services, |     services, | ||||||
|     types::{ |     types::{ | ||||||
|         api::{self, routing as routing_types, PaymentMethodCreateExt}, |         api::{self, routing as routing_types, PaymentMethodCreateExt}, | ||||||
|         domain::{ |         domain::{self, types::decrypt}, | ||||||
|             self, |  | ||||||
|             types::{decrypt, encrypt_optional, AsyncLift}, |  | ||||||
|         }, |  | ||||||
|         storage::{self, enums, PaymentMethodListContext, PaymentTokenData}, |         storage::{self, enums, PaymentMethodListContext, PaymentTokenData}, | ||||||
|         transformers::{ForeignFrom, ForeignTryFrom}, |         transformers::{ForeignFrom, ForeignTryFrom}, | ||||||
|     }, |     }, | ||||||
| @ -351,9 +350,14 @@ pub async fn skip_locker_call_and_migrate_payment_method( | |||||||
|         .change_context(errors::ApiErrorResponse::InternalServerError) |         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|         .attach_printable("Failed to parse connector mandate details")?; |         .attach_printable("Failed to parse connector mandate details")?; | ||||||
|  |  | ||||||
|     let payment_method_billing_address = create_encrypted_data(key_store, req.billing.clone()) |     let payment_method_billing_address: Option<Encryptable<Secret<serde_json::Value>>> = req | ||||||
|  |         .billing | ||||||
|  |         .clone() | ||||||
|  |         .async_map(|billing| create_encrypted_data(key_store, billing)) | ||||||
|         .await |         .await | ||||||
|         .map(|details| details.into()); |         .transpose() | ||||||
|  |         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |         .attach_printable("Unable to encrypt Payment method billing address")?; | ||||||
|  |  | ||||||
|     let customer = db |     let customer = db | ||||||
|         .find_customer_by_customer_id_merchant_id( |         .find_customer_by_customer_id_merchant_id( | ||||||
| @ -484,10 +488,13 @@ pub async fn skip_locker_call_and_migrate_payment_method( | |||||||
|         .as_ref() |         .as_ref() | ||||||
|         .map(|card| PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone()))); |         .map(|card| PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone()))); | ||||||
|  |  | ||||||
|     let payment_method_data_encrypted = |     let payment_method_data_encrypted: Option<Encryptable<Secret<serde_json::Value>>> = | ||||||
|         create_encrypted_data(key_store, payment_method_card_details) |         payment_method_card_details | ||||||
|  |             .async_map(|card_details| create_encrypted_data(key_store, card_details)) | ||||||
|             .await |             .await | ||||||
|             .map(|details| details.into()); |             .transpose() | ||||||
|  |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt Payment method card details")?; | ||||||
|  |  | ||||||
|     let payment_method_metadata: Option<serde_json::Value> = |     let payment_method_metadata: Option<serde_json::Value> = | ||||||
|         req.metadata.as_ref().map(|data| data.peek()).cloned(); |         req.metadata.as_ref().map(|data| data.peek()).cloned(); | ||||||
| @ -508,7 +515,7 @@ pub async fn skip_locker_call_and_migrate_payment_method( | |||||||
|                 payment_method_issuer: req.payment_method_issuer.clone(), |                 payment_method_issuer: req.payment_method_issuer.clone(), | ||||||
|                 scheme: req.card_network.clone(), |                 scheme: req.card_network.clone(), | ||||||
|                 metadata: payment_method_metadata.map(Secret::new), |                 metadata: payment_method_metadata.map(Secret::new), | ||||||
|                 payment_method_data: payment_method_data_encrypted, |                 payment_method_data: payment_method_data_encrypted.map(Into::into), | ||||||
|                 connector_mandate_details: Some(connector_mandate_details), |                 connector_mandate_details: Some(connector_mandate_details), | ||||||
|                 customer_acceptance: None, |                 customer_acceptance: None, | ||||||
|                 client_secret: None, |                 client_secret: None, | ||||||
| @ -527,7 +534,7 @@ pub async fn skip_locker_call_and_migrate_payment_method( | |||||||
|                 created_at: current_time, |                 created_at: current_time, | ||||||
|                 last_modified: current_time, |                 last_modified: current_time, | ||||||
|                 last_used_at: current_time, |                 last_used_at: current_time, | ||||||
|                 payment_method_billing_address, |                 payment_method_billing_address: payment_method_billing_address.map(Into::into), | ||||||
|                 updated_by: None, |                 updated_by: None, | ||||||
|             }, |             }, | ||||||
|             merchant_account.storage_scheme, |             merchant_account.storage_scheme, | ||||||
| @ -591,9 +598,14 @@ pub async fn get_client_secret_or_add_payment_method( | |||||||
|     #[cfg(feature = "payouts")] |     #[cfg(feature = "payouts")] | ||||||
|     let condition = req.card.is_some() || req.bank_transfer.is_some() || req.wallet.is_some(); |     let condition = req.card.is_some() || req.bank_transfer.is_some() || req.wallet.is_some(); | ||||||
|  |  | ||||||
|     let payment_method_billing_address = create_encrypted_data(key_store, req.billing.clone()) |     let payment_method_billing_address: Option<Encryptable<Secret<serde_json::Value>>> = req | ||||||
|  |         .billing | ||||||
|  |         .clone() | ||||||
|  |         .async_map(|billing| create_encrypted_data(key_store, billing)) | ||||||
|         .await |         .await | ||||||
|         .map(|details| details.into()); |         .transpose() | ||||||
|  |         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |         .attach_printable("Unable to encrypt Payment method billing address")?; | ||||||
|  |  | ||||||
|     let connector_mandate_details = req |     let connector_mandate_details = req | ||||||
|         .connector_mandate_details |         .connector_mandate_details | ||||||
| @ -622,7 +634,7 @@ pub async fn get_client_secret_or_add_payment_method( | |||||||
|             Some(enums::PaymentMethodStatus::AwaitingData), |             Some(enums::PaymentMethodStatus::AwaitingData), | ||||||
|             None, |             None, | ||||||
|             merchant_account.storage_scheme, |             merchant_account.storage_scheme, | ||||||
|             payment_method_billing_address, |             payment_method_billing_address.map(Into::into), | ||||||
|             None, |             None, | ||||||
|         ) |         ) | ||||||
|         .await?; |         .await?; | ||||||
| @ -793,13 +805,17 @@ pub async fn add_payment_method_data( | |||||||
|                             saved_to_locker: true, |                             saved_to_locker: true, | ||||||
|                         }; |                         }; | ||||||
|  |  | ||||||
|                         let updated_pmd = Some(PaymentMethodsData::Card(updated_card)); |                         let pm_data_encrypted: Encryptable<Secret<serde_json::Value>> = | ||||||
|                         let pm_data_encrypted = create_encrypted_data(&key_store, updated_pmd) |                             create_encrypted_data( | ||||||
|  |                                 &key_store, | ||||||
|  |                                 PaymentMethodsData::Card(updated_card), | ||||||
|  |                             ) | ||||||
|                             .await |                             .await | ||||||
|                             .map(|details| details.into()); |                             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |                             .attach_printable("Unable to encrypt payment method data")?; | ||||||
|  |  | ||||||
|                         let pm_update = storage::PaymentMethodUpdate::AdditionalDataUpdate { |                         let pm_update = storage::PaymentMethodUpdate::AdditionalDataUpdate { | ||||||
|                             payment_method_data: pm_data_encrypted, |                             payment_method_data: Some(pm_data_encrypted.into()), | ||||||
|                             status: Some(enums::PaymentMethodStatus::Active), |                             status: Some(enums::PaymentMethodStatus::Active), | ||||||
|                             locker_id: Some(locker_id), |                             locker_id: Some(locker_id), | ||||||
|                             payment_method: req.payment_method, |                             payment_method: req.payment_method, | ||||||
| @ -870,9 +886,14 @@ pub async fn add_payment_method( | |||||||
|     let merchant_id = &merchant_account.merchant_id; |     let merchant_id = &merchant_account.merchant_id; | ||||||
|     let customer_id = req.customer_id.clone().get_required_value("customer_id")?; |     let customer_id = req.customer_id.clone().get_required_value("customer_id")?; | ||||||
|     let payment_method = req.payment_method.get_required_value("payment_method")?; |     let payment_method = req.payment_method.get_required_value("payment_method")?; | ||||||
|     let payment_method_billing_address = create_encrypted_data(key_store, req.billing.clone()) |     let payment_method_billing_address: Option<Encryptable<Secret<serde_json::Value>>> = req | ||||||
|  |         .billing | ||||||
|  |         .clone() | ||||||
|  |         .async_map(|billing| create_encrypted_data(key_store, billing)) | ||||||
|         .await |         .await | ||||||
|         .map(|details| details.into()); |         .transpose() | ||||||
|  |         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |         .attach_printable("Unable to encrypt Payment method billing address")?; | ||||||
|  |  | ||||||
|     let connector_mandate_details = req |     let connector_mandate_details = req | ||||||
|         .connector_mandate_details |         .connector_mandate_details | ||||||
| @ -1041,12 +1062,16 @@ pub async fn add_payment_method( | |||||||
|                     let updated_pmd = updated_card.as_ref().map(|card| { |                     let updated_pmd = updated_card.as_ref().map(|card| { | ||||||
|                         PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone())) |                         PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone())) | ||||||
|                     }); |                     }); | ||||||
|                     let pm_data_encrypted = create_encrypted_data(key_store, updated_pmd) |                     let pm_data_encrypted: Option<Encryptable<Secret<serde_json::Value>>> = | ||||||
|                         .await |                         updated_pmd | ||||||
|                         .map(|details| details.into()); |                             .async_map(|updated_pmd| create_encrypted_data(key_store, updated_pmd)) | ||||||
|  |                             .await | ||||||
|  |                             .transpose() | ||||||
|  |                             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |                             .attach_printable("Unable to encrypt payment method data")?; | ||||||
|  |  | ||||||
|                     let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { |                     let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { | ||||||
|                         payment_method_data: pm_data_encrypted, |                         payment_method_data: pm_data_encrypted.map(Into::into), | ||||||
|                     }; |                     }; | ||||||
|  |  | ||||||
|                     db.update_payment_method( |                     db.update_payment_method( | ||||||
| @ -1086,7 +1111,7 @@ pub async fn add_payment_method( | |||||||
|                 connector_mandate_details, |                 connector_mandate_details, | ||||||
|                 req.network_transaction_id.clone(), |                 req.network_transaction_id.clone(), | ||||||
|                 merchant_account.storage_scheme, |                 merchant_account.storage_scheme, | ||||||
|                 payment_method_billing_address, |                 payment_method_billing_address.map(Into::into), | ||||||
|             ) |             ) | ||||||
|             .await?; |             .await?; | ||||||
|  |  | ||||||
| @ -1117,9 +1142,14 @@ pub async fn insert_payment_method( | |||||||
|         .card |         .card | ||||||
|         .clone() |         .clone() | ||||||
|         .map(|card| PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone()))); |         .map(|card| PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone()))); | ||||||
|     let pm_data_encrypted = create_encrypted_data(key_store, pm_card_details) |  | ||||||
|  |     let pm_data_encrypted: Option<Encryptable<Secret<serde_json::Value>>> = pm_card_details | ||||||
|  |         .clone() | ||||||
|  |         .async_map(|pm_card| create_encrypted_data(key_store, pm_card)) | ||||||
|         .await |         .await | ||||||
|         .map(|details| details.into()); |         .transpose() | ||||||
|  |         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |         .attach_printable("Unable to encrypt payment method data")?; | ||||||
|  |  | ||||||
|     create_payment_method( |     create_payment_method( | ||||||
|         db, |         db, | ||||||
| @ -1130,7 +1160,7 @@ pub async fn insert_payment_method( | |||||||
|         merchant_id, |         merchant_id, | ||||||
|         pm_metadata, |         pm_metadata, | ||||||
|         customer_acceptance, |         customer_acceptance, | ||||||
|         pm_data_encrypted, |         pm_data_encrypted.map(Into::into), | ||||||
|         key_store, |         key_store, | ||||||
|         connector_mandate_details, |         connector_mandate_details, | ||||||
|         None, |         None, | ||||||
| @ -1307,12 +1337,16 @@ pub async fn update_customer_payment_method( | |||||||
|             let updated_pmd = updated_card |             let updated_pmd = updated_card | ||||||
|                 .as_ref() |                 .as_ref() | ||||||
|                 .map(|card| PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone()))); |                 .map(|card| PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone()))); | ||||||
|             let pm_data_encrypted = create_encrypted_data(&key_store, updated_pmd) |  | ||||||
|  |             let pm_data_encrypted: Option<Encryptable<Secret<serde_json::Value>>> = updated_pmd | ||||||
|  |                 .async_map(|updated_pmd| create_encrypted_data(&key_store, updated_pmd)) | ||||||
|                 .await |                 .await | ||||||
|                 .map(|details| details.into()); |                 .transpose() | ||||||
|  |                 .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |                 .attach_printable("Unable to encrypt payment method data")?; | ||||||
|  |  | ||||||
|             let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { |             let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { | ||||||
|                 payment_method_data: pm_data_encrypted, |                 payment_method_data: pm_data_encrypted.map(Into::into), | ||||||
|             }; |             }; | ||||||
|  |  | ||||||
|             add_card_resp |             add_card_resp | ||||||
| @ -1436,7 +1470,7 @@ pub async fn add_bank_to_locker( | |||||||
|                 let secret: Secret<String> = Secret::new(v.to_string()); |                 let secret: Secret<String> = Secret::new(v.to_string()); | ||||||
|                 secret |                 secret | ||||||
|             }) |             }) | ||||||
|             .async_lift(|inner| encrypt_optional(inner, key)) |             .async_lift(|inner| domain::types::encrypt_optional(inner, key)) | ||||||
|             .await |             .await | ||||||
|     } |     } | ||||||
|     .await |     .await | ||||||
| @ -4563,31 +4597,25 @@ pub async fn delete_payment_method( | |||||||
|  |  | ||||||
| pub async fn create_encrypted_data<T>( | pub async fn create_encrypted_data<T>( | ||||||
|     key_store: &domain::MerchantKeyStore, |     key_store: &domain::MerchantKeyStore, | ||||||
|     data: Option<T>, |     data: T, | ||||||
| ) -> Option<Encryptable<Secret<serde_json::Value>>> | ) -> Result<Encryptable<Secret<serde_json::Value>>, error_stack::Report<errors::StorageError>> | ||||||
| where | where | ||||||
|     T: Debug + serde::Serialize, |     T: Debug + serde::Serialize, | ||||||
| { | { | ||||||
|     let key = key_store.key.get_inner().peek(); |     let key = key_store.key.get_inner().peek(); | ||||||
|  |  | ||||||
|     data.as_ref() |     let encoded_data = Encode::encode_to_value(&data) | ||||||
|         .map(Encode::encode_to_value) |  | ||||||
|         .transpose() |  | ||||||
|         .change_context(errors::StorageError::SerializationFailed) |         .change_context(errors::StorageError::SerializationFailed) | ||||||
|         .attach_printable("Unable to convert data to a value") |         .attach_printable("Unable to encode data")?; | ||||||
|         .unwrap_or_else(|error| { |  | ||||||
|             logger::error!(?error); |     let secret_data = Secret::<_, masking::WithType>::new(encoded_data); | ||||||
|             None |  | ||||||
|         }) |     let encrypted_data = domain::types::encrypt(secret_data, key) | ||||||
|         .map(Secret::<_, masking::WithType>::new) |  | ||||||
|         .async_lift(|inner| encrypt_optional(inner, key)) |  | ||||||
|         .await |         .await | ||||||
|         .change_context(errors::StorageError::EncryptionError) |         .change_context(errors::StorageError::EncryptionError) | ||||||
|         .attach_printable("Unable to encrypt data") |         .attach_printable("Unable to encrypt data")?; | ||||||
|         .unwrap_or_else(|error| { |  | ||||||
|             logger::error!(?error); |     Ok(encrypted_data) | ||||||
|             None |  | ||||||
|         }) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| pub async fn list_countries_currencies_for_connector_payment_method( | pub async fn list_countries_currencies_for_connector_payment_method( | ||||||
|  | |||||||
| @ -1612,8 +1612,11 @@ pub async fn create_customer_if_not_exist<'a, F: Clone, R>( | |||||||
|  |  | ||||||
|     payment_data.payment_intent.customer_details = raw_customer_details |     payment_data.payment_intent.customer_details = raw_customer_details | ||||||
|         .clone() |         .clone() | ||||||
|         .async_and_then(|_| async { create_encrypted_data(key_store, raw_customer_details).await }) |         .async_map(|customer_details| create_encrypted_data(key_store, customer_details)) | ||||||
|         .await; |         .await | ||||||
|  |         .transpose() | ||||||
|  |         .change_context(errors::StorageError::EncryptionError) | ||||||
|  |         .attach_printable("Unable to encrypt customer details")?; | ||||||
|  |  | ||||||
|     let customer_id = request_customer_details |     let customer_id = request_customer_details | ||||||
|         .customer_id |         .customer_id | ||||||
|  | |||||||
| @ -1222,12 +1222,20 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen | |||||||
|  |  | ||||||
|         let billing_address = payment_data.address.get_payment_billing(); |         let billing_address = payment_data.address.get_payment_billing(); | ||||||
|         let billing_details = billing_address |         let billing_details = billing_address | ||||||
|             .async_and_then(|_| async { create_encrypted_data(key_store, billing_address).await }) |             .async_map(|billing_details| create_encrypted_data(key_store, billing_details)) | ||||||
|             .await; |             .await | ||||||
|  |             .transpose() | ||||||
|  |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt billing details")?; | ||||||
|  |  | ||||||
|         let shipping_address = payment_data.address.get_shipping(); |         let shipping_address = payment_data.address.get_shipping(); | ||||||
|         let shipping_details = shipping_address |         let shipping_details = shipping_address | ||||||
|             .async_and_then(|_| async { create_encrypted_data(key_store, shipping_address).await }) |             .async_map(|shipping_details| create_encrypted_data(key_store, shipping_details)) | ||||||
|             .await; |             .await | ||||||
|  |             .transpose() | ||||||
|  |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt shipping details")?; | ||||||
|  |  | ||||||
|         let m_payment_data_payment_intent = payment_data.payment_intent.clone(); |         let m_payment_data_payment_intent = payment_data.payment_intent.clone(); | ||||||
|         let m_customer_id = customer_id.clone(); |         let m_customer_id = customer_id.clone(); | ||||||
|         let m_shipping_address_id = shipping_address_id.clone(); |         let m_shipping_address_id = shipping_address_id.clone(); | ||||||
|  | |||||||
| @ -642,10 +642,11 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen | |||||||
|         // details are provided in Payment Create Request |         // details are provided in Payment Create Request | ||||||
|         let customer_details = raw_customer_details |         let customer_details = raw_customer_details | ||||||
|             .clone() |             .clone() | ||||||
|             .async_and_then(|_| async { |             .async_map(|customer_details| create_encrypted_data(key_store, customer_details)) | ||||||
|                 create_encrypted_data(key_store, raw_customer_details).await |             .await | ||||||
|             }) |             .transpose() | ||||||
|             .await; |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt customer details")?; | ||||||
|  |  | ||||||
|         payment_data.payment_intent = state |         payment_data.payment_intent = state | ||||||
|             .store |             .store | ||||||
| @ -1047,20 +1048,22 @@ impl PaymentCreate { | |||||||
|         let billing_details = request |         let billing_details = request | ||||||
|             .billing |             .billing | ||||||
|             .clone() |             .clone() | ||||||
|             .async_and_then(|_| async { |             .async_map(|billing_details| create_encrypted_data(key_store, billing_details)) | ||||||
|                 create_encrypted_data(key_store, request.billing.clone()).await |             .await | ||||||
|             }) |             .transpose() | ||||||
|             .await; |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt billing details")?; | ||||||
|  |  | ||||||
|         // Derivation of directly supplied Shipping Address data in our Payment Create Request |         // Derivation of directly supplied Shipping Address data in our Payment Create Request | ||||||
|         // Encrypting our Shipping Address Details to be stored in Payment Intent |         // Encrypting our Shipping Address Details to be stored in Payment Intent | ||||||
|         let shipping_details = request |         let shipping_details = request | ||||||
|             .shipping |             .shipping | ||||||
|             .clone() |             .clone() | ||||||
|             .async_and_then(|_| async { |             .async_map(|shipping_details| create_encrypted_data(key_store, shipping_details)) | ||||||
|                 create_encrypted_data(key_store, request.shipping.clone()).await |             .await | ||||||
|             }) |             .transpose() | ||||||
|             .await; |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt shipping details")?; | ||||||
|  |  | ||||||
|         // Derivation of directly supplied Customer data in our Payment Create Request |         // Derivation of directly supplied Customer data in our Payment Create Request | ||||||
|         let raw_customer_details = if request.get_customer_id().is_none() |         let raw_customer_details = if request.get_customer_id().is_none() | ||||||
| @ -1080,11 +1083,12 @@ impl PaymentCreate { | |||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         // Encrypting our Customer Details to be stored in Payment Intent |         // Encrypting our Customer Details to be stored in Payment Intent | ||||||
|         let customer_details = if raw_customer_details.is_some() { |         let customer_details = raw_customer_details | ||||||
|             create_encrypted_data(key_store, raw_customer_details).await |             .async_map(|customer_details| create_encrypted_data(key_store, customer_details)) | ||||||
|         } else { |             .await | ||||||
|             None |             .transpose() | ||||||
|         }; |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt customer details")?; | ||||||
|  |  | ||||||
|         Ok(storage::PaymentIntent { |         Ok(storage::PaymentIntent { | ||||||
|             payment_id: payment_id.to_string(), |             payment_id: payment_id.to_string(), | ||||||
|  | |||||||
| @ -715,22 +715,21 @@ impl<F: Clone> UpdateTracker<F, PaymentData<F>, api::PaymentsRequest> for Paymen | |||||||
|         let billing_details = payment_data |         let billing_details = payment_data | ||||||
|             .address |             .address | ||||||
|             .get_payment_billing() |             .get_payment_billing() | ||||||
|             .async_and_then(|_| async { |             .async_map(|billing_details| create_encrypted_data(key_store, billing_details)) | ||||||
|                 create_encrypted_data( |             .await | ||||||
|                     key_store, |             .transpose() | ||||||
|                     payment_data.address.get_payment_billing().cloned(), |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|                 ) |             .attach_printable("Unable to encrypt billing details")?; | ||||||
|                 .await |  | ||||||
|             }) |  | ||||||
|             .await; |  | ||||||
|  |  | ||||||
|         let shipping_details = payment_data |         let shipping_details = payment_data | ||||||
|             .address |             .address | ||||||
|             .get_shipping() |             .get_shipping() | ||||||
|             .async_and_then(|_| async { |             .async_map(|shipping_details| create_encrypted_data(key_store, shipping_details)) | ||||||
|                 create_encrypted_data(key_store, payment_data.address.get_shipping().cloned()).await |             .await | ||||||
|             }) |             .transpose() | ||||||
|             .await; |             .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |             .attach_printable("Unable to encrypt shipping details")?; | ||||||
|  |  | ||||||
|         let order_details = payment_data.payment_intent.order_details.clone(); |         let order_details = payment_data.payment_intent.order_details.clone(); | ||||||
|         let metadata = payment_data.payment_intent.metadata.clone(); |         let metadata = payment_data.payment_intent.metadata.clone(); | ||||||
|         let frm_metadata = payment_data.payment_intent.frm_metadata.clone(); |         let frm_metadata = payment_data.payment_intent.frm_metadata.clone(); | ||||||
|  | |||||||
| @ -3,11 +3,12 @@ use std::collections::HashMap; | |||||||
| use api_models::payment_methods::PaymentMethodsData; | use api_models::payment_methods::PaymentMethodsData; | ||||||
| use common_enums::PaymentMethod; | use common_enums::PaymentMethod; | ||||||
| use common_utils::{ | use common_utils::{ | ||||||
|     ext_traits::{Encode, ValueExt}, |     crypto::Encryptable, | ||||||
|  |     ext_traits::{AsyncExt, Encode, ValueExt}, | ||||||
|     id_type, pii, |     id_type, pii, | ||||||
| }; | }; | ||||||
| use error_stack::{report, ResultExt}; | use error_stack::{report, ResultExt}; | ||||||
| use masking::{ExposeInterface, PeekInterface}; | use masking::{ExposeInterface, PeekInterface, Secret}; | ||||||
| use router_env::{instrument, metrics::add_attributes, tracing}; | use router_env::{instrument, metrics::add_attributes, tracing}; | ||||||
|  |  | ||||||
| use super::helpers; | use super::helpers; | ||||||
| @ -15,7 +16,9 @@ use crate::{ | |||||||
|     consts, |     consts, | ||||||
|     core::{ |     core::{ | ||||||
|         errors::{self, ConnectorErrorExt, RouterResult, StorageErrorExt}, |         errors::{self, ConnectorErrorExt, RouterResult, StorageErrorExt}, | ||||||
|         mandate, payment_methods, payments, |         mandate, | ||||||
|  |         payment_methods::{self, cards::create_encrypted_data}, | ||||||
|  |         payments, | ||||||
|     }, |     }, | ||||||
|     logger, |     logger, | ||||||
|     routes::{metrics, SessionState}, |     routes::{metrics, SessionState}, | ||||||
| @ -64,7 +67,7 @@ pub async fn save_payment_method<FData>( | |||||||
|     key_store: &domain::MerchantKeyStore, |     key_store: &domain::MerchantKeyStore, | ||||||
|     amount: Option<i64>, |     amount: Option<i64>, | ||||||
|     currency: Option<storage_enums::Currency>, |     currency: Option<storage_enums::Currency>, | ||||||
|     billing_name: Option<masking::Secret<String>>, |     billing_name: Option<Secret<String>>, | ||||||
|     payment_method_billing_address: Option<&api::Address>, |     payment_method_billing_address: Option<&api::Address>, | ||||||
|     business_profile: &storage::business_profile::BusinessProfile, |     business_profile: &storage::business_profile::BusinessProfile, | ||||||
| ) -> RouterResult<(Option<String>, Option<common_enums::PaymentMethodStatus>)> | ) -> RouterResult<(Option<String>, Option<common_enums::PaymentMethodStatus>)> | ||||||
| @ -209,18 +212,22 @@ where | |||||||
|                     PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone())) |                     PaymentMethodsData::Card(CardDetailsPaymentMethod::from(card.clone())) | ||||||
|                 }); |                 }); | ||||||
|  |  | ||||||
|                 let pm_data_encrypted = |                 let pm_data_encrypted: Option<Encryptable<Secret<serde_json::Value>>> = | ||||||
|                     payment_methods::cards::create_encrypted_data(key_store, pm_card_details) |                     pm_card_details | ||||||
|  |                         .async_map(|pm_card| create_encrypted_data(key_store, pm_card)) | ||||||
|                         .await |                         .await | ||||||
|                         .map(|details| details.into()); |                         .transpose() | ||||||
|  |                         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |                         .attach_printable("Unable to encrypt payment method data")?; | ||||||
|  |  | ||||||
|                 let encrypted_payment_method_billing_address = |                 let encrypted_payment_method_billing_address: Option< | ||||||
|                     payment_methods::cards::create_encrypted_data( |                     Encryptable<Secret<serde_json::Value>>, | ||||||
|                         key_store, |                 > = payment_method_billing_address | ||||||
|                         payment_method_billing_address, |                     .async_map(|address| create_encrypted_data(key_store, address.clone())) | ||||||
|                     ) |  | ||||||
|                     .await |                     .await | ||||||
|                     .map(|details| details.into()); |                     .transpose() | ||||||
|  |                     .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |                     .attach_printable("Unable to encrypt payment method billing address")?; | ||||||
|  |  | ||||||
|                 let mut payment_method_id = resp.payment_method_id.clone(); |                 let mut payment_method_id = resp.payment_method_id.clone(); | ||||||
|                 let mut locker_id = None; |                 let mut locker_id = None; | ||||||
| @ -311,13 +318,14 @@ where | |||||||
|                                             merchant_id, |                                             merchant_id, | ||||||
|                                             pm_metadata, |                                             pm_metadata, | ||||||
|                                             customer_acceptance, |                                             customer_acceptance, | ||||||
|                                             pm_data_encrypted, |                                             pm_data_encrypted.map(Into::into), | ||||||
|                                             key_store, |                                             key_store, | ||||||
|                                             connector_mandate_details, |                                             connector_mandate_details, | ||||||
|                                             None, |                                             None, | ||||||
|                                             network_transaction_id, |                                             network_transaction_id, | ||||||
|                                             merchant_account.storage_scheme, |                                             merchant_account.storage_scheme, | ||||||
|                                             encrypted_payment_method_billing_address, |                                             encrypted_payment_method_billing_address | ||||||
|  |                                                 .map(Into::into), | ||||||
|                                             resp.card.and_then(|card| { |                                             resp.card.and_then(|card| { | ||||||
|                                                 card.card_network |                                                 card.card_network | ||||||
|                                                     .map(|card_network| card_network.to_string()) |                                                     .map(|card_network| card_network.to_string()) | ||||||
| @ -411,7 +419,8 @@ where | |||||||
|                                                 connector_mandate_details, |                                                 connector_mandate_details, | ||||||
|                                                 network_transaction_id, |                                                 network_transaction_id, | ||||||
|                                                 merchant_account.storage_scheme, |                                                 merchant_account.storage_scheme, | ||||||
|                                                 encrypted_payment_method_billing_address, |                                                 encrypted_payment_method_billing_address | ||||||
|  |                                                     .map(Into::into), | ||||||
|                                             ) |                                             ) | ||||||
|                                             .await |                                             .await | ||||||
|                                         } else { |                                         } else { | ||||||
| @ -506,18 +515,19 @@ where | |||||||
|                                         card.clone(), |                                         card.clone(), | ||||||
|                                     )) |                                     )) | ||||||
|                                 }); |                                 }); | ||||||
|                                 let pm_data_encrypted = |                                 let pm_data_encrypted: Option< | ||||||
|                                     payment_methods::cards::create_encrypted_data( |                                     Encryptable<Secret<serde_json::Value>>, | ||||||
|                                         key_store, |                                 > = updated_pmd | ||||||
|                                         updated_pmd, |                                     .async_map(|pmd| create_encrypted_data(key_store, pmd)) | ||||||
|                                     ) |  | ||||||
|                                     .await |                                     .await | ||||||
|                                     .map(|details| details.into()); |                                     .transpose() | ||||||
|  |                                     .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |                                     .attach_printable("Unable to encrypt payment method data")?; | ||||||
|  |  | ||||||
|                                 payment_methods::cards::update_payment_method_and_last_used( |                                 payment_methods::cards::update_payment_method_and_last_used( | ||||||
|                                     db, |                                     db, | ||||||
|                                     existing_pm, |                                     existing_pm, | ||||||
|                                     pm_data_encrypted, |                                     pm_data_encrypted.map(Into::into), | ||||||
|                                     merchant_account.storage_scheme, |                                     merchant_account.storage_scheme, | ||||||
|                                 ) |                                 ) | ||||||
|                                 .await |                                 .await | ||||||
| @ -599,13 +609,13 @@ where | |||||||
|                                 merchant_id, |                                 merchant_id, | ||||||
|                                 pm_metadata, |                                 pm_metadata, | ||||||
|                                 customer_acceptance, |                                 customer_acceptance, | ||||||
|                                 pm_data_encrypted, |                                 pm_data_encrypted.map(Into::into), | ||||||
|                                 key_store, |                                 key_store, | ||||||
|                                 connector_mandate_details, |                                 connector_mandate_details, | ||||||
|                                 None, |                                 None, | ||||||
|                                 network_transaction_id, |                                 network_transaction_id, | ||||||
|                                 merchant_account.storage_scheme, |                                 merchant_account.storage_scheme, | ||||||
|                                 encrypted_payment_method_billing_address, |                                 encrypted_payment_method_billing_address.map(Into::into), | ||||||
|                                 resp.card.and_then(|card| { |                                 resp.card.and_then(|card| { | ||||||
|                                     card.card_network |                                     card.card_network | ||||||
|                                         .map(|card_network| card_network.to_string()) |                                         .map(|card_network| card_network.to_string()) | ||||||
| @ -877,9 +887,9 @@ pub fn update_router_data_with_payment_method_token_result<F: Clone, T>( | |||||||
|         match payment_method_token_result.payment_method_token_result { |         match payment_method_token_result.payment_method_token_result { | ||||||
|             Ok(pm_token_result) => { |             Ok(pm_token_result) => { | ||||||
|                 router_data.payment_method_token = pm_token_result.map(|pm_token| { |                 router_data.payment_method_token = pm_token_result.map(|pm_token| { | ||||||
|                     hyperswitch_domain_models::router_data::PaymentMethodToken::Token( |                     hyperswitch_domain_models::router_data::PaymentMethodToken::Token(Secret::new( | ||||||
|                         masking::Secret::new(pm_token), |                         pm_token, | ||||||
|                     ) |                     )) | ||||||
|                 }); |                 }); | ||||||
|  |  | ||||||
|                 true |                 true | ||||||
|  | |||||||
| @ -452,9 +452,12 @@ pub async fn save_payout_data_to_locker( | |||||||
|                     ) |                     ) | ||||||
|                 }); |                 }); | ||||||
|             ( |             ( | ||||||
|                 cards::create_encrypted_data(key_store, Some(pm_data)) |                 Some( | ||||||
|                     .await |                     cards::create_encrypted_data(key_store, pm_data) | ||||||
|                     .map(|details| details.into()), |                         .await | ||||||
|  |                         .change_context(errors::ApiErrorResponse::InternalServerError) | ||||||
|  |                         .attach_printable("Unable to encrypt customer details")?, | ||||||
|  |                 ), | ||||||
|                 payment_method, |                 payment_method, | ||||||
|             ) |             ) | ||||||
|         } else { |         } else { | ||||||
| @ -494,7 +497,7 @@ pub async fn save_payout_data_to_locker( | |||||||
|             &merchant_account.merchant_id, |             &merchant_account.merchant_id, | ||||||
|             None, |             None, | ||||||
|             None, |             None, | ||||||
|             card_details_encrypted.clone(), |             card_details_encrypted.clone().map(Into::into), | ||||||
|             key_store, |             key_store, | ||||||
|             None, |             None, | ||||||
|             None, |             None, | ||||||
| @ -557,7 +560,7 @@ pub async fn save_payout_data_to_locker( | |||||||
|  |  | ||||||
|         // Update card's metadata in payment_methods table |         // Update card's metadata in payment_methods table | ||||||
|         let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { |         let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { | ||||||
|             payment_method_data: card_details_encrypted, |             payment_method_data: card_details_encrypted.map(Into::into), | ||||||
|         }; |         }; | ||||||
|         db.update_payment_method(existing_pm, pm_update, merchant_account.storage_scheme) |         db.update_payment_method(existing_pm, pm_update, merchant_account.storage_scheme) | ||||||
|             .await |             .await | ||||||
|  | |||||||
| @ -432,13 +432,13 @@ async fn store_bank_details_in_payment_methods( | |||||||
|             ); |             ); | ||||||
|  |  | ||||||
|             let payment_method_data = payment_methods::PaymentMethodsData::BankDetails(pmd); |             let payment_method_data = payment_methods::PaymentMethodsData::BankDetails(pmd); | ||||||
|             let encrypted_data = |             let encrypted_data = cards::create_encrypted_data(&key_store, payment_method_data) | ||||||
|                 cards::create_encrypted_data(&key_store, Some(payment_method_data)) |                 .await | ||||||
|                     .await |                 .change_context(ApiErrorResponse::InternalServerError) | ||||||
|                     .map(|details| details.into()) |                 .attach_printable("Unable to encrypt customer details")?; | ||||||
|                     .ok_or(ApiErrorResponse::InternalServerError)?; |  | ||||||
|             let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { |             let pm_update = storage::PaymentMethodUpdate::PaymentMethodDataUpdate { | ||||||
|                 payment_method_data: Some(encrypted_data), |                 payment_method_data: Some(encrypted_data.into()), | ||||||
|             }; |             }; | ||||||
|  |  | ||||||
|             update_entries.push((pm.clone(), pm_update)); |             update_entries.push((pm.clone(), pm_update)); | ||||||
| @ -447,8 +447,8 @@ async fn store_bank_details_in_payment_methods( | |||||||
|             let encrypted_data = |             let encrypted_data = | ||||||
|                 cards::create_encrypted_data(&key_store, Some(payment_method_data)) |                 cards::create_encrypted_data(&key_store, Some(payment_method_data)) | ||||||
|                     .await |                     .await | ||||||
|                     .map(|details| details.into()) |                     .change_context(ApiErrorResponse::InternalServerError) | ||||||
|                     .ok_or(ApiErrorResponse::InternalServerError)?; |                     .attach_printable("Unable to encrypt customer details")?; | ||||||
|             let pm_id = generate_id(consts::ID_LENGTH, "pm"); |             let pm_id = generate_id(consts::ID_LENGTH, "pm"); | ||||||
|             let now = common_utils::date_time::now(); |             let now = common_utils::date_time::now(); | ||||||
|             let pm_new = storage::PaymentMethodNew { |             let pm_new = storage::PaymentMethodNew { | ||||||
| @ -461,7 +461,7 @@ async fn store_bank_details_in_payment_methods( | |||||||
|                 payment_method_issuer: None, |                 payment_method_issuer: None, | ||||||
|                 scheme: None, |                 scheme: None, | ||||||
|                 metadata: None, |                 metadata: None, | ||||||
|                 payment_method_data: Some(encrypted_data), |                 payment_method_data: Some(encrypted_data.into()), | ||||||
|                 payment_method_issuer_code: None, |                 payment_method_issuer_code: None, | ||||||
|                 accepted_currency: None, |                 accepted_currency: None, | ||||||
|                 token: None, |                 token: None, | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Prajjwal Kumar
					Prajjwal Kumar