From 0aa6b30d2c9056e9a21a88bdc064daa7e8659bd6 Mon Sep 17 00:00:00 2001 From: SamraatBansal <55536657+SamraatBansal@users.noreply.github.com> Date: Tue, 3 Oct 2023 19:57:42 +0530 Subject: [PATCH] fix(connector): [noon] add connector_auth params and update description (#2429) --- config/config.example.toml | 1 + config/development.toml | 1 + config/docker_compose.toml | 1 + crates/router/src/configs/settings.rs | 11 +++- crates/router/src/connector/noon.rs | 57 +++++++++++-------- .../router/src/connector/noon/transformers.rs | 11 +++- loadtest/config/development.toml | 1 + 7 files changed, 57 insertions(+), 26 deletions(-) diff --git a/config/config.example.toml b/config/config.example.toml index 0841b26a37ca..d4b44a062a08 100644 --- a/config/config.example.toml +++ b/config/config.example.toml @@ -187,6 +187,7 @@ multisafepay.base_url = "https://testapi.multisafepay.com/" nexinets.base_url = "https://apitest.payengine.de/v1" nmi.base_url = "https://secure.nmi.com/" noon.base_url = "https://api-test.noonpayments.com/" +noon.key_mode = "Test" nuvei.base_url = "https://ppp-test.nuvei.com/" opayo.base_url = "https://pi-test.sagepay.com/" opennode.base_url = "https://dev-api.opennode.com" diff --git a/config/development.toml b/config/development.toml index 0f95dcb6577d..26a7251dc9fb 100644 --- a/config/development.toml +++ b/config/development.toml @@ -161,6 +161,7 @@ multisafepay.base_url = "https://testapi.multisafepay.com/" nexinets.base_url = "https://apitest.payengine.de/v1" nmi.base_url = "https://secure.nmi.com/" noon.base_url = "https://api-test.noonpayments.com/" +noon.key_mode = "Test" nuvei.base_url = "https://ppp-test.nuvei.com/" opayo.base_url = "https://pi-test.sagepay.com/" opennode.base_url = "https://dev-api.opennode.com" diff --git a/config/docker_compose.toml b/config/docker_compose.toml index 2ff1c6ab3364..ca6b9eeda5b0 100644 --- a/config/docker_compose.toml +++ b/config/docker_compose.toml @@ -105,6 +105,7 @@ multisafepay.base_url = "https://testapi.multisafepay.com/" nexinets.base_url = "https://apitest.payengine.de/v1" nmi.base_url = "https://secure.nmi.com/" noon.base_url = "https://api-test.noonpayments.com/" +noon.key_mode = "Test" nuvei.base_url = "https://ppp-test.nuvei.com/" opayo.base_url = "https://pi-test.sagepay.com/" opennode.base_url = "https://dev-api.opennode.com" diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs index b5e522cf843a..fd4a398fd568 100644 --- a/crates/router/src/configs/settings.rs +++ b/crates/router/src/configs/settings.rs @@ -523,7 +523,7 @@ pub struct Connectors { pub multisafepay: ConnectorParams, pub nexinets: ConnectorParams, pub nmi: ConnectorParams, - pub noon: ConnectorParams, + pub noon: ConnectorParamsWithModeType, pub nuvei: ConnectorParams, pub opayo: ConnectorParams, pub opennode: ConnectorParams, @@ -552,6 +552,15 @@ pub struct ConnectorParams { pub secondary_base_url: Option, } +#[derive(Debug, Deserialize, Clone, Default, router_derive::ConfigValidate)] +#[serde(default)] +pub struct ConnectorParamsWithModeType { + pub base_url: String, + pub secondary_base_url: Option, + /// Can take values like Test or Live for Noon + pub key_mode: String, +} + #[derive(Debug, Deserialize, Clone, Default, router_derive::ConfigValidate)] #[serde(default)] pub struct ConnectorParamsWithMoreUrls { diff --git a/crates/router/src/connector/noon.rs b/crates/router/src/connector/noon.rs index deda24bfbd66..8b7c984083c7 100644 --- a/crates/router/src/connector/noon.rs +++ b/crates/router/src/connector/noon.rs @@ -65,7 +65,7 @@ where fn build_headers( &self, req: &types::RouterData, - _connectors: &settings::Connectors, + connectors: &settings::Connectors, ) -> CustomResult)>, errors::ConnectorError> { let mut header = vec![( headers::CONTENT_TYPE.to_string(), @@ -73,12 +73,43 @@ where .to_string() .into(), )]; - let mut api_key = self.get_auth_header(&req.connector_auth_type)?; + let mut api_key = get_auth_header(&req.connector_auth_type, connectors, req.test_mode)?; header.append(&mut api_key); Ok(header) } } +fn get_auth_header( + auth_type: &types::ConnectorAuthType, + connectors: &settings::Connectors, + test_mode: Option, +) -> CustomResult)>, errors::ConnectorError> { + let auth = noon::NoonAuthType::try_from(auth_type)?; + + let encoded_api_key = auth + .business_identifier + .zip(auth.application_identifier) + .zip(auth.api_key) + .map(|((business_identifier, application_identifier), api_key)| { + consts::BASE64_ENGINE.encode(format!( + "{}.{}:{}", + business_identifier, application_identifier, api_key + )) + }); + let key_mode = test_mode.map_or(connectors.noon.key_mode.clone(), |is_test_mode| { + if is_test_mode { + "Test".to_string() + } else { + "Live".to_string() + } + }); + + Ok(vec![( + headers::AUTHORIZATION.to_string(), + format!("Key_{} {}", key_mode, encoded_api_key.peek()).into_masked(), + )]) +} + impl ConnectorCommon for Noon { fn id(&self) -> &'static str { "noon" @@ -92,28 +123,6 @@ impl ConnectorCommon for Noon { connectors.noon.base_url.as_ref() } - fn get_auth_header( - &self, - auth_type: &types::ConnectorAuthType, - ) -> CustomResult)>, errors::ConnectorError> { - let auth = noon::NoonAuthType::try_from(auth_type)?; - - let encoded_api_key = auth - .business_identifier - .zip(auth.application_identifier) - .zip(auth.api_key) - .map(|((business_identifier, application_identifier), api_key)| { - consts::BASE64_ENGINE.encode(format!( - "{}.{}:{}", - business_identifier, application_identifier, api_key - )) - }); - Ok(vec![( - headers::AUTHORIZATION.to_string(), - format!("Key_Test {}", encoded_api_key.peek()).into_masked(), - )]) - } - fn build_error_response( &self, res: Response, diff --git a/crates/router/src/connector/noon/transformers.rs b/crates/router/src/connector/noon/transformers.rs index eff626189103..d7dc832b445d 100644 --- a/crates/router/src/connector/noon/transformers.rs +++ b/crates/router/src/connector/noon/transformers.rs @@ -238,7 +238,16 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for NoonPaymentsRequest { item.request.order_category.clone(), ), }; - let name = item.get_description()?; + + // The description should not have leading or trailing whitespaces, also it should not have double whitespaces and a max 50 chars according to Noon's Docs + let name: String = item + .get_description()? + .trim() + .replace(" ", " ") + .chars() + .take(50) + .collect(); + let (subscription, tokenize_c_c) = match item.request.setup_future_usage.is_some().then_some(( NoonSubscriptionData { diff --git a/loadtest/config/development.toml b/loadtest/config/development.toml index ebab00cac144..1b413b1cdf8c 100644 --- a/loadtest/config/development.toml +++ b/loadtest/config/development.toml @@ -91,6 +91,7 @@ multisafepay.base_url = "https://testapi.multisafepay.com/" nexinets.base_url = "https://apitest.payengine.de/v1" nmi.base_url = "https://secure.nmi.com/" noon.base_url = "https://api-test.noonpayments.com/" +noon.key_mode = "Test" nuvei.base_url = "https://ppp-test.nuvei.com/" opayo.base_url = "https://pi-test.sagepay.com/" opennode.base_url = "https://dev-api.opennode.com"