@strimz/sdk-react 0.3.8 → 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/README.md +25 -3
- package/dist/index.cjs +230 -78
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +72 -5
- package/dist/index.d.ts +72 -5
- package/dist/index.js +228 -80
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/checkout-message.ts +37 -3
- package/src/components/StrimzPayButton.tsx +9 -60
- package/src/components/StrimzSubscribeButton.tsx +81 -0
- package/src/components/StrimzSubscriptionEmbed.tsx +89 -0
- package/src/components/button-style.tsx +61 -0
- package/src/hooks/useHostedPopup.ts +91 -0
- package/src/hooks/useStrimzCheckout.ts +16 -88
- package/src/hooks/useStrimzSubscribe.ts +54 -0
- package/src/index.ts +15 -0
- package/src/subscription-error.ts +12 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { useStrimzContext } from '../provider.js'
|
|
5
|
+
import type { CheckoutHostParams } from '../checkout-message.js'
|
|
6
|
+
|
|
7
|
+
export type CheckoutMode = 'redirect' | 'popup'
|
|
8
|
+
export type HostedPopupStatus = 'idle' | 'open' | 'success' | 'cancelled' | 'error'
|
|
9
|
+
type Outcome = Exclude<HostedPopupStatus, 'idle' | 'open'>
|
|
10
|
+
|
|
11
|
+
/** The popup plumbing `useStrimzCheckout` and `useStrimzSubscribe` share. */
|
|
12
|
+
export function useHostedPopup<M>(config: {
|
|
13
|
+
url: (checkoutOrigin: string, id: string, host: CheckoutHostParams | null) => string
|
|
14
|
+
read: (
|
|
15
|
+
ev: MessageEvent,
|
|
16
|
+
expected: { origin: string; source: MessageEventSource | null; id: string | null },
|
|
17
|
+
) => M | null
|
|
18
|
+
mode?: CheckoutMode
|
|
19
|
+
onMessage: (message: M) => Outcome
|
|
20
|
+
onClosed?: () => void
|
|
21
|
+
}) {
|
|
22
|
+
const { checkoutOrigin } = useStrimzContext()
|
|
23
|
+
const [status, setStatus] = React.useState<HostedPopupStatus>('idle')
|
|
24
|
+
const statusRef = React.useRef(status)
|
|
25
|
+
const popupRef = React.useRef<Window | null>(null)
|
|
26
|
+
const idRef = React.useRef<string | null>(null)
|
|
27
|
+
const configRef = React.useRef(config)
|
|
28
|
+
configRef.current = config
|
|
29
|
+
|
|
30
|
+
const close = React.useCallback(() => {
|
|
31
|
+
if (popupRef.current && !popupRef.current.closed) {
|
|
32
|
+
popupRef.current.close()
|
|
33
|
+
}
|
|
34
|
+
popupRef.current = null
|
|
35
|
+
}, [])
|
|
36
|
+
|
|
37
|
+
const transition = React.useCallback((next: HostedPopupStatus) => {
|
|
38
|
+
statusRef.current = next
|
|
39
|
+
setStatus(next)
|
|
40
|
+
}, [])
|
|
41
|
+
|
|
42
|
+
React.useEffect(() => {
|
|
43
|
+
function onMessage(ev: MessageEvent) {
|
|
44
|
+
const data = configRef.current.read(ev, {
|
|
45
|
+
origin: checkoutOrigin,
|
|
46
|
+
source: popupRef.current,
|
|
47
|
+
id: idRef.current,
|
|
48
|
+
})
|
|
49
|
+
if (data === null) return
|
|
50
|
+
transition(configRef.current.onMessage(data))
|
|
51
|
+
close()
|
|
52
|
+
}
|
|
53
|
+
window.addEventListener('message', onMessage)
|
|
54
|
+
return () => window.removeEventListener('message', onMessage)
|
|
55
|
+
}, [checkoutOrigin, close, transition])
|
|
56
|
+
|
|
57
|
+
const open = React.useCallback(
|
|
58
|
+
(id: string) => {
|
|
59
|
+
const { url, mode = 'popup' } = configRef.current
|
|
60
|
+
if (mode === 'redirect') {
|
|
61
|
+
window.location.assign(url(checkoutOrigin, id, null))
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
const w = window.open(
|
|
65
|
+
url(checkoutOrigin, id, { mode: 'popup', parentOrigin: window.location.origin }),
|
|
66
|
+
'strimz-checkout',
|
|
67
|
+
'width=480,height=720',
|
|
68
|
+
)
|
|
69
|
+
popupRef.current = w
|
|
70
|
+
idRef.current = id
|
|
71
|
+
transition('open')
|
|
72
|
+
const closeWatch = setInterval(() => {
|
|
73
|
+
if (!w || w.closed) {
|
|
74
|
+
clearInterval(closeWatch)
|
|
75
|
+
if (popupRef.current === w) {
|
|
76
|
+
// A result message already moved us out of `open`; that isn't a cancel.
|
|
77
|
+
const wasOpen = statusRef.current === 'open'
|
|
78
|
+
popupRef.current = null
|
|
79
|
+
if (wasOpen) {
|
|
80
|
+
transition('cancelled')
|
|
81
|
+
configRef.current.onClosed?.()
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}, 500)
|
|
86
|
+
},
|
|
87
|
+
[checkoutOrigin, transition],
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
return { open, close, status }
|
|
91
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import * as React from 'react'
|
|
4
|
-
import { useStrimzContext } from '../provider.js'
|
|
5
3
|
import { StrimzCheckoutError } from '../checkout-error.js'
|
|
6
4
|
import { checkoutUrl, readCheckoutResult } from '../checkout-message.js'
|
|
5
|
+
import { useHostedPopup, type CheckoutMode, type HostedPopupStatus } from './useHostedPopup.js'
|
|
7
6
|
|
|
8
|
-
export type CheckoutMode
|
|
7
|
+
export type { CheckoutMode }
|
|
9
8
|
|
|
10
9
|
export interface UseStrimzCheckoutOptions {
|
|
11
10
|
/** How to open the hosted checkout. Default `popup`. */
|
|
@@ -21,7 +20,7 @@ export interface UseStrimzCheckoutOptions {
|
|
|
21
20
|
export interface UseStrimzCheckoutResult {
|
|
22
21
|
open: (sessionId: string) => void
|
|
23
22
|
close: () => void
|
|
24
|
-
status:
|
|
23
|
+
status: HostedPopupStatus
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
/**
|
|
@@ -31,94 +30,23 @@ export interface UseStrimzCheckoutResult {
|
|
|
31
30
|
* <button onClick={() => checkout.open(sessionId)}>Pay</button>
|
|
32
31
|
*/
|
|
33
32
|
export function useStrimzCheckout(options: UseStrimzCheckoutOptions = {}): UseStrimzCheckoutResult {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
optsRef.current = options
|
|
41
|
-
|
|
42
|
-
const close = React.useCallback(() => {
|
|
43
|
-
if (popupRef.current && !popupRef.current.closed) {
|
|
44
|
-
popupRef.current.close()
|
|
45
|
-
}
|
|
46
|
-
popupRef.current = null
|
|
47
|
-
}, [])
|
|
48
|
-
|
|
49
|
-
const transition = React.useCallback((next: UseStrimzCheckoutResult['status']) => {
|
|
50
|
-
statusRef.current = next
|
|
51
|
-
setStatus(next)
|
|
52
|
-
}, [])
|
|
53
|
-
|
|
54
|
-
React.useEffect(() => {
|
|
55
|
-
function onMessage(ev: MessageEvent) {
|
|
56
|
-
const data = readCheckoutResult(ev, {
|
|
57
|
-
origin: checkoutOrigin,
|
|
58
|
-
source: popupRef.current,
|
|
59
|
-
sessionId: sessionIdRef.current,
|
|
60
|
-
})
|
|
61
|
-
if (!data) return
|
|
62
|
-
|
|
33
|
+
return useHostedPopup({
|
|
34
|
+
url: checkoutUrl,
|
|
35
|
+
read: (ev, expected) => readCheckoutResult(ev, { ...expected, sessionId: expected.id }),
|
|
36
|
+
mode: options.mode,
|
|
37
|
+
onClosed: options.onCancel,
|
|
38
|
+
onMessage: (data) => {
|
|
63
39
|
switch (data.type) {
|
|
64
40
|
case 'strimz:checkout:success':
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
close()
|
|
68
|
-
break
|
|
41
|
+
options.onSuccess?.(data.txHash)
|
|
42
|
+
return 'success'
|
|
69
43
|
case 'strimz:checkout:cancel':
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
close()
|
|
73
|
-
break
|
|
44
|
+
options.onCancel?.()
|
|
45
|
+
return 'cancelled'
|
|
74
46
|
case 'strimz:checkout:error':
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
new StrimzCheckoutError(data.code, data.sessionId, data.message),
|
|
78
|
-
)
|
|
79
|
-
close()
|
|
80
|
-
break
|
|
47
|
+
options.onError?.(new StrimzCheckoutError(data.code, data.sessionId, data.message))
|
|
48
|
+
return 'error'
|
|
81
49
|
}
|
|
82
|
-
}
|
|
83
|
-
window.addEventListener('message', onMessage)
|
|
84
|
-
return () => window.removeEventListener('message', onMessage)
|
|
85
|
-
}, [checkoutOrigin, close, transition])
|
|
86
|
-
|
|
87
|
-
const open = React.useCallback(
|
|
88
|
-
(sessionId: string) => {
|
|
89
|
-
// Payment sessions are hosted at /pay/{id}. (Subscriptions use /sub/{planId},
|
|
90
|
-
// which is a plan-link flow, not this payment-session primitive.)
|
|
91
|
-
const mode = optsRef.current.mode ?? 'popup'
|
|
92
|
-
if (mode === 'redirect') {
|
|
93
|
-
window.location.assign(checkoutUrl(checkoutOrigin, sessionId, null))
|
|
94
|
-
return
|
|
95
|
-
}
|
|
96
|
-
const url = checkoutUrl(checkoutOrigin, sessionId, {
|
|
97
|
-
mode: 'popup',
|
|
98
|
-
parentOrigin: window.location.origin,
|
|
99
|
-
})
|
|
100
|
-
const w = window.open(url, 'strimz-checkout', 'width=480,height=720')
|
|
101
|
-
popupRef.current = w
|
|
102
|
-
sessionIdRef.current = sessionId
|
|
103
|
-
transition('open')
|
|
104
|
-
// Detect manual close.
|
|
105
|
-
const closeWatch = setInterval(() => {
|
|
106
|
-
if (!w || w.closed) {
|
|
107
|
-
clearInterval(closeWatch)
|
|
108
|
-
if (popupRef.current === w) {
|
|
109
|
-
// Only treat as cancel if we haven't already transitioned out of `open`.
|
|
110
|
-
const wasOpen = statusRef.current === 'open'
|
|
111
|
-
popupRef.current = null
|
|
112
|
-
if (wasOpen) {
|
|
113
|
-
transition('cancelled')
|
|
114
|
-
optsRef.current.onCancel?.()
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}, 500)
|
|
119
50
|
},
|
|
120
|
-
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
return { open, close, status }
|
|
51
|
+
})
|
|
124
52
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { StrimzSubscriptionError } from '../subscription-error.js'
|
|
4
|
+
import { readSubscriptionResult, subscriptionUrl } from '../checkout-message.js'
|
|
5
|
+
import { useHostedPopup, type CheckoutMode, type HostedPopupStatus } from './useHostedPopup.js'
|
|
6
|
+
|
|
7
|
+
export interface StrimzSubscriptionResult {
|
|
8
|
+
/** Null when Strimz hadn't recorded the subscription yet; use the webhook. */
|
|
9
|
+
subscriptionId: string | null
|
|
10
|
+
txHash: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface UseStrimzSubscribeOptions {
|
|
14
|
+
/** How to open the plan checkout. Default `popup`. */
|
|
15
|
+
mode?: CheckoutMode
|
|
16
|
+
onSuccess?: (result: StrimzSubscriptionResult) => void
|
|
17
|
+
/** Called when the payer cancels or closes the popup. */
|
|
18
|
+
onCancel?: () => void
|
|
19
|
+
onError?: (error: StrimzSubscriptionError) => void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface UseStrimzSubscribeResult {
|
|
23
|
+
open: (planId: string) => void
|
|
24
|
+
close: () => void
|
|
25
|
+
status: HostedPopupStatus
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* const subscribe = useStrimzSubscribe({ onSuccess: ({ subscriptionId }) => ... })
|
|
30
|
+
* <button onClick={() => subscribe.open(planId)}>Subscribe</button>
|
|
31
|
+
*/
|
|
32
|
+
export function useStrimzSubscribe(
|
|
33
|
+
options: UseStrimzSubscribeOptions = {},
|
|
34
|
+
): UseStrimzSubscribeResult {
|
|
35
|
+
return useHostedPopup({
|
|
36
|
+
url: subscriptionUrl,
|
|
37
|
+
read: (ev, expected) => readSubscriptionResult(ev, { ...expected, planId: expected.id }),
|
|
38
|
+
mode: options.mode,
|
|
39
|
+
onClosed: options.onCancel,
|
|
40
|
+
onMessage: (data) => {
|
|
41
|
+
switch (data.type) {
|
|
42
|
+
case 'strimz:subscription:success':
|
|
43
|
+
options.onSuccess?.({ subscriptionId: data.subscriptionId, txHash: data.txHash })
|
|
44
|
+
return 'success'
|
|
45
|
+
case 'strimz:subscription:cancel':
|
|
46
|
+
options.onCancel?.()
|
|
47
|
+
return 'cancelled'
|
|
48
|
+
case 'strimz:subscription:error':
|
|
49
|
+
options.onError?.(new StrimzSubscriptionError(data.code, data.planId, data.message))
|
|
50
|
+
return 'error'
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -36,4 +36,19 @@ export {
|
|
|
36
36
|
type StrimzCheckoutEmbedProps,
|
|
37
37
|
type StrimzCheckoutTheme,
|
|
38
38
|
} from './components/StrimzCheckoutEmbed.js'
|
|
39
|
+
export {
|
|
40
|
+
useStrimzSubscribe,
|
|
41
|
+
type UseStrimzSubscribeOptions,
|
|
42
|
+
type UseStrimzSubscribeResult,
|
|
43
|
+
type StrimzSubscriptionResult,
|
|
44
|
+
} from './hooks/useStrimzSubscribe.js'
|
|
45
|
+
export {
|
|
46
|
+
StrimzSubscribeButton,
|
|
47
|
+
type StrimzSubscribeButtonProps,
|
|
48
|
+
} from './components/StrimzSubscribeButton.js'
|
|
49
|
+
export {
|
|
50
|
+
StrimzSubscriptionEmbed,
|
|
51
|
+
type StrimzSubscriptionEmbedProps,
|
|
52
|
+
} from './components/StrimzSubscriptionEmbed.js'
|
|
39
53
|
export { StrimzCheckoutError } from './checkout-error.js'
|
|
54
|
+
export { StrimzSubscriptionError } from './subscription-error.js'
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SubscriptionResultErrorCode } from '@strimz/shared-types'
|
|
2
|
+
|
|
3
|
+
export class StrimzSubscriptionError extends Error {
|
|
4
|
+
constructor(
|
|
5
|
+
readonly code: SubscriptionResultErrorCode,
|
|
6
|
+
readonly planId: string,
|
|
7
|
+
message: string,
|
|
8
|
+
) {
|
|
9
|
+
super(message)
|
|
10
|
+
this.name = 'StrimzSubscriptionError'
|
|
11
|
+
}
|
|
12
|
+
}
|