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(checkout-portal): sync checkout-portal SDK with merchant-sdk

ArunGupta152604a0681af0d7
diff --git a/apps/checkout-portal/src/__tests__/checkout-xss.test.js b/apps/checkout-portal/src/__tests__/checkout-xss.test.js
index f34f3f5..552b420 100644
--- a/apps/checkout-portal/src/__tests__/checkout-xss.test.js
+++ b/apps/checkout-portal/src/__tests__/checkout-xss.test.js
@@ -262,15 +262,17 @@ describe('Checkout Portal XSS / Redirect Hardening', () => {
});
describe('merchant-sdk message listener origin checks', () => {
- for (const file of ['youpi.js', 'youpi-checkout.js']) {
- test(`${file} ignores messages from an unexpected origin`, () => {
- const src = fs.readFileSync(
- path.join(__dirname, '../../../../packages/merchant-sdk/src', file),
- 'utf8'
- );
- assert.ok(src.includes('_checkoutOrigin()'));
- assert.ok(src.includes('if (expectedOrigin && e.origin !== expectedOrigin) return;'));
- });
+ for (const dir of [
+ path.join(__dirname, '../../../../packages/merchant-sdk/src'),
+ path.join(__dirname, '../sdk')
+ ]) {
+ for (const file of ['youpi.js', 'youpi-checkout.js']) {
+ test(`${path.basename(dir)}/${file} ignores messages from an unexpected origin`, () => {
+ const src = fs.readFileSync(path.join(dir, file), 'utf8');
+ assert.ok(src.includes('_checkoutOrigin()'));
+ assert.ok(src.includes('if (expectedOrigin && e.origin !== expectedOrigin) return;'));
+ });
+ }
}
});
diff --git a/apps/checkout-portal/src/sdk/youpi-checkout.js b/apps/checkout-portal/src/sdk/youpi-checkout.js
index d9f2441..37875bc 100644
--- a/apps/checkout-portal/src/sdk/youpi-checkout.js
+++ b/apps/checkout-portal/src/sdk/youpi-checkout.js
@@ -57,6 +57,25 @@
const scriptConfig = getScriptConfig();
+ /**
+ * 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;
+ }
+ }
+
/**
* YoupiCheckout class
*/
@@ -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)
})
});
@@ -267,6 +286,16 @@
const sep = targetUrl.includes('?') ? '&' : '?';
targetUrl = `${targetUrl}${sep}embedded=1`;
+ // Declare this page's origin so the checkout can target its postMessages
+ // even when the browser strips the referrer. Opaque origins (file:) and
+ // non-http(s) schemes must not be announced.
+ try {
+ const parsed = new URL(window.location.href);
+ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
+ targetUrl += `&youpiParentOrigin=${encodeURIComponent(parsed.origin)}`;
+ }
+ } catch {}
+
if (this.options.amount) {
targetUrl += `&customAmount=${encodeURIComponent(this.options.amount)}`;
}
@@ -388,12 +417,29 @@
}
}
+ /**
+ * Origin of the checkout iframe this instance embeds. Messages from any
+ * other origin are ignored so a third party cannot forge payment events.
+ */
+ _checkoutOrigin() {
+ try {
+ // Only an explicitly configured checkout URL is authoritative. `portalUrl` defaults
+ // to localhost, so including it would make this check reject legitimate messages.
+ const url = this.options.checkoutUrl;
+ if (url) return new URL(url, window.location.href).origin;
+ } catch (e) { }
+ return null;
+ }
+
_handleMessage(e) {
// Ignore messages from any window other than our checkout iframe (spoofed success events).
if (!this._iframe || e.source !== this._iframe.contentWindow) return;
if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;
if (!e.data || typeof e.data !== 'object') return;
+ const expectedOrigin = this._checkoutOrigin();
+ if (expectedOrigin && e.origin !== expectedOrigin) return;
+
const event = e.data.event;
const rawData = e.data.data || e.data;
diff --git a/apps/checkout-portal/src/sdk/youpi.js b/apps/checkout-portal/src/sdk/youpi.js
index ae5c910..7231ba9 100644
--- a/apps/checkout-portal/src/sdk/youpi.js
+++ b/apps/checkout-portal/src/sdk/youpi.js
@@ -57,6 +57,25 @@
const scriptConfig = getScriptConfig();
+ /**
+ * 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;
+ }
+ }
+
/**
* YoupiCheckout class
*/
@@ -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)
})
});
@@ -267,6 +286,16 @@
const sep = targetUrl.includes('?') ? '&' : '?';
targetUrl = `${targetUrl}${sep}embedded=1`;
+ // Declare this page's origin so the checkout can target its postMessages
+ // even when the browser strips the referrer. Opaque origins (file:) and
+ // non-http(s) schemes must not be announced.
+ try {
+ const parsed = new URL(window.location.href);
+ if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
+ targetUrl += `&youpiParentOrigin=${encodeURIComponent(parsed.origin)}`;
+ }
+ } catch {}
+
if (this.options.amount) {
targetUrl += `&customAmount=${encodeURIComponent(this.options.amount)}`;
}
@@ -386,12 +415,28 @@
}
}
+ /**
+ * Origin of the checkout iframe this instance embeds. Messages from any
+ * other origin are ignored so a third party cannot forge payment events.
+ */
+ _checkoutOrigin() {
+ try {
+ // Only an explicitly configured checkout URL is authoritative. `portalUrl` defaults
+ // to localhost, so including it would make this check reject legitimate messages.
+ if (this.options.checkoutUrl) return new URL(this.options.checkoutUrl, window.location.href).origin;
+ } catch (e) { }
+ return null;
+ }
+
_handleMessage(e) {
// Ignore messages from any window other than our checkout iframe (spoofed success events).
if (!this._iframe || e.source !== this._iframe.contentWindow) return;
if (!this._iframeOrigin || e.origin !== this._iframeOrigin) return;
if (!e.data || typeof e.data !== 'object') return;
+ const expectedOrigin = this._checkoutOrigin();
+ if (expectedOrigin && e.origin !== expectedOrigin) return;
+
const event = e.data.event;
const rawData = e.data.data || e.data;