Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(router): add accept-language from request headers into browser-info (#7074)

Sai Harsha Vardhan5381eb992228
diff --git a/api-reference-v2/openapi_spec.json b/api-reference-v2/openapi_spec.json
index 892931256b..13014a44e0 100644
--- a/api-reference-v2/openapi_spec.json
+++ b/api-reference-v2/openapi_spec.json
@@ -5696,6 +5696,11 @@
"type": "string",
"description": "The device model of the client",
"nullable": true
+ },
+ "accept_language": {
+ "type": "string",
+ "description": "Accept-language of the browser",
+ "nullable": true
}
}
},
diff --git a/crates/common_utils/src/types.rs b/crates/common_utils/src/types.rs
index 9e71ca7673..4e88d5c0fd 100644
--- a/crates/common_utils/src/types.rs
+++ b/crates/common_utils/src/types.rs
@@ -1408,6 +1408,9 @@ pub struct BrowserInformation {
/// The device model of the client
pub device_model: Option<String>,
+
+ /// Accept-language of the browser
+ pub accept_language: Option<String>,
}
#[cfg(feature = "v2")]
diff --git a/crates/hyperswitch_domain_models/src/router_request_types.rs b/crates/hyperswitch_domain_models/src/router_request_types.rs
index 2f8594662e..4598fa5d1b 100644
--- a/crates/hyperswitch_domain_models/src/router_request_types.rs
+++ b/crates/hyperswitch_domain_models/src/router_request_types.rs
@@ -489,6 +489,7 @@ pub struct BrowserInformation {
pub os_type: Option<String>,
pub os_version: Option<String>,
pub device_model: Option<String>,
+ pub accept_language: Option<String>,
}
#[cfg(feature = "v2")]
@@ -508,6 +509,7 @@ impl From<common_utils::types::BrowserInformation> for BrowserInformation {
os_type: value.os_type,
os_version: value.os_version,
device_model: value.device_model,
+ accept_language: value.accept_language,
}
}
}
diff --git a/crates/router/src/connector/netcetera/netcetera_types.rs b/crates/router/src/connector/netcetera/netcetera_types.rs
index a69798314b..0ecbbf63e3 100644
--- a/crates/router/src/connector/netcetera/netcetera_types.rs
+++ b/crates/router/src/connector/netcetera/netcetera_types.rs
@@ -1372,6 +1372,15 @@ pub struct Browser {
accept_language: Option<Vec<String>>,
}
+// Split by comma and return the list of accept languages
+// If Accept-Language is : fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, List should be [fr-CH, fr, en, de]
+pub fn get_list_of_accept_languages(accept_language: String) -> Vec<String> {
+ accept_language
+ .split(',')
+ .map(|lang| lang.split(';').next().unwrap_or(lang).trim().to_string())
+ .collect()
+}
+
impl From<crate::types::BrowserInformation> for Browser {
fn from(value: crate::types::BrowserInformation) -> Self {
Self {
@@ -1388,8 +1397,11 @@ impl From<crate::types::BrowserInformation> for Browser {
browser_user_agent: value.user_agent,
challenge_window_size: Some(ChallengeWindowSizeEnum::FullScreen),
browser_javascript_enabled: value.java_script_enabled,
- // Hardcoding to "en" for now, as there's no accept_language in BrowserInformation
- accept_language: Some(vec!["en".to_string()]),
+ // Default to ["en"] locale if accept_language is not provided
+ accept_language: value
+ .accept_language
+ .map(get_list_of_accept_languages)
+ .or(Some(vec!["en".to_string()])),
}
}
}
diff --git a/crates/router/src/connector/trustpay/transformers.rs b/crates/router/src/connector/trustpay/transformers.rs
index 83587609b3..31c992a7f5 100644
--- a/crates/router/src/connector/trustpay/transformers.rs
+++ b/crates/router/src/connector/trustpay/transformers.rs
@@ -401,6 +401,7 @@ impl TryFrom<&TrustpayRouterData<&types::PaymentsAuthorizeRouterData>> for Trust
os_type: None,
os_version: None,
device_model: None,
+ accept_language: Some(browser_info.accept_language.unwrap_or("en".to_string())),
};
let params = get_mandatory_fields(item.router_data)?;
let amount = item.amount.to_owned();
diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rs
index cd8aa1c3c8..16468aef74 100644
--- a/crates/router/src/routes/payments.rs
+++ b/crates/router/src/routes/payments.rs
@@ -602,7 +602,14 @@ pub async fn payments_confirm(
return http_not_implemented();
};
- if let Err(err) = helpers::populate_ip_into_browser_info(&req, &mut payload) {
+ let header_payload = match HeaderPayload::foreign_try_from(req.headers()) {
+ Ok(headers) => headers,
+ Err(err) => {
+ return api::log_and_return_error_response(err);
+ }
+ };
+
+ if let Err(err) = helpers::populate_browser_info(&req, &mut payload, &header_payload) {
return api::log_and_return_error_response(err);
}
@@ -610,12 +617,6 @@ pub async fn payments_confirm(
tracing::Span::current().record("payment_id", payment_id.get_string_repr());
payload.payment_id = Some(payment_types::PaymentIdType::PaymentIntentId(payment_id));
payload.confirm = Some(true);
- let header_payload = match HeaderPayload::foreign_try_from(req.headers()) {
- Ok(headers) => headers,
- Err(err) => {
- return api::log_and_return_error_response(err);
- }
- };
let (auth_type, auth_flow) =
match auth::check_client_secret_and_get_auth(req.headers(), &payload) {
diff --git a/crates/router/src/routes/payments/helpers.rs b/crates/router/src/routes/payments/helpers.rs
index 529475a358..39a836a838 100644
--- a/crates/router/src/routes/payments/helpers.rs
+++ b/crates/router/src/routes/payments/helpers.rs
@@ -8,9 +8,10 @@ use crate::{
};
#[cfg(feature = "v1")]
-pub fn populate_ip_into_browser_info(
+pub fn populate_browser_info(
req: &actix_web::HttpRequest,
payload: &mut api::PaymentsRequest,
+ header_payload: &hyperswitch_domain_models::payments::HeaderPayload,
) -> RouterResult<()> {
let mut browser_info: types::BrowserInformation = payload
.browser_info
@@ -34,6 +35,7 @@ pub fn populate_ip_into_browser_info(
os_type: None,
os_version: None,
device_model: None,
+ accept_language: None,
});
let ip_address = req
@@ -59,6 +61,13 @@ pub fn populate_ip_into_browser_info(
})
});
+ // If the locale is present in the header payload, we will use it as the accept language
+ if header_payload.locale.is_some() {
+ browser_info.accept_language = browser_info
+ .accept_language
+ .or(header_payload.locale.clone());
+ }
+
if let Some(api::MandateData {
customer_acceptance:
Some(api::CustomerAcceptance {
diff --git a/crates/router/tests/connectors/trustpay.rs b/crates/router/tests/connectors/trustpay.rs
index 2e5ea6d50f..a5eb08c67e 100644
--- a/crates/router/tests/connectors/trustpay.rs
+++ b/crates/router/tests/connectors/trustpay.rs
@@ -52,6 +52,7 @@ fn get_default_browser_info() -> BrowserInformation {
os_type: None,
os_version: None,
device_model: None,
+ accept_language: Some("en".to_string()),
}
}
diff --git a/crates/router/tests/connectors/utils.rs b/crates/router/tests/connectors/utils.rs
index 2cf3dc965f..ca818e6175 100644
--- a/crates/router/tests/connectors/utils.rs
+++ b/crates/router/tests/connectors/utils.rs
@@ -1022,6 +1022,7 @@ impl Default for BrowserInfoType {
device_model: Some("Apple IPHONE 7".to_string()),
os_type: Some("IOS or ANDROID".to_string()),
os_version: Some("IOS 14.5".to_string()),
+ accept_language: Some("en".to_string()),
};
Self(data)
}