mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-01 02:57:02 +08:00 
			
		
		
		
	feat(router): saving verified domains to business_profile table (#2109)
This commit is contained in:
		| @ -1012,6 +1012,9 @@ pub struct BusinessProfileCreate { | ||||
|         deserialize_with = "payout_routing_algorithm::deserialize_option" | ||||
|     )] | ||||
|     pub payout_routing_algorithm: Option<serde_json::Value>, | ||||
|  | ||||
|     /// Verified applepay domains for a particular profile | ||||
|     pub applepay_verified_domains: Option<Vec<String>>, | ||||
| } | ||||
|  | ||||
| #[derive(Clone, Debug, ToSchema, Serialize)] | ||||
| @ -1073,6 +1076,9 @@ pub struct BusinessProfileResponse { | ||||
|         deserialize_with = "payout_routing_algorithm::deserialize_option" | ||||
|     )] | ||||
|     pub payout_routing_algorithm: Option<serde_json::Value>, | ||||
|  | ||||
|     /// Verified applepay domains for a particular profile | ||||
|     pub applepay_verified_domains: Option<Vec<String>>, | ||||
| } | ||||
|  | ||||
| #[derive(Clone, Debug, Deserialize, ToSchema)] | ||||
| @ -1127,4 +1133,7 @@ pub struct BusinessProfileUpdate { | ||||
|         deserialize_with = "payout_routing_algorithm::deserialize_option" | ||||
|     )] | ||||
|     pub payout_routing_algorithm: Option<serde_json::Value>, | ||||
|  | ||||
|     /// Verified applepay domains for a particular profile | ||||
|     pub applepay_verified_domains: Option<Vec<String>>, | ||||
| } | ||||
|  | ||||
| @ -13,6 +13,7 @@ pub struct ApplepayMerchantVerificationConfigs { | ||||
| #[serde(rename_all = "camelCase")] | ||||
| pub struct ApplepayMerchantVerificationRequest { | ||||
|     pub domain_names: Vec<String>, | ||||
|     pub business_profile_id: String, | ||||
| } | ||||
|  | ||||
| /// Response to be sent for the verify/applepay api | ||||
|  | ||||
| @ -30,6 +30,8 @@ pub struct BusinessProfile { | ||||
|     pub frm_routing_algorithm: Option<serde_json::Value>, | ||||
|     pub payout_routing_algorithm: Option<serde_json::Value>, | ||||
|     pub is_recon_enabled: bool, | ||||
|     #[diesel(deserialize_as = super::OptionalDieselArray<String>)] | ||||
|     pub applepay_verified_domains: Option<Vec<String>>, | ||||
| } | ||||
|  | ||||
| #[derive(Clone, Debug, Insertable, router_derive::DebugAsDisplay)] | ||||
| @ -51,6 +53,8 @@ pub struct BusinessProfileNew { | ||||
|     pub frm_routing_algorithm: Option<serde_json::Value>, | ||||
|     pub payout_routing_algorithm: Option<serde_json::Value>, | ||||
|     pub is_recon_enabled: bool, | ||||
|     #[diesel(deserialize_as = super::OptionalDieselArray<String>)] | ||||
|     pub applepay_verified_domains: Option<Vec<String>>, | ||||
| } | ||||
|  | ||||
| #[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)] | ||||
| @ -69,6 +73,8 @@ pub struct BusinessProfileUpdateInternal { | ||||
|     pub frm_routing_algorithm: Option<serde_json::Value>, | ||||
|     pub payout_routing_algorithm: Option<serde_json::Value>, | ||||
|     pub is_recon_enabled: Option<bool>, | ||||
|     #[diesel(deserialize_as = super::OptionalDieselArray<String>)] | ||||
|     pub applepay_verified_domains: Option<Vec<String>>, | ||||
| } | ||||
|  | ||||
| impl From<BusinessProfileNew> for BusinessProfile { | ||||
| @ -90,6 +96,7 @@ impl From<BusinessProfileNew> for BusinessProfile { | ||||
|             frm_routing_algorithm: new.frm_routing_algorithm, | ||||
|             payout_routing_algorithm: new.payout_routing_algorithm, | ||||
|             is_recon_enabled: new.is_recon_enabled, | ||||
|             applepay_verified_domains: new.applepay_verified_domains, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -78,6 +78,7 @@ diesel::table! { | ||||
|         frm_routing_algorithm -> Nullable<Jsonb>, | ||||
|         payout_routing_algorithm -> Nullable<Jsonb>, | ||||
|         is_recon_enabled -> Bool, | ||||
|         applepay_verified_domains -> Nullable<Array<Nullable<Text>>>, | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| @ -1166,6 +1166,7 @@ pub async fn update_business_profile( | ||||
|         frm_routing_algorithm: request.frm_routing_algorithm, | ||||
|         payout_routing_algorithm: request.payout_routing_algorithm, | ||||
|         is_recon_enabled: None, | ||||
|         applepay_verified_domains: request.applepay_verified_domains, | ||||
|     }; | ||||
|  | ||||
|     let updated_business_profile = db | ||||
|  | ||||
| @ -70,6 +70,7 @@ impl ForeignTryFrom<storage::business_profile::BusinessProfile> for BusinessProf | ||||
|             intent_fulfillment_time: item.intent_fulfillment_time, | ||||
|             frm_routing_algorithm: item.frm_routing_algorithm, | ||||
|             payout_routing_algorithm: item.payout_routing_algorithm, | ||||
|             applepay_verified_domains: item.applepay_verified_domains, | ||||
|         }) | ||||
|     } | ||||
| } | ||||
| @ -136,6 +137,7 @@ impl ForeignTryFrom<(domain::MerchantAccount, BusinessProfileCreate)> | ||||
|                 .payout_routing_algorithm | ||||
|                 .or(merchant_account.payout_routing_algorithm), | ||||
|             is_recon_enabled: merchant_account.is_recon_enabled, | ||||
|             applepay_verified_domains: request.applepay_verified_domains, | ||||
|         }) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,6 +1,8 @@ | ||||
| use actix_web::web; | ||||
| #[cfg(all(feature = "olap", feature = "kms"))] | ||||
| use api_models::verifications::{self, ApplepayMerchantResponse}; | ||||
| use common_utils::errors::CustomResult; | ||||
| use diesel_models::business_profile::{BusinessProfile, BusinessProfileUpdateInternal}; | ||||
| use error_stack::{Report, ResultExt}; | ||||
| #[cfg(feature = "kms")] | ||||
| use external_services::kms; | ||||
| @ -19,7 +21,7 @@ pub async fn verify_merchant_creds_for_applepay( | ||||
|     _req: &actix_web::HttpRequest, | ||||
|     body: web::Json<verifications::ApplepayMerchantVerificationRequest>, | ||||
|     kms_config: &kms::KmsConfig, | ||||
| ) -> common_utils::errors::CustomResult< | ||||
| ) -> CustomResult< | ||||
|     services::ApplicationResponse<ApplepayMerchantResponse>, | ||||
|     api_error_response::ApiErrorResponse, | ||||
| > { | ||||
| @ -30,7 +32,6 @@ pub async fn verify_merchant_creds_for_applepay( | ||||
|     let encrypted_cert = &state.conf.applepay_merchant_configs.merchant_cert; | ||||
|     let encrypted_key = &state.conf.applepay_merchant_configs.merchant_cert_key; | ||||
|     let applepay_endpoint = &state.conf.applepay_merchant_configs.applepay_endpoint; | ||||
|  | ||||
|     let applepay_internal_merchant_identifier = kms::get_kms_client(kms_config) | ||||
|         .await | ||||
|         .decrypt(encrypted_merchant_identifier) | ||||
| @ -84,10 +85,19 @@ pub async fn verify_merchant_creds_for_applepay( | ||||
|  | ||||
|     // Error is already logged | ||||
|     Ok(match applepay_response { | ||||
|         Ok(_) => services::api::ApplicationResponse::Json(ApplepayMerchantResponse { | ||||
|         Ok(_) => { | ||||
|             check_existence_and_add_domain_to_db( | ||||
|                 state, | ||||
|                 body.business_profile_id.clone(), | ||||
|                 body.domain_names.clone(), | ||||
|             ) | ||||
|             .await | ||||
|             .change_context(api_error_response::ApiErrorResponse::InternalServerError)?; | ||||
|             services::api::ApplicationResponse::Json(ApplepayMerchantResponse { | ||||
|                 status_code: "200".to_string(), | ||||
|                 status_message: "Applepay verification Completed".to_string(), | ||||
|         }), | ||||
|             }) | ||||
|         } | ||||
|         Err(error) => { | ||||
|             logger::error!(?error); | ||||
|             services::api::ApplicationResponse::Json(ApplepayMerchantResponse { | ||||
| @ -98,6 +108,52 @@ pub async fn verify_merchant_creds_for_applepay( | ||||
|     }) | ||||
| } | ||||
|  | ||||
| // Checks whether or not the domain verified is already present in db if not adds it | ||||
| async fn check_existence_and_add_domain_to_db( | ||||
|     state: &AppState, | ||||
|     business_profile_id: String, | ||||
|     domain_from_req: Vec<String>, | ||||
| ) -> CustomResult<BusinessProfile, errors::StorageError> { | ||||
|     let business_profile = state | ||||
|         .store | ||||
|         .find_business_profile_by_profile_id(&business_profile_id) | ||||
|         .await?; | ||||
|     let business_profile_to_update = business_profile.clone(); | ||||
|     let mut already_verified_domains = business_profile | ||||
|         .applepay_verified_domains | ||||
|         .unwrap_or_default(); | ||||
|  | ||||
|     let mut new_verified_domains: Vec<String> = domain_from_req | ||||
|         .into_iter() | ||||
|         .filter(|req_domain| !already_verified_domains.contains(req_domain)) | ||||
|         .collect(); | ||||
|  | ||||
|     already_verified_domains.append(&mut new_verified_domains); | ||||
|  | ||||
|     let update_business_profile = BusinessProfileUpdateInternal { | ||||
|         applepay_verified_domains: Some(already_verified_domains), | ||||
|         profile_name: Some(business_profile.profile_name), | ||||
|         modified_at: Some(business_profile.modified_at), | ||||
|         return_url: business_profile.return_url, | ||||
|         enable_payment_response_hash: Some(business_profile.enable_payment_response_hash), | ||||
|         payment_response_hash_key: business_profile.payment_response_hash_key, | ||||
|         redirect_to_merchant_with_http_post: Some( | ||||
|             business_profile.redirect_to_merchant_with_http_post, | ||||
|         ), | ||||
|         webhook_details: business_profile.webhook_details, | ||||
|         metadata: business_profile.metadata, | ||||
|         routing_algorithm: business_profile.routing_algorithm, | ||||
|         intent_fulfillment_time: business_profile.intent_fulfillment_time, | ||||
|         frm_routing_algorithm: business_profile.frm_routing_algorithm, | ||||
|         payout_routing_algorithm: business_profile.payout_routing_algorithm, | ||||
|         is_recon_enabled: Some(business_profile.is_recon_enabled), | ||||
|     }; | ||||
|  | ||||
|     state | ||||
|         .store | ||||
|         .update_business_profile_by_profile_id(business_profile_to_update, update_business_profile) | ||||
|         .await | ||||
| } | ||||
| fn log_applepay_verification_response_if_error( | ||||
|     response: &Result<Result<types::Response, types::Response>, Report<errors::ApiClientError>>, | ||||
| ) { | ||||
|  | ||||
| @ -0,0 +1,2 @@ | ||||
| ALTER TABLE business_profile DROP COLUMN IF EXISTS applepay_verified_domains; | ||||
|  | ||||
| @ -0,0 +1,3 @@ | ||||
| ALTER TABLE business_profile | ||||
| ADD COLUMN IF NOT EXISTS applepay_verified_domains text[]; | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Prajjwal Kumar
					Prajjwal Kumar