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(payment-link): add config for enabling form button only when form is complete (#7517)

Kashif0be1f878ed58
diff --git a/api-reference-v2/openapi_spec.json b/api-reference-v2/openapi_spec.json
index 263d27aa46..dbe13e3776 100644
--- a/api-reference-v2/openapi_spec.json
+++ b/api-reference-v2/openapi_spec.json
@@ -14068,7 +14068,8 @@
"display_sdk_only",
"enabled_saved_payment_method",
"hide_card_nickname_field",
- "show_card_form_by_default"
+ "show_card_form_by_default",
+ "enable_button_only_on_form_ready"
],
"properties": {
"theme": {
@@ -14192,6 +14193,10 @@
}
},
"nullable": true
+ },
+ "enable_button_only_on_form_ready": {
+ "type": "boolean",
+ "description": "Flag to enable the button only when the payment form is ready for submission"
}
}
},
@@ -14329,6 +14334,11 @@
}
},
"nullable": true
+ },
+ "enable_button_only_on_form_ready": {
+ "type": "boolean",
+ "description": "Flag to enable the button only when the payment form is ready for submission",
+ "nullable": true
}
}
},
diff --git a/api-reference/openapi_spec.json b/api-reference/openapi_spec.json
index 4d4dc65d9b..917efcea98 100644
--- a/api-reference/openapi_spec.json
+++ b/api-reference/openapi_spec.json
@@ -16604,7 +16604,8 @@
"display_sdk_only",
"enabled_saved_payment_method",
"hide_card_nickname_field",
- "show_card_form_by_default"
+ "show_card_form_by_default",
+ "enable_button_only_on_form_ready"
],
"properties": {
"theme": {
@@ -16728,6 +16729,10 @@
}
},
"nullable": true
+ },
+ "enable_button_only_on_form_ready": {
+ "type": "boolean",
+ "description": "Flag to enable the button only when the payment form is ready for submission"
}
}
},
@@ -16865,6 +16870,11 @@
}
},
"nullable": true
+ },
+ "enable_button_only_on_form_ready": {
+ "type": "boolean",
+ "description": "Flag to enable the button only when the payment form is ready for submission",
+ "nullable": true
}
}
},
diff --git a/crates/api_models/src/admin.rs b/crates/api_models/src/admin.rs
index 61bb7c009e..b6f1e0a8b9 100644
--- a/crates/api_models/src/admin.rs
+++ b/crates/api_models/src/admin.rs
@@ -2839,6 +2839,8 @@ pub struct PaymentLinkConfigRequest {
pub sdk_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
/// Payment link configuration rules
pub payment_link_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
+ /// Flag to enable the button only when the payment form is ready for submission
+ pub enable_button_only_on_form_ready: Option<bool>,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq, ToSchema)]
@@ -2924,6 +2926,8 @@ pub struct PaymentLinkConfig {
pub sdk_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
/// Payment link configuration rules
pub payment_link_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
+ /// Flag to enable the button only when the payment form is ready for submission
+ pub enable_button_only_on_form_ready: bool,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index 2e21de6108..27d7b82e54 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -7944,6 +7944,7 @@ pub struct PaymentLinkDetails {
pub background_colour: Option<String>,
pub sdk_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
pub payment_link_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
+ pub enable_button_only_on_form_ready: bool,
}
#[derive(Debug, serde::Serialize, Clone)]
@@ -7961,6 +7962,7 @@ pub struct SecurePaymentLinkDetails {
pub background_colour: Option<String>,
pub sdk_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
pub payment_link_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
+ pub enable_button_only_on_form_ready: bool,
}
#[derive(Debug, serde::Serialize)]
diff --git a/crates/diesel_models/src/business_profile.rs b/crates/diesel_models/src/business_profile.rs
index 9d47ed7b59..384f7c7df2 100644
--- a/crates/diesel_models/src/business_profile.rs
+++ b/crates/diesel_models/src/business_profile.rs
@@ -653,6 +653,7 @@ pub struct PaymentLinkConfigRequest {
pub background_colour: Option<String>,
pub sdk_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
pub payment_link_ui_rules: Option<HashMap<String, HashMap<String, String>>>,
+ pub enable_button_only_on_form_ready: Option<bool>,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq)]
diff --git a/crates/diesel_models/src/payment_intent.rs b/crates/diesel_models/src/payment_intent.rs
index e51d8daa79..4f2f4c49ea 100644
--- a/crates/diesel_models/src/payment_intent.rs
+++ b/crates/diesel_models/src/payment_intent.rs
@@ -193,6 +193,8 @@ pub struct PaymentLinkConfigRequestForPayments {
/// Payment link configuration rules
pub payment_link_ui_rules:
Option<std::collections::HashMap<String, std::collections::HashMap<String, String>>>,
+ /// Flag to enable the button only when the payment form is ready for submission
+ pub enable_button_only_on_form_ready: Option<bool>,
}
common_utils::impl_to_sql_from_sql_json!(PaymentLinkConfigRequestForPayments);
diff --git a/crates/hyperswitch_domain_models/src/lib.rs b/crates/hyperswitch_domain_models/src/lib.rs
index 1b83e845ed..12a128269f 100644
--- a/crates/hyperswitch_domain_models/src/lib.rs
+++ b/crates/hyperswitch_domain_models/src/lib.rs
@@ -419,6 +419,7 @@ impl ApiModelToDieselModelConvertor<api_models::admin::PaymentLinkConfigRequest>
payment_button_text_colour: item.payment_button_text_colour,
sdk_ui_rules: item.sdk_ui_rules,
payment_link_ui_rules: item.payment_link_ui_rules,
+ enable_button_only_on_form_ready: item.enable_button_only_on_form_ready,
}
}
fn convert_back(self) -> api_models::admin::PaymentLinkConfigRequest {
@@ -442,6 +443,7 @@ impl ApiModelToDieselModelConvertor<api_models::admin::PaymentLinkConfigRequest>
payment_button_text_colour,
sdk_ui_rules,
payment_link_ui_rules,
+ enable_button_only_on_form_ready,
} = self;
api_models::admin::PaymentLinkConfigRequest {
theme,
@@ -469,6 +471,7 @@ impl ApiModelToDieselModelConvertor<api_models::admin::PaymentLinkConfigRequest>
payment_button_text_colour,
sdk_ui_rules,
payment_link_ui_rules,
+ enable_button_only_on_form_ready,
}
}
}
diff --git a/crates/router/src/consts.rs b/crates/router/src/consts.rs
index b343ebccee..0fce7048e6 100644
--- a/crates/router/src/consts.rs
+++ b/crates/router/src/consts.rs
@@ -152,6 +152,9 @@ pub const DEFAULT_DISPLAY_SDK_ONLY: bool = false;
/// Default bool to enable saved payment method
pub const DEFAULT_ENABLE_SAVED_PAYMENT_METHOD: bool = false;
+/// [PaymentLink] Default bool for enabling button only when form is ready
+pub const DEFAULT_ENABLE_BUTTON_ONLY_ON_FORM_READY: bool = false;
+
/// Default Merchant Logo Link
pub const DEFAULT_MERCHANT_LOGO: &str =
"https://live.hyperswitch.io/payment-link-assets/Merchant_placeholder.png";
diff --git a/crates/router/src/core/payment_link.rs b/crates/router/src/core/payment_link.rs
index 292f582df9..c13e087947 100644
--- a/crates/router/src/core/payment_link.rs
+++ b/crates/router/src/core/payment_link.rs
@@ -23,8 +23,9 @@ use super::{
use crate::{
consts::{
self, DEFAULT_ALLOWED_DOMAINS, DEFAULT_BACKGROUND_COLOR, DEFAULT_DISPLAY_SDK_ONLY,
- DEFAULT_ENABLE_SAVED_PAYMENT_METHOD, DEFAULT_HIDE_CARD_NICKNAME_FIELD,
- DEFAULT_MERCHANT_LOGO, DEFAULT_PRODUCT_IMG, DEFAULT_SDK_LAYOUT, DEFAULT_SHOW_CARD_FORM,
+ DEFAULT_ENABLE_BUTTON_ONLY_ON_FORM_READY, DEFAULT_ENABLE_SAVED_PAYMENT_METHOD,
+ DEFAULT_HIDE_CARD_NICKNAME_FIELD, DEFAULT_MERCHANT_LOGO, DEFAULT_PRODUCT_IMG,
+ DEFAULT_SDK_LAYOUT, DEFAULT_SHOW_CARD_FORM,
},
errors::RouterResponse,
get_payment_link_config_value, get_payment_link_config_value_based_on_priority,
@@ -137,6 +138,7 @@ pub async fn form_payment_link_data(
payment_button_text_colour: None,
sdk_ui_rules: None,
payment_link_ui_rules: None,
+ enable_button_only_on_form_ready: DEFAULT_ENABLE_BUTTON_ONLY_ON_FORM_READY,
}
};
@@ -290,6 +292,7 @@ pub async fn form_payment_link_data(
payment_button_text_colour: payment_link_config.payment_button_text_colour.clone(),
sdk_ui_rules: payment_link_config.sdk_ui_rules.clone(),
payment_link_ui_rules: payment_link_config.payment_link_ui_rules.clone(),
+ enable_button_only_on_form_ready: payment_link_config.enable_button_only_on_form_ready,
};
Ok((
@@ -348,6 +351,8 @@ pub async fn initiate_secure_payment_link_flow(
payment_button_text_colour: payment_link_config.payment_button_text_colour,
sdk_ui_rules: payment_link_config.sdk_ui_rules,
payment_link_ui_rules: payment_link_config.payment_link_ui_rules,
+ enable_button_only_on_form_ready: payment_link_config
+ .enable_button_only_on_form_ready,
};
let js_script = format!(
"window.__PAYMENT_DETAILS = {}",
@@ -634,6 +639,7 @@ pub fn get_payment_link_config_based_on_priority(
enabled_saved_payment_method,
hide_card_nickname_field,
show_card_form_by_default,
+ enable_button_only_on_form_ready,
) = get_payment_link_config_value!(
payment_create_link_config,
business_theme_configs,
@@ -647,7 +653,11 @@ pub fn get_payment_link_config_based_on_priority(
DEFAULT_ENABLE_SAVED_PAYMENT_METHOD
),
(hide_card_nickname_field, DEFAULT_HIDE_CARD_NICKNAME_FIELD),
- (show_card_form_by_default, DEFAULT_SHOW_CARD_FORM)
+ (show_card_form_by_default, DEFAULT_SHOW_CARD_FORM),
+ (
+ enable_button_only_on_form_ready,
+ DEFAULT_ENABLE_BUTTON_ONLY_ON_FORM_READY
+ )
);
let (
@@ -702,6 +712,7 @@ pub fn get_payment_link_config_based_on_priority(
payment_button_text_colour,
sdk_ui_rules,
payment_link_ui_rules,
+ enable_button_only_on_form_ready,
};
Ok((payment_link_config, domain_name))
@@ -812,6 +823,7 @@ pub async fn get_payment_link_status(
payment_button_text_colour: None,
sdk_ui_rules: None,
payment_link_ui_rules: None,
+ enable_button_only_on_form_ready: DEFAULT_ENABLE_BUTTON_ONLY_ON_FORM_READY,
}
};
diff --git a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.css b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.css
index 7b34730680..f58617261d 100644
--- a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.css
+++ b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.css
@@ -652,6 +652,10 @@ body {
cursor: not-allowed;
}
+#submit.not-ready {
+ background-color: #C2C2C2 !important;
+}
+
#submit-spinner {
width: 28px;
height: 28px;
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.html
index 9abcc44006..ac04479e44 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
@@ -290,9 +290,9 @@
<div></div>
</div>
</div>
- <form id="payment-form" onclick="handleSubmit(); return false;">
+ <form id="payment-form">
<div id="unified-checkout"></div>
- <button id="submit" class="hidden">
+ <button type="submit" id="submit" class="hidden">
<span id="submit-spinner" class="hidden"></span>
<span id="submit-button-text"></span>
</button>
diff --git a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.js b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.js
index 5b8267c57a..e73e23076a 100644
--- a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.js
+++ b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.js
@@ -257,7 +257,7 @@ function boot() {
// Update payment link styles
var paymentLinkUiRules = paymentDetails.payment_link_ui_rules;
- if (paymentLinkUiRules !== null && typeof paymentLinkUiRules === "object" && Object.getPrototypeOf(paymentLinkUiRules) === Object.prototype) {
+ if (isObject(paymentLinkUiRules)) {
updatePaymentLinkUi(paymentLinkUiRules);
}
@@ -279,7 +279,18 @@ function boot() {
boot();
/**
- * Use - add event listeners for changing UI on screen resize
+ * Use - checks if a given value is an object
+ * @param {any} val
+ * @returns {boolean}
+ */
+function isObject(val) {
+ return val !== null && typeof val === "object" && Object.getPrototypeOf(val) === Object.prototype
+}
+
+/**
+ * Use - add event listeners for changing UI on
+ * - Screen resize
+ * - Form inputs
* @param {PaymentDetails} paymentDetails
*/
function initializeEventListeners(paymentDetails) {
@@ -384,17 +395,58 @@ function initializeEventListeners(paymentDetails) {
// @ts-ignore
window.state.isMobileView = currentWidth <= 1199;
});
+
+ var paymentForm = document.getElementById("payment-form");
+ if (paymentForm instanceof HTMLFormElement) {
+ paymentForm.addEventListener("submit", function (event) {
+ event.preventDefault();
+ handleSubmit(event);
+ })
+ }
+
+ if (paymentDetails.enable_button_only_on_form_ready) {
+ handleFormReadyForSubmission();
+ }
+}
+
+function handleFormReadyForSubmission() {
+ window.addEventListener("message", function (event) {
+ // Event listener for updating the button rules
+ if (isObject(event.data) && event.data["isFormReadyForSubmission"] !== null) {
+ let isFormReadyForSubmission = event.data["isFormReadyForSubmission"];
+ var submitButtonNode = document.getElementById("submit");
+ if (submitButtonNode instanceof HTMLButtonElement) {
+ if (isFormReadyForSubmission === false) {
+ submitButtonNode.disabled = true;
+ addClass("#submit", "not-ready");
+ addClass("#submit", "disabled");
+ } else if (isFormReadyForSubmission === true) {
+ submitButtonNode.disabled = false;
+ removeClass("#submit", "not-ready");
+ removeClass("#submit", "disabled");
+ }
+ }
+ }
+ });
}
/**
* Trigger - post mounting SDK
* Use - set relevant classes to elements in the doc for showing SDK
**/
-function showSDK(display_sdk_only) {
+function showSDK(display_sdk_only, enable_button_only_on_form_ready) {
if (!display_sdk_only) {
show("#hyper-checkout-details");
}
show("#hyper-checkout-sdk");
+ if (enable_button_only_on_form_ready) {
+ addClass("#submit", "not-ready");
+ addClass("#submit", "disabled");
+ var submitButtonNode = document.getElementById("submit");
+ if (submitButtonNode instanceof HTMLButtonElement) {
+ submitButtonNode.disabled = true;
+ }
+ }
show("#submit");
show("#unified-checkout");
hide("#sdk-spinner");
@@ -426,10 +478,11 @@ function handleSubmit(e) {
// Update button loader
hide("#submit-button-text");
show("#submit-spinner");
+ addClass("#submit", "processing");
+ addClass("#submit", "disabled");
var submitButtonNode = document.getElementById("submit");
if (submitButtonNode instanceof HTMLButtonElement) {
submitButtonNode.disabled = true;
- submitButtonNode.classList.add("disabled");
}
hyper
@@ -472,11 +525,12 @@ function handleSubmit(e) {
console.error("Error confirming payment_intent", error);
})
.finally(() => {
+ removeClass("#submit", "processing");
hide("#submit-spinner");
show("#submit-button-text");
+ removeClass("#submit", "disabled");
if (submitButtonNode instanceof HTMLButtonElement) {
submitButtonNode.disabled = false;
- submitButtonNode.classList.remove("disabled");
}
});
}
diff --git a/crates/router/src/core/payment_link/payment_link_initiate/payment_link_initiator.js b/crates/router/src/core/payment_link/payment_link_initiate/payment_link_initiator.js
index ca6a944baa..f9b0fe892c 100644
--- a/crates/router/src/core/payment_link/payment_link_initiate/payment_link_initiator.js
+++ b/crates/router/src/core/payment_link/payment_link_initiate/payment_link_initiator.js
@@ -75,7 +75,7 @@ function initializeSDK() {
// @ts-ignore
mountUnifiedCheckout("#unified-checkout");
// @ts-ignore
- showSDK(paymentDetails.display_sdk_only);
+ showSDK(paymentDetails.display_sdk_only, paymentDetails.enable_button_only_on_form_ready);
let shimmer = document.getElementById("payment-details-shimmer");
shimmer.classList.add("reduce-opacity");
diff --git a/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js b/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js
index 9b5a144d29..12ae76a208 100644
--- a/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js
+++ b/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js
@@ -98,7 +98,7 @@ if (!isFramed) {
// @ts-ignore
mountUnifiedCheckout("#unified-checkout");
// @ts-ignore
- showSDK(paymentDetails.display_sdk_only);
+ showSDK(paymentDetails.display_sdk_only, paymentDetails.enable_button_only_on_form_ready);
let shimmer = document.getElementById("payment-details-shimmer");
shimmer.classList.add("reduce-opacity");
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 91afa38b15..dde25318b1 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -4532,6 +4532,7 @@ impl ForeignFrom<api_models::admin::PaymentLinkConfigRequest>
payment_button_text_colour: config.payment_button_text_colour,
sdk_ui_rules: config.sdk_ui_rules,
payment_link_ui_rules: config.payment_link_ui_rules,
+ enable_button_only_on_form_ready: config.enable_button_only_on_form_ready,
}
}
}
@@ -4602,6 +4603,7 @@ impl ForeignFrom<diesel_models::PaymentLinkConfigRequestForPayments>
payment_button_text_colour: config.payment_button_text_colour,
sdk_ui_rules: config.sdk_ui_rules,
payment_link_ui_rules: config.payment_link_ui_rules,
+ enable_button_only_on_form_ready: config.enable_button_only_on_form_ready,
}
}
}
diff --git a/crates/router/src/types/transformers.rs b/crates/router/src/types/transformers.rs
index 450cf7c3d6..22bc3d5d4e 100644
--- a/crates/router/src/types/transformers.rs
+++ b/crates/router/src/types/transformers.rs
@@ -2174,6 +2174,7 @@ impl ForeignFrom<api_models::admin::PaymentLinkConfigRequest>
payment_button_text_colour: item.payment_button_text_colour,
sdk_ui_rules: item.sdk_ui_rules,
payment_link_ui_rules: item.payment_link_ui_rules,
+ enable_button_only_on_form_ready: item.enable_button_only_on_form_ready,
}
}
}
@@ -2204,6 +2205,7 @@ impl ForeignFrom<diesel_models::business_profile::PaymentLinkConfigRequest>
payment_button_text_colour: item.payment_button_text_colour,
sdk_ui_rules: item.sdk_ui_rules,
payment_link_ui_rules: item.payment_link_ui_rules,
+ enable_button_only_on_form_ready: item.enable_button_only_on_form_ready,
}
}
}