Skip to content

Commit 73da641

Browse files
feat(router): saving verified domains to business_profile table (#2109)
1 parent 5b29c25 commit 73da641

File tree

9 files changed

+88
-6
lines changed

9 files changed

+88
-6
lines changed

crates/api_models/src/admin.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,9 @@ pub struct BusinessProfileCreate {
10121012
deserialize_with = "payout_routing_algorithm::deserialize_option"
10131013
)]
10141014
pub payout_routing_algorithm: Option<serde_json::Value>,
1015+
1016+
/// Verified applepay domains for a particular profile
1017+
pub applepay_verified_domains: Option<Vec<String>>,
10151018
}
10161019

10171020
#[derive(Clone, Debug, ToSchema, Serialize)]
@@ -1073,6 +1076,9 @@ pub struct BusinessProfileResponse {
10731076
deserialize_with = "payout_routing_algorithm::deserialize_option"
10741077
)]
10751078
pub payout_routing_algorithm: Option<serde_json::Value>,
1079+
1080+
/// Verified applepay domains for a particular profile
1081+
pub applepay_verified_domains: Option<Vec<String>>,
10761082
}
10771083

10781084
#[derive(Clone, Debug, Deserialize, ToSchema)]
@@ -1127,4 +1133,7 @@ pub struct BusinessProfileUpdate {
11271133
deserialize_with = "payout_routing_algorithm::deserialize_option"
11281134
)]
11291135
pub payout_routing_algorithm: Option<serde_json::Value>,
1136+
1137+
/// Verified applepay domains for a particular profile
1138+
pub applepay_verified_domains: Option<Vec<String>>,
11301139
}

crates/api_models/src/verifications.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct ApplepayMerchantVerificationConfigs {
1313
#[serde(rename_all = "camelCase")]
1414
pub struct ApplepayMerchantVerificationRequest {
1515
pub domain_names: Vec<String>,
16+
pub business_profile_id: String,
1617
}
1718

1819
/// Response to be sent for the verify/applepay api

crates/diesel_models/src/business_profile.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct BusinessProfile {
3030
pub frm_routing_algorithm: Option<serde_json::Value>,
3131
pub payout_routing_algorithm: Option<serde_json::Value>,
3232
pub is_recon_enabled: bool,
33+
#[diesel(deserialize_as = super::OptionalDieselArray<String>)]
34+
pub applepay_verified_domains: Option<Vec<String>>,
3335
}
3436

3537
#[derive(Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
@@ -51,6 +53,8 @@ pub struct BusinessProfileNew {
5153
pub frm_routing_algorithm: Option<serde_json::Value>,
5254
pub payout_routing_algorithm: Option<serde_json::Value>,
5355
pub is_recon_enabled: bool,
56+
#[diesel(deserialize_as = super::OptionalDieselArray<String>)]
57+
pub applepay_verified_domains: Option<Vec<String>>,
5458
}
5559

5660
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
@@ -69,6 +73,8 @@ pub struct BusinessProfileUpdateInternal {
6973
pub frm_routing_algorithm: Option<serde_json::Value>,
7074
pub payout_routing_algorithm: Option<serde_json::Value>,
7175
pub is_recon_enabled: Option<bool>,
76+
#[diesel(deserialize_as = super::OptionalDieselArray<String>)]
77+
pub applepay_verified_domains: Option<Vec<String>>,
7278
}
7379

7480
impl From<BusinessProfileNew> for BusinessProfile {
@@ -90,6 +96,7 @@ impl From<BusinessProfileNew> for BusinessProfile {
9096
frm_routing_algorithm: new.frm_routing_algorithm,
9197
payout_routing_algorithm: new.payout_routing_algorithm,
9298
is_recon_enabled: new.is_recon_enabled,
99+
applepay_verified_domains: new.applepay_verified_domains,
93100
}
94101
}
95102
}

crates/diesel_models/src/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ diesel::table! {
7878
frm_routing_algorithm -> Nullable<Jsonb>,
7979
payout_routing_algorithm -> Nullable<Jsonb>,
8080
is_recon_enabled -> Bool,
81+
applepay_verified_domains -> Nullable<Array<Nullable<Text>>>,
8182
}
8283
}
8384

crates/router/src/core/admin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ pub async fn update_business_profile(
11661166
frm_routing_algorithm: request.frm_routing_algorithm,
11671167
payout_routing_algorithm: request.payout_routing_algorithm,
11681168
is_recon_enabled: None,
1169+
applepay_verified_domains: request.applepay_verified_domains,
11691170
};
11701171

11711172
let updated_business_profile = db

crates/router/src/types/api/admin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl ForeignTryFrom<storage::business_profile::BusinessProfile> for BusinessProf
7070
intent_fulfillment_time: item.intent_fulfillment_time,
7171
frm_routing_algorithm: item.frm_routing_algorithm,
7272
payout_routing_algorithm: item.payout_routing_algorithm,
73+
applepay_verified_domains: item.applepay_verified_domains,
7374
})
7475
}
7576
}
@@ -136,6 +137,7 @@ impl ForeignTryFrom<(domain::MerchantAccount, BusinessProfileCreate)>
136137
.payout_routing_algorithm
137138
.or(merchant_account.payout_routing_algorithm),
138139
is_recon_enabled: merchant_account.is_recon_enabled,
140+
applepay_verified_domains: request.applepay_verified_domains,
139141
})
140142
}
141143
}

