Skip to content

A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.

JavaScript

fix(sdk): resolve a relative successUrl before sending it to the gateway

rishabh5180ca8e943a
diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md
index 237c84f..c42c695 100644
--- a/docs/CONFIGURATION.md
+++ b/docs/CONFIGURATION.md
@@ -33,6 +33,19 @@
---
+## 1a. `returnUrl` requirements
+
+`POST /api/v1/payments` accepts a `returnUrl` that is echoed back to the customer's
+browser as a redirect target after the payment completes, so the gateway stores only
+**absolute** `http:` / `https:` URLs. A relative value, a `javascript:` URL or any other
+scheme is rejected with `400`.
+
+The official SDK resolves a relative `data-success-url` (for example `/success`) against
+the storefront's own origin before sending it, so relative storefront paths keep working
+without any change to merchant code. If you call the API directly, send an absolute URL.
+
+---
+
## 1b. Merchant API Authentication (`X-Merchant-Key`)
Endpoints that expose merchant-private or customer-contact data require the caller to
diff --git a/packages/merchant-sdk/src/youpi-checkout.js b/packages/merchant-sdk/src/youpi-checkout.js
index c8fa2b4..62bbedb 100644
--- a/packages/merchant-sdk/src/youpi-checkout.js
+++ b/packages/merchant-sdk/src/youpi-checkout.js
@@ -60,6 +60,25 @@
/**
* YoupiCheckout class
*/
+ /**
+ * The gateway stores only an absolute http(s) returnUrl, so a relative successUrl is
+ * resolved against this storefront's own origin before it is sent. Without this a
+ * merchant using `data-success-url="/success"` would get a 400 from the API.
+ * @param {string} [raw]
+ * @returns {string|undefined} absolute http(s) URL, or undefined when unusable
+ */
+ function resolveReturnUrl(raw) {
+ if (!raw) return undefined;
+ try {
+ const base = (typeof window !== 'undefined' && window.location) ? window.location.href : undefined;
+ const parsed = new URL(raw, base);
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return undefined;
+ return parsed.href;
+ } catch (e) {
+ return undefined;
+ }
+ }
+
class YoupiCheckout {
/**
* @param {{
@@ -192,7 +211,7 @@
customer: (custPayload.email || custPayload.phoneNumber || custPayload.fullName) ? custPayload : undefined,
description: instance.options.description,
metadata: instance.options.metadata,
- returnUrl: instance.options.successUrl || undefined
+ returnUrl: resolveReturnUrl(instance.options.successUrl)
})
});
diff --git a/packages/merchant-sdk/src/youpi.js b/packages/merchant-sdk/src/youpi.js
index 5f36c8d..0bf5006 100644
--- a/packages/merchant-sdk/src/youpi.js
+++ b/packages/merchant-sdk/src/youpi.js
@@ -60,6 +60,25 @@
/**
* YoupiCheckout class
*/
+ /**
+ * The gateway stores only an absolute http(s) returnUrl, so a relative successUrl is
+ * resolved against this storefront's own origin before it is sent. Without this a
+ * merchant using `data-success-url="/success"` would get a 400 from the API.
+ * @param {string} [raw]
+ * @returns {string|undefined} absolute http(s) URL, or undefined when unusable
+ */
+ function resolveReturnUrl(raw) {
+ if (!raw) return undefined;
+ try {
+ const base = (typeof window !== 'undefined' && window.location) ? window.location.href : undefined;
+ const parsed = new URL(raw, base);
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return undefined;
+ return parsed.href;
+ } catch (e) {
+ return undefined;
+ }
+ }
+
class YoupiCheckout {
/**
* @param {{
@@ -192,7 +211,7 @@
customer: (custPayload.email || custPayload.phoneNumber || custPayload.fullName) ? custPayload : undefined,
description: instance.options.description,
metadata: instance.options.metadata,
- returnUrl: instance.options.successUrl || undefined
+ returnUrl: resolveReturnUrl(instance.options.successUrl)
})
});