@sunbay/checkout-sdk-react 0.0.0-stage → 0.5.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 +166 -2
- package/dist/index.cjs +629 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +107 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.mjs +581 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -3
package/README.md
CHANGED
|
@@ -1,3 +1,167 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sunbay/checkout-sdk-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React bindings for the Sunbay Checkout Web SDK. Payment fields render inside Sunbay-hosted
|
|
4
|
+
iframes, so card data never touches your page (PCI scope stays out of your integration). This
|
|
5
|
+
package wraps [`@sunbay/checkout-sdk-js`](../js/README.md) in a provider, Element components and
|
|
6
|
+
hooks.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i @sunbay/checkout-sdk-react @sunbay/checkout-sdk-js react react-dom
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
- `react` and `react-dom`: `>=16.9.0 <20.0.0` (tested on 16.14, 17, 18 and 19).
|
|
15
|
+
- `@sunbay/checkout-sdk-js` is a peer dependency. Install it next to this package. The js package
|
|
16
|
+
brings `@sunbay/checkout-sdk-protocol` with it.
|
|
17
|
+
- This package, the js package and the protocol package share one lockstep version number. Keep
|
|
18
|
+
them on the same version.
|
|
19
|
+
- The bundle starts with `"use client"` and uses the classic JSX runtime, so it works with every
|
|
20
|
+
supported React version without extra compiler settings.
|
|
21
|
+
|
|
22
|
+
## 5-minute example
|
|
23
|
+
|
|
24
|
+
The snippets in this README are minimal sketches. Replace the markup, loading states and error
|
|
25
|
+
handling with your own UI code.
|
|
26
|
+
|
|
27
|
+
1. **Server:** create the checkout session with `sessionType: 'embedded'` using your API key, and
|
|
28
|
+
return `sessionId` and `checkoutClientSecret` to the browser over an authenticated HTTPS
|
|
29
|
+
response. Never put the secret in a URL or in logs.
|
|
30
|
+
2. **Client:** wrap your payment UI in `CheckoutProvider`, render a card Element, and confirm with
|
|
31
|
+
`useConfirm`.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { CardElement, CheckoutProvider, useConfirm } from '@sunbay/checkout-sdk-react';
|
|
35
|
+
import type { CheckoutError } from '@sunbay/checkout-sdk-react';
|
|
36
|
+
|
|
37
|
+
function PayButton() {
|
|
38
|
+
const { confirm, isConfirming } = useConfirm();
|
|
39
|
+
|
|
40
|
+
async function onPay() {
|
|
41
|
+
try {
|
|
42
|
+
const outcome = await confirm();
|
|
43
|
+
if (outcome.status === 'succeeded') {
|
|
44
|
+
// Show the success page.
|
|
45
|
+
} else if (outcome.status === 'pending') {
|
|
46
|
+
showProcessing();
|
|
47
|
+
}
|
|
48
|
+
} catch (e) {
|
|
49
|
+
showError(e as CheckoutError);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<button type="button" disabled={isConfirming} onClick={onPay}>
|
|
55
|
+
Pay
|
|
56
|
+
</button>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function CheckoutPage(props: { sessionId: string; checkoutClientSecret: string }) {
|
|
61
|
+
return (
|
|
62
|
+
<CheckoutProvider
|
|
63
|
+
options={{
|
|
64
|
+
sessionId: props.sessionId,
|
|
65
|
+
checkoutClientSecret: props.checkoutClientSecret,
|
|
66
|
+
env: 'test',
|
|
67
|
+
}}
|
|
68
|
+
onError={(e) => console.error(e.code)}
|
|
69
|
+
>
|
|
70
|
+
<CardElement onChange={(e) => console.log(e)} onLoadError={(e) => console.error(e.code)} />
|
|
71
|
+
<PayButton />
|
|
72
|
+
</CheckoutProvider>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Split fields instead of the combined `CardElement`:
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
<>
|
|
81
|
+
<CardNumberElement />
|
|
82
|
+
<CardExpiryElement />
|
|
83
|
+
<CardCvcElement />
|
|
84
|
+
</>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### `pending` is an outcome, not an error
|
|
88
|
+
|
|
89
|
+
`confirm()` resolves with `outcome.status` of `succeeded`, `failed`, `canceled` or `pending`.
|
|
90
|
+
`pending` means the SDK could not learn the final state in time (for example a network failure
|
|
91
|
+
after submit). The buyer may already have paid, so do not let them pay again. Show a "processing"
|
|
92
|
+
state and rely on the `paymentResult` event or your backend's session query for the final result.
|
|
93
|
+
`useConfirm().outcome` also holds `pending`, and `error` stays `null` for it.
|
|
94
|
+
|
|
95
|
+
### Cancellation and Strict Mode
|
|
96
|
+
|
|
97
|
+
`CheckoutProvider` aborts an in-flight `createCheckout` through the js `signal` option when it
|
|
98
|
+
unmounts or when the identity options change. React 18 Strict Mode runs effects twice in
|
|
99
|
+
development, so the first creation is cancelled (`CANCELLED`) and silently replaced. `onError`
|
|
100
|
+
never receives `CANCELLED`, and no iframe is left behind.
|
|
101
|
+
|
|
102
|
+
## Express wallets (Apple Pay / Google Pay)
|
|
103
|
+
|
|
104
|
+
A wallet button click has no Promise, so listen to both `paymentResult` and `error`. Some
|
|
105
|
+
failures emit both events with the same `operationId`, so de-duplicate by `operationId` (the
|
|
106
|
+
same semantics as the [js README](../js/README.md#express-wallets)).
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
function ExpressWallets() {
|
|
110
|
+
const seen = useRef(new Set<string>());
|
|
111
|
+
|
|
112
|
+
useCheckoutEvent('paymentResult', (e) => {
|
|
113
|
+
if (seen.current.has(e.operationId)) return;
|
|
114
|
+
seen.current.add(e.operationId);
|
|
115
|
+
showOutcome(e.outcome);
|
|
116
|
+
});
|
|
117
|
+
useCheckoutEvent('error', (e) => {
|
|
118
|
+
if (e.operationId && seen.current.has(e.operationId)) return; // already handled
|
|
119
|
+
reportError(e.error, e.method, e.stage);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<ExpressCheckoutElement
|
|
124
|
+
options={{ buttonHeight: 44, paymentMethods: { applePay: true, googlePay: true } }}
|
|
125
|
+
onAvailabilityChange={(w) => console.log(w)}
|
|
126
|
+
/>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Render `ExpressWallets` inside `CheckoutProvider`. `useCheckoutEvent` always calls the latest
|
|
132
|
+
handler, and changing the handler does not resubscribe.
|
|
133
|
+
|
|
134
|
+
## Next.js (App Router)
|
|
135
|
+
|
|
136
|
+
The package bundle already starts with `"use client"`, so you can import it from a Server
|
|
137
|
+
Component file and render the components directly. Hooks and event handlers still need a Client
|
|
138
|
+
Component of your own (add `"use client"` to that file). Create the session in a Server Component
|
|
139
|
+
or Route Handler, and pass `sessionId` and `checkoutClientSecret` to the client component as props.
|
|
140
|
+
|
|
141
|
+
Function props (`onError`, `onWarning`, `onReady`, `onChange`, `onLoadError`, ...) cannot be passed
|
|
142
|
+
from a Server Component: React only serializes plain data across the server/client boundary. Render
|
|
143
|
+
`CheckoutProvider` and the Elements inside your own Client Component and define the callbacks
|
|
144
|
+
there.
|
|
145
|
+
|
|
146
|
+
## Stripe concept mapping
|
|
147
|
+
|
|
148
|
+
| Stripe (`@stripe/react-stripe-js`) | This package |
|
|
149
|
+
| --- | --- |
|
|
150
|
+
| `Elements` (with `stripe` and `clientSecret`) | `CheckoutProvider` (with `options`) |
|
|
151
|
+
| `useStripe()` / `useElements()` | `useCheckout()` returns `{ checkout, status, error }` |
|
|
152
|
+
| `stripe.confirmPayment(...)` | `useConfirm().confirm()` or `checkout.confirm()` |
|
|
153
|
+
| `CardElement` | `CardElement` |
|
|
154
|
+
| `CardNumberElement`, `CardExpiryElement`, `CardCvcElement` | Same names |
|
|
155
|
+
| `AddressElement` | `AddressElement` (`options.mode` is required) |
|
|
156
|
+
| `ExpressCheckoutElement` | `ExpressCheckoutElement` |
|
|
157
|
+
| `onReady`, `onChange`, `onFocus`, `onBlur`, `onLoadError` | Same prop names on every Element |
|
|
158
|
+
| `elements.getElement(...)` | `ref` on the Element component (a `CheckoutElement`) |
|
|
159
|
+
|
|
160
|
+
Differences: you do not fetch card data or tokens (card data never reaches your page), the
|
|
161
|
+
session is created on your server with an `embedded` session type, and `confirm()` can resolve
|
|
162
|
+
with `pending`.
|
|
163
|
+
|
|
164
|
+
## More
|
|
165
|
+
|
|
166
|
+
Every export, the js to react capability table, updatable options and error mapping are in
|
|
167
|
+
[docs/api.md](./docs/api.md).
|