@xpayeg/sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +529 -0
- package/dist/index.cjs +99 -0
- package/dist/index.d.cts +3874 -0
- package/dist/index.d.mts +3874 -0
- package/dist/index.mjs +98 -0
- package/package.json +66 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
const DEFAULT_SDK_URL = "https://checkout.xpay.app/v1/sdk.js";
|
|
3
|
+
let loadPromise = null;
|
|
4
|
+
/**
|
|
5
|
+
* Load the XPay SDK from CDN.
|
|
6
|
+
*
|
|
7
|
+
* Returns a promise that resolves to an XPayInstance with full TypeScript support.
|
|
8
|
+
* The SDK script is loaded once and cached — subsequent calls return the same instance.
|
|
9
|
+
*
|
|
10
|
+
* @param publishableKey - Your publishable API key (pk_test_... or pk_live_...)
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { loadXPay } from '@xpayeg/sdk';
|
|
15
|
+
*
|
|
16
|
+
* const xpay = await loadXPay('pk_test_xxx');
|
|
17
|
+
* const elements = xpay.elements({ clientSecret: 'cs_test_abc_secret_xyz' });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
async function loadXPay(publishableKey) {
|
|
21
|
+
if (typeof window === "undefined") return null;
|
|
22
|
+
return (await ensureLoaded())(publishableKey);
|
|
23
|
+
}
|
|
24
|
+
function ensureLoaded() {
|
|
25
|
+
if (window.XPay) return Promise.resolve(window.XPay);
|
|
26
|
+
if (loadPromise) return loadPromise;
|
|
27
|
+
loadPromise = new Promise((resolve, reject) => {
|
|
28
|
+
const existing = findExistingScript();
|
|
29
|
+
if (existing) {
|
|
30
|
+
if (window.XPay) {
|
|
31
|
+
resolve(window.XPay);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const onLoad = () => {
|
|
35
|
+
cleanup();
|
|
36
|
+
if (window.XPay) resolve(window.XPay);
|
|
37
|
+
else reject(/* @__PURE__ */ new Error("XPay SDK loaded but window.XPay is not available"));
|
|
38
|
+
};
|
|
39
|
+
const onError = () => {
|
|
40
|
+
cleanup();
|
|
41
|
+
loadPromise = null;
|
|
42
|
+
reject(/* @__PURE__ */ new Error("Failed to load XPay SDK"));
|
|
43
|
+
};
|
|
44
|
+
const cleanup = () => {
|
|
45
|
+
existing.removeEventListener("load", onLoad);
|
|
46
|
+
existing.removeEventListener("error", onError);
|
|
47
|
+
};
|
|
48
|
+
existing.addEventListener("load", onLoad);
|
|
49
|
+
existing.addEventListener("error", onError);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const script = document.createElement("script");
|
|
53
|
+
script.src = DEFAULT_SDK_URL;
|
|
54
|
+
script.async = true;
|
|
55
|
+
const onLoad = () => {
|
|
56
|
+
cleanup();
|
|
57
|
+
if (window.XPay) resolve(window.XPay);
|
|
58
|
+
else {
|
|
59
|
+
loadPromise = null;
|
|
60
|
+
reject(/* @__PURE__ */ new Error("XPay SDK loaded but window.XPay is not available"));
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const onError = () => {
|
|
64
|
+
cleanup();
|
|
65
|
+
loadPromise = null;
|
|
66
|
+
reject(/* @__PURE__ */ new Error(`Failed to load XPay SDK from ${DEFAULT_SDK_URL}`));
|
|
67
|
+
};
|
|
68
|
+
const cleanup = () => {
|
|
69
|
+
script.removeEventListener("load", onLoad);
|
|
70
|
+
script.removeEventListener("error", onError);
|
|
71
|
+
};
|
|
72
|
+
script.addEventListener("load", onLoad);
|
|
73
|
+
script.addEventListener("error", onError);
|
|
74
|
+
document.head.appendChild(script);
|
|
75
|
+
});
|
|
76
|
+
return loadPromise;
|
|
77
|
+
}
|
|
78
|
+
/** Find an existing XPay SDK script tag in the document — must match the
|
|
79
|
+
* versioned `/v1/sdk.js` path AND be served from an xpay-controlled origin
|
|
80
|
+
* so a random third-party `/v1/sdk.js` on the page is never mistaken for ours. */
|
|
81
|
+
function findExistingScript() {
|
|
82
|
+
const exact = document.querySelector(`script[src="${DEFAULT_SDK_URL}"]`);
|
|
83
|
+
if (exact) return exact;
|
|
84
|
+
let expectedOrigin = "";
|
|
85
|
+
try {
|
|
86
|
+
expectedOrigin = new URL(DEFAULT_SDK_URL).origin;
|
|
87
|
+
} catch {}
|
|
88
|
+
const scripts = document.querySelectorAll("script[src*=\"/v1/sdk.js\"]");
|
|
89
|
+
for (const script of scripts) try {
|
|
90
|
+
const url = new URL(script.src);
|
|
91
|
+
const pathOk = url.pathname.endsWith("/v1/sdk.js");
|
|
92
|
+
const originOk = url.origin === expectedOrigin || url.hostname.endsWith(".xpay.app");
|
|
93
|
+
if (pathOk && originOk) return script;
|
|
94
|
+
} catch {}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
export { loadXPay };
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xpayeg/sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "XPay JavaScript SDK — loader and TypeScript types for embedding XPay payments",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=18"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"default": "./dist/index.mjs"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./dist/index.d.cts",
|
|
22
|
+
"default": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"CHANGELOG.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsdown && rollup -c rollup.dts.config.mjs",
|
|
37
|
+
"lint:pkg": "publint && attw --pack .",
|
|
38
|
+
"typecheck": "tsc --noEmit"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@arethetypeswrong/cli": "^0.18.2",
|
|
42
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
43
|
+
"@xpay/checkout-api": "workspace:*",
|
|
44
|
+
"@xpay/tsconfig": "workspace:*",
|
|
45
|
+
"publint": "^0.3.21",
|
|
46
|
+
"rollup": "^4.60.4",
|
|
47
|
+
"rollup-plugin-dts": "^6.4.1",
|
|
48
|
+
"tsdown": "^0.22.0",
|
|
49
|
+
"typescript": "catalog:"
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/xpayeg/xpay-js.git"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/xpayeg/xpay-js/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/xpayeg/xpay-js#readme",
|
|
59
|
+
"keywords": [
|
|
60
|
+
"xpay",
|
|
61
|
+
"payments",
|
|
62
|
+
"egypt",
|
|
63
|
+
"checkout",
|
|
64
|
+
"sdk"
|
|
65
|
+
]
|
|
66
|
+
}
|