crates/router/src/utils/verification.rs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use actix_web::web;
22
#[cfg(all(feature = "olap", feature = "kms"))]
33
use api_models::verifications::{self, ApplepayMerchantResponse};
4+
use common_utils::errors::CustomResult;
5+
use diesel_models::business_profile::{BusinessProfile, BusinessProfileUpdateInternal};
46
use error_stack::{Report, ResultExt};
57
#[cfg(feature = "kms")]
68
use external_services::kms;
@@ -19,7 +21,7 @@ pub async fn verify_merchant_creds_for_applepay(
1921
_req: &actix_web::HttpRequest,
2022
body: web::Json<verifications::ApplepayMerchantVerificationRequest>,
2123
kms_config: &kms::KmsConfig,
22-
) -> common_utils::errors::CustomResult<
24+
) -> CustomResult<
2325
services::ApplicationResponse<ApplepayMerchantResponse>,
2426
api_error_response::ApiErrorResponse,
2527
> {
@@ -30,7 +32,6 @@ pub async fn verify_merchant_creds_for_applepay(
3032
let encrypted_cert = &state.conf.applepay_merchant_configs.merchant_cert;
3133
let encrypted_key = &state.conf.applepay_merchant_configs.merchant_cert_key;
3234
let applepay_endpoint = &state.conf.applepay_merchant_configs.applepay_endpoint;
33-
3435
let applepay_internal_merchant_identifier = kms::get_kms_client(kms_config)
3536
.await
3637
.decrypt(encrypted_merchant_identifier)
@@ -84,10 +85,19 @@ pub async fn verify_merchant_creds_for_applepay(
8485

8586
// Error is already logged
8687
Ok(match applepay_response {
87-
Ok(_) => services::api::ApplicationResponse::Json(ApplepayMerchantResponse {
88-
status_code: "200".to_string(),
89-
status_message: "Applepay verification Completed".to_string(),
90-
}),
88+
Ok(_) => {
89+
check_existence_and_add_domain_to_db(
90+
state,
91+
body.business_profile_id.clone(),
92+
body.domain_names.clone(),
93+
)
94+
.await
95+
.change_context(api_error_response::ApiErrorResponse::InternalServerError)?;
96+
services::api::ApplicationResponse::Json(ApplepayMerchantResponse {
97+
status_code: "200".to_string(),
98+
status_message: "Applepay verification Completed".to_string(),
99+
})
100+
}
91101
Err(error) => {
92102
logger::error!(?error);
93103
services::api::ApplicationResponse::Json(ApplepayMerchantResponse {
@@ -98,6 +108,52 @@ pub async fn verify_merchant_creds_for_applepay(
98108
})
99109
}
100110

111+
// Checks whether or not the domain verified is already present in db if not adds it
112+
async fn check_existence_and_add_domain_to_db(
113+
state: &AppState,
114+
business_profile_id: String,
115+
domain_from_req: Vec<String>,
116+
) -> CustomResult<BusinessProfile, errors::StorageError> {
117+
let business_profile = state
118+
.store
119+
.find_business_profile_by_profile_id(&business_profile_id)
120+
.await?;
121+
let business_profile_to_update = business_profile.clone();
122+
let mut already_verified_domains = business_profile
123+
.applepay_verified_domains
124+
.unwrap_or_default();
125+
126+
let mut new_verified_domains: Vec<String> = domain_from_req
127+
.into_iter()
128+
.filter(|req_domain| !already_verified_domains.contains(req_domain))
129+
.collect();
130+
131+
already_verified_domains.append(&mut new_verified_domains);
132+
133+
let update_business_profile = BusinessProfileUpdateInternal {
134+
applepay_verified_domains: Some(already_verified_domains),
135+
profile_name: Some(business_profile.profile_name),
136+
modified_at: Some(business_profile.modified_at),
137+
return_url: business_profile.return_url,
138+
enable_payment_response_hash: Some(business_profile.enable_payment_response_hash),
139+
payment_response_hash_key: business_profile.payment_response_hash_key,
140+
redirect_to_merchant_with_http_post: Some(
141+
business_profile.redirect_to_merchant_with_http_post,
142+
),
143+
webhook_details: business_profile.webhook_details,
144+
metadata: business_profile.metadata,
145+
routing_algorithm: business_profile.routing_algorithm,
146+
intent_fulfillment_time: business_profile.intent_fulfillment_time,
147+
frm_routing_algorithm: business_profile.frm_routing_algorithm,
148+
payout_routing_algorithm: business_profile.payout_routing_algorithm,
149+
is_recon_enabled: Some(business_profile.is_recon_enabled),
150+
};
151+
152+
state
153+
.store
154+
.update_business_profile_by_profile_id(business_profile_to_update, update_business_profile)
155+
.await
156+
}
101157
fn log_applepay_verification_response_if_error(
102158
response: &Result<Result<types::Response, types::Response>, Report<errors::ApiClientError>>,
103159
) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE business_profile DROP COLUMN IF EXISTS applepay_verified_domains;
2+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE business_profile
2+
ADD COLUMN IF NOT EXISTS applepay_verified_domains text[];
3+

0 commit comments

Comments
 (0)