Skip to content

Commit

Permalink
fix(connector): [noon] add connector_auth params and update descripti…
Browse files Browse the repository at this point in the history
…on (#2429)
  • Loading branch information
SamraatBansal authored Oct 3, 2023
1 parent e5ad9c5 commit 0aa6b30
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 26 deletions.
1 change: 1 addition & 0 deletions config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions config/docker_compose.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion crates/router/src/configs/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -552,6 +552,15 @@ pub struct ConnectorParams {
pub secondary_base_url: Option<String>,
}

#[derive(Debug, Deserialize, Clone, Default, router_derive::ConfigValidate)]
#[serde(default)]
pub struct ConnectorParamsWithModeType {
pub base_url: String,
pub secondary_base_url: Option<String>,
/// 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 {
Expand Down
57 changes: 33 additions & 24 deletions crates/router/src/connector/noon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,51 @@ where
fn build_headers(
&self,
req: &types::RouterData<Flow, Request, Response>,
_connectors: &settings::Connectors,
connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
let mut header = vec![(
headers::CONTENT_TYPE.to_string(),
types::PaymentsAuthorizeType::get_content_type(self)
.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<bool>,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, 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"
Expand All @@ -92,28 +123,6 @@ impl ConnectorCommon for Noon {
connectors.noon.base_url.as_ref()
}

fn get_auth_header(
&self,
auth_type: &types::ConnectorAuthType,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, 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,
Expand Down
11 changes: 10 additions & 1 deletion crates/router/src/connector/noon/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions loadtest/config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0aa6b30

Please sign in to comment.