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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(payment_link): Add payment info metadata to payment link (#4270)
Sahkal Poddar97fbc899c12a
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex b719d1f7d1..2748ccbff4 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -4376,12 +4376,12 @@ pub struct PaymentLinkInitiateRequest {#[derive(Debug, serde::Serialize)]#[serde(untagged)]-pub enum PaymentLinkData {- PaymentLinkDetails(PaymentLinkDetails),+pub enum PaymentLinkData<'a> {+ PaymentLinkDetails(&'a PaymentLinkDetails),PaymentLinkStatusDetails(PaymentLinkStatusDetails),}-#[derive(Debug, serde::Serialize)]+#[derive(Debug, serde::Serialize, Clone)]pub struct PaymentLinkDetails {pub amount: String,pub currency: api_enums::Currency,diff --git a/crates/router/src/core/payment_link.rs b/crates/router/src/core/payment_link.rsindex 722aad26c0..94d8227989 100644--- a/crates/router/src/core/payment_link.rs+++ b/crates/router/src/core/payment_link.rs@@ -187,7 +187,7 @@ pub async fn intiate_payment_link_flow(return_url: return_url.clone(),};let js_script = get_js_script(- api_models::payments::PaymentLinkData::PaymentLinkStatusDetails(payment_details),+ &api_models::payments::PaymentLinkData::PaymentLinkStatusDetails(payment_details),)?;let payment_link_error_data = services::PaymentLinkStatusData {js_script,@@ -215,13 +215,17 @@ pub async fn intiate_payment_link_flow(sdk_layout: payment_link_config.sdk_layout.clone(),};- let js_script = get_js_script(api_models::payments::PaymentLinkData::PaymentLinkDetails(- payment_details,+ let js_script = get_js_script(&api_models::payments::PaymentLinkData::PaymentLinkDetails(+ &payment_details,))?;++ let html_meta_tags = get_meta_tags_html(payment_details);+let payment_link_data = services::PaymentLinkFormData {js_script,sdk_url: state.conf.payment_link.sdk_url.clone(),css_script,+ html_meta_tags,};Ok(services::ApplicationResponse::PaymentLinkForm(Box::new(services::api::PaymentLinkAction::PaymentLinkFormData(payment_link_data),@@ -232,8 +236,10 @@ pub async fn intiate_payment_link_flow(The get_js_script function is used to inject dynamic value to payment_link sdk, which is unique to every payment.*/-fn get_js_script(payment_details: api_models::payments::PaymentLinkData) -> RouterResult<String> {- let payment_details_str = serde_json::to_string(&payment_details)+fn get_js_script(+ payment_details: &api_models::payments::PaymentLinkData<'_>,+) -> RouterResult<String> {+ let payment_details_str = serde_json::to_string(payment_details).change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to serialize PaymentLinkData")?;Ok(format!("window.__PAYMENT_DETAILS = {payment_details_str};"))@@ -248,6 +254,15 @@ fn get_color_scheme_css(payment_link_config: api_models::admin::PaymentLinkConfi)}+fn get_meta_tags_html(payment_details: api_models::payments::PaymentLinkDetails) -> String {+ format!(+ r#"<meta property="og:title" content="Payment request from {0}"/>+ <meta property="og:description" content="{1}"/>"#,+ payment_details.merchant_name,+ payment_details.merchant_description.unwrap_or_default()+ )+}+fn validate_sdk_requirements(pub_key: Option<String>,currency: Option<api_models::enums::Currency>,@@ -562,7 +577,7 @@ pub async fn get_payment_link_status(return_url,};let js_script = get_js_script(- api_models::payments::PaymentLinkData::PaymentLinkStatusDetails(payment_details),+ &api_models::payments::PaymentLinkData::PaymentLinkStatusDetails(payment_details),)?;let payment_link_status_data = services::PaymentLinkStatusData {js_script,diff --git a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.html b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.htmlindex 732e331892..6b487395c6 100644--- a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.html+++ b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.html@@ -3,6 +3,7 @@<head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" />+ {{rendered_meta_tag_html}}<title>Payments requested by HyperSwitch</title>{{ preload_link_tags }}<link rel="preconnect" href="https://fonts.gstatic.com">diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rsindex 95eb4a44c8..5a139d8da3 100644--- a/crates/router/src/services/api.rs+++ b/crates/router/src/services/api.rs@@ -853,6 +853,7 @@ pub struct PaymentLinkFormData {pub js_script: String,pub css_script: String,pub sdk_url: String,+ pub html_meta_tags: String,}#[derive(Debug, Eq, PartialEq, Clone, serde::Serialize, serde::Deserialize)]@@ -1977,6 +1978,13 @@ pub fn build_payment_link_html(let _ = tera.add_raw_template("payment_link", &html_template);+ context.insert("rendered_meta_tag_html", &payment_link_data.html_meta_tags);++ context.insert(+ "preload_link_tags",+ &get_preload_link_html_template(&payment_link_data.sdk_url),+ );+context.insert("preload_link_tags",&get_preload_link_html_template(&payment_link_data.sdk_url),