@usethrottle/checkout-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/README.md +66 -0
- package/dist/index.cjs +99 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +86 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.js +72 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# @usethrottle/checkout-sdk
|
|
2
|
+
|
|
3
|
+
Throttle Checkout SDK — Phase 1.
|
|
4
|
+
|
|
5
|
+
Wraps `@usethrottle/checkout-react` with idempotency, telemetry, and brand theming.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @usethrottle/checkout-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { ThrottleCheckout } from '@usethrottle/checkout-sdk';
|
|
17
|
+
|
|
18
|
+
<ThrottleCheckout
|
|
19
|
+
sessionId="sess_abc"
|
|
20
|
+
parentOrigin="https://shop.example.com"
|
|
21
|
+
theme={{ primary: '#ff6600', logo: 'https://shop.example.com/logo.png' }}
|
|
22
|
+
onSucceeded={({ orderId }) => router.push(`/orders/${orderId}`)}
|
|
23
|
+
onTelemetry={(event) => analytics.track(event.type, event)}
|
|
24
|
+
/>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Props
|
|
28
|
+
|
|
29
|
+
| Prop | Type | Required | Description |
|
|
30
|
+
|------|------|----------|-------------|
|
|
31
|
+
| `sessionId` | `string` | Yes | Throttle checkout session id (`cs_*`) |
|
|
32
|
+
| `parentOrigin` | `string` | No | Origin of the page mounting the embed |
|
|
33
|
+
| `baseUrl` | `string` | No | Override the hosted checkout URL (default: `https://throttle-checkout.vercel.app`) |
|
|
34
|
+
| `theme` | `BrandTheme` | No | Brand colour + logo forwarded to hosted page |
|
|
35
|
+
| `onSucceeded` | `(args) => void` | No | Called once on successful payment |
|
|
36
|
+
| `onFailed` | `(args) => void` | No | Called once on terminal payment failure |
|
|
37
|
+
| `onCancelled` | `() => void` | No | Called when the user cancels |
|
|
38
|
+
| `onTelemetry` | `(event) => void` | No | Receives every lifecycle event |
|
|
39
|
+
|
|
40
|
+
## Telemetry events
|
|
41
|
+
|
|
42
|
+
| `event.type` | Additional payload | Description |
|
|
43
|
+
|---|---|---|
|
|
44
|
+
| `mounted` | `sessionId`, `timestamp` | Component mounted in the DOM |
|
|
45
|
+
| `ready` | `sessionId`, `timestamp` | Hosted iframe fully loaded |
|
|
46
|
+
| `processing` | `sessionId`, `timestamp` | Payment authorization in-flight |
|
|
47
|
+
| `succeeded` | `sessionId`, `orderId`, `paymentId`, `timestamp` | Payment completed successfully |
|
|
48
|
+
| `failed` | `sessionId`, `code`, `message`, `timestamp` | Terminal payment failure |
|
|
49
|
+
| `cancelled` | `sessionId`, `timestamp` | User cancelled checkout |
|
|
50
|
+
| `step_changed` | `sessionId`, `step`, `timestamp` | Checkout step navigation (full embed only) |
|
|
51
|
+
|
|
52
|
+
## Idempotency
|
|
53
|
+
|
|
54
|
+
`onSucceeded` and `onFailed` are terminal events — the underlying iframe can occasionally emit duplicate postMessages on network hiccups or React strict-mode double-renders. `ThrottleCheckout` maintains a `settledRef` that ensures only the first terminal event fires the callback. All subsequent duplicates are silently dropped.
|
|
55
|
+
|
|
56
|
+
## Brand theming
|
|
57
|
+
|
|
58
|
+
`theme.primary` (a CSS colour string) and `theme.logo` (an absolute HTTPS URL) are forwarded as `?primary=...&logo=...` query params on the hosted checkout iframe src. The hosted page (`apps/checkout-web`) reads these to apply the merchant's brand.
|
|
59
|
+
|
|
60
|
+
## Build
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pnpm --filter @usethrottle/checkout-sdk build
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Emits CJS (`dist/index.cjs`), ESM (`dist/index.js`), and TypeScript declarations (`dist/index.d.ts`).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ThrottleCheckout: () => ThrottleCheckout
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/ThrottleCheckout.tsx
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
var import_checkout_react = require("@usethrottle/checkout-react");
|
|
30
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
31
|
+
function ThrottleCheckout({
|
|
32
|
+
sessionId,
|
|
33
|
+
parentOrigin = "",
|
|
34
|
+
baseUrl,
|
|
35
|
+
theme,
|
|
36
|
+
onSucceeded,
|
|
37
|
+
onFailed,
|
|
38
|
+
onCancelled,
|
|
39
|
+
onTelemetry
|
|
40
|
+
}) {
|
|
41
|
+
const settledRef = (0, import_react.useRef)(false);
|
|
42
|
+
const emit = (0, import_react.useCallback)(
|
|
43
|
+
(event) => {
|
|
44
|
+
onTelemetry?.(event);
|
|
45
|
+
},
|
|
46
|
+
[onTelemetry]
|
|
47
|
+
);
|
|
48
|
+
(0, import_react.useEffect)(() => {
|
|
49
|
+
emit({ type: "mounted", sessionId, timestamp: Date.now() });
|
|
50
|
+
}, []);
|
|
51
|
+
const handleReady = (0, import_react.useCallback)(() => {
|
|
52
|
+
emit({ type: "ready", sessionId, timestamp: Date.now() });
|
|
53
|
+
}, [emit, sessionId]);
|
|
54
|
+
const handleProcessing = (0, import_react.useCallback)(() => {
|
|
55
|
+
emit({ type: "processing", sessionId, timestamp: Date.now() });
|
|
56
|
+
}, [emit, sessionId]);
|
|
57
|
+
const handleSucceeded = (0, import_react.useCallback)(
|
|
58
|
+
({ orderId, paymentId }) => {
|
|
59
|
+
if (settledRef.current) return;
|
|
60
|
+
settledRef.current = true;
|
|
61
|
+
emit({ type: "succeeded", sessionId, orderId, paymentId, timestamp: Date.now() });
|
|
62
|
+
onSucceeded?.({ orderId, paymentId });
|
|
63
|
+
},
|
|
64
|
+
[emit, sessionId, onSucceeded]
|
|
65
|
+
);
|
|
66
|
+
const handleFailed = (0, import_react.useCallback)(
|
|
67
|
+
({ code, message }) => {
|
|
68
|
+
if (settledRef.current) return;
|
|
69
|
+
settledRef.current = true;
|
|
70
|
+
emit({ type: "failed", sessionId, code, message, timestamp: Date.now() });
|
|
71
|
+
onFailed?.({ code, message });
|
|
72
|
+
},
|
|
73
|
+
[emit, sessionId, onFailed]
|
|
74
|
+
);
|
|
75
|
+
const handleCanceled = (0, import_react.useCallback)(() => {
|
|
76
|
+
emit({ type: "cancelled", sessionId, timestamp: Date.now() });
|
|
77
|
+
onCancelled?.();
|
|
78
|
+
}, [emit, sessionId, onCancelled]);
|
|
79
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
80
|
+
import_checkout_react.PaymentEmbed,
|
|
81
|
+
{
|
|
82
|
+
sessionId,
|
|
83
|
+
parentOrigin,
|
|
84
|
+
...baseUrl !== void 0 ? { baseUrl } : {},
|
|
85
|
+
primary: theme?.primary,
|
|
86
|
+
logo: theme?.logo,
|
|
87
|
+
onReady: handleReady,
|
|
88
|
+
onProcessing: handleProcessing,
|
|
89
|
+
onSucceeded: handleSucceeded,
|
|
90
|
+
onFailed: handleFailed,
|
|
91
|
+
onCanceled: handleCanceled
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
+
0 && (module.exports = {
|
|
97
|
+
ThrottleCheckout
|
|
98
|
+
});
|
|
99
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/ThrottleCheckout.tsx"],"sourcesContent":["export { ThrottleCheckout } from './ThrottleCheckout.js';\nexport type { BrandTheme, TelemetryEvent, ThrottleCheckoutProps } from './types.js';\n","import { useCallback, useEffect, useRef } from 'react';\nimport { PaymentEmbed } from '@usethrottle/checkout-react';\nimport type { ThrottleCheckoutProps, TelemetryEvent } from './types.js';\n\n/**\n * ThrottleCheckout — Phase 1 platform SDK wrapper.\n *\n * Wraps `<PaymentEmbed>` from `@usethrottle/checkout-react` and adds:\n * 1. Idempotency — settledRef ensures `onSucceeded` / `onFailed` fire at most once\n * per component lifetime, even if the iframe sends duplicate events.\n * 2. Telemetry — `onTelemetry` callback receives every lifecycle event with a\n * typed payload so merchants can pipe to Datadog / Segment / etc.\n * 3. Brand theming — `theme.primary` + `theme.logo` are forwarded to the underlying\n * `<PaymentEmbed>` which appends them as URL query params on the\n * hosted checkout iframe src.\n */\nexport function ThrottleCheckout({\n sessionId,\n parentOrigin = '',\n baseUrl,\n theme,\n onSucceeded,\n onFailed,\n onCancelled,\n onTelemetry,\n}: ThrottleCheckoutProps): JSX.Element {\n // Idempotency guard — once a terminal event (success or failure) fires,\n // all subsequent duplicate events from the iframe are silently dropped.\n const settledRef = useRef(false);\n\n const emit = useCallback(\n (event: TelemetryEvent) => {\n onTelemetry?.(event);\n },\n [onTelemetry],\n );\n\n // Fire \"mounted\" telemetry once on initial render.\n useEffect(() => {\n emit({ type: 'mounted', sessionId, timestamp: Date.now() });\n // intentionally only on mount — sessionId identity change re-mounts anyway\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n const handleReady = useCallback(() => {\n emit({ type: 'ready', sessionId, timestamp: Date.now() });\n }, [emit, sessionId]);\n\n const handleProcessing = useCallback(() => {\n emit({ type: 'processing', sessionId, timestamp: Date.now() });\n }, [emit, sessionId]);\n\n const handleSucceeded = useCallback(\n ({ orderId, paymentId }: { orderId: string; paymentId: string }) => {\n if (settledRef.current) return;\n settledRef.current = true;\n emit({ type: 'succeeded', sessionId, orderId, paymentId, timestamp: Date.now() });\n onSucceeded?.({ orderId, paymentId });\n },\n [emit, sessionId, onSucceeded],\n );\n\n const handleFailed = useCallback(\n ({ code, message }: { code: string; message: string }) => {\n if (settledRef.current) return;\n settledRef.current = true;\n emit({ type: 'failed', sessionId, code, message, timestamp: Date.now() });\n onFailed?.({ code, message });\n },\n [emit, sessionId, onFailed],\n );\n\n const handleCanceled = useCallback(() => {\n emit({ type: 'cancelled', sessionId, timestamp: Date.now() });\n onCancelled?.();\n }, [emit, sessionId, onCancelled]);\n\n return (\n <PaymentEmbed\n sessionId={sessionId}\n parentOrigin={parentOrigin}\n {...(baseUrl !== undefined ? { baseUrl } : {})}\n primary={theme?.primary}\n logo={theme?.logo}\n onReady={handleReady}\n onProcessing={handleProcessing}\n onSucceeded={handleSucceeded}\n onFailed={handleFailed}\n onCanceled={handleCanceled}\n />\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA+C;AAC/C,4BAA6B;AA6EzB;AA9DG,SAAS,iBAAiB;AAAA,EAC/B;AAAA,EACA,eAAe;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAuC;AAGrC,QAAM,iBAAa,qBAAO,KAAK;AAE/B,QAAM,WAAO;AAAA,IACX,CAAC,UAA0B;AACzB,oBAAc,KAAK;AAAA,IACrB;AAAA,IACA,CAAC,WAAW;AAAA,EACd;AAGA,8BAAU,MAAM;AACd,SAAK,EAAE,MAAM,WAAW,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAG5D,GAAG,CAAC,CAAC;AAEL,QAAM,kBAAc,0BAAY,MAAM;AACpC,SAAK,EAAE,MAAM,SAAS,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAC1D,GAAG,CAAC,MAAM,SAAS,CAAC;AAEpB,QAAM,uBAAmB,0BAAY,MAAM;AACzC,SAAK,EAAE,MAAM,cAAc,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAC/D,GAAG,CAAC,MAAM,SAAS,CAAC;AAEpB,QAAM,sBAAkB;AAAA,IACtB,CAAC,EAAE,SAAS,UAAU,MAA8C;AAClE,UAAI,WAAW,QAAS;AACxB,iBAAW,UAAU;AACrB,WAAK,EAAE,MAAM,aAAa,WAAW,SAAS,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAChF,oBAAc,EAAE,SAAS,UAAU,CAAC;AAAA,IACtC;AAAA,IACA,CAAC,MAAM,WAAW,WAAW;AAAA,EAC/B;AAEA,QAAM,mBAAe;AAAA,IACnB,CAAC,EAAE,MAAM,QAAQ,MAAyC;AACxD,UAAI,WAAW,QAAS;AACxB,iBAAW,UAAU;AACrB,WAAK,EAAE,MAAM,UAAU,WAAW,MAAM,SAAS,WAAW,KAAK,IAAI,EAAE,CAAC;AACxE,iBAAW,EAAE,MAAM,QAAQ,CAAC;AAAA,IAC9B;AAAA,IACA,CAAC,MAAM,WAAW,QAAQ;AAAA,EAC5B;AAEA,QAAM,qBAAiB,0BAAY,MAAM;AACvC,SAAK,EAAE,MAAM,aAAa,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAC5D,kBAAc;AAAA,EAChB,GAAG,CAAC,MAAM,WAAW,WAAW,CAAC;AAEjC,SACE;AAAA,IAAC;AAAA;AAAA,MACC;AAAA,MACA;AAAA,MACC,GAAI,YAAY,SAAY,EAAE,QAAQ,IAAI,CAAC;AAAA,MAC5C,SAAS,OAAO;AAAA,MAChB,MAAM,OAAO;AAAA,MACb,SAAS;AAAA,MACT,cAAc;AAAA,MACd,aAAa;AAAA,MACb,UAAU;AAAA,MACV,YAAY;AAAA;AAAA,EACd;AAEJ;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @usethrottle/checkout-sdk — Phase 1 types.
|
|
3
|
+
*/
|
|
4
|
+
interface BrandTheme {
|
|
5
|
+
/** Brand primary colour (#rrggbb or CSS named colour). */
|
|
6
|
+
primary?: string;
|
|
7
|
+
/** Absolute URL of merchant logo image. */
|
|
8
|
+
logo?: string;
|
|
9
|
+
}
|
|
10
|
+
type TelemetryEvent = {
|
|
11
|
+
type: 'mounted';
|
|
12
|
+
sessionId: string;
|
|
13
|
+
timestamp: number;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'ready';
|
|
16
|
+
sessionId: string;
|
|
17
|
+
timestamp: number;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'processing';
|
|
20
|
+
sessionId: string;
|
|
21
|
+
timestamp: number;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'succeeded';
|
|
24
|
+
sessionId: string;
|
|
25
|
+
orderId: string;
|
|
26
|
+
paymentId: string;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
} | {
|
|
29
|
+
type: 'failed';
|
|
30
|
+
sessionId: string;
|
|
31
|
+
code: string;
|
|
32
|
+
message: string;
|
|
33
|
+
timestamp: number;
|
|
34
|
+
} | {
|
|
35
|
+
type: 'cancelled';
|
|
36
|
+
sessionId: string;
|
|
37
|
+
timestamp: number;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'step_changed';
|
|
40
|
+
sessionId: string;
|
|
41
|
+
step: string;
|
|
42
|
+
timestamp: number;
|
|
43
|
+
};
|
|
44
|
+
interface ThrottleCheckoutProps {
|
|
45
|
+
/** Throttle checkout session id (cs_*). Required. */
|
|
46
|
+
sessionId: string;
|
|
47
|
+
/**
|
|
48
|
+
* Parent origin where this embed is mounted (e.g. https://shop.example.com).
|
|
49
|
+
* Must be in the merchant's allowed_origins list.
|
|
50
|
+
*/
|
|
51
|
+
parentOrigin?: string;
|
|
52
|
+
/** Base URL of the deployed checkout-web app. */
|
|
53
|
+
baseUrl?: string;
|
|
54
|
+
/** Optional brand theming forwarded to the hosted checkout page. */
|
|
55
|
+
theme?: BrandTheme;
|
|
56
|
+
/** Fired once when payment completes successfully. */
|
|
57
|
+
onSucceeded?: (args: {
|
|
58
|
+
orderId: string;
|
|
59
|
+
paymentId: string;
|
|
60
|
+
}) => void;
|
|
61
|
+
/** Fired once when payment fails terminally. */
|
|
62
|
+
onFailed?: (args: {
|
|
63
|
+
code: string;
|
|
64
|
+
message: string;
|
|
65
|
+
}) => void;
|
|
66
|
+
/** Fired when the user cancels checkout. */
|
|
67
|
+
onCancelled?: () => void;
|
|
68
|
+
/** Optional sink for all lifecycle events — pipe to Datadog / Segment / custom analytics. */
|
|
69
|
+
onTelemetry?: (event: TelemetryEvent) => void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* ThrottleCheckout — Phase 1 platform SDK wrapper.
|
|
74
|
+
*
|
|
75
|
+
* Wraps `<PaymentEmbed>` from `@usethrottle/checkout-react` and adds:
|
|
76
|
+
* 1. Idempotency — settledRef ensures `onSucceeded` / `onFailed` fire at most once
|
|
77
|
+
* per component lifetime, even if the iframe sends duplicate events.
|
|
78
|
+
* 2. Telemetry — `onTelemetry` callback receives every lifecycle event with a
|
|
79
|
+
* typed payload so merchants can pipe to Datadog / Segment / etc.
|
|
80
|
+
* 3. Brand theming — `theme.primary` + `theme.logo` are forwarded to the underlying
|
|
81
|
+
* `<PaymentEmbed>` which appends them as URL query params on the
|
|
82
|
+
* hosted checkout iframe src.
|
|
83
|
+
*/
|
|
84
|
+
declare function ThrottleCheckout({ sessionId, parentOrigin, baseUrl, theme, onSucceeded, onFailed, onCancelled, onTelemetry, }: ThrottleCheckoutProps): JSX.Element;
|
|
85
|
+
|
|
86
|
+
export { type BrandTheme, type TelemetryEvent, ThrottleCheckout, type ThrottleCheckoutProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @usethrottle/checkout-sdk — Phase 1 types.
|
|
3
|
+
*/
|
|
4
|
+
interface BrandTheme {
|
|
5
|
+
/** Brand primary colour (#rrggbb or CSS named colour). */
|
|
6
|
+
primary?: string;
|
|
7
|
+
/** Absolute URL of merchant logo image. */
|
|
8
|
+
logo?: string;
|
|
9
|
+
}
|
|
10
|
+
type TelemetryEvent = {
|
|
11
|
+
type: 'mounted';
|
|
12
|
+
sessionId: string;
|
|
13
|
+
timestamp: number;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'ready';
|
|
16
|
+
sessionId: string;
|
|
17
|
+
timestamp: number;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'processing';
|
|
20
|
+
sessionId: string;
|
|
21
|
+
timestamp: number;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'succeeded';
|
|
24
|
+
sessionId: string;
|
|
25
|
+
orderId: string;
|
|
26
|
+
paymentId: string;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
} | {
|
|
29
|
+
type: 'failed';
|
|
30
|
+
sessionId: string;
|
|
31
|
+
code: string;
|
|
32
|
+
message: string;
|
|
33
|
+
timestamp: number;
|
|
34
|
+
} | {
|
|
35
|
+
type: 'cancelled';
|
|
36
|
+
sessionId: string;
|
|
37
|
+
timestamp: number;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'step_changed';
|
|
40
|
+
sessionId: string;
|
|
41
|
+
step: string;
|
|
42
|
+
timestamp: number;
|
|
43
|
+
};
|
|
44
|
+
interface ThrottleCheckoutProps {
|
|
45
|
+
/** Throttle checkout session id (cs_*). Required. */
|
|
46
|
+
sessionId: string;
|
|
47
|
+
/**
|
|
48
|
+
* Parent origin where this embed is mounted (e.g. https://shop.example.com).
|
|
49
|
+
* Must be in the merchant's allowed_origins list.
|
|
50
|
+
*/
|
|
51
|
+
parentOrigin?: string;
|
|
52
|
+
/** Base URL of the deployed checkout-web app. */
|
|
53
|
+
baseUrl?: string;
|
|
54
|
+
/** Optional brand theming forwarded to the hosted checkout page. */
|
|
55
|
+
theme?: BrandTheme;
|
|
56
|
+
/** Fired once when payment completes successfully. */
|
|
57
|
+
onSucceeded?: (args: {
|
|
58
|
+
orderId: string;
|
|
59
|
+
paymentId: string;
|
|
60
|
+
}) => void;
|
|
61
|
+
/** Fired once when payment fails terminally. */
|
|
62
|
+
onFailed?: (args: {
|
|
63
|
+
code: string;
|
|
64
|
+
message: string;
|
|
65
|
+
}) => void;
|
|
66
|
+
/** Fired when the user cancels checkout. */
|
|
67
|
+
onCancelled?: () => void;
|
|
68
|
+
/** Optional sink for all lifecycle events — pipe to Datadog / Segment / custom analytics. */
|
|
69
|
+
onTelemetry?: (event: TelemetryEvent) => void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* ThrottleCheckout — Phase 1 platform SDK wrapper.
|
|
74
|
+
*
|
|
75
|
+
* Wraps `<PaymentEmbed>` from `@usethrottle/checkout-react` and adds:
|
|
76
|
+
* 1. Idempotency — settledRef ensures `onSucceeded` / `onFailed` fire at most once
|
|
77
|
+
* per component lifetime, even if the iframe sends duplicate events.
|
|
78
|
+
* 2. Telemetry — `onTelemetry` callback receives every lifecycle event with a
|
|
79
|
+
* typed payload so merchants can pipe to Datadog / Segment / etc.
|
|
80
|
+
* 3. Brand theming — `theme.primary` + `theme.logo` are forwarded to the underlying
|
|
81
|
+
* `<PaymentEmbed>` which appends them as URL query params on the
|
|
82
|
+
* hosted checkout iframe src.
|
|
83
|
+
*/
|
|
84
|
+
declare function ThrottleCheckout({ sessionId, parentOrigin, baseUrl, theme, onSucceeded, onFailed, onCancelled, onTelemetry, }: ThrottleCheckoutProps): JSX.Element;
|
|
85
|
+
|
|
86
|
+
export { type BrandTheme, type TelemetryEvent, ThrottleCheckout, type ThrottleCheckoutProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// src/ThrottleCheckout.tsx
|
|
2
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
3
|
+
import { PaymentEmbed } from "@usethrottle/checkout-react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
function ThrottleCheckout({
|
|
6
|
+
sessionId,
|
|
7
|
+
parentOrigin = "",
|
|
8
|
+
baseUrl,
|
|
9
|
+
theme,
|
|
10
|
+
onSucceeded,
|
|
11
|
+
onFailed,
|
|
12
|
+
onCancelled,
|
|
13
|
+
onTelemetry
|
|
14
|
+
}) {
|
|
15
|
+
const settledRef = useRef(false);
|
|
16
|
+
const emit = useCallback(
|
|
17
|
+
(event) => {
|
|
18
|
+
onTelemetry?.(event);
|
|
19
|
+
},
|
|
20
|
+
[onTelemetry]
|
|
21
|
+
);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
emit({ type: "mounted", sessionId, timestamp: Date.now() });
|
|
24
|
+
}, []);
|
|
25
|
+
const handleReady = useCallback(() => {
|
|
26
|
+
emit({ type: "ready", sessionId, timestamp: Date.now() });
|
|
27
|
+
}, [emit, sessionId]);
|
|
28
|
+
const handleProcessing = useCallback(() => {
|
|
29
|
+
emit({ type: "processing", sessionId, timestamp: Date.now() });
|
|
30
|
+
}, [emit, sessionId]);
|
|
31
|
+
const handleSucceeded = useCallback(
|
|
32
|
+
({ orderId, paymentId }) => {
|
|
33
|
+
if (settledRef.current) return;
|
|
34
|
+
settledRef.current = true;
|
|
35
|
+
emit({ type: "succeeded", sessionId, orderId, paymentId, timestamp: Date.now() });
|
|
36
|
+
onSucceeded?.({ orderId, paymentId });
|
|
37
|
+
},
|
|
38
|
+
[emit, sessionId, onSucceeded]
|
|
39
|
+
);
|
|
40
|
+
const handleFailed = useCallback(
|
|
41
|
+
({ code, message }) => {
|
|
42
|
+
if (settledRef.current) return;
|
|
43
|
+
settledRef.current = true;
|
|
44
|
+
emit({ type: "failed", sessionId, code, message, timestamp: Date.now() });
|
|
45
|
+
onFailed?.({ code, message });
|
|
46
|
+
},
|
|
47
|
+
[emit, sessionId, onFailed]
|
|
48
|
+
);
|
|
49
|
+
const handleCanceled = useCallback(() => {
|
|
50
|
+
emit({ type: "cancelled", sessionId, timestamp: Date.now() });
|
|
51
|
+
onCancelled?.();
|
|
52
|
+
}, [emit, sessionId, onCancelled]);
|
|
53
|
+
return /* @__PURE__ */ jsx(
|
|
54
|
+
PaymentEmbed,
|
|
55
|
+
{
|
|
56
|
+
sessionId,
|
|
57
|
+
parentOrigin,
|
|
58
|
+
...baseUrl !== void 0 ? { baseUrl } : {},
|
|
59
|
+
primary: theme?.primary,
|
|
60
|
+
logo: theme?.logo,
|
|
61
|
+
onReady: handleReady,
|
|
62
|
+
onProcessing: handleProcessing,
|
|
63
|
+
onSucceeded: handleSucceeded,
|
|
64
|
+
onFailed: handleFailed,
|
|
65
|
+
onCanceled: handleCanceled
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
ThrottleCheckout
|
|
71
|
+
};
|
|
72
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ThrottleCheckout.tsx"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { PaymentEmbed } from '@usethrottle/checkout-react';\nimport type { ThrottleCheckoutProps, TelemetryEvent } from './types.js';\n\n/**\n * ThrottleCheckout — Phase 1 platform SDK wrapper.\n *\n * Wraps `<PaymentEmbed>` from `@usethrottle/checkout-react` and adds:\n * 1. Idempotency — settledRef ensures `onSucceeded` / `onFailed` fire at most once\n * per component lifetime, even if the iframe sends duplicate events.\n * 2. Telemetry — `onTelemetry` callback receives every lifecycle event with a\n * typed payload so merchants can pipe to Datadog / Segment / etc.\n * 3. Brand theming — `theme.primary` + `theme.logo` are forwarded to the underlying\n * `<PaymentEmbed>` which appends them as URL query params on the\n * hosted checkout iframe src.\n */\nexport function ThrottleCheckout({\n sessionId,\n parentOrigin = '',\n baseUrl,\n theme,\n onSucceeded,\n onFailed,\n onCancelled,\n onTelemetry,\n}: ThrottleCheckoutProps): JSX.Element {\n // Idempotency guard — once a terminal event (success or failure) fires,\n // all subsequent duplicate events from the iframe are silently dropped.\n const settledRef = useRef(false);\n\n const emit = useCallback(\n (event: TelemetryEvent) => {\n onTelemetry?.(event);\n },\n [onTelemetry],\n );\n\n // Fire \"mounted\" telemetry once on initial render.\n useEffect(() => {\n emit({ type: 'mounted', sessionId, timestamp: Date.now() });\n // intentionally only on mount — sessionId identity change re-mounts anyway\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n const handleReady = useCallback(() => {\n emit({ type: 'ready', sessionId, timestamp: Date.now() });\n }, [emit, sessionId]);\n\n const handleProcessing = useCallback(() => {\n emit({ type: 'processing', sessionId, timestamp: Date.now() });\n }, [emit, sessionId]);\n\n const handleSucceeded = useCallback(\n ({ orderId, paymentId }: { orderId: string; paymentId: string }) => {\n if (settledRef.current) return;\n settledRef.current = true;\n emit({ type: 'succeeded', sessionId, orderId, paymentId, timestamp: Date.now() });\n onSucceeded?.({ orderId, paymentId });\n },\n [emit, sessionId, onSucceeded],\n );\n\n const handleFailed = useCallback(\n ({ code, message }: { code: string; message: string }) => {\n if (settledRef.current) return;\n settledRef.current = true;\n emit({ type: 'failed', sessionId, code, message, timestamp: Date.now() });\n onFailed?.({ code, message });\n },\n [emit, sessionId, onFailed],\n );\n\n const handleCanceled = useCallback(() => {\n emit({ type: 'cancelled', sessionId, timestamp: Date.now() });\n onCancelled?.();\n }, [emit, sessionId, onCancelled]);\n\n return (\n <PaymentEmbed\n sessionId={sessionId}\n parentOrigin={parentOrigin}\n {...(baseUrl !== undefined ? { baseUrl } : {})}\n primary={theme?.primary}\n logo={theme?.logo}\n onReady={handleReady}\n onProcessing={handleProcessing}\n onSucceeded={handleSucceeded}\n onFailed={handleFailed}\n onCanceled={handleCanceled}\n />\n );\n}\n"],"mappings":";AAAA,SAAS,aAAa,WAAW,cAAc;AAC/C,SAAS,oBAAoB;AA6EzB;AA9DG,SAAS,iBAAiB;AAAA,EAC/B;AAAA,EACA,eAAe;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAuC;AAGrC,QAAM,aAAa,OAAO,KAAK;AAE/B,QAAM,OAAO;AAAA,IACX,CAAC,UAA0B;AACzB,oBAAc,KAAK;AAAA,IACrB;AAAA,IACA,CAAC,WAAW;AAAA,EACd;AAGA,YAAU,MAAM;AACd,SAAK,EAAE,MAAM,WAAW,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAG5D,GAAG,CAAC,CAAC;AAEL,QAAM,cAAc,YAAY,MAAM;AACpC,SAAK,EAAE,MAAM,SAAS,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAC1D,GAAG,CAAC,MAAM,SAAS,CAAC;AAEpB,QAAM,mBAAmB,YAAY,MAAM;AACzC,SAAK,EAAE,MAAM,cAAc,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAAA,EAC/D,GAAG,CAAC,MAAM,SAAS,CAAC;AAEpB,QAAM,kBAAkB;AAAA,IACtB,CAAC,EAAE,SAAS,UAAU,MAA8C;AAClE,UAAI,WAAW,QAAS;AACxB,iBAAW,UAAU;AACrB,WAAK,EAAE,MAAM,aAAa,WAAW,SAAS,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAChF,oBAAc,EAAE,SAAS,UAAU,CAAC;AAAA,IACtC;AAAA,IACA,CAAC,MAAM,WAAW,WAAW;AAAA,EAC/B;AAEA,QAAM,eAAe;AAAA,IACnB,CAAC,EAAE,MAAM,QAAQ,MAAyC;AACxD,UAAI,WAAW,QAAS;AACxB,iBAAW,UAAU;AACrB,WAAK,EAAE,MAAM,UAAU,WAAW,MAAM,SAAS,WAAW,KAAK,IAAI,EAAE,CAAC;AACxE,iBAAW,EAAE,MAAM,QAAQ,CAAC;AAAA,IAC9B;AAAA,IACA,CAAC,MAAM,WAAW,QAAQ;AAAA,EAC5B;AAEA,QAAM,iBAAiB,YAAY,MAAM;AACvC,SAAK,EAAE,MAAM,aAAa,WAAW,WAAW,KAAK,IAAI,EAAE,CAAC;AAC5D,kBAAc;AAAA,EAChB,GAAG,CAAC,MAAM,WAAW,WAAW,CAAC;AAEjC,SACE;AAAA,IAAC;AAAA;AAAA,MACC;AAAA,MACA;AAAA,MACC,GAAI,YAAY,SAAY,EAAE,QAAQ,IAAI,CAAC;AAAA,MAC5C,SAAS,OAAO;AAAA,MAChB,MAAM,OAAO;AAAA,MACb,SAAS;AAAA,MACT,cAAc;AAAA,MACd,aAAa;AAAA,MACb,UAAU;AAAA,MACV,YAAY;AAAA;AAAA,EACd;AAEJ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@usethrottle/checkout-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"react": ">=18 <20",
|
|
21
|
+
"react-dom": ">=18 <20"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@usethrottle/checkout-react": "1.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@testing-library/react": "^16.0.0",
|
|
28
|
+
"@types/react": "^18.0.0",
|
|
29
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
30
|
+
"react": "^18.0.0",
|
|
31
|
+
"react-dom": "^18.0.0",
|
|
32
|
+
"tsup": "^8.0.0",
|
|
33
|
+
"typescript": "^5.7.0",
|
|
34
|
+
"vite": "^6.0.0",
|
|
35
|
+
"vitest": "^2.1.0",
|
|
36
|
+
"jsdom": "^25.0.0"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"typecheck": "tsc --noEmit"
|
|
45
|
+
}
|
|
46
|
+
}
|