Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(core): add support for payment links localization #5530

Merged
merged 18 commits into from
Aug 7, 2024
Merged
1 change: 1 addition & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ extend-exclude = [
"openapi/open_api_spec.yaml", # no longer updated
"crates/router/src/utils/user/blocker_emails.txt", # this file contains various email domains
"CHANGELOG.md", # This file contains all the commits
"crates/router/src/core/payment_link/locale.js" # this file contains converision of static strings in various languages
]
3 changes: 3 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ pub struct HeaderPayload {
pub browser_name: Option<api_enums::BrowserName>,
pub x_client_platform: Option<api_enums::ClientPlatform>,
pub x_merchant_domain: Option<String>,
pub locale: Option<String>,
}

impl HeaderPayload {
Expand Down Expand Up @@ -5406,6 +5407,7 @@ pub struct PaymentLinkDetails {
pub merchant_description: Option<String>,
pub sdk_layout: String,
pub display_sdk_only: bool,
pub locale: Option<String>,
}

#[derive(Debug, serde::Serialize, Clone)]
Expand All @@ -5430,6 +5432,7 @@ pub struct PaymentLinkStatusDetails {
pub redirect: bool,
pub theme: String,
pub return_url: String,
pub locale: Option<String>,
}

#[derive(Clone, Debug, serde::Deserialize, ToSchema, serde::Serialize)]
Expand Down
37 changes: 29 additions & 8 deletions crates/router/src/core/payment_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ use super::errors::{self, RouterResult, StorageErrorExt};
use crate::{
errors::RouterResponse,
get_payment_link_config_value, get_payment_link_config_value_based_on_priority,
headers::ACCEPT_LANGUAGE,
routes::SessionState,
services,
services::{self, authentication::try_get_header_value_by_key},
types::{
api::payment_link::PaymentLinkResponseExt,
domain,
Expand Down Expand Up @@ -64,6 +65,7 @@ pub async fn form_payment_link_data(
key_store: domain::MerchantKeyStore,
merchant_id: common_utils::id_type::MerchantId,
payment_id: String,
locale: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be an enum for validation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to throw an error incase if client passes invalid enum of accept headers, that's the reason it was Option.

) -> RouterResult<(PaymentLink, PaymentLinkData, PaymentLinkConfig)> {
let db = &*state.store;
let payment_intent = db
Expand Down Expand Up @@ -214,6 +216,7 @@ pub async fn form_payment_link_data(
redirect: false,
theme: payment_link_config.theme.clone(),
return_url: return_url.clone(),
locale: locale.clone(),
};

return Ok((
Expand All @@ -240,6 +243,7 @@ pub async fn form_payment_link_data(
merchant_description: payment_intent.description,
sdk_layout: payment_link_config.sdk_layout.clone(),
display_sdk_only: payment_link_config.display_sdk_only,
locale,
});

Ok((payment_link, payment_link_details, payment_link_config))
Expand All @@ -253,9 +257,16 @@ pub async fn initiate_secure_payment_link_flow(
payment_id: String,
request_headers: &header::HeaderMap,
) -> RouterResponse<services::PaymentLinkFormData> {
let (payment_link, payment_link_details, payment_link_config) =
form_payment_link_data(&state, merchant_account, key_store, merchant_id, payment_id)
.await?;
let locale = try_get_header_value_by_key(ACCEPT_LANGUAGE.into(), request_headers);
let (payment_link, payment_link_details, payment_link_config) = form_payment_link_data(
&state,
merchant_account,
key_store,
merchant_id,
payment_id,
locale,
)
.await?;

validator::validate_secure_payment_link_render_request(
request_headers,
Expand Down Expand Up @@ -341,10 +352,18 @@ pub async fn initiate_payment_link_flow(
key_store: domain::MerchantKeyStore,
merchant_id: common_utils::id_type::MerchantId,
payment_id: String,
request_headers: &header::HeaderMap,
) -> RouterResponse<services::PaymentLinkFormData> {
let (_, payment_details, payment_link_config) =
form_payment_link_data(&state, merchant_account, key_store, merchant_id, payment_id)
.await?;
let locale = try_get_header_value_by_key(ACCEPT_LANGUAGE.into(), request_headers);
let (_, payment_details, payment_link_config) = form_payment_link_data(
&state,
merchant_account,
key_store,
merchant_id,
payment_id,
locale,
)
.await?;

let css_script = get_color_scheme_css(&payment_link_config);
let js_script = get_js_script(&payment_details)?;
Expand Down Expand Up @@ -577,7 +596,6 @@ pub fn get_payment_link_config_based_on_priority(
DEFAULT_ENABLE_SAVED_PAYMENT_METHOD
)
);

let payment_link_config = PaymentLinkConfig {
theme,
logo,
Expand Down Expand Up @@ -617,7 +635,9 @@ pub async fn get_payment_link_status(
key_store: domain::MerchantKeyStore,
merchant_id: common_utils::id_type::MerchantId,
payment_id: String,
request_headers: &header::HeaderMap,
) -> RouterResponse<services::PaymentLinkFormData> {
let locale = try_get_header_value_by_key(ACCEPT_LANGUAGE.into(), request_headers);
let db = &*state.store;
let payment_intent = db
.find_payment_intent_by_payment_id_merchant_id(
Expand Down Expand Up @@ -726,6 +746,7 @@ pub async fn get_payment_link_status(
redirect: true,
theme: payment_link_config.theme.clone(),
return_url,
locale,
};
let js_script = get_js_script(&PaymentLinkData::PaymentLinkStatusDetails(payment_details))?;
let payment_link_status_data = services::PaymentLinkStatusData {
Expand Down
Loading
Loading