@integralscope/command-transfer-checkout-js 0.1.1 → 0.1.3
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/README.md +4 -63
- package/dist/commandtransfer-checkout.js +79 -80
- package/dist/commandtransfer-checkout.umd.cjs +1 -1
- package/dist/constants.d.ts +4 -13
- package/dist/constants.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -6
- package/dist/types/index.d.ts.map +1 -1
- package/dist/url.d.ts +1 -6
- package/dist/url.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -109,7 +109,7 @@ checkout.on("error", ({ code, message }) => {
|
|
|
109
109
|
});
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
-
|
|
112
|
+
The SDK opens Checkout Web from the origin Command Transfer bakes in at build time (`VITE_CHECKOUT_ORIGIN`).
|
|
113
113
|
|
|
114
114
|
### Inline
|
|
115
115
|
|
|
@@ -222,7 +222,6 @@ checkout.on("success", async ({ intentId }) => {
|
|
|
222
222
|
| `clientToken` | yes | Payer token (`ctc_…`) from your backend |
|
|
223
223
|
| `mode` | no | `inline` (default), `modal`, or `redirect` |
|
|
224
224
|
| `container` | no | Used by `start()` when `mode` is `inline` |
|
|
225
|
-
| `checkoutOrigin` | no | Staging host from Command Transfer. Omit in production. |
|
|
226
225
|
| `returnUrl` | no | Redirect return URL. Ignored for inline/modal. |
|
|
227
226
|
|
|
228
227
|
## Events
|
|
@@ -234,25 +233,14 @@ checkout.on("success", async ({ intentId }) => {
|
|
|
234
233
|
| `cancel` | — | Customer closed checkout |
|
|
235
234
|
| `error` | `{ code, message }` | Expired, failed, or could not load |
|
|
236
235
|
|
|
237
|
-
##
|
|
236
|
+
## Checkout origin
|
|
238
237
|
|
|
239
|
-
|
|
238
|
+
Merchants do not pass a checkout URL. Command Transfer bakes the Checkout Web origin into the SDK at build time.
|
|
240
239
|
|
|
241
240
|
```js
|
|
242
241
|
new Checkout({
|
|
243
242
|
intentId: session.intentId,
|
|
244
243
|
clientToken: session.clientToken,
|
|
245
|
-
checkoutOrigin: "https://xxxxx",
|
|
246
|
-
});
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
Local Command Transfer checkout app:
|
|
250
|
-
|
|
251
|
-
```js
|
|
252
|
-
new Checkout({
|
|
253
|
-
intentId: session.intentId,
|
|
254
|
-
clientToken: session.clientToken,
|
|
255
|
-
checkoutOrigin: "http://localhost:5173",
|
|
256
244
|
});
|
|
257
245
|
```
|
|
258
246
|
|
|
@@ -263,51 +251,4 @@ If you set a Content-Security-Policy on your site, allow Command Transfer’s ch
|
|
|
263
251
|
- Secret API keys stay on your server.
|
|
264
252
|
- `clientToken` is a payer capability for that checkout. Do not log it, put it in query strings, or persist it in `localStorage`.
|
|
265
253
|
- `success` in the browser is UX only. Fulfill from server-side confirmation.
|
|
266
|
-
- Do not
|
|
267
|
-
|
|
268
|
-
## Releasing
|
|
269
|
-
|
|
270
|
-
[`.github/workflows/publish-sdk.yml`](.github/workflows/publish-sdk.yml) publishes to the public npm
|
|
271
|
-
registry on a `v*` tag:
|
|
272
|
-
|
|
273
|
-
```bash
|
|
274
|
-
# bump "version" in package.json, commit, then:
|
|
275
|
-
git tag v0.1.1 && git push origin v0.1.1
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
It runs the tests, builds, checks the tag matches `package.json`, refuses to republish an existing
|
|
279
|
-
version, and publishes.
|
|
280
|
-
|
|
281
|
-
One-time setup:
|
|
282
|
-
|
|
283
|
-
- The `@integralscope` scope (the parent company org) must exist on npmjs.com, with the publishing
|
|
284
|
-
account a member holding publish rights.
|
|
285
|
-
- Add the repository secret `NPM_TOKEN`: a **granular access token** with **Bypass two-factor
|
|
286
|
-
authentication** enabled and read+write on the `@integralscope` **scope**. Scope, not package — a
|
|
287
|
-
package-scoped token cannot create a package that does not exist yet.
|
|
288
|
-
|
|
289
|
-
npm normally requires proof-of-presence (2FA) to publish and CI cannot provide it; the bypass-2FA
|
|
290
|
-
token is what makes automated publishing possible. `publishConfig.access` is `public` in
|
|
291
|
-
`package.json` — scoped packages are private by default and would otherwise fail with a 402.
|
|
292
|
-
|
|
293
|
-
`prepack` runs `npm run build`, so `dist/` is always rebuilt from source at pack time and the
|
|
294
|
-
published tarball can never contain a stale or missing build.
|
|
295
|
-
|
|
296
|
-
> The token bypasses 2FA, so it is strictly more powerful than a login: anyone holding it can publish
|
|
297
|
-
> to `@integralscope` without a second factor. Keep it in the GitHub secret only, and note its expiry
|
|
298
|
-
> — granular tokens expire, and CI will then fail with a 401 that looks like a broken pipeline.
|
|
299
|
-
|
|
300
|
-
### Staged releases (alternative, currently disabled)
|
|
301
|
-
|
|
302
|
-
The workflow also carries a commented-out staged-release path. With it, CI stages a version that
|
|
303
|
-
stays invisible until a maintainer approves it:
|
|
304
|
-
|
|
305
|
-
```bash
|
|
306
|
-
npm stage list @integralscope/command-transfer-checkout-js
|
|
307
|
-
npm stage view <stage-id> # inspect exactly what CI built
|
|
308
|
-
npm stage approve <stage-id> --otp=<code> # or npm stage reject
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
That path works with a stage-only token and adds a human check before merchants can install, at the
|
|
312
|
-
cost of a manual step per release. It requires npm >= 12 and cannot create a package that does not
|
|
313
|
-
exist yet.
|
|
254
|
+
- Do not load checkout from your own storefront.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class f {
|
|
2
2
|
constructor() {
|
|
3
3
|
this.listeners = /* @__PURE__ */ new Map();
|
|
4
4
|
}
|
|
@@ -10,11 +10,11 @@ class d {
|
|
|
10
10
|
}
|
|
11
11
|
emit(e, t) {
|
|
12
12
|
const n = this.listeners.get(e);
|
|
13
|
-
n && [...n].forEach((
|
|
13
|
+
n && [...n].forEach((i) => {
|
|
14
14
|
try {
|
|
15
|
-
|
|
16
|
-
} catch (
|
|
17
|
-
console.error(`Checkout "${String(e)}" handler failed.`,
|
|
15
|
+
i(t);
|
|
16
|
+
} catch (o) {
|
|
17
|
+
console.error(`Checkout "${String(e)}" handler failed.`, o);
|
|
18
18
|
}
|
|
19
19
|
});
|
|
20
20
|
}
|
|
@@ -22,16 +22,20 @@ class d {
|
|
|
22
22
|
this.listeners.clear();
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
const s = "commandtransfer.checkout",
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const s = "commandtransfer.checkout", u = "ctc_";
|
|
26
|
+
function m(r) {
|
|
27
|
+
return r?.trim() || "";
|
|
28
|
+
}
|
|
29
|
+
const a = m(
|
|
30
|
+
"https://ct-dev-checkout.web.app"
|
|
31
|
+
), g = /* @__PURE__ */ new Set([
|
|
28
32
|
"checkout.ready",
|
|
29
33
|
"checkout.success",
|
|
30
34
|
"checkout.cancel",
|
|
31
35
|
"checkout.error",
|
|
32
36
|
"checkout.resize"
|
|
33
37
|
]);
|
|
34
|
-
function
|
|
38
|
+
function w(r) {
|
|
35
39
|
return !!r && typeof r == "object" && !Array.isArray(r);
|
|
36
40
|
}
|
|
37
41
|
function c(r, e) {
|
|
@@ -39,8 +43,8 @@ function c(r, e) {
|
|
|
39
43
|
const t = r.trim();
|
|
40
44
|
return !t || t.length > e || /[\u0000-\u001f\u007f]/.test(t) ? null : t;
|
|
41
45
|
}
|
|
42
|
-
function
|
|
43
|
-
if (!
|
|
46
|
+
function k(r, e) {
|
|
47
|
+
if (!w(r) || r.source !== s)
|
|
44
48
|
return null;
|
|
45
49
|
const t = c(r.type, 64);
|
|
46
50
|
if (!t || !g.has(t))
|
|
@@ -56,12 +60,12 @@ function p(r, e) {
|
|
|
56
60
|
checkoutId: n
|
|
57
61
|
};
|
|
58
62
|
case "checkout.success": {
|
|
59
|
-
const
|
|
60
|
-
return
|
|
63
|
+
const i = c(r.reference, 128);
|
|
64
|
+
return i ? {
|
|
61
65
|
source: s,
|
|
62
66
|
type: t,
|
|
63
67
|
checkoutId: n,
|
|
64
|
-
reference:
|
|
68
|
+
reference: i
|
|
65
69
|
} : null;
|
|
66
70
|
}
|
|
67
71
|
case "checkout.cancel":
|
|
@@ -71,34 +75,34 @@ function p(r, e) {
|
|
|
71
75
|
checkoutId: n
|
|
72
76
|
};
|
|
73
77
|
case "checkout.error": {
|
|
74
|
-
const
|
|
75
|
-
return !
|
|
78
|
+
const i = c(r.code, 64), o = c(r.message, 280);
|
|
79
|
+
return !i || !o ? null : {
|
|
76
80
|
source: s,
|
|
77
81
|
type: t,
|
|
78
82
|
checkoutId: n,
|
|
79
|
-
code:
|
|
80
|
-
message:
|
|
83
|
+
code: i,
|
|
84
|
+
message: o
|
|
81
85
|
};
|
|
82
86
|
}
|
|
83
87
|
case "checkout.resize": {
|
|
84
|
-
const
|
|
85
|
-
return !Number.isFinite(
|
|
88
|
+
const i = typeof r.height == "number" ? r.height : Number(r.height);
|
|
89
|
+
return !Number.isFinite(i) || i < 200 || i > 4e3 ? null : {
|
|
86
90
|
source: s,
|
|
87
91
|
type: t,
|
|
88
92
|
checkoutId: n,
|
|
89
|
-
height: Math.round(
|
|
93
|
+
height: Math.round(i)
|
|
90
94
|
};
|
|
91
95
|
}
|
|
92
96
|
default:
|
|
93
97
|
return null;
|
|
94
98
|
}
|
|
95
99
|
}
|
|
96
|
-
class
|
|
100
|
+
class p {
|
|
97
101
|
constructor(e) {
|
|
98
102
|
this.sourceWindow = null, this.handleMessage = (t) => {
|
|
99
103
|
if (t.origin !== this.checkoutOrigin || !this.sourceWindow || t.source !== this.sourceWindow)
|
|
100
104
|
return;
|
|
101
|
-
const n =
|
|
105
|
+
const n = k(t.data, this.checkoutId);
|
|
102
106
|
n && this.onMessage(n);
|
|
103
107
|
}, this.checkoutId = e.checkoutId, this.checkoutOrigin = e.checkoutOrigin, this.onMessage = e.onMessage, window.addEventListener("message", this.handleMessage);
|
|
104
108
|
}
|
|
@@ -109,9 +113,9 @@ class y {
|
|
|
109
113
|
window.removeEventListener("message", this.handleMessage), this.sourceWindow = null;
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
|
-
class
|
|
116
|
+
class y {
|
|
113
117
|
constructor(e) {
|
|
114
|
-
this.iframe = null, this.overlay = null, this.previousBodyOverflow = null, this.escapeListener = null, this.checkoutUrl = e.checkoutUrl, this.presentation = e.presentation, this.onCloseRequest = e.onCloseRequest, this.bridge = new
|
|
118
|
+
this.iframe = null, this.overlay = null, this.previousBodyOverflow = null, this.escapeListener = null, this.checkoutUrl = e.checkoutUrl, this.presentation = e.presentation, this.onCloseRequest = e.onCloseRequest, this.bridge = new p({
|
|
115
119
|
checkoutId: e.checkoutId,
|
|
116
120
|
checkoutOrigin: e.checkoutOrigin,
|
|
117
121
|
onMessage: e.onMessage
|
|
@@ -169,8 +173,8 @@ class b {
|
|
|
169
173
|
"border-radius:16px",
|
|
170
174
|
"box-shadow:0 24px 64px rgba(15,12,41,0.28)"
|
|
171
175
|
].join(";");
|
|
172
|
-
const
|
|
173
|
-
|
|
176
|
+
const i = document.createElement("button");
|
|
177
|
+
i.type = "button", i.setAttribute("aria-label", "Close checkout"), i.textContent = "×", i.style.cssText = [
|
|
174
178
|
"position:absolute",
|
|
175
179
|
"top:8px",
|
|
176
180
|
"right:8px",
|
|
@@ -185,64 +189,60 @@ class b {
|
|
|
185
189
|
"line-height:1",
|
|
186
190
|
"cursor:pointer",
|
|
187
191
|
"box-shadow:0 2px 8px rgba(15,12,41,0.16)"
|
|
188
|
-
].join(";"),
|
|
189
|
-
|
|
190
|
-
}), this.escapeListener = (
|
|
191
|
-
|
|
192
|
-
}, window.addEventListener("keydown", this.escapeListener), this.previousBodyOverflow = document.body.style.overflow, document.body.style.overflow = "hidden", n.append(
|
|
192
|
+
].join(";"), i.addEventListener("click", this.onCloseRequest), t.addEventListener("click", (o) => {
|
|
193
|
+
o.target === t && this.onCloseRequest();
|
|
194
|
+
}), this.escapeListener = (o) => {
|
|
195
|
+
o.key === "Escape" && this.onCloseRequest();
|
|
196
|
+
}, window.addEventListener("keydown", this.escapeListener), this.previousBodyOverflow = document.body.style.overflow, document.body.style.overflow = "hidden", n.append(i, e), t.appendChild(n), document.body.appendChild(t), this.overlay = t, i.focus();
|
|
193
197
|
}
|
|
194
198
|
teardownModal() {
|
|
195
199
|
this.escapeListener && (window.removeEventListener("keydown", this.escapeListener), this.escapeListener = null), this.overlay?.remove(), this.overlay = null, this.previousBodyOverflow !== null && (document.body.style.overflow = this.previousBodyOverflow, this.previousBodyOverflow = null);
|
|
196
200
|
}
|
|
197
201
|
}
|
|
198
|
-
function
|
|
202
|
+
function b(r) {
|
|
199
203
|
if (typeof r != "string")
|
|
200
204
|
return r;
|
|
201
205
|
const e = r.trim();
|
|
202
206
|
return e ? e.startsWith("#") || e.startsWith(".") || e.startsWith("[") ? document.querySelector(e) : document.getElementById(e) ?? document.querySelector(e) : null;
|
|
203
207
|
}
|
|
204
|
-
const
|
|
205
|
-
function
|
|
208
|
+
const v = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
209
|
+
function h(r) {
|
|
206
210
|
return r.replace(/^\[|\]$/g, "").toLowerCase();
|
|
207
211
|
}
|
|
208
|
-
function
|
|
209
|
-
const e =
|
|
212
|
+
function l(r) {
|
|
213
|
+
const e = h(r);
|
|
210
214
|
return e === "localhost" || e === "127.0.0.1" || e === "::1";
|
|
211
215
|
}
|
|
212
216
|
function E(r) {
|
|
213
217
|
const e = r.split(".");
|
|
214
218
|
if (e.length !== 4) return !1;
|
|
215
|
-
const t = e.map((
|
|
216
|
-
if (t.some((
|
|
219
|
+
const t = e.map((o) => Number(o));
|
|
220
|
+
if (t.some((o) => !Number.isInteger(o) || o < 0 || o > 255))
|
|
217
221
|
return !1;
|
|
218
|
-
const [n,
|
|
219
|
-
return n === 10 || n === 127 || n === 192 &&
|
|
222
|
+
const [n, i] = t;
|
|
223
|
+
return n === 10 || n === 127 || n === 192 && i === 168 || n === 172 && i >= 16 && i <= 31;
|
|
220
224
|
}
|
|
221
225
|
function I(r) {
|
|
222
|
-
const e =
|
|
223
|
-
return
|
|
226
|
+
const e = h(r);
|
|
227
|
+
return l(e) || E(e) ? !0 : e.endsWith(".local") || e.endsWith(".test") || e.endsWith(".localhost");
|
|
224
228
|
}
|
|
225
|
-
function
|
|
226
|
-
const e = u(r);
|
|
227
|
-
return a(e) || f.includes(e) ? !0 : w.some((t) => e.endsWith(t));
|
|
228
|
-
}
|
|
229
|
-
function x(r) {
|
|
229
|
+
function C(r) {
|
|
230
230
|
const e = r.trim();
|
|
231
|
-
if (!
|
|
231
|
+
if (!v.test(e))
|
|
232
232
|
throw new Error("intentId is invalid.");
|
|
233
233
|
return e;
|
|
234
234
|
}
|
|
235
|
-
function
|
|
235
|
+
function x(r) {
|
|
236
236
|
const e = r.trim(), t = e.toLowerCase().startsWith("bearer ") ? e.slice(7).trim() : e;
|
|
237
|
-
if (!t.toLowerCase().startsWith(
|
|
237
|
+
if (!t.toLowerCase().startsWith(u))
|
|
238
238
|
throw new Error(
|
|
239
|
-
`clientToken is invalid. Expected a token starting with ${
|
|
239
|
+
`clientToken is invalid. Expected a token starting with ${u}.`
|
|
240
240
|
);
|
|
241
241
|
if (t.length > 512 || /\s/.test(t))
|
|
242
242
|
throw new Error("clientToken is invalid.");
|
|
243
243
|
return t;
|
|
244
244
|
}
|
|
245
|
-
function
|
|
245
|
+
function d(r, e, t) {
|
|
246
246
|
let n;
|
|
247
247
|
try {
|
|
248
248
|
n = new URL(r);
|
|
@@ -255,43 +255,42 @@ function l(r, e, t) {
|
|
|
255
255
|
return n;
|
|
256
256
|
throw new Error(`${e} must be an https URL.`);
|
|
257
257
|
}
|
|
258
|
-
function M(
|
|
259
|
-
|
|
260
|
-
(
|
|
261
|
-
|
|
262
|
-
a
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
return e.origin;
|
|
258
|
+
function M() {
|
|
259
|
+
if (!a)
|
|
260
|
+
throw new Error("checkout origin is not configured.");
|
|
261
|
+
return d(
|
|
262
|
+
a,
|
|
263
|
+
"checkout origin",
|
|
264
|
+
l
|
|
265
|
+
).origin;
|
|
267
266
|
}
|
|
268
|
-
function
|
|
269
|
-
return
|
|
267
|
+
function O(r, e) {
|
|
268
|
+
return d(r, e, I).toString();
|
|
270
269
|
}
|
|
271
|
-
function
|
|
270
|
+
function U(r) {
|
|
272
271
|
const e = encodeURIComponent(r.intentId), t = new URL(`/pay/${e}`, r.checkoutOrigin);
|
|
273
272
|
return r.mode === "redirect" ? r.returnUrl && t.searchParams.set("return_url", r.returnUrl) : (t.searchParams.set("embed", "1"), r.parentOrigin && t.searchParams.set("origin", r.parentOrigin)), t.hash = `token=${encodeURIComponent(r.clientToken)}`, t.toString();
|
|
274
273
|
}
|
|
275
|
-
function
|
|
274
|
+
function L(r) {
|
|
276
275
|
if (!r || typeof r != "object")
|
|
277
276
|
throw new Error("Checkout config is required.");
|
|
278
277
|
if (!r.intentId?.trim())
|
|
279
278
|
throw new Error("intentId is required.");
|
|
280
|
-
const e =
|
|
281
|
-
if (
|
|
279
|
+
const e = C(r.intentId), t = x(r.clientToken ?? ""), n = M(), i = r.mode ?? "inline";
|
|
280
|
+
if (i !== "inline" && i !== "modal" && i !== "redirect")
|
|
282
281
|
throw new Error('mode must be "inline", "modal", or "redirect".');
|
|
283
|
-
const
|
|
282
|
+
const o = r.returnUrl ? O(r.returnUrl, "returnUrl") : void 0;
|
|
284
283
|
return {
|
|
285
284
|
intentId: e,
|
|
286
285
|
clientToken: t,
|
|
287
286
|
checkoutOrigin: n,
|
|
288
|
-
mode:
|
|
289
|
-
returnUrl:
|
|
287
|
+
mode: i,
|
|
288
|
+
returnUrl: o
|
|
290
289
|
};
|
|
291
290
|
}
|
|
292
|
-
class
|
|
291
|
+
class T {
|
|
293
292
|
constructor(e) {
|
|
294
|
-
this.events = new
|
|
293
|
+
this.events = new f(), this.iframeManager = null, this.mounted = !1, this.settled = !1, this.handleMessage = (t) => {
|
|
295
294
|
switch (t.type) {
|
|
296
295
|
case "checkout.ready":
|
|
297
296
|
this.events.emit("ready", { intentId: t.checkoutId });
|
|
@@ -315,7 +314,7 @@ class W {
|
|
|
315
314
|
this.iframeManager?.setHeight(t.height);
|
|
316
315
|
break;
|
|
317
316
|
}
|
|
318
|
-
}, this.config =
|
|
317
|
+
}, this.config = L(e), this.containerOption = e.container;
|
|
319
318
|
}
|
|
320
319
|
/**
|
|
321
320
|
* Start checkout using `config.mode`.
|
|
@@ -352,7 +351,7 @@ class W {
|
|
|
352
351
|
this.teardownFrame(), e && !this.settled && (this.settled = !0, this.events.emit("cancel", void 0));
|
|
353
352
|
}
|
|
354
353
|
getCheckoutUrl(e) {
|
|
355
|
-
return
|
|
354
|
+
return U({
|
|
356
355
|
intentId: this.config.intentId,
|
|
357
356
|
clientToken: this.config.clientToken,
|
|
358
357
|
checkoutOrigin: this.config.checkoutOrigin,
|
|
@@ -366,13 +365,13 @@ class W {
|
|
|
366
365
|
throw new Error("Checkout is already mounted.");
|
|
367
366
|
if (!t)
|
|
368
367
|
throw new Error("Checkout container was not found.");
|
|
369
|
-
const n = e === "modal" ? document.body :
|
|
368
|
+
const n = e === "modal" ? document.body : b(t);
|
|
370
369
|
if (!n)
|
|
371
370
|
throw new Error("Checkout container was not found.");
|
|
372
|
-
const
|
|
373
|
-
this.iframeManager = new
|
|
371
|
+
const i = this.getCheckoutUrl(e);
|
|
372
|
+
this.iframeManager = new y({
|
|
374
373
|
checkoutId: this.config.intentId,
|
|
375
|
-
checkoutUrl:
|
|
374
|
+
checkoutUrl: i,
|
|
376
375
|
checkoutOrigin: this.config.checkoutOrigin,
|
|
377
376
|
presentation: e,
|
|
378
377
|
onMessage: this.handleMessage,
|
|
@@ -384,5 +383,5 @@ class W {
|
|
|
384
383
|
}
|
|
385
384
|
}
|
|
386
385
|
export {
|
|
387
|
-
|
|
386
|
+
T as Checkout
|
|
388
387
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(s=typeof globalThis<"u"?globalThis:s||self,u(s.CommandTransferCheckout={}))})(this,(function(s){"use strict";class u{constructor(){this.listeners=new Map}on(e,t){let n=this.listeners.get(e);return n||(n=new Set,this.listeners.set(e,n)),n.add(t),()=>{n?.delete(t)}}emit(e,t){const n=this.listeners.get(e);n&&[...n].forEach(o=>{try{o(t)}catch(i){console.error(`Checkout "${String(e)}" handler failed.`,i)}})}removeAllListeners(){this.listeners.clear()}}const c="commandtransfer.checkout",
|
|
1
|
+
(function(s,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(s=typeof globalThis<"u"?globalThis:s||self,u(s.CommandTransferCheckout={}))})(this,(function(s){"use strict";class u{constructor(){this.listeners=new Map}on(e,t){let n=this.listeners.get(e);return n||(n=new Set,this.listeners.set(e,n)),n.add(t),()=>{n?.delete(t)}}emit(e,t){const n=this.listeners.get(e);n&&[...n].forEach(o=>{try{o(t)}catch(i){console.error(`Checkout "${String(e)}" handler failed.`,i)}})}removeAllListeners(){this.listeners.clear()}}const c="commandtransfer.checkout",h="ctc_";function g(r){return r?.trim()||""}const l=g("https://ct-dev-checkout.web.app"),w=new Set(["checkout.ready","checkout.success","checkout.cancel","checkout.error","checkout.resize"]);function k(r){return!!r&&typeof r=="object"&&!Array.isArray(r)}function a(r,e){if(typeof r!="string")return null;const t=r.trim();return!t||t.length>e||/[\u0000-\u001f\u007f]/.test(t)?null:t}function p(r,e){if(!k(r)||r.source!==c)return null;const t=a(r.type,64);if(!t||!w.has(t))return null;const n=a(r.checkoutId,128);if(n!==e)return null;switch(t){case"checkout.ready":return{source:c,type:t,checkoutId:n};case"checkout.success":{const o=a(r.reference,128);return o?{source:c,type:t,checkoutId:n,reference:o}:null}case"checkout.cancel":return{source:c,type:t,checkoutId:n};case"checkout.error":{const o=a(r.code,64),i=a(r.message,280);return!o||!i?null:{source:c,type:t,checkoutId:n,code:o,message:i}}case"checkout.resize":{const o=typeof r.height=="number"?r.height:Number(r.height);return!Number.isFinite(o)||o<200||o>4e3?null:{source:c,type:t,checkoutId:n,height:Math.round(o)}}default:return null}}class y{constructor(e){this.sourceWindow=null,this.handleMessage=t=>{if(t.origin!==this.checkoutOrigin||!this.sourceWindow||t.source!==this.sourceWindow)return;const n=p(t.data,this.checkoutId);n&&this.onMessage(n)},this.checkoutId=e.checkoutId,this.checkoutOrigin=e.checkoutOrigin,this.onMessage=e.onMessage,window.addEventListener("message",this.handleMessage)}setSourceWindow(e){this.sourceWindow=e}destroy(){window.removeEventListener("message",this.handleMessage),this.sourceWindow=null}}class b{constructor(e){this.iframe=null,this.overlay=null,this.previousBodyOverflow=null,this.escapeListener=null,this.checkoutUrl=e.checkoutUrl,this.presentation=e.presentation,this.onCloseRequest=e.onCloseRequest,this.bridge=new y({checkoutId:e.checkoutId,checkoutOrigin:e.checkoutOrigin,onMessage:e.onMessage})}mount(e){if(this.iframe)throw new Error("Checkout iframe is already mounted.");const t=this.createIframe();this.presentation==="modal"?this.mountModal(t):e.appendChild(t),this.iframe=t,this.bridge.setSourceWindow(t.contentWindow),t.addEventListener("load",()=>{this.bridge.setSourceWindow(t.contentWindow)})}setHeight(e){this.iframe&&(this.iframe.style.height=`${Math.round(e)}px`)}destroy(){this.bridge.destroy(),this.iframe?.remove(),this.iframe=null,this.teardownModal()}createIframe(){const e=document.createElement("iframe");return e.src=this.checkoutUrl,e.title="Command Transfer checkout",e.setAttribute("allow","payment; clipboard-write; publickey-credentials-get"),e.setAttribute("referrerpolicy","no-referrer"),e.setAttribute("sandbox",["allow-scripts","allow-same-origin","allow-forms","allow-popups","allow-popups-to-escape-sandbox","allow-top-navigation-by-user-activation"].join(" ")),e.style.width="100%",e.style.height="640px",e.style.border="0",e.style.display="block",e.style.background="#f3f0ff",e.style.borderRadius=this.presentation==="modal"?"16px":"0",e}mountModal(e){const t=document.createElement("div");t.setAttribute("data-ct-checkout","overlay"),t.setAttribute("role","dialog"),t.setAttribute("aria-modal","true"),t.setAttribute("aria-label","Payment checkout"),t.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","padding:24px 16px","background:rgba(15,12,41,0.55)"].join(";");const n=document.createElement("div");n.style.cssText=["position:relative","width:min(420px,100%)","max-height:calc(100vh - 48px)","overflow:auto","border-radius:16px","box-shadow:0 24px 64px rgba(15,12,41,0.28)"].join(";");const o=document.createElement("button");o.type="button",o.setAttribute("aria-label","Close checkout"),o.textContent="×",o.style.cssText=["position:absolute","top:8px","right:8px","z-index:1","width:32px","height:32px","border:0","border-radius:999px","background:#ffffff","color:#1e1654","font-size:22px","line-height:1","cursor:pointer","box-shadow:0 2px 8px rgba(15,12,41,0.16)"].join(";"),o.addEventListener("click",this.onCloseRequest),t.addEventListener("click",i=>{i.target===t&&this.onCloseRequest()}),this.escapeListener=i=>{i.key==="Escape"&&this.onCloseRequest()},window.addEventListener("keydown",this.escapeListener),this.previousBodyOverflow=document.body.style.overflow,document.body.style.overflow="hidden",n.append(o,e),t.appendChild(n),document.body.appendChild(t),this.overlay=t,o.focus()}teardownModal(){this.escapeListener&&(window.removeEventListener("keydown",this.escapeListener),this.escapeListener=null),this.overlay?.remove(),this.overlay=null,this.previousBodyOverflow!==null&&(document.body.style.overflow=this.previousBodyOverflow,this.previousBodyOverflow=null)}}function v(r){if(typeof r!="string")return r;const e=r.trim();return e?e.startsWith("#")||e.startsWith(".")||e.startsWith("[")?document.querySelector(e):document.getElementById(e)??document.querySelector(e):null}const C=/^[A-Za-z0-9._:-]{1,128}$/;function d(r){return r.replace(/^\[|\]$/g,"").toLowerCase()}function f(r){const e=d(r);return e==="localhost"||e==="127.0.0.1"||e==="::1"}function E(r){const e=r.split(".");if(e.length!==4)return!1;const t=e.map(i=>Number(i));if(t.some(i=>!Number.isInteger(i)||i<0||i>255))return!1;const[n,o]=t;return n===10||n===127||n===192&&o===168||n===172&&o>=16&&o<=31}function I(r){const e=d(r);return f(e)||E(e)?!0:e.endsWith(".local")||e.endsWith(".test")||e.endsWith(".localhost")}function x(r){const e=r.trim();if(!C.test(e))throw new Error("intentId is invalid.");return e}function M(r){const e=r.trim(),t=e.toLowerCase().startsWith("bearer ")?e.slice(7).trim():e;if(!t.toLowerCase().startsWith(h))throw new Error(`clientToken is invalid. Expected a token starting with ${h}.`);if(t.length>512||/\s/.test(t))throw new Error("clientToken is invalid.");return t}function m(r,e,t){let n;try{n=new URL(r)}catch{throw new Error(`${e} is not a valid URL.`)}if(n.username||n.password)throw new Error(`${e} must not include credentials.`);if(n.protocol==="https:"||n.protocol==="http:"&&t(n.hostname))return n;throw new Error(`${e} must be an https URL.`)}function O(){if(!l)throw new Error("checkout origin is not configured.");return m(l,"checkout origin",f).origin}function U(r,e){return m(r,e,I).toString()}function T(r){const e=encodeURIComponent(r.intentId),t=new URL(`/pay/${e}`,r.checkoutOrigin);return r.mode==="redirect"?r.returnUrl&&t.searchParams.set("return_url",r.returnUrl):(t.searchParams.set("embed","1"),r.parentOrigin&&t.searchParams.set("origin",r.parentOrigin)),t.hash=`token=${encodeURIComponent(r.clientToken)}`,t.toString()}function L(r){if(!r||typeof r!="object")throw new Error("Checkout config is required.");if(!r.intentId?.trim())throw new Error("intentId is required.");const e=x(r.intentId),t=M(r.clientToken??""),n=O(),o=r.mode??"inline";if(o!=="inline"&&o!=="modal"&&o!=="redirect")throw new Error('mode must be "inline", "modal", or "redirect".');const i=r.returnUrl?U(r.returnUrl,"returnUrl"):void 0;return{intentId:e,clientToken:t,checkoutOrigin:n,mode:o,returnUrl:i}}class S{constructor(e){this.events=new u,this.iframeManager=null,this.mounted=!1,this.settled=!1,this.handleMessage=t=>{switch(t.type){case"checkout.ready":this.events.emit("ready",{intentId:t.checkoutId});break;case"checkout.success":this.settled=!0,this.events.emit("success",{intentId:t.checkoutId,reference:t.reference}),this.teardownFrame();break;case"checkout.cancel":this.settled=!0,this.events.emit("cancel",void 0),this.teardownFrame();break;case"checkout.error":this.settled=!0,this.events.emit("error",{code:t.code,message:t.message}),this.teardownFrame();break;case"checkout.resize":this.iframeManager?.setHeight(t.height);break}},this.config=L(e),this.containerOption=e.container}start(e){if(this.config.mode==="redirect"){this.redirect();return}if(this.config.mode==="modal"){this.open();return}this.mount(e??this.containerOption)}mount(e){this.mountFrame("inline",e??this.containerOption)}open(){this.mountFrame("modal",document.body)}redirect(){window.location.assign(this.getCheckoutUrl("redirect"))}on(e,t){return this.events.on(e,t)}close(){const e=this.mounted;this.teardownFrame(),e&&!this.settled&&(this.settled=!0,this.events.emit("cancel",void 0))}getCheckoutUrl(e){return T({intentId:this.config.intentId,clientToken:this.config.clientToken,checkoutOrigin:this.config.checkoutOrigin,mode:e,returnUrl:this.config.returnUrl,parentOrigin:e==="redirect"?void 0:window.location.origin})}mountFrame(e,t){if(this.mounted)throw new Error("Checkout is already mounted.");if(!t)throw new Error("Checkout container was not found.");const n=e==="modal"?document.body:v(t);if(!n)throw new Error("Checkout container was not found.");const o=this.getCheckoutUrl(e);this.iframeManager=new b({checkoutId:this.config.intentId,checkoutUrl:o,checkoutOrigin:this.config.checkoutOrigin,presentation:e,onMessage:this.handleMessage,onCloseRequest:()=>this.close()}),this.iframeManager.mount(n),this.mounted=!0}teardownFrame(){this.iframeManager?.destroy(),this.iframeManager=null,this.mounted=!1}}s.Checkout=S,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
export declare const CHECKOUT_MESSAGE_SOURCE = "commandtransfer.checkout";
|
|
2
|
+
export declare const CLIENT_TOKEN_PREFIX = "ctc_";
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
export declare const DEFAULT_CHECKOUT_ORIGIN = "https://checkout.commandtransfer.com";
|
|
7
|
-
/**
|
|
8
|
-
* Extra exact hostnames that may serve checkout (Firebase, Cloud Run, etc.).
|
|
9
|
-
* Add hosts here when production hosting is chosen.
|
|
10
|
-
*/
|
|
11
|
-
export declare const CHECKOUT_HOSTS: readonly string[];
|
|
12
|
-
/**
|
|
13
|
-
* Allowed checkout host suffixes. Any subdomain of these is accepted.
|
|
4
|
+
* Hosted Checkout Web origin.
|
|
5
|
+
* Set `VITE_CHECKOUT_ORIGIN` when building the SDK.
|
|
14
6
|
*/
|
|
15
|
-
export declare const
|
|
16
|
-
export declare const CLIENT_TOKEN_PREFIX = "ctc_";
|
|
7
|
+
export declare const CHECKOUT_ORIGIN: string;
|
|
17
8
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,6BAA6B,CAAC;AAElE
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,6BAA6B,CAAC;AAElE,eAAO,MAAM,mBAAmB,SAAS,CAAC;AAM1C;;;GAGG;AACH,eAAO,MAAM,eAAe,QAE3B,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -11,12 +11,6 @@ export interface CheckoutConfig {
|
|
|
11
11
|
mode?: CheckoutMode;
|
|
12
12
|
/** Required for `mode: "inline"` when calling `start()`. */
|
|
13
13
|
container?: string | HTMLElement;
|
|
14
|
-
/**
|
|
15
|
-
* Hosted checkout origin. Production merchants should omit this.
|
|
16
|
-
* Pass a host Command Transfer gives you for staging.
|
|
17
|
-
* @default https://checkout.commandtransfer.com
|
|
18
|
-
*/
|
|
19
|
-
checkoutOrigin?: string;
|
|
20
14
|
/**
|
|
21
15
|
* After a hosted (redirect) payment, send the customer here.
|
|
22
16
|
* Ignored for inline/modal — handle `success` / `cancel` events instead.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,OAAO,GAAG,UAAU,CAAC;AAE3D,MAAM,WAAW,cAAc;IAC7B,wEAAwE;IACxE,QAAQ,EAAE,MAAM,CAAC;IAEjB,+DAA+D;IAC/D,WAAW,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,IAAI,CAAC,EAAE,YAAY,CAAC;IAEpB,4DAA4D;IAC5D,SAAS,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC;IAEjC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,OAAO,GAAG,UAAU,CAAC;AAE3D,MAAM,WAAW,cAAc;IAC7B,wEAAwE;IACxE,QAAQ,EAAE,MAAM,CAAC;IAEjB,+DAA+D;IAC/D,WAAW,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,IAAI,CAAC,EAAE,YAAY,CAAC;IAEpB,4DAA4D;IAC5D,SAAS,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC;IAEjC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,aAAa,CAAC;IACrB,OAAO,EAAE,eAAe,CAAC;IACzB,MAAM,EAAE,SAAS,CAAC;IAClB,KAAK,EAAE,aAAa,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAC3B,gBAAgB,GAChB,kBAAkB,GAClB,iBAAiB,GACjB,gBAAgB,GAChB,iBAAiB,CAAC;AAEtB,MAAM,MAAM,eAAe,GACvB;IACE,MAAM,EAAE,0BAA0B,CAAC;IACnC,IAAI,EAAE,gBAAgB,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;CACpB,GACD;IACE,MAAM,EAAE,0BAA0B,CAAC;IACnC,IAAI,EAAE,kBAAkB,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB,GACD;IACE,MAAM,EAAE,0BAA0B,CAAC;IACnC,IAAI,EAAE,iBAAiB,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;CACpB,GACD;IACE,MAAM,EAAE,0BAA0B,CAAC;IACnC,IAAI,EAAE,gBAAgB,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,GACD;IACE,MAAM,EAAE,0BAA0B,CAAC;IACnC,IAAI,EAAE,iBAAiB,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC"}
|
package/dist/url.d.ts
CHANGED
|
@@ -3,14 +3,9 @@ import type { CheckoutConfig, CheckoutMode } from "./types";
|
|
|
3
3
|
export declare function isLocalHostname(hostname: string): boolean;
|
|
4
4
|
/** Merchant sites on loopback, LAN, or local-dev TLDs. */
|
|
5
5
|
export declare function isMerchantDevHostname(hostname: string): boolean;
|
|
6
|
-
/**
|
|
7
|
-
* Hosts that may serve Command Transfer checkout.
|
|
8
|
-
* Localhost for local apps; otherwise the list in `constants.ts`.
|
|
9
|
-
*/
|
|
10
|
-
export declare function isAllowedCheckoutHost(hostname: string): boolean;
|
|
11
6
|
export declare function normalizeIntentId(value: string): string;
|
|
12
7
|
export declare function normalizeClientToken(value: string): string;
|
|
13
|
-
export declare function resolveCheckoutOrigin(
|
|
8
|
+
export declare function resolveCheckoutOrigin(): string;
|
|
14
9
|
/** Merchant return / parent URLs — any https site, or http on local/LAN. */
|
|
15
10
|
export declare function assertSafeHttpUrl(value: string, field: string): string;
|
|
16
11
|
export declare function buildCheckoutUrl(options: {
|
package/dist/url.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../src/url.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../src/url.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAQ5D,gFAAgF;AAChF,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAGzD;AAkBD,0DAA0D;AAC1D,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAQ/D;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMvD;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAiB1D;AA6BD,wBAAgB,qBAAqB,IAAI,MAAM,CAU9C;AAED,4EAA4E;AAC5E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtE;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IACxC,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,OAAO,CAAC,YAAY,EAAE,UAAU,CAAC,GAAG,UAAU,CAAC;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,GAAG,MAAM,CAiBT;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,GAAG;IACtD,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,YAAY,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CA6BA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@integralscope/command-transfer-checkout-js",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Command Transfer Checkout Web SDK",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -37,4 +37,4 @@
|
|
|
37
37
|
"registry": "https://registry.npmjs.org",
|
|
38
38
|
"access": "public"
|
|
39
39
|
}
|
|
40
|
-
}
|
|
40
|
+
}
|