@strimz/sdk-react 0.3.7 → 0.4.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/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
3
  import { StrimzBrowserClient } from '@strimz/sdk/browser';
4
- import { PaymentSession, CheckoutResultErrorCode } from '@strimz/shared-types';
4
+ import { PaymentSession, SubscriptionResultErrorCode, CheckoutResultErrorCode } from '@strimz/shared-types';
5
5
 
6
6
  interface StrimzContextValue {
7
7
  client: StrimzBrowserClient;
@@ -44,6 +44,8 @@ interface UseStrimzSessionOptions {
44
44
  declare function useStrimzSession(sessionId: string | null | undefined, options?: UseStrimzSessionOptions): UseStrimzSessionResult;
45
45
 
46
46
  type CheckoutMode = 'redirect' | 'popup';
47
+ type HostedPopupStatus = 'idle' | 'open' | 'success' | 'cancelled' | 'error';
48
+
47
49
  interface UseStrimzCheckoutOptions {
48
50
  /** How to open the hosted checkout. Default `popup`. */
49
51
  mode?: CheckoutMode;
@@ -57,7 +59,7 @@ interface UseStrimzCheckoutOptions {
57
59
  interface UseStrimzCheckoutResult {
58
60
  open: (sessionId: string) => void;
59
61
  close: () => void;
60
- status: 'idle' | 'open' | 'success' | 'cancelled' | 'error';
62
+ status: HostedPopupStatus;
61
63
  }
62
64
  /**
63
65
  * Imperative checkout opener. Lower-level than `<StrimzPayButton/>`.
@@ -67,8 +69,11 @@ interface UseStrimzCheckoutResult {
67
69
  */
68
70
  declare function useStrimzCheckout(options?: UseStrimzCheckoutOptions): UseStrimzCheckoutResult;
69
71
 
70
- type StrimzPayButtonTheme = 'auto' | 'light' | 'dark';
71
- type StrimzPayButtonSize = 'sm' | 'md' | 'lg';
72
+ type StrimzButtonTheme = 'auto' | 'light' | 'dark';
73
+ type StrimzButtonSize = 'sm' | 'md' | 'lg';
74
+
75
+ type StrimzPayButtonTheme = StrimzButtonTheme;
76
+ type StrimzPayButtonSize = StrimzButtonSize;
72
77
  interface StrimzPayButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onError'> {
73
78
  /** Strimz session id created via `@strimz/sdk` on your backend. */
74
79
  sessionId: string;
@@ -118,10 +123,72 @@ interface StrimzCheckoutEmbedProps {
118
123
  */
119
124
  declare function StrimzCheckoutEmbed({ sessionId, onSuccess, onCancel, onError, className, style, height, theme, }: StrimzCheckoutEmbedProps): react_jsx_runtime.JSX.Element;
120
125
 
126
+ declare class StrimzSubscriptionError extends Error {
127
+ readonly code: SubscriptionResultErrorCode;
128
+ readonly planId: string;
129
+ constructor(code: SubscriptionResultErrorCode, planId: string, message: string);
130
+ }
131
+
132
+ interface StrimzSubscriptionResult {
133
+ /** Null when Strimz hadn't recorded the subscription yet; use the webhook. */
134
+ subscriptionId: string | null;
135
+ txHash: string;
136
+ }
137
+ interface UseStrimzSubscribeOptions {
138
+ /** How to open the plan checkout. Default `popup`. */
139
+ mode?: CheckoutMode;
140
+ onSuccess?: (result: StrimzSubscriptionResult) => void;
141
+ /** Called when the payer cancels or closes the popup. */
142
+ onCancel?: () => void;
143
+ onError?: (error: StrimzSubscriptionError) => void;
144
+ }
145
+ interface UseStrimzSubscribeResult {
146
+ open: (planId: string) => void;
147
+ close: () => void;
148
+ status: HostedPopupStatus;
149
+ }
150
+ /**
151
+ * const subscribe = useStrimzSubscribe({ onSuccess: ({ subscriptionId }) => ... })
152
+ * <button onClick={() => subscribe.open(planId)}>Subscribe</button>
153
+ */
154
+ declare function useStrimzSubscribe(options?: UseStrimzSubscribeOptions): UseStrimzSubscribeResult;
155
+
156
+ interface StrimzSubscribeButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onError'> {
157
+ /** Plan id from the dashboard or `subscriptionPlans.create`. */
158
+ planId: string;
159
+ /** Hosted checkout open mode. Default `popup`. */
160
+ mode?: CheckoutMode;
161
+ theme?: StrimzButtonTheme;
162
+ size?: StrimzButtonSize;
163
+ onSuccess?: (result: StrimzSubscriptionResult) => void;
164
+ onCancel?: () => void;
165
+ onError?: (error: StrimzSubscriptionError) => void;
166
+ /** Override the button label. Default "Subscribe with Strimz". */
167
+ label?: React.ReactNode;
168
+ }
169
+ /**
170
+ * <StrimzSubscribeButton planId={plan.id} onSuccess={({ subscriptionId }) => ...} />
171
+ */
172
+ declare const StrimzSubscribeButton: React.ForwardRefExoticComponent<StrimzSubscribeButtonProps & React.RefAttributes<HTMLButtonElement>>;
173
+
174
+ interface StrimzSubscriptionEmbedProps {
175
+ planId: string;
176
+ onSuccess?: (result: StrimzSubscriptionResult) => void;
177
+ onCancel?: () => void;
178
+ onError?: (error: StrimzSubscriptionError) => void;
179
+ className?: string;
180
+ style?: React.CSSProperties;
181
+ /** Iframe height. Default 640. */
182
+ height?: number | string;
183
+ theme?: StrimzCheckoutTheme;
184
+ }
185
+ /** Results only count from this iframe, for this plan. */
186
+ declare function StrimzSubscriptionEmbed({ planId, onSuccess, onCancel, onError, className, style, height, theme, }: StrimzSubscriptionEmbedProps): react_jsx_runtime.JSX.Element;
187
+
121
188
  declare class StrimzCheckoutError extends Error {
122
189
  readonly code: CheckoutResultErrorCode;
123
190
  readonly sessionId: string;
124
191
  constructor(code: CheckoutResultErrorCode, sessionId: string, message: string);
125
192
  }
126
193
 
127
- export { type CheckoutMode, StrimzCheckoutEmbed, type StrimzCheckoutEmbedProps, StrimzCheckoutError, type StrimzCheckoutTheme, type StrimzContextValue, StrimzPayButton, type StrimzPayButtonProps, type StrimzPayButtonSize, type StrimzPayButtonTheme, StrimzProvider, type StrimzProviderProps, type UseStrimzCheckoutOptions, type UseStrimzCheckoutResult, type UseStrimzSessionOptions, type UseStrimzSessionResult, useStrimzCheckout, useStrimzClient, useStrimzContext, useStrimzSession };
194
+ export { type CheckoutMode, StrimzCheckoutEmbed, type StrimzCheckoutEmbedProps, StrimzCheckoutError, type StrimzCheckoutTheme, type StrimzContextValue, StrimzPayButton, type StrimzPayButtonProps, type StrimzPayButtonSize, type StrimzPayButtonTheme, StrimzProvider, type StrimzProviderProps, StrimzSubscribeButton, type StrimzSubscribeButtonProps, StrimzSubscriptionEmbed, type StrimzSubscriptionEmbedProps, StrimzSubscriptionError, type StrimzSubscriptionResult, type UseStrimzCheckoutOptions, type UseStrimzCheckoutResult, type UseStrimzSessionOptions, type UseStrimzSessionResult, type UseStrimzSubscribeOptions, type UseStrimzSubscribeResult, useStrimzCheckout, useStrimzClient, useStrimzContext, useStrimzSession, useStrimzSubscribe };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
3
  import { StrimzBrowserClient } from '@strimz/sdk/browser';
4
- import { PaymentSession, CheckoutResultErrorCode } from '@strimz/shared-types';
4
+ import { PaymentSession, SubscriptionResultErrorCode, CheckoutResultErrorCode } from '@strimz/shared-types';
5
5
 
6
6
  interface StrimzContextValue {
7
7
  client: StrimzBrowserClient;
@@ -44,6 +44,8 @@ interface UseStrimzSessionOptions {
44
44
  declare function useStrimzSession(sessionId: string | null | undefined, options?: UseStrimzSessionOptions): UseStrimzSessionResult;
45
45
 
46
46
  type CheckoutMode = 'redirect' | 'popup';
47
+ type HostedPopupStatus = 'idle' | 'open' | 'success' | 'cancelled' | 'error';
48
+
47
49
  interface UseStrimzCheckoutOptions {
48
50
  /** How to open the hosted checkout. Default `popup`. */
49
51
  mode?: CheckoutMode;
@@ -57,7 +59,7 @@ interface UseStrimzCheckoutOptions {
57
59
  interface UseStrimzCheckoutResult {
58
60
  open: (sessionId: string) => void;
59
61
  close: () => void;
60
- status: 'idle' | 'open' | 'success' | 'cancelled' | 'error';
62
+ status: HostedPopupStatus;
61
63
  }
62
64
  /**
63
65
  * Imperative checkout opener. Lower-level than `<StrimzPayButton/>`.
@@ -67,8 +69,11 @@ interface UseStrimzCheckoutResult {
67
69
  */
68
70
  declare function useStrimzCheckout(options?: UseStrimzCheckoutOptions): UseStrimzCheckoutResult;
69
71
 
70
- type StrimzPayButtonTheme = 'auto' | 'light' | 'dark';
71
- type StrimzPayButtonSize = 'sm' | 'md' | 'lg';
72
+ type StrimzButtonTheme = 'auto' | 'light' | 'dark';
73
+ type StrimzButtonSize = 'sm' | 'md' | 'lg';
74
+
75
+ type StrimzPayButtonTheme = StrimzButtonTheme;
76
+ type StrimzPayButtonSize = StrimzButtonSize;
72
77
  interface StrimzPayButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onError'> {
73
78
  /** Strimz session id created via `@strimz/sdk` on your backend. */
74
79
  sessionId: string;
@@ -118,10 +123,72 @@ interface StrimzCheckoutEmbedProps {
118
123
  */
119
124
  declare function StrimzCheckoutEmbed({ sessionId, onSuccess, onCancel, onError, className, style, height, theme, }: StrimzCheckoutEmbedProps): react_jsx_runtime.JSX.Element;
120
125
 
126
+ declare class StrimzSubscriptionError extends Error {
127
+ readonly code: SubscriptionResultErrorCode;
128
+ readonly planId: string;
129
+ constructor(code: SubscriptionResultErrorCode, planId: string, message: string);
130
+ }
131
+
132
+ interface StrimzSubscriptionResult {
133
+ /** Null when Strimz hadn't recorded the subscription yet; use the webhook. */
134
+ subscriptionId: string | null;
135
+ txHash: string;
136
+ }
137
+ interface UseStrimzSubscribeOptions {
138
+ /** How to open the plan checkout. Default `popup`. */
139
+ mode?: CheckoutMode;
140
+ onSuccess?: (result: StrimzSubscriptionResult) => void;
141
+ /** Called when the payer cancels or closes the popup. */
142
+ onCancel?: () => void;
143
+ onError?: (error: StrimzSubscriptionError) => void;
144
+ }
145
+ interface UseStrimzSubscribeResult {
146
+ open: (planId: string) => void;
147
+ close: () => void;
148
+ status: HostedPopupStatus;
149
+ }
150
+ /**
151
+ * const subscribe = useStrimzSubscribe({ onSuccess: ({ subscriptionId }) => ... })
152
+ * <button onClick={() => subscribe.open(planId)}>Subscribe</button>
153
+ */
154
+ declare function useStrimzSubscribe(options?: UseStrimzSubscribeOptions): UseStrimzSubscribeResult;
155
+
156
+ interface StrimzSubscribeButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onError'> {
157
+ /** Plan id from the dashboard or `subscriptionPlans.create`. */
158
+ planId: string;
159
+ /** Hosted checkout open mode. Default `popup`. */
160
+ mode?: CheckoutMode;
161
+ theme?: StrimzButtonTheme;
162
+ size?: StrimzButtonSize;
163
+ onSuccess?: (result: StrimzSubscriptionResult) => void;
164
+ onCancel?: () => void;
165
+ onError?: (error: StrimzSubscriptionError) => void;
166
+ /** Override the button label. Default "Subscribe with Strimz". */
167
+ label?: React.ReactNode;
168
+ }
169
+ /**
170
+ * <StrimzSubscribeButton planId={plan.id} onSuccess={({ subscriptionId }) => ...} />
171
+ */
172
+ declare const StrimzSubscribeButton: React.ForwardRefExoticComponent<StrimzSubscribeButtonProps & React.RefAttributes<HTMLButtonElement>>;
173
+
174
+ interface StrimzSubscriptionEmbedProps {
175
+ planId: string;
176
+ onSuccess?: (result: StrimzSubscriptionResult) => void;
177
+ onCancel?: () => void;
178
+ onError?: (error: StrimzSubscriptionError) => void;
179
+ className?: string;
180
+ style?: React.CSSProperties;
181
+ /** Iframe height. Default 640. */
182
+ height?: number | string;
183
+ theme?: StrimzCheckoutTheme;
184
+ }
185
+ /** Results only count from this iframe, for this plan. */
186
+ declare function StrimzSubscriptionEmbed({ planId, onSuccess, onCancel, onError, className, style, height, theme, }: StrimzSubscriptionEmbedProps): react_jsx_runtime.JSX.Element;
187
+
121
188
  declare class StrimzCheckoutError extends Error {
122
189
  readonly code: CheckoutResultErrorCode;
123
190
  readonly sessionId: string;
124
191
  constructor(code: CheckoutResultErrorCode, sessionId: string, message: string);
125
192
  }
126
193
 
127
- export { type CheckoutMode, StrimzCheckoutEmbed, type StrimzCheckoutEmbedProps, StrimzCheckoutError, type StrimzCheckoutTheme, type StrimzContextValue, StrimzPayButton, type StrimzPayButtonProps, type StrimzPayButtonSize, type StrimzPayButtonTheme, StrimzProvider, type StrimzProviderProps, type UseStrimzCheckoutOptions, type UseStrimzCheckoutResult, type UseStrimzSessionOptions, type UseStrimzSessionResult, useStrimzCheckout, useStrimzClient, useStrimzContext, useStrimzSession };
194
+ export { type CheckoutMode, StrimzCheckoutEmbed, type StrimzCheckoutEmbedProps, StrimzCheckoutError, type StrimzCheckoutTheme, type StrimzContextValue, StrimzPayButton, type StrimzPayButtonProps, type StrimzPayButtonSize, type StrimzPayButtonTheme, StrimzProvider, type StrimzProviderProps, StrimzSubscribeButton, type StrimzSubscribeButtonProps, StrimzSubscriptionEmbed, type StrimzSubscriptionEmbedProps, StrimzSubscriptionError, type StrimzSubscriptionResult, type UseStrimzCheckoutOptions, type UseStrimzCheckoutResult, type UseStrimzSessionOptions, type UseStrimzSessionResult, type UseStrimzSubscribeOptions, type UseStrimzSubscribeResult, useStrimzCheckout, useStrimzClient, useStrimzContext, useStrimzSession, useStrimzSubscribe };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React3 from 'react';
2
2
  import { StrimzBrowserClient } from '@strimz/sdk/browser';
3
- import { checkoutResultMessageSchema } from '@strimz/shared-types';
3
+ import { checkoutResultMessageSchema, subscriptionResultMessageSchema } from '@strimz/shared-types';
4
4
  import { jsx, jsxs } from 'react/jsx-runtime';
5
5
 
6
6
  var DEFAULT_CHECKOUT_ORIGIN = "https://www.strimz.finance";
@@ -28,7 +28,20 @@ function readCheckoutResult(ev, expected) {
28
28
  return parsed.data;
29
29
  }
30
30
  function checkoutUrl(checkoutOrigin, sessionId, host) {
31
- const url = new URL(`${checkoutOrigin}/pay/${encodeURIComponent(sessionId)}`);
31
+ return hostedUrl(checkoutOrigin, `/pay/${encodeURIComponent(sessionId)}`, host);
32
+ }
33
+ function subscriptionUrl(checkoutOrigin, planId, host) {
34
+ return hostedUrl(checkoutOrigin, `/sub/${encodeURIComponent(planId)}`, host);
35
+ }
36
+ function readSubscriptionResult(ev, expected) {
37
+ if (ev.origin !== expected.origin) return null;
38
+ if (!expected.source || ev.source !== expected.source) return null;
39
+ const parsed = subscriptionResultMessageSchema.safeParse(ev.data);
40
+ if (!parsed.success || parsed.data.planId !== expected.planId) return null;
41
+ return parsed.data;
42
+ }
43
+ function hostedUrl(checkoutOrigin, path, host) {
44
+ const url = new URL(`${checkoutOrigin}${path}`);
32
45
  if (host) {
33
46
  url.searchParams.set(host.mode, "1");
34
47
  url.searchParams.set("parent", host.parentOrigin);
@@ -123,16 +136,14 @@ var StrimzCheckoutError = class extends Error {
123
136
  code;
124
137
  sessionId;
125
138
  };
126
-
127
- // src/hooks/useStrimzCheckout.ts
128
- function useStrimzCheckout(options = {}) {
139
+ function useHostedPopup(config) {
129
140
  const { checkoutOrigin } = useStrimzContext();
130
141
  const [status, setStatus] = React3.useState("idle");
131
142
  const statusRef = React3.useRef(status);
132
143
  const popupRef = React3.useRef(null);
133
- const sessionIdRef = React3.useRef(null);
134
- const optsRef = React3.useRef(options);
135
- optsRef.current = options;
144
+ const idRef = React3.useRef(null);
145
+ const configRef = React3.useRef(config);
146
+ configRef.current = config;
136
147
  const close = React3.useCallback(() => {
137
148
  if (popupRef.current && !popupRef.current.closed) {
138
149
  popupRef.current.close();
@@ -145,49 +156,32 @@ function useStrimzCheckout(options = {}) {
145
156
  }, []);
146
157
  React3.useEffect(() => {
147
158
  function onMessage(ev) {
148
- const data = readCheckoutResult(ev, {
159
+ const data = configRef.current.read(ev, {
149
160
  origin: checkoutOrigin,
150
161
  source: popupRef.current,
151
- sessionId: sessionIdRef.current
162
+ id: idRef.current
152
163
  });
153
- if (!data) return;
154
- switch (data.type) {
155
- case "strimz:checkout:success":
156
- transition("success");
157
- optsRef.current.onSuccess?.(data.txHash);
158
- close();
159
- break;
160
- case "strimz:checkout:cancel":
161
- transition("cancelled");
162
- optsRef.current.onCancel?.();
163
- close();
164
- break;
165
- case "strimz:checkout:error":
166
- transition("error");
167
- optsRef.current.onError?.(
168
- new StrimzCheckoutError(data.code, data.sessionId, data.message)
169
- );
170
- close();
171
- break;
172
- }
164
+ if (data === null) return;
165
+ transition(configRef.current.onMessage(data));
166
+ close();
173
167
  }
174
168
  window.addEventListener("message", onMessage);
175
169
  return () => window.removeEventListener("message", onMessage);
176
170
  }, [checkoutOrigin, close, transition]);
177
171
  const open = React3.useCallback(
178
- (sessionId) => {
179
- const mode = optsRef.current.mode ?? "popup";
172
+ (id) => {
173
+ const { url, mode = "popup" } = configRef.current;
180
174
  if (mode === "redirect") {
181
- window.location.assign(checkoutUrl(checkoutOrigin, sessionId, null));
175
+ window.location.assign(url(checkoutOrigin, id, null));
182
176
  return;
183
177
  }
184
- const url = checkoutUrl(checkoutOrigin, sessionId, {
185
- mode: "popup",
186
- parentOrigin: window.location.origin
187
- });
188
- const w = window.open(url, "strimz-checkout", "width=480,height=720");
178
+ const w = window.open(
179
+ url(checkoutOrigin, id, { mode: "popup", parentOrigin: window.location.origin }),
180
+ "strimz-checkout",
181
+ "width=480,height=720"
182
+ );
189
183
  popupRef.current = w;
190
- sessionIdRef.current = sessionId;
184
+ idRef.current = id;
191
185
  transition("open");
192
186
  const closeWatch = setInterval(() => {
193
187
  if (!w || w.closed) {
@@ -197,7 +191,7 @@ function useStrimzCheckout(options = {}) {
197
191
  popupRef.current = null;
198
192
  if (wasOpen) {
199
193
  transition("cancelled");
200
- optsRef.current.onCancel?.();
194
+ configRef.current.onClosed?.();
201
195
  }
202
196
  }
203
197
  }
@@ -207,45 +201,29 @@ function useStrimzCheckout(options = {}) {
207
201
  );
208
202
  return { open, close, status };
209
203
  }
210
- var StrimzPayButton = React3.forwardRef(
211
- function StrimzPayButton2({
212
- sessionId,
213
- mode = "popup",
214
- theme = "auto",
215
- size = "md",
216
- onSuccess,
217
- onCancel,
218
- onError,
219
- label,
220
- onClick,
221
- style,
222
- ...rest
223
- }, ref) {
224
- const checkout = useStrimzCheckout({ mode, onSuccess, onCancel, onError });
225
- const handleClick = (ev) => {
226
- onClick?.(ev);
227
- if (ev.defaultPrevented || !sessionId) return;
228
- checkout.open(sessionId);
229
- };
230
- return /* @__PURE__ */ jsx(
231
- "button",
232
- {
233
- ref,
234
- type: "button",
235
- onClick: handleClick,
236
- style: { ...buttonStyle({ theme, size }), ...style ?? {} },
237
- "data-strimz-pay-button": true,
238
- "data-status": checkout.status,
239
- ...rest,
240
- children: label ?? defaultLabel
204
+
205
+ // src/hooks/useStrimzCheckout.ts
206
+ function useStrimzCheckout(options = {}) {
207
+ return useHostedPopup({
208
+ url: checkoutUrl,
209
+ read: (ev, expected) => readCheckoutResult(ev, { ...expected, sessionId: expected.id }),
210
+ mode: options.mode,
211
+ onClosed: options.onCancel,
212
+ onMessage: (data) => {
213
+ switch (data.type) {
214
+ case "strimz:checkout:success":
215
+ options.onSuccess?.(data.txHash);
216
+ return "success";
217
+ case "strimz:checkout:cancel":
218
+ options.onCancel?.();
219
+ return "cancelled";
220
+ case "strimz:checkout:error":
221
+ options.onError?.(new StrimzCheckoutError(data.code, data.sessionId, data.message));
222
+ return "error";
241
223
  }
242
- );
243
- }
244
- );
245
- var defaultLabel = /* @__PURE__ */ jsxs("span", { style: { display: "inline-flex", alignItems: "center", gap: 8 }, children: [
246
- /* @__PURE__ */ jsx(StrimzMark, { size: 16 }),
247
- "Pay with Strimz"
248
- ] });
224
+ }
225
+ });
226
+ }
249
227
  function StrimzMark({ size }) {
250
228
  return /* @__PURE__ */ jsxs(
251
229
  "svg",
@@ -263,7 +241,7 @@ function StrimzMark({ size }) {
263
241
  }
264
242
  );
265
243
  }
266
- function buttonStyle({ theme, size }) {
244
+ function strimzButtonStyle({ theme, size }) {
267
245
  const dimensions = sizeMap[size];
268
246
  const palette = paletteFor(theme);
269
247
  return {
@@ -293,6 +271,45 @@ function paletteFor(theme) {
293
271
  if (theme === "dark") return { background: "#02C76A", foreground: "#FFFFFF" };
294
272
  return { background: "#02C76A", foreground: "#FFFFFF" };
295
273
  }
274
+ var StrimzPayButton = React3.forwardRef(
275
+ function StrimzPayButton2({
276
+ sessionId,
277
+ mode = "popup",
278
+ theme = "auto",
279
+ size = "md",
280
+ onSuccess,
281
+ onCancel,
282
+ onError,
283
+ label,
284
+ onClick,
285
+ style,
286
+ ...rest
287
+ }, ref) {
288
+ const checkout = useStrimzCheckout({ mode, onSuccess, onCancel, onError });
289
+ const handleClick = (ev) => {
290
+ onClick?.(ev);
291
+ if (ev.defaultPrevented || !sessionId) return;
292
+ checkout.open(sessionId);
293
+ };
294
+ return /* @__PURE__ */ jsx(
295
+ "button",
296
+ {
297
+ ref,
298
+ type: "button",
299
+ onClick: handleClick,
300
+ style: { ...strimzButtonStyle({ theme, size }), ...style ?? {} },
301
+ "data-strimz-pay-button": true,
302
+ "data-status": checkout.status,
303
+ ...rest,
304
+ children: label ?? defaultLabel
305
+ }
306
+ );
307
+ }
308
+ );
309
+ var defaultLabel = /* @__PURE__ */ jsxs("span", { style: { display: "inline-flex", alignItems: "center", gap: 8 }, children: [
310
+ /* @__PURE__ */ jsx(StrimzMark, { size: 16 }),
311
+ "Pay with Strimz"
312
+ ] });
296
313
  function StrimzCheckoutEmbed({
297
314
  sessionId,
298
315
  onSuccess,
@@ -350,6 +367,137 @@ function StrimzCheckoutEmbed({
350
367
  ) });
351
368
  }
352
369
 
353
- export { StrimzCheckoutEmbed, StrimzCheckoutError, StrimzPayButton, StrimzProvider, useStrimzCheckout, useStrimzClient, useStrimzContext, useStrimzSession };
370
+ // src/subscription-error.ts
371
+ var StrimzSubscriptionError = class extends Error {
372
+ constructor(code, planId, message) {
373
+ super(message);
374
+ this.code = code;
375
+ this.planId = planId;
376
+ this.name = "StrimzSubscriptionError";
377
+ }
378
+ code;
379
+ planId;
380
+ };
381
+
382
+ // src/hooks/useStrimzSubscribe.ts
383
+ function useStrimzSubscribe(options = {}) {
384
+ return useHostedPopup({
385
+ url: subscriptionUrl,
386
+ read: (ev, expected) => readSubscriptionResult(ev, { ...expected, planId: expected.id }),
387
+ mode: options.mode,
388
+ onClosed: options.onCancel,
389
+ onMessage: (data) => {
390
+ switch (data.type) {
391
+ case "strimz:subscription:success":
392
+ options.onSuccess?.({ subscriptionId: data.subscriptionId, txHash: data.txHash });
393
+ return "success";
394
+ case "strimz:subscription:cancel":
395
+ options.onCancel?.();
396
+ return "cancelled";
397
+ case "strimz:subscription:error":
398
+ options.onError?.(new StrimzSubscriptionError(data.code, data.planId, data.message));
399
+ return "error";
400
+ }
401
+ }
402
+ });
403
+ }
404
+ var StrimzSubscribeButton = React3.forwardRef(function StrimzSubscribeButton2({
405
+ planId,
406
+ mode = "popup",
407
+ theme = "auto",
408
+ size = "md",
409
+ onSuccess,
410
+ onCancel,
411
+ onError,
412
+ label,
413
+ onClick,
414
+ style,
415
+ ...rest
416
+ }, ref) {
417
+ const subscribe = useStrimzSubscribe({ mode, onSuccess, onCancel, onError });
418
+ const handleClick = (ev) => {
419
+ onClick?.(ev);
420
+ if (ev.defaultPrevented || !planId) return;
421
+ subscribe.open(planId);
422
+ };
423
+ return /* @__PURE__ */ jsx(
424
+ "button",
425
+ {
426
+ ref,
427
+ type: "button",
428
+ onClick: handleClick,
429
+ style: { ...strimzButtonStyle({ theme, size }), ...style ?? {} },
430
+ "data-strimz-subscribe-button": true,
431
+ "data-status": subscribe.status,
432
+ ...rest,
433
+ children: label ?? defaultLabel2
434
+ }
435
+ );
436
+ });
437
+ var defaultLabel2 = /* @__PURE__ */ jsxs("span", { style: { display: "inline-flex", alignItems: "center", gap: 8 }, children: [
438
+ /* @__PURE__ */ jsx(StrimzMark, { size: 16 }),
439
+ "Subscribe with Strimz"
440
+ ] });
441
+ function StrimzSubscriptionEmbed({
442
+ planId,
443
+ onSuccess,
444
+ onCancel,
445
+ onError,
446
+ className,
447
+ style,
448
+ height = 640,
449
+ theme
450
+ }) {
451
+ const { checkoutOrigin } = useStrimzContext();
452
+ const frameRef = React3.useRef(null);
453
+ const [parentOrigin, setParentOrigin] = React3.useState(null);
454
+ const handlersRef = React3.useRef({ onSuccess, onCancel, onError });
455
+ handlersRef.current = { onSuccess, onCancel, onError };
456
+ React3.useEffect(() => {
457
+ setParentOrigin(window.location.origin);
458
+ }, []);
459
+ React3.useEffect(() => {
460
+ function onMessage(ev) {
461
+ const data = readSubscriptionResult(ev, {
462
+ origin: checkoutOrigin,
463
+ source: frameRef.current?.contentWindow ?? null,
464
+ planId
465
+ });
466
+ if (!data) return;
467
+ switch (data.type) {
468
+ case "strimz:subscription:success":
469
+ handlersRef.current.onSuccess?.({
470
+ subscriptionId: data.subscriptionId,
471
+ txHash: data.txHash
472
+ });
473
+ break;
474
+ case "strimz:subscription:cancel":
475
+ handlersRef.current.onCancel?.();
476
+ break;
477
+ case "strimz:subscription:error":
478
+ handlersRef.current.onError?.(
479
+ new StrimzSubscriptionError(data.code, data.planId, data.message)
480
+ );
481
+ break;
482
+ }
483
+ }
484
+ window.addEventListener("message", onMessage);
485
+ return () => window.removeEventListener("message", onMessage);
486
+ }, [checkoutOrigin, planId]);
487
+ const src = parentOrigin ? subscriptionUrl(checkoutOrigin, planId, { mode: "embed", parentOrigin, theme }) : void 0;
488
+ return /* @__PURE__ */ jsx("div", { className, style: { width: "100%", ...style ?? {} }, children: /* @__PURE__ */ jsx(
489
+ "iframe",
490
+ {
491
+ ref: frameRef,
492
+ src,
493
+ title: "Strimz subscription checkout",
494
+ allow: "payment *; clipboard-write *",
495
+ style: { width: "100%", height, border: 0 },
496
+ "data-strimz-subscription-embed": true
497
+ }
498
+ ) });
499
+ }
500
+
501
+ export { StrimzCheckoutEmbed, StrimzCheckoutError, StrimzPayButton, StrimzProvider, StrimzSubscribeButton, StrimzSubscriptionEmbed, StrimzSubscriptionError, useStrimzCheckout, useStrimzClient, useStrimzContext, useStrimzSession, useStrimzSubscribe };
354
502
  //# sourceMappingURL=index.js.map
355
503
  //# sourceMappingURL=index.js.map