@integralscope/command-transfer-checkout-js 0.1.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/README.md ADDED
@@ -0,0 +1,313 @@
1
+ # Command Transfer Checkout JS
2
+
3
+ Merchant Web SDK. Customers pay on your page (inline or modal) or on Command Transfer’s hosted page (redirect).
4
+
5
+ The SDK is not proof of payment. Confirm the checkout on **your server** with the Command Transfer API before you fulfill an order.
6
+
7
+ ## How it fits together
8
+
9
+ ```
10
+ Customer clicks Pay
11
+ │
12
+ ▼
13
+ Your backend creates a checkout
14
+ (secret API key stays on the server)
15
+ │
16
+ ▼
17
+ Browser receives { intentId, clientToken }
18
+ │
19
+ ▼
20
+ new Checkout(...) → mount / open / redirect
21
+ │
22
+ ▼
23
+ Hosted checkout on Command Transfer (not your domain)
24
+ │
25
+ ▼
26
+ success event or return_url query
27
+ │
28
+ ▼
29
+ Your backend verifies the intent, then you fulfill
30
+ ```
31
+
32
+ Your site never talks to the pay UI as source code. The SDK only opens Command Transfer checkout and reports what happened.
33
+
34
+ ## 1. Create the checkout on your server
35
+
36
+ When the customer is ready to pay, your backend creates a checkout with your **secret API key**. Return only the public session to the browser:
37
+
38
+ ```json
39
+ {
40
+ "intentId": "intent_123",
41
+ "clientToken": "ctc_..."
42
+ }
43
+ ```
44
+
45
+ Do not put the secret key in frontend env, HTML, or the SDK.
46
+
47
+ Example merchant endpoint:
48
+
49
+ ```js
50
+ // server
51
+ app.post("/api/checkout", async (req, res) => {
52
+ const session = await createCommandTransferCheckout({
53
+ amount: 450000,
54
+ currency: "NGN",
55
+ // ...your order fields
56
+ });
57
+
58
+ res.json({
59
+ intentId: session.id,
60
+ clientToken: session.client_token,
61
+ });
62
+ });
63
+ ```
64
+
65
+ The browser then:
66
+
67
+ ```js
68
+ const session = await fetch("/api/checkout", { method: "POST" }).then((r) =>
69
+ r.json(),
70
+ );
71
+ ```
72
+
73
+ ## 2. Install the SDK
74
+
75
+ ```bash
76
+ npm install @integralscope/command-transfer-checkout-js
77
+ ```
78
+
79
+ Or a script tag:
80
+
81
+ ```html
82
+ <script src="https://unpkg.com/@integralscope/command-transfer-checkout-js/dist/commandtransfer-checkout.umd.cjs"></script>
83
+ ```
84
+
85
+ ```js
86
+ const { Checkout } = window.CommandTransferCheckout;
87
+ ```
88
+
89
+ ## 3. Start checkout in the browser
90
+
91
+ ```js
92
+ import { Checkout } from "@integralscope/command-transfer-checkout-js";
93
+
94
+ const checkout = new Checkout({
95
+ intentId: session.intentId,
96
+ clientToken: session.clientToken,
97
+ });
98
+
99
+ checkout.on("success", ({ intentId, reference }) => {
100
+ // Ask your backend to verify intentId, then fulfill.
101
+ });
102
+
103
+ checkout.on("cancel", () => {
104
+ // Customer closed checkout without paying.
105
+ });
106
+
107
+ checkout.on("error", ({ code, message }) => {
108
+ // Expired, unauthorized, or failed to load.
109
+ });
110
+ ```
111
+
112
+ Production merchants omit `checkoutOrigin`. The SDK uses Command Transfer’s hosted checkout (`DEFAULT_CHECKOUT_ORIGIN` in the SDK). Pass `checkoutOrigin` only if Command Transfer gives you a different host.
113
+
114
+ ### Inline
115
+
116
+ Embed checkout in an element on your page:
117
+
118
+ ```html
119
+ <div id="checkout"></div>
120
+ ```
121
+
122
+ ```js
123
+ checkout.mount("#checkout");
124
+ ```
125
+
126
+ `mount("checkout")` also works if that is the element id. You can pass the DOM node itself.
127
+
128
+ ### Modal
129
+
130
+ Open checkout in an overlay on your page:
131
+
132
+ ```js
133
+ checkout.open();
134
+ ```
135
+
136
+ ### Redirect
137
+
138
+ Send the customer to hosted checkout, then back to your site:
139
+
140
+ ```js
141
+ const checkout = new Checkout({
142
+ intentId: session.intentId,
143
+ clientToken: session.clientToken,
144
+ mode: "redirect",
145
+ returnUrl: "https://merchant.example/order/complete",
146
+ });
147
+
148
+ checkout.redirect();
149
+ // or checkout.start() when mode is "redirect"
150
+ ```
151
+
152
+ On completion they land on `returnUrl` with query params:
153
+
154
+ | Param | Meaning |
155
+ | -------------- | -------------------------------------------- |
156
+ | `checkout_id` | The checkout you created |
157
+ | `reference` | Present when payment succeeded |
158
+ | `status` | `success` or an error code |
159
+
160
+ Treat those params as a hint. Confirm the intent on your server before fulfilling.
161
+
162
+ `returnUrl` must be `https` on a public host. `http` is allowed on localhost and LAN for development.
163
+
164
+ ### `start()`
165
+
166
+ Uses `mode` from the constructor:
167
+
168
+ | `mode` | What `start()` does |
169
+ | ---------- | -------------------------------- |
170
+ | `inline` (default) | `mount()` — needs `container` |
171
+ | `modal` | `open()` |
172
+ | `redirect` | `redirect()` |
173
+
174
+ ```js
175
+ const checkout = new Checkout({
176
+ intentId: session.intentId,
177
+ clientToken: session.clientToken,
178
+ mode: "modal",
179
+ });
180
+
181
+ checkout.start();
182
+ ```
183
+
184
+ ### Close
185
+
186
+ ```js
187
+ checkout.close();
188
+ ```
189
+
190
+ If the customer had not finished paying, this emits `cancel`.
191
+
192
+ `on()` returns an unsubscribe function.
193
+
194
+ ## 4. Confirm payment on your server
195
+
196
+ After `success` or a redirect return:
197
+
198
+ 1. Send `intentId` to your backend.
199
+ 2. Fetch the checkout from Command Transfer with your secret key.
200
+ 3. Fulfill only when status is `SUCCEEDED`.
201
+ 4. Ignore a client `success` event that your API does not confirm.
202
+
203
+ ```js
204
+ checkout.on("success", async ({ intentId }) => {
205
+ const result = await fetch("/api/orders/confirm", {
206
+ method: "POST",
207
+ headers: { "Content-Type": "application/json" },
208
+ body: JSON.stringify({ intentId }),
209
+ }).then((r) => r.json());
210
+
211
+ if (result.paid) {
212
+ window.location.assign("/order/thank-you");
213
+ }
214
+ });
215
+ ```
216
+
217
+ ## Config
218
+
219
+ | Field | Required | Notes |
220
+ | ---------------- | -------- | ----- |
221
+ | `intentId` | yes | Intent id from your backend |
222
+ | `clientToken` | yes | Payer token (`ctc_…`) from your backend |
223
+ | `mode` | no | `inline` (default), `modal`, or `redirect` |
224
+ | `container` | no | Used by `start()` when `mode` is `inline` |
225
+ | `checkoutOrigin` | no | Staging host from Command Transfer. Omit in production. |
226
+ | `returnUrl` | no | Redirect return URL. Ignored for inline/modal. |
227
+
228
+ ## Events
229
+
230
+ | Event | Payload | When |
231
+ | --------- | ------------------------------- | ---- |
232
+ | `ready` | `{ intentId }` | Hosted checkout loaded |
233
+ | `success` | `{ intentId, reference }` | Intent succeeded (verify on your server) |
234
+ | `cancel` | — | Customer closed checkout |
235
+ | `error` | `{ code, message }` | Expired, failed, or could not load |
236
+
237
+ ## Staging
238
+
239
+ Use a Command Transfer checkout host, not your storefront origin:
240
+
241
+ ```js
242
+ new Checkout({
243
+ intentId: session.intentId,
244
+ 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
+ });
257
+ ```
258
+
259
+ If you set a Content-Security-Policy on your site, allow Command Transfer’s checkout origin in `frame-src`.
260
+
261
+ ## Rules
262
+
263
+ - Secret API keys stay on your server.
264
+ - `clientToken` is a payer capability for that checkout. Do not log it, put it in query strings, or persist it in `localStorage`.
265
+ - `success` in the browser is UX only. Fulfill from server-side confirmation.
266
+ - Do not point `checkoutOrigin` at your own domain.
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.
@@ -0,0 +1,28 @@
1
+ import type { CheckoutConfig, CheckoutEventMap } from "./types";
2
+ export declare class Checkout {
3
+ private readonly config;
4
+ private readonly containerOption?;
5
+ private readonly events;
6
+ private iframeManager;
7
+ private mounted;
8
+ private settled;
9
+ constructor(config: CheckoutConfig);
10
+ /**
11
+ * Start checkout using `config.mode`.
12
+ * `inline` needs a container, `modal` opens an overlay, `redirect` leaves the page.
13
+ */
14
+ start(container?: string | HTMLElement): void;
15
+ /** Embed checkout in a merchant-page element. */
16
+ mount(container?: string | HTMLElement): void;
17
+ /** Open checkout in an overlay on the merchant page. */
18
+ open(): void;
19
+ /** Send the customer to the hosted checkout page. */
20
+ redirect(): void;
21
+ on<K extends keyof CheckoutEventMap>(event: K, handler: (data: CheckoutEventMap[K]) => void): () => void;
22
+ close(): void;
23
+ private getCheckoutUrl;
24
+ private mountFrame;
25
+ private teardownFrame;
26
+ private handleMessage;
27
+ }
28
+ //# sourceMappingURL=Checkout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Checkout.d.ts","sourceRoot":"","sources":["../src/Checkout.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAmB,MAAM,SAAS,CAAC;AAKjF,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmB;IAC1C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAuB;IACxD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwC;IAC/D,OAAO,CAAC,aAAa,CAA8B;IACnD,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;gBAEZ,MAAM,EAAE,cAAc;IAKlC;;;OAGG;IACH,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI;IAc7C,iDAAiD;IACjD,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI;IAI7C,wDAAwD;IACxD,IAAI,IAAI,IAAI;IAIZ,qDAAqD;IACrD,QAAQ,IAAI,IAAI;IAIhB,EAAE,CAAC,CAAC,SAAS,MAAM,gBAAgB,EACjC,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAC,KAAK,IAAI,GAC3C,MAAM,IAAI;IAIb,KAAK,IAAI,IAAI;IAUb,OAAO,CAAC,cAAc;IAYtB,OAAO,CAAC,UAAU;IAmClB,OAAO,CAAC,aAAa;IAMrB,OAAO,CAAC,aAAa,CAkCnB;CACH"}
@@ -0,0 +1,388 @@
1
+ class d {
2
+ constructor() {
3
+ this.listeners = /* @__PURE__ */ new Map();
4
+ }
5
+ on(e, t) {
6
+ let n = this.listeners.get(e);
7
+ return n || (n = /* @__PURE__ */ new Set(), this.listeners.set(e, n)), n.add(t), () => {
8
+ n?.delete(t);
9
+ };
10
+ }
11
+ emit(e, t) {
12
+ const n = this.listeners.get(e);
13
+ n && [...n].forEach((o) => {
14
+ try {
15
+ o(t);
16
+ } catch (i) {
17
+ console.error(`Checkout "${String(e)}" handler failed.`, i);
18
+ }
19
+ });
20
+ }
21
+ removeAllListeners() {
22
+ this.listeners.clear();
23
+ }
24
+ }
25
+ const s = "commandtransfer.checkout", m = "https://checkout.commandtransfer.com", f = ["commandtransfer.com"], w = [
26
+ ".commandtransfer.com"
27
+ ], h = "ctc_", g = /* @__PURE__ */ new Set([
28
+ "checkout.ready",
29
+ "checkout.success",
30
+ "checkout.cancel",
31
+ "checkout.error",
32
+ "checkout.resize"
33
+ ]);
34
+ function k(r) {
35
+ return !!r && typeof r == "object" && !Array.isArray(r);
36
+ }
37
+ function c(r, e) {
38
+ if (typeof r != "string") return null;
39
+ const t = r.trim();
40
+ return !t || t.length > e || /[\u0000-\u001f\u007f]/.test(t) ? null : t;
41
+ }
42
+ function p(r, e) {
43
+ if (!k(r) || r.source !== s)
44
+ return null;
45
+ const t = c(r.type, 64);
46
+ if (!t || !g.has(t))
47
+ return null;
48
+ const n = c(r.checkoutId, 128);
49
+ if (n !== e)
50
+ return null;
51
+ switch (t) {
52
+ case "checkout.ready":
53
+ return {
54
+ source: s,
55
+ type: t,
56
+ checkoutId: n
57
+ };
58
+ case "checkout.success": {
59
+ const o = c(r.reference, 128);
60
+ return o ? {
61
+ source: s,
62
+ type: t,
63
+ checkoutId: n,
64
+ reference: o
65
+ } : null;
66
+ }
67
+ case "checkout.cancel":
68
+ return {
69
+ source: s,
70
+ type: t,
71
+ checkoutId: n
72
+ };
73
+ case "checkout.error": {
74
+ const o = c(r.code, 64), i = c(r.message, 280);
75
+ return !o || !i ? null : {
76
+ source: s,
77
+ type: t,
78
+ checkoutId: n,
79
+ code: o,
80
+ message: i
81
+ };
82
+ }
83
+ case "checkout.resize": {
84
+ const o = typeof r.height == "number" ? r.height : Number(r.height);
85
+ return !Number.isFinite(o) || o < 200 || o > 4e3 ? null : {
86
+ source: s,
87
+ type: t,
88
+ checkoutId: n,
89
+ height: Math.round(o)
90
+ };
91
+ }
92
+ default:
93
+ return null;
94
+ }
95
+ }
96
+ class y {
97
+ constructor(e) {
98
+ this.sourceWindow = null, this.handleMessage = (t) => {
99
+ if (t.origin !== this.checkoutOrigin || !this.sourceWindow || t.source !== this.sourceWindow)
100
+ return;
101
+ const n = p(t.data, this.checkoutId);
102
+ n && this.onMessage(n);
103
+ }, this.checkoutId = e.checkoutId, this.checkoutOrigin = e.checkoutOrigin, this.onMessage = e.onMessage, window.addEventListener("message", this.handleMessage);
104
+ }
105
+ setSourceWindow(e) {
106
+ this.sourceWindow = e;
107
+ }
108
+ destroy() {
109
+ window.removeEventListener("message", this.handleMessage), this.sourceWindow = null;
110
+ }
111
+ }
112
+ class b {
113
+ 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 y({
115
+ checkoutId: e.checkoutId,
116
+ checkoutOrigin: e.checkoutOrigin,
117
+ onMessage: e.onMessage
118
+ });
119
+ }
120
+ mount(e) {
121
+ if (this.iframe)
122
+ throw new Error("Checkout iframe is already mounted.");
123
+ const t = this.createIframe();
124
+ this.presentation === "modal" ? this.mountModal(t) : e.appendChild(t), this.iframe = t, this.bridge.setSourceWindow(t.contentWindow), t.addEventListener("load", () => {
125
+ this.bridge.setSourceWindow(t.contentWindow);
126
+ });
127
+ }
128
+ setHeight(e) {
129
+ this.iframe && (this.iframe.style.height = `${Math.round(e)}px`);
130
+ }
131
+ destroy() {
132
+ this.bridge.destroy(), this.iframe?.remove(), this.iframe = null, this.teardownModal();
133
+ }
134
+ createIframe() {
135
+ const e = document.createElement("iframe");
136
+ return e.src = this.checkoutUrl, e.title = "Command Transfer checkout", e.setAttribute(
137
+ "allow",
138
+ "payment; clipboard-write; publickey-credentials-get"
139
+ ), e.setAttribute("referrerpolicy", "no-referrer"), e.setAttribute(
140
+ "sandbox",
141
+ [
142
+ "allow-scripts",
143
+ "allow-same-origin",
144
+ "allow-forms",
145
+ "allow-popups",
146
+ "allow-popups-to-escape-sandbox",
147
+ "allow-top-navigation-by-user-activation"
148
+ ].join(" ")
149
+ ), 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;
150
+ }
151
+ mountModal(e) {
152
+ const t = document.createElement("div");
153
+ t.setAttribute("data-ct-checkout", "overlay"), t.setAttribute("role", "dialog"), t.setAttribute("aria-modal", "true"), t.setAttribute("aria-label", "Payment checkout"), t.style.cssText = [
154
+ "position:fixed",
155
+ "inset:0",
156
+ "z-index:2147483647",
157
+ "display:flex",
158
+ "align-items:center",
159
+ "justify-content:center",
160
+ "padding:24px 16px",
161
+ "background:rgba(15,12,41,0.55)"
162
+ ].join(";");
163
+ const n = document.createElement("div");
164
+ n.style.cssText = [
165
+ "position:relative",
166
+ "width:min(420px,100%)",
167
+ "max-height:calc(100vh - 48px)",
168
+ "overflow:auto",
169
+ "border-radius:16px",
170
+ "box-shadow:0 24px 64px rgba(15,12,41,0.28)"
171
+ ].join(";");
172
+ const o = document.createElement("button");
173
+ o.type = "button", o.setAttribute("aria-label", "Close checkout"), o.textContent = "×", o.style.cssText = [
174
+ "position:absolute",
175
+ "top:8px",
176
+ "right:8px",
177
+ "z-index:1",
178
+ "width:32px",
179
+ "height:32px",
180
+ "border:0",
181
+ "border-radius:999px",
182
+ "background:#ffffff",
183
+ "color:#1e1654",
184
+ "font-size:22px",
185
+ "line-height:1",
186
+ "cursor:pointer",
187
+ "box-shadow:0 2px 8px rgba(15,12,41,0.16)"
188
+ ].join(";"), o.addEventListener("click", this.onCloseRequest), t.addEventListener("click", (i) => {
189
+ i.target === t && this.onCloseRequest();
190
+ }), this.escapeListener = (i) => {
191
+ i.key === "Escape" && this.onCloseRequest();
192
+ }, 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();
193
+ }
194
+ teardownModal() {
195
+ 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
+ }
197
+ }
198
+ function v(r) {
199
+ if (typeof r != "string")
200
+ return r;
201
+ const e = r.trim();
202
+ return e ? e.startsWith("#") || e.startsWith(".") || e.startsWith("[") ? document.querySelector(e) : document.getElementById(e) ?? document.querySelector(e) : null;
203
+ }
204
+ const C = /^[A-Za-z0-9._:-]{1,128}$/;
205
+ function u(r) {
206
+ return r.replace(/^\[|\]$/g, "").toLowerCase();
207
+ }
208
+ function a(r) {
209
+ const e = u(r);
210
+ return e === "localhost" || e === "127.0.0.1" || e === "::1";
211
+ }
212
+ function E(r) {
213
+ const e = r.split(".");
214
+ if (e.length !== 4) return !1;
215
+ const t = e.map((i) => Number(i));
216
+ if (t.some((i) => !Number.isInteger(i) || i < 0 || i > 255))
217
+ return !1;
218
+ const [n, o] = t;
219
+ return n === 10 || n === 127 || n === 192 && o === 168 || n === 172 && o >= 16 && o <= 31;
220
+ }
221
+ function I(r) {
222
+ const e = u(r);
223
+ return a(e) || E(e) ? !0 : e.endsWith(".local") || e.endsWith(".test") || e.endsWith(".localhost");
224
+ }
225
+ function O(r) {
226
+ const e = u(r);
227
+ return a(e) || f.includes(e) ? !0 : w.some((t) => e.endsWith(t));
228
+ }
229
+ function x(r) {
230
+ const e = r.trim();
231
+ if (!C.test(e))
232
+ throw new Error("intentId is invalid.");
233
+ return e;
234
+ }
235
+ function U(r) {
236
+ const e = r.trim(), t = e.toLowerCase().startsWith("bearer ") ? e.slice(7).trim() : e;
237
+ if (!t.toLowerCase().startsWith(h))
238
+ throw new Error(
239
+ `clientToken is invalid. Expected a token starting with ${h}.`
240
+ );
241
+ if (t.length > 512 || /\s/.test(t))
242
+ throw new Error("clientToken is invalid.");
243
+ return t;
244
+ }
245
+ function l(r, e, t) {
246
+ let n;
247
+ try {
248
+ n = new URL(r);
249
+ } catch {
250
+ throw new Error(`${e} is not a valid URL.`);
251
+ }
252
+ if (n.username || n.password)
253
+ throw new Error(`${e} must not include credentials.`);
254
+ if (n.protocol === "https:" || n.protocol === "http:" && t(n.hostname))
255
+ return n;
256
+ throw new Error(`${e} must be an https URL.`);
257
+ }
258
+ function M(r) {
259
+ const e = l(
260
+ (r ?? m).trim(),
261
+ "checkoutOrigin",
262
+ a
263
+ );
264
+ if (!O(e.hostname))
265
+ throw new Error("checkoutOrigin is not an allowed Command Transfer host.");
266
+ return e.origin;
267
+ }
268
+ function T(r, e) {
269
+ return l(r, e, I).toString();
270
+ }
271
+ function L(r) {
272
+ const e = encodeURIComponent(r.intentId), t = new URL(`/pay/${e}`, r.checkoutOrigin);
273
+ 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
+ }
275
+ function S(r) {
276
+ if (!r || typeof r != "object")
277
+ throw new Error("Checkout config is required.");
278
+ if (!r.intentId?.trim())
279
+ throw new Error("intentId is required.");
280
+ const e = x(r.intentId), t = U(r.clientToken ?? ""), n = M(r.checkoutOrigin), o = r.mode ?? "inline";
281
+ if (o !== "inline" && o !== "modal" && o !== "redirect")
282
+ throw new Error('mode must be "inline", "modal", or "redirect".');
283
+ const i = r.returnUrl ? T(r.returnUrl, "returnUrl") : void 0;
284
+ return {
285
+ intentId: e,
286
+ clientToken: t,
287
+ checkoutOrigin: n,
288
+ mode: o,
289
+ returnUrl: i
290
+ };
291
+ }
292
+ class W {
293
+ constructor(e) {
294
+ this.events = new d(), this.iframeManager = null, this.mounted = !1, this.settled = !1, this.handleMessage = (t) => {
295
+ switch (t.type) {
296
+ case "checkout.ready":
297
+ this.events.emit("ready", { intentId: t.checkoutId });
298
+ break;
299
+ case "checkout.success":
300
+ this.settled = !0, this.events.emit("success", {
301
+ intentId: t.checkoutId,
302
+ reference: t.reference
303
+ }), this.teardownFrame();
304
+ break;
305
+ case "checkout.cancel":
306
+ this.settled = !0, this.events.emit("cancel", void 0), this.teardownFrame();
307
+ break;
308
+ case "checkout.error":
309
+ this.settled = !0, this.events.emit("error", {
310
+ code: t.code,
311
+ message: t.message
312
+ }), this.teardownFrame();
313
+ break;
314
+ case "checkout.resize":
315
+ this.iframeManager?.setHeight(t.height);
316
+ break;
317
+ }
318
+ }, this.config = S(e), this.containerOption = e.container;
319
+ }
320
+ /**
321
+ * Start checkout using `config.mode`.
322
+ * `inline` needs a container, `modal` opens an overlay, `redirect` leaves the page.
323
+ */
324
+ start(e) {
325
+ if (this.config.mode === "redirect") {
326
+ this.redirect();
327
+ return;
328
+ }
329
+ if (this.config.mode === "modal") {
330
+ this.open();
331
+ return;
332
+ }
333
+ this.mount(e ?? this.containerOption);
334
+ }
335
+ /** Embed checkout in a merchant-page element. */
336
+ mount(e) {
337
+ this.mountFrame("inline", e ?? this.containerOption);
338
+ }
339
+ /** Open checkout in an overlay on the merchant page. */
340
+ open() {
341
+ this.mountFrame("modal", document.body);
342
+ }
343
+ /** Send the customer to the hosted checkout page. */
344
+ redirect() {
345
+ window.location.assign(this.getCheckoutUrl("redirect"));
346
+ }
347
+ on(e, t) {
348
+ return this.events.on(e, t);
349
+ }
350
+ close() {
351
+ const e = this.mounted;
352
+ this.teardownFrame(), e && !this.settled && (this.settled = !0, this.events.emit("cancel", void 0));
353
+ }
354
+ getCheckoutUrl(e) {
355
+ return L({
356
+ intentId: this.config.intentId,
357
+ clientToken: this.config.clientToken,
358
+ checkoutOrigin: this.config.checkoutOrigin,
359
+ mode: e,
360
+ returnUrl: this.config.returnUrl,
361
+ parentOrigin: e === "redirect" ? void 0 : window.location.origin
362
+ });
363
+ }
364
+ mountFrame(e, t) {
365
+ if (this.mounted)
366
+ throw new Error("Checkout is already mounted.");
367
+ if (!t)
368
+ throw new Error("Checkout container was not found.");
369
+ const n = e === "modal" ? document.body : v(t);
370
+ if (!n)
371
+ throw new Error("Checkout container was not found.");
372
+ const o = this.getCheckoutUrl(e);
373
+ this.iframeManager = new b({
374
+ checkoutId: this.config.intentId,
375
+ checkoutUrl: o,
376
+ checkoutOrigin: this.config.checkoutOrigin,
377
+ presentation: e,
378
+ onMessage: this.handleMessage,
379
+ onCloseRequest: () => this.close()
380
+ }), this.iframeManager.mount(n), this.mounted = !0;
381
+ }
382
+ teardownFrame() {
383
+ this.iframeManager?.destroy(), this.iframeManager = null, this.mounted = !1;
384
+ }
385
+ }
386
+ export {
387
+ W as Checkout
388
+ };
@@ -0,0 +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",m="https://checkout.commandtransfer.com",w=["commandtransfer.com"],g=[".commandtransfer.com"],d="ctc_",k=new Set(["checkout.ready","checkout.success","checkout.cancel","checkout.error","checkout.resize"]);function p(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 y(r,e){if(!p(r)||r.source!==c)return null;const t=a(r.type,64);if(!t||!k.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 b{constructor(e){this.sourceWindow=null,this.handleMessage=t=>{if(t.origin!==this.checkoutOrigin||!this.sourceWindow||t.source!==this.sourceWindow)return;const n=y(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 v{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 b({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 C(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 E=/^[A-Za-z0-9._:-]{1,128}$/;function h(r){return r.replace(/^\[|\]$/g,"").toLowerCase()}function l(r){const e=h(r);return e==="localhost"||e==="127.0.0.1"||e==="::1"}function I(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 O(r){const e=h(r);return l(e)||I(e)?!0:e.endsWith(".local")||e.endsWith(".test")||e.endsWith(".localhost")}function x(r){const e=h(r);return l(e)||w.includes(e)?!0:g.some(t=>e.endsWith(t))}function M(r){const e=r.trim();if(!E.test(e))throw new Error("intentId is invalid.");return e}function U(r){const e=r.trim(),t=e.toLowerCase().startsWith("bearer ")?e.slice(7).trim():e;if(!t.toLowerCase().startsWith(d))throw new Error(`clientToken is invalid. Expected a token starting with ${d}.`);if(t.length>512||/\s/.test(t))throw new Error("clientToken is invalid.");return t}function f(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 T(r){const e=f((r??m).trim(),"checkoutOrigin",l);if(!x(e.hostname))throw new Error("checkoutOrigin is not an allowed Command Transfer host.");return e.origin}function S(r,e){return f(r,e,O).toString()}function L(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 W(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=M(r.intentId),t=U(r.clientToken??""),n=T(r.checkoutOrigin),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?S(r.returnUrl,"returnUrl"):void 0;return{intentId:e,clientToken:t,checkoutOrigin:n,mode:o,returnUrl:i}}class R{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=W(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 L({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:C(t);if(!n)throw new Error("Checkout container was not found.");const o=this.getCheckoutUrl(e);this.iframeManager=new v({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=R,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
@@ -0,0 +1,17 @@
1
+ export declare const CHECKOUT_MESSAGE_SOURCE = "commandtransfer.checkout";
2
+ /**
3
+ * Production default until the hosted checkout domain is finalized.
4
+ * Change this in one place; merchants omit `checkoutOrigin` in production.
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.
14
+ */
15
+ export declare const CHECKOUT_HOST_SUFFIXES: readonly string[];
16
+ export declare const CLIENT_TOKEN_PREFIX = "ctc_";
17
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,6BAA6B,CAAC;AAElE;;;GAGG;AACH,eAAO,MAAM,uBAAuB,yCAAyC,CAAC;AAE9E;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,SAAS,MAAM,EAA4B,CAAC;AAEzE;;GAEG;AACH,eAAO,MAAM,sBAAsB,EAAE,SAAS,MAAM,EAEnD,CAAC;AAEF,eAAO,MAAM,mBAAmB,SAAS,CAAC"}
package/dist/dom.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function resolveContainer(container: string | HTMLElement): HTMLElement | null;
2
+ //# sourceMappingURL=dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AAAA,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,MAAM,GAAG,WAAW,GAC9B,WAAW,GAAG,IAAI,CAmBpB"}
@@ -0,0 +1,8 @@
1
+ export type EventHandler<T> = (data: T) => void;
2
+ export declare class EventEmitter<Events extends Record<string, unknown>> {
3
+ private listeners;
4
+ on<K extends keyof Events>(event: K, handler: EventHandler<Events[K]>): () => void;
5
+ emit<K extends keyof Events>(event: K, data: Events[K]): void;
6
+ removeAllListeners(): void;
7
+ }
8
+ //# sourceMappingURL=EventEmitter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventEmitter.d.ts","sourceRoot":"","sources":["../../src/events/EventEmitter.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC;AAEhD,qBAAa,YAAY,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC9D,OAAO,CAAC,SAAS,CAAuD;IAExE,EAAE,CAAC,CAAC,SAAS,MAAM,MAAM,EACvB,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAC/B,MAAM,IAAI;IAeb,IAAI,CAAC,CAAC,SAAS,MAAM,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI;IAe7D,kBAAkB,IAAI,IAAI;CAG3B"}
@@ -0,0 +1,28 @@
1
+ import type { CheckoutMessage } from "../types";
2
+ interface IframeManagerOptions {
3
+ checkoutId: string;
4
+ checkoutUrl: string;
5
+ checkoutOrigin: string;
6
+ presentation: "inline" | "modal";
7
+ onMessage: (message: CheckoutMessage) => void;
8
+ onCloseRequest: () => void;
9
+ }
10
+ export declare class IframeManager {
11
+ private iframe;
12
+ private overlay;
13
+ private previousBodyOverflow;
14
+ private escapeListener;
15
+ private readonly checkoutUrl;
16
+ private readonly presentation;
17
+ private readonly onCloseRequest;
18
+ private readonly bridge;
19
+ constructor(options: IframeManagerOptions);
20
+ mount(container: HTMLElement): void;
21
+ setHeight(height: number): void;
22
+ destroy(): void;
23
+ private createIframe;
24
+ private mountModal;
25
+ private teardownModal;
26
+ }
27
+ export {};
28
+ //# sourceMappingURL=IframeManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IframeManager.d.ts","sourceRoot":"","sources":["../../src/iframe/IframeManager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,UAAU,oBAAoB;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,QAAQ,GAAG,OAAO,CAAC;IACjC,SAAS,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;IAC9C,cAAc,EAAE,MAAM,IAAI,CAAC;CAC5B;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,OAAO,CAA4B;IAC3C,OAAO,CAAC,oBAAoB,CAAuB;IACnD,OAAO,CAAC,cAAc,CAAiD;IACvE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAqB;IAClD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAa;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;gBAE3B,OAAO,EAAE,oBAAoB;IAYzC,KAAK,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI;IAoBnC,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAQ/B,OAAO,IAAI,IAAI;IAOf,OAAO,CAAC,YAAY;IA8BpB,OAAO,CAAC,UAAU;IAwElB,OAAO,CAAC,aAAa;CActB"}
@@ -0,0 +1,3 @@
1
+ export { Checkout } from "./Checkout";
2
+ export type { CheckoutConfig, CheckoutMode, CheckoutSuccess, CheckoutError, CheckoutReady, } from "./types";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,YAAY,EACV,cAAc,EACd,YAAY,EACZ,eAAe,EACf,aAAa,EACb,aAAa,GACd,MAAM,SAAS,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { CheckoutMessage } from "../types";
2
+ interface MessageBridgeOptions {
3
+ checkoutId: string;
4
+ checkoutOrigin: string;
5
+ onMessage: (message: CheckoutMessage) => void;
6
+ }
7
+ export declare class MessageBridge {
8
+ private readonly checkoutId;
9
+ private readonly checkoutOrigin;
10
+ private readonly onMessage;
11
+ private sourceWindow;
12
+ constructor(options: MessageBridgeOptions);
13
+ setSourceWindow(sourceWindow: Window | null): void;
14
+ destroy(): void;
15
+ private handleMessage;
16
+ }
17
+ export {};
18
+ //# sourceMappingURL=MessageBridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessageBridge.d.ts","sourceRoot":"","sources":["../../src/messaging/MessageBridge.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,UAAU,oBAAoB;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;CAC/C;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqC;IAC/D,OAAO,CAAC,YAAY,CAAuB;gBAE/B,OAAO,EAAE,oBAAoB;IAQzC,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAIlD,OAAO,IAAI,IAAI;IAKf,OAAO,CAAC,aAAa,CAenB;CACH"}
@@ -0,0 +1,3 @@
1
+ import type { CheckoutMessage } from "./types";
2
+ export declare function parseCheckoutMessage(data: unknown, checkoutId: string): CheckoutMessage | null;
3
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAuB/C,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,MAAM,GACjB,eAAe,GAAG,IAAI,CA+ExB"}
@@ -0,0 +1,69 @@
1
+ export type CheckoutMode = "inline" | "modal" | "redirect";
2
+ export interface CheckoutConfig {
3
+ /** Checkout intent id returned when the merchant creates a checkout. */
4
+ intentId: string;
5
+ /** Payer client token (`ctc_…`) returned with the checkout. */
6
+ clientToken: string;
7
+ /**
8
+ * How `start()` presents checkout.
9
+ * `mount()`, `open()`, and `redirect()` ignore this and always use their own flow.
10
+ */
11
+ mode?: CheckoutMode;
12
+ /** Required for `mode: "inline"` when calling `start()`. */
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
+ /**
21
+ * After a hosted (redirect) payment, send the customer here.
22
+ * Ignored for inline/modal — handle `success` / `cancel` events instead.
23
+ */
24
+ returnUrl?: string;
25
+ }
26
+ export interface CheckoutReady {
27
+ intentId: string;
28
+ }
29
+ export interface CheckoutSuccess {
30
+ intentId: string;
31
+ reference: string;
32
+ }
33
+ export interface CheckoutError {
34
+ code: string;
35
+ message: string;
36
+ }
37
+ export type CheckoutEventMap = {
38
+ ready: CheckoutReady;
39
+ success: CheckoutSuccess;
40
+ cancel: undefined;
41
+ error: CheckoutError;
42
+ };
43
+ export type CheckoutMessageType = "checkout.ready" | "checkout.success" | "checkout.cancel" | "checkout.error" | "checkout.resize";
44
+ export type CheckoutMessage = {
45
+ source: "commandtransfer.checkout";
46
+ type: "checkout.ready";
47
+ checkoutId: string;
48
+ } | {
49
+ source: "commandtransfer.checkout";
50
+ type: "checkout.success";
51
+ checkoutId: string;
52
+ reference: string;
53
+ } | {
54
+ source: "commandtransfer.checkout";
55
+ type: "checkout.cancel";
56
+ checkoutId: string;
57
+ } | {
58
+ source: "commandtransfer.checkout";
59
+ type: "checkout.error";
60
+ checkoutId: string;
61
+ code: string;
62
+ message: string;
63
+ } | {
64
+ source: "commandtransfer.checkout";
65
+ type: "checkout.resize";
66
+ checkoutId: string;
67
+ height: number;
68
+ };
69
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;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 ADDED
@@ -0,0 +1,31 @@
1
+ import type { CheckoutConfig, CheckoutMode } from "./types";
2
+ /** Loopback only — used for a locally running Command Transfer checkout app. */
3
+ export declare function isLocalHostname(hostname: string): boolean;
4
+ /** Merchant sites on loopback, LAN, or local-dev TLDs. */
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
+ export declare function normalizeIntentId(value: string): string;
12
+ export declare function normalizeClientToken(value: string): string;
13
+ export declare function resolveCheckoutOrigin(origin?: string): string;
14
+ /** Merchant return / parent URLs — any https site, or http on local/LAN. */
15
+ export declare function assertSafeHttpUrl(value: string, field: string): string;
16
+ export declare function buildCheckoutUrl(options: {
17
+ intentId: string;
18
+ clientToken: string;
19
+ checkoutOrigin: string;
20
+ mode: Exclude<CheckoutMode, "redirect"> | "redirect";
21
+ returnUrl?: string;
22
+ parentOrigin?: string;
23
+ }): string;
24
+ export declare function validateConfig(config: CheckoutConfig): {
25
+ intentId: string;
26
+ clientToken: string;
27
+ checkoutOrigin: string;
28
+ mode: CheckoutMode;
29
+ returnUrl?: string;
30
+ };
31
+ //# sourceMappingURL=url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../src/url.ts"],"names":[],"mappings":"AAMA,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;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAK/D;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMvD;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAiB1D;AA6BD,wBAAgB,qBAAqB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAY7D;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 ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@integralscope/command-transfer-checkout-js",
3
+ "version": "0.1.0",
4
+ "description": "Command Transfer Checkout Web SDK",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/Integral-Scope/commandtransfer-checkout-js.git"
8
+ },
9
+ "type": "module",
10
+ "main": "./dist/commandtransfer-checkout.umd.cjs",
11
+ "module": "./dist/commandtransfer-checkout.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/commandtransfer-checkout.js",
17
+ "require": "./dist/commandtransfer-checkout.umd.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "sideEffects": false,
24
+ "scripts": {
25
+ "build": "vite build && tsc -p tsconfig.build.json",
26
+ "prepack": "npm run build",
27
+ "test": "vitest run",
28
+ "test:watch": "vitest"
29
+ },
30
+ "devDependencies": {
31
+ "jsdom": "^26.1.0",
32
+ "typescript": "^5.0.0",
33
+ "vite": "^7.0.0",
34
+ "vitest": "^3.0.0"
35
+ },
36
+ "publishConfig": {
37
+ "registry": "https://registry.npmjs.org",
38
+ "access": "public"
39
+ }
40
+ }