payluk-escrow-inline-checkout 0.2.0 → 0.2.1
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 +196 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# Escrow Checkout JS/TS SDK
|
|
2
|
+
|
|
3
|
+
A lightweight client SDK that initializes your checkout configuration, creates a session via your API, and launches the escrow checkout widget. Includes a React hook for easy integration in React apps.
|
|
4
|
+
|
|
5
|
+
- Zero manual script tags: the widget script is loaded automatically.
|
|
6
|
+
- Promise-based API.
|
|
7
|
+
- First-class TypeScript types.
|
|
8
|
+
- Optional React hook with loading/error state.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm i payluk-escrow-inline-checkout
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quick Start (Vanilla JS/TS)
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// app.ts
|
|
20
|
+
import { initEscrowCheckout, pay } from 'payluk-escrow-inline-checkout';
|
|
21
|
+
|
|
22
|
+
// 1) Initialize once at app startup
|
|
23
|
+
initEscrowCheckout({
|
|
24
|
+
apiBaseUrl: 'https://api.example.com', // your backend base URL
|
|
25
|
+
publicKey: '<YOUR_PUBLISHABLE_KEY>' // publishable key only
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// 2) Trigger a payment flow (e.g., on a button click)
|
|
29
|
+
async function onPayClick() {
|
|
30
|
+
try {
|
|
31
|
+
await pay({
|
|
32
|
+
paymentToken: '<PAYMENT_TOKEN_FROM_YOUR_BACKEND_OR_PROVIDER>',
|
|
33
|
+
reference: '<ORDER_OR_REFERENCE_ID>',
|
|
34
|
+
redirectUrl: 'https://your-app.example.com/checkout/complete',
|
|
35
|
+
logoUrl: 'https://your-cdn.example.com/logo.png', // optional
|
|
36
|
+
brand: 'YourBrand', // optional
|
|
37
|
+
extra: { theme: 'dark' }, // optional
|
|
38
|
+
callback: (result) => {
|
|
39
|
+
console.log('Checkout result:', result);
|
|
40
|
+
},
|
|
41
|
+
onClose: () => {
|
|
42
|
+
console.log('Widget closed');
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
} catch (err) {
|
|
46
|
+
console.error('Payment failed:', err);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## React Usage
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
// CheckoutButton.tsx
|
|
55
|
+
import React from 'react';
|
|
56
|
+
import { useEscrowCheckout } from 'payluk-escrow-inline-checkout/react';
|
|
57
|
+
|
|
58
|
+
export function CheckoutButton() {
|
|
59
|
+
const { pay, loading, error, ready } = useEscrowCheckout();
|
|
60
|
+
|
|
61
|
+
const handleClick = async () => {
|
|
62
|
+
try {
|
|
63
|
+
await pay({
|
|
64
|
+
paymentToken: '<PAYMENT_TOKEN>',
|
|
65
|
+
reference: '<ORDER_OR_REFERENCE_ID>',
|
|
66
|
+
redirectUrl: 'https://your-app.example.com/checkout/complete',
|
|
67
|
+
logoUrl: 'https://your-cdn.example.com/logo.png',
|
|
68
|
+
brand: 'YourBrand',
|
|
69
|
+
extra: { theme: 'light' },
|
|
70
|
+
callback: (result) => console.log(result)
|
|
71
|
+
});
|
|
72
|
+
} catch {
|
|
73
|
+
// error is also exposed via `error` state
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<button onClick={handleClick} disabled={loading || !ready}>
|
|
79
|
+
{loading ? 'Processing…' : 'Pay now'}
|
|
80
|
+
{!ready && <span>Preparing checkout…</span>}
|
|
81
|
+
{error && <small style={{ color: 'red' }}>{error.message}</small>}
|
|
82
|
+
</button>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**Note:** In React apps, call `initEscrowCheckout(...)` once in your app bootstrap (e.g., in the root component or an app initializer). The hook uses that configuration.
|
|
88
|
+
|
|
89
|
+
**Important:**
|
|
90
|
+
- Only use publishable keys in the browser. Keep any secret keys on your server.
|
|
91
|
+
- Validate inputs on your backend and return the required session payload.
|
|
92
|
+
|
|
93
|
+
## API
|
|
94
|
+
|
|
95
|
+
### `initEscrowCheckout(config)`
|
|
96
|
+
|
|
97
|
+
Initializes the SDK. Must be called before any `pay(...)`.
|
|
98
|
+
|
|
99
|
+
**Required:**
|
|
100
|
+
- `apiBaseUrl`: `string` — your backend base URL (no trailing slash required)
|
|
101
|
+
- `publicKey`: `string` — publishable key only
|
|
102
|
+
|
|
103
|
+
**Advanced (optional):**
|
|
104
|
+
- `scriptUrlOverride?`: `string` — custom widget script URL
|
|
105
|
+
- `globalName?`: `string` — custom global widget function name
|
|
106
|
+
- `crossOrigin?`: `'' | 'anonymous' | 'use-credentials'` — script tag crossOrigin
|
|
107
|
+
|
|
108
|
+
**Example:**
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { initEscrowCheckout } from 'payluk-escrow-inline-checkout';
|
|
112
|
+
|
|
113
|
+
initEscrowCheckout({
|
|
114
|
+
apiBaseUrl: 'https://api.example.com',
|
|
115
|
+
publicKey: '<YOUR_PUBLISHABLE_KEY>',
|
|
116
|
+
// scriptUrlOverride: 'https://cdn.example.com/custom-widget.js',
|
|
117
|
+
// globalName: 'EscrowCheckout',
|
|
118
|
+
// crossOrigin: 'anonymous'
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### `pay(input): Promise<void>`
|
|
123
|
+
|
|
124
|
+
Creates a checkout session via your backend and opens the widget.
|
|
125
|
+
|
|
126
|
+
**Required:**
|
|
127
|
+
- `paymentToken`: `string`
|
|
128
|
+
- `reference`: `string`
|
|
129
|
+
- `redirectUrl`: `string`
|
|
130
|
+
|
|
131
|
+
**Optional:**
|
|
132
|
+
- `logoUrl?`: `string`
|
|
133
|
+
- `brand?`: `string`
|
|
134
|
+
- `extra?`: `Record<string, unknown>`
|
|
135
|
+
- `callback?`: `(result: unknown) => void`
|
|
136
|
+
- `onClose?`: `() => void`
|
|
137
|
+
|
|
138
|
+
**Returns:**
|
|
139
|
+
- `Promise<void>` that resolves when the widget is opened (and rejects on errors).
|
|
140
|
+
|
|
141
|
+
### `useEscrowCheckout(): { ready, loading, error, pay }`
|
|
142
|
+
|
|
143
|
+
React hook that exposes:
|
|
144
|
+
- `ready`: `boolean` — becomes true after a successful load/pay attempt
|
|
145
|
+
- `loading`: `boolean` — true while pay is running
|
|
146
|
+
- `error`: `Error | null` — last error encountered
|
|
147
|
+
- `pay`: same function as `pay(...)`
|
|
148
|
+
|
|
149
|
+
**Import:**
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { useEscrowCheckout } from 'payluk-escrow-inline-checkout/react';
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Framework and SSR Notes
|
|
156
|
+
|
|
157
|
+
- **Browser-only:** `pay(...)` and the widget require `window`. Avoid calling them during server-side rendering.
|
|
158
|
+
- **Initialize on the client:** If using frameworks like Next.js, call `initEscrowCheckout(...)` in a client component or in an effect.
|
|
159
|
+
- **Preloading:** The hook marks `ready` after the first successful `pay`. If you need earlier preloading, you can trigger a preparatory flow (depending on your setup).
|
|
160
|
+
|
|
161
|
+
## Error Handling
|
|
162
|
+
|
|
163
|
+
Common issues:
|
|
164
|
+
- **Not initialized:** Ensure `initEscrowCheckout({ apiBaseUrl, publicKey })` is called before `pay(...)`.
|
|
165
|
+
- **Browser-only:** Do not call `pay(...)` on the server.
|
|
166
|
+
- **Network/API errors:** If the session endpoint fails, `pay(...)` will reject with the error message from your backend (if any).
|
|
167
|
+
|
|
168
|
+
**Example:**
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
try {
|
|
172
|
+
await pay({ /* ... */ });
|
|
173
|
+
} catch (err) {
|
|
174
|
+
alert((err as Error).message);
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Security
|
|
179
|
+
|
|
180
|
+
- Use only publishable keys in the client.
|
|
181
|
+
- Keep any secret or private keys on your server.
|
|
182
|
+
- Validate and authorize requests on your backend before creating sessions.
|
|
183
|
+
|
|
184
|
+
## Types
|
|
185
|
+
|
|
186
|
+
This package ships with TypeScript types. No additional type packages are required.
|
|
187
|
+
|
|
188
|
+
## Contributing
|
|
189
|
+
|
|
190
|
+
- Install dependencies: `npm install`
|
|
191
|
+
- Build: `npm run build`
|
|
192
|
+
- Lint/Test: add scripts as needed for your project
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
MIT (or your chosen license)
|
package/package.json
CHANGED