Skip to content

Commit

Permalink
feat(router): saving verified domains to business_profile table (#2109)
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwalkumar17 authored Sep 11, 2023
1 parent 5b29c25 commit 73da641
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 6 deletions.
9 changes: 9 additions & 0 deletions crates/api_models/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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>>,
}
1 change: 1 addition & 0 deletions crates/api_models/src/verifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions crates/diesel_models/src/business_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>,
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/types/api/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}
Expand Down Expand Up @@ -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,
})
}
}
68 changes: 62 additions & 6 deletions crates/router/src/utils/verification.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
> {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
status_code: "200".to_string(),
status_message: "Applepay verification Completed".to_string(),
}),
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 {
Expand All @@ -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>>,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE business_profile DROP COLUMN IF EXISTS applepay_verified_domains;

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE business_profile
ADD COLUMN IF NOT EXISTS applepay_verified_domains text[];

0 comments on commit 73da641

Please sign in to comment.