@nuvei-connect/nc-websdk 1.0.10 → 1.12.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.
Files changed (2) hide show
  1. package/README.md +541 -100
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,149 +1,590 @@
1
- # 🧩 WebSDK
2
1
 
3
- The **Nuvei WebSDK** allows third-party applications to easily embed Micro Frontends into their applications.
2
+ # Nuvei WebSDK – React Integration Guide
4
3
 
5
- ## 📦 Installation
4
+ A practical guide for integrating **Nuvei WebSDK** into a React application using the `@nuvei-connect/nc-websdk` package.
6
5
 
7
- Install the SDK from npm:
6
+ ---
7
+
8
+ # Step 1 - Installation
9
+
10
+ Install the Nuvei WebSDK package:
8
11
 
9
12
  ```bash
10
13
  npm install @nuvei-connect/nc-websdk
11
14
  ```
15
+ or:
16
+ ```bash
17
+ yarn add @nuvei-connect/nc-websdk
18
+ ```
19
+
20
+ > **Important:** Nuvei authorization credentials should remain on the backend. They should not be exposed in the React application.
21
+
22
+ ---
23
+
24
+ # Step 2 – Import the SDK
25
+
26
+ Import `NuveiSDK` and `ModulesNameEnum`. In `App.tsx`.
27
+
28
+ Create a reference for the DOM container where the SDK will be rendered.
29
+
30
+ ```tsx
31
+ import React, { useEffect, useRef } from "react";
32
+
33
+ import NuveiSDK, { ModulesNameEnum } from "@nuvei-connect/nc-websdk";
34
+
35
+ const containerRef = useRef<HTMLDivElement>(null);
36
+ ```
37
+
38
+ ---
39
+
40
+ # Step 3 – Mount the Nuvei WebSDK
41
+
42
+ Initialize the SDK once the container is available. In `App.tsx`.
43
+
44
+ ```ts
45
+ useEffect(() => {
46
+ if (!containerRef.current) {
47
+ return;
48
+ }
49
+
50
+ NuveiSDK.mount({
51
+ config: {environment: "dev2"},
52
+ container: containerRef.current.id,
53
+ componentName: ModulesNameEnum.ONBOARDING_MERCHANT,
54
+ fetchClientSession : async() => {
55
+ const response = await fetch(`${backendApiUrl}/api/nuvei/auth/session`, { method: "POST"});
56
+ if (!response.ok) { throw new Error(`Session error: ${response.status}`);}
57
+ return await response.json();
58
+ };,
59
+
60
+ onError: (error: any) => { console.error("NuveiWebSDK Error:", error)},
61
+ }).catch((error: any) => {
62
+ console.error("NuveiWebSDK Mount Error:", error);
63
+ });
64
+ }, []);
65
+
66
+ return (
67
+ <div id="nuvei-websdk-container" ref={containerRef} style={{width: "100%",height: "100%" }}/>
68
+ );
69
+ ```
70
+
71
+ # Step 4. Backend Configuration
72
+
73
+ The backend then calls the Nuvei token/session endpoint : in nodejs backend
74
+
75
+ ```ts
76
+ app.post("/api/nuvei/auth/session", async (_req: Request, res: Response) => {
77
+ try {
78
+ const response = await fetch('https://nonprodmanagement-us-e.azure-api.net/dev2/v1/auth/micro-frontend/tokens', {
79
+ method: "POST",
80
+ headers: {
81
+ "Content-Type": "application/json",
82
+ Authorization: 'your_provided_backend_token',
83
+ },
84
+ body: JSON.stringify({
85
+ modules: [
86
+ {
87
+ name: "ONBOARDING",
88
+ },
89
+ ],
90
+ isGuest: false,
91
+ emailAddress: "your_email_address",
92
+ partnerId:"your_partner_id"
93
+ }),
94
+ });
95
+
96
+ const data = await response.json();
97
+
98
+ return res.json(data);
99
+ } catch (error) {
100
+ console.log("error", error)
101
+ }
102
+ });
103
+
104
+ ```
105
+
106
+
107
+ ---
108
+
109
+ # Complete React Implementation
110
+
111
+ ## `App.tsx`
112
+
113
+ The following is the complete minimal React implementation:
114
+
115
+ ```tsx
116
+ import React, { useEffect, useRef } from "react";
117
+
118
+ import NuveiSDK, { ModulesNameEnum } from "@nuvei-connect/nc-websdk";
119
+
120
+ const App: React.FC = () => {
121
+ const containerRef = useRef<HTMLDivElement>(null);
122
+
123
+ const backendApiUrl =
124
+ import.meta.env.BACKEND_API_URL || "http://localhost:3005";
125
+
126
+ useEffect(() => {
127
+ if (!containerRef.current) {
128
+ return;
129
+ }
130
+
131
+ NuveiSDK.mount({
132
+ config: {environment: "dev2"},
133
+ container: containerRef.current.id,
134
+ componentName: ModulesNameEnum.ONBOARDING_MERCHANT,
135
+ fetchClientSession : async() => {
136
+ const response = await fetch(`${backendApiUrl}/api/nuvei/auth/session`, { method: "POST"});
137
+ if (!response.ok) { throw new Error(`Session error: ${response.status}`);}
138
+ return await response.json();
139
+ },
140
+
141
+ onError: (error: any) => { console.error("NuveiWebSDK Error:", error)},
142
+ }).catch((error: any) => {
143
+ console.error("NuveiWebSDK Mount Error:", error);
144
+ });
145
+ }, []);
146
+
147
+
148
+ return (
149
+ <div className="app-container">
150
+ <header className="app-header">
151
+ <h1>Nuvei WebSDK React Demo</h1>
152
+
153
+ <p>
154
+ Minimal implementation using <code>@nuvei-connect/nc-websdk</code>.
155
+ </p>
156
+ </header>
157
+
158
+ <main className="app-main">
159
+ <div
160
+ id="nuvei-websdk-container"
161
+ ref={containerRef}
162
+ style={{
163
+ width: "100%",
164
+ minHeight: "calc(100vh - 100px)",
165
+ border: "none",
166
+ }}
167
+ />
168
+ </main>
169
+ </div>
170
+ );
171
+ };
172
+
173
+ export default App;
174
+ ```
175
+
176
+ ---
177
+
178
+ # 5. Loading Different Modules
179
+
180
+ The module is selected through `componentName`.
181
+
182
+ ## Partner Onboarding
183
+
184
+ Use:
185
+
186
+ ```tsx
187
+ componentName: ModulesNameEnum.ONBOARDING_PARTNER;
188
+ ```
12
189
 
13
- ## 🚀 Usage
190
+ Example:
14
191
 
15
- Import and mount the SDK. The container must be mounted in the DOM before calling
16
- `NuveiSDK.mount()`:
192
+ ```tsx
193
+ NuveiSDK.mount({
194
+ container: containerRef.current.id,
195
+ componentName: ModulesNameEnum.ONBOARDING_PARTNER,
17
196
 
18
- ```typescript
19
- import NuveiSDK from "@nuvei-connect/nc-websdk";
197
+ fetchClientSession,
20
198
 
21
- const instance = await NuveiSDK.mount({
22
- componentName: moduleName,
23
- container: containerRef.current,
199
+ onError: (error: any) => {
200
+ console.error("NuveiWebSDK Error:", error);
201
+ },
24
202
 
25
203
  config: {
26
- environment: "preprod",
204
+ environment: "dev2",
27
205
  },
28
206
  });
29
207
  ```
30
208
 
31
- The `mount()` call returns an SDK instance. Keep the instance and call `destroy()`
32
- when the host component is unmounted or the integration is no longer needed.
209
+ ---
33
210
 
34
- ### ⚛️ React example
211
+ ## Merchant Onboarding
35
212
 
36
- Keep the SDK container rendered independently from the loading state. If a loader
37
- conditionally replaces the container, `containerRef.current` will be `null` when
38
- the SDK is initialized.
213
+ Use:
39
214
 
40
215
  ```tsx
41
- "use client";
216
+ componentName: ModulesNameEnum.ONBOARDING_MERCHANT;
217
+ ```
42
218
 
43
- import { useEffect, useRef, useState } from "react";
44
- import NuveiSDK from "@nuvei-connect/nc-websdk";
219
+ Example:
45
220
 
46
- async function fetchClientSession() {
47
- const response = await fetch("/api/sdk/session", { method: "POST" });
48
- return response.json();
49
- }
221
+ ```tsx
222
+ NuveiSDK.mount({
223
+ container: containerRef.current.id,
224
+ componentName: ModulesNameEnum.ONBOARDING_MERCHANT,
225
+
226
+ fetchClientSession,
227
+
228
+ onError: (error: any) => {
229
+ console.error("NuveiWebSDK Error:", error);
230
+ },
231
+
232
+ config: {
233
+ environment: "dev2",
234
+ },
235
+ });
236
+ ```
237
+
238
+ ---
239
+
240
+ ## Document Utility
241
+
242
+ Use the document utility module exposed by your SDK.
243
+
244
+ Example structure:
245
+
246
+ ```tsx
247
+ NuveiSDK.mount({
248
+ container: containerRef.current.id,
249
+ componentName: ModulesNameEnum.ONBOARDING_MERCHANT,
50
250
 
51
- async function refreshClientSession(payload: {
52
- accessToken: string;
53
- refreshToken: string;
54
- }) {
55
- const response = await fetch("/api/sdk/session/refresh", {
251
+ fetchClientSession,
252
+
253
+ onError: (error: any) => {
254
+ console.error("NuveiWebSDK Error:", error);
255
+ },
256
+
257
+ config: {
258
+ environment: "dev2",
259
+ },
260
+ });
261
+ ```
262
+
263
+ > **Note:** Verify the exact `ModulesNameEnum` value for Document Utility in the installed SDK version before using it. The original implementation should not be changed here unless that enum is confirmed.
264
+
265
+ ---
266
+
267
+ # 6. Session Lifecycle Callbacks
268
+
269
+ For applications that need full session lifecycle management, the SDK can receive:
270
+
271
+ - `fetchClientSession`
272
+ - `refreshClientSession`
273
+ - `destroySession`
274
+
275
+ ```tsx
276
+ const fetchClientSession = async () => {
277
+ const response = await fetch(`${apiBaseUrl}/api/nuvei/auth/session`, {
56
278
  method: "POST",
57
- headers: { "Content-Type": "application/json" },
58
- body: JSON.stringify(payload),
59
279
  });
60
- return response.json();
61
- }
62
280
 
63
- async function destroySession() {
64
- const response = await fetch("/api/sdk/session", { method: "POST" });
65
- return response.ok;
66
- }
281
+ if (!response.ok) {
282
+ throw new Error(`Session error: ${response.status}`);
283
+ }
67
284
 
68
- export function MicroFrontend({ componentName }: { componentName: string }) {
69
- const containerRef = useRef<HTMLDivElement>(null);
70
- const instanceRef = useRef<any>(null);
71
- const [isLoading, setIsLoading] = useState(true);
285
+ return await response.json();
286
+ };
287
+ ```
72
288
 
73
- useEffect(() => {
74
- let isMounted = true;
75
-
76
- async function initialize() {
77
- if (!containerRef.current) return;
78
-
79
- const instance = await NuveiSDK.mount({
80
- componentName,
81
- container: containerRef.current,
82
- fetchClientSession,
83
- refreshClientSession,
84
- destroySession,
85
- config: { environment: "preprod" },
289
+ ```tsx
290
+ const refreshClientSession = async (refreshToken: string) => {
291
+ const response = await fetch(`${apiBaseUrl}/api/nuvei/auth/session/refresh`, {
292
+ method: "POST",
293
+
294
+ headers: {
295
+ "Content-Type": "application/json",
296
+ },
297
+
298
+ body: JSON.stringify({
299
+ refreshToken,
300
+ }),
301
+ });
302
+
303
+ if (!response.ok) {
304
+ throw new Error(`Session refresh error: ${response.status}`);
305
+ }
306
+
307
+ return await response.json();
308
+ };
309
+ ```
310
+
311
+ ```tsx
312
+ const destroySession = async () => {
313
+ const response = await fetch(`${apiBaseUrl}/api/nuvei/auth/session/destroy`, {
314
+ method: "POST",
315
+ });
316
+
317
+ if (!response.ok) {
318
+ throw new Error(`Session destroy error: ${response.status}`);
319
+ }
320
+
321
+ return await response.json();
322
+ };
323
+ ```
324
+
325
+ ---
326
+
327
+ # 7. Mount With Full Session Lifecycle
328
+
329
+ When all callbacks are available, pass them to `NuveiSDK.mount()`:
330
+
331
+ ```tsx
332
+ NuveiSDK.mount({
333
+ container: containerRef.current.id,
334
+
335
+ componentName: ModulesNameEnum.ONBOARDING_MERCHANT,
336
+
337
+ fetchClientSession,
338
+ refreshClientSession,
339
+ destroySession,
340
+
341
+ onError: (error: any) => {
342
+ console.error("NuveiWebSDK Error:", error);
343
+ },
344
+
345
+ config: {
346
+ environment: "dev2",
347
+ },
348
+ }).catch((error: any) => {
349
+ console.error("NuveiWebSDK Mount Error:", error);
350
+ });
351
+ ```
352
+
353
+ ---
354
+
355
+ # 8. Backend Session Refresh
356
+
357
+ The frontend refresh callback should call your backend.
358
+
359
+ Example:
360
+
361
+ ```ts
362
+ app.post("/api/nuvei/auth/refresh", async (req: Request, res: Response) => {
363
+ try {
364
+ const { refreshToken } = req.body as {
365
+ refreshToken?: string;
366
+ };
367
+
368
+ if (!refreshToken) {
369
+ return res.status(400).json({
370
+ isSuccess: false,
371
+ message: "refreshToken is required",
86
372
  });
373
+ }
374
+
375
+ console.log("Refreshing Nuvei session...");
87
376
 
88
- if (!isMounted) {
89
- instance.destroy();
90
- return;
91
- }
377
+ // Replace this URL with your actual
378
+ // Nuvei refresh-token endpoint.
379
+ const refreshUrl = "YOUR_NUVEI_REFRESH_ENDPOINT";
92
380
 
93
- instanceRef.current = instance;
94
- setIsLoading(false);
381
+ const response = await fetch(refreshUrl, {
382
+ method: "POST",
383
+
384
+ headers: {
385
+ "Content-Type": "application/json",
386
+ Authorization: NUVEI_AUTHORIZATION,
387
+ },
388
+
389
+ body: JSON.stringify({
390
+ refreshToken,
391
+ }),
392
+ });
393
+
394
+ const data: unknown = await response.json();
395
+
396
+ if (!response.ok) {
397
+ console.error("Nuvei refresh failed:", data);
398
+
399
+ return res.status(response.status).json(data);
95
400
  }
96
401
 
97
- void initialize();
402
+ console.log("Nuvei session refreshed");
98
403
 
99
- return () => {
100
- isMounted = false;
101
- instanceRef.current?.destroy();
102
- instanceRef.current = null;
103
- };
104
- }, [componentName]);
404
+ return res.json(data);
405
+ } catch (error) {
406
+ console.error("Session refresh error:", error);
105
407
 
106
- return (
107
- <>
108
- {isLoading && <div>Loading…</div>}
109
- <div ref={containerRef} />
110
- </>
111
- );
408
+ return res.status(500).json({
409
+ isSuccess: false,
410
+ message: "Internal server error while refreshing session",
411
+ });
412
+ }
413
+ });
414
+ ```
415
+
416
+ ---
417
+
418
+ # 9. Backend Logout / Destroy Session
419
+
420
+ ```ts
421
+ app.post("/api/nuvei/auth/logout", async (_req: Request, res: Response) => {
422
+ try {
423
+ console.log("Destroying Nuvei session...");
424
+
425
+ return res.json({
426
+ isSuccess: true,
427
+ });
428
+ } catch (error) {
429
+ console.error("Logout error:", error);
430
+
431
+ return res.status(500).json({
432
+ isSuccess: false,
433
+ message: "Failed to destroy session",
434
+ });
435
+ }
436
+ });
437
+ ```
438
+
439
+ > The exact Nuvei destroy/logout behavior should follow the session API supported by the environment you are integrating with.
440
+
441
+ ---
442
+
443
+ # 13. Configuration Reference
444
+
445
+ | Property | Type | Default | Description |
446
+ | ------------------- | --------------------------- | ----------------------- | --------------------------------- |
447
+ | `componentName` | `ModulesNameEnum \| string` | `ONBOARDING_MERCHANT` | Nuvei module to render |
448
+ | `requestId` | `string` | `undefined` | Onboarding application ID |
449
+ | `documentRequestId` | `string` | `undefined` | Document Utility request ID |
450
+ | `environment` | `string` | — | Target environment |
451
+ | `backendApiUrl` | `string` | `http://localhost:3005` | Backend API base URL |
452
+ | `onCompleted` | `(data) => void` | `undefined` | Called when the flow is completed |
453
+ | `onError` | `(error) => void` | `undefined` | Called when an error occurs |
454
+ | `style` | `React.CSSProperties` | `undefined` | Optional container styling |
455
+
456
+ ---
457
+
458
+ # 15. Production Checklist
459
+
460
+ Before deploying the integration, verify:
461
+
462
+ ### Frontend
463
+
464
+ - [ ] `@nuvei-connect/nc-websdk` is installed.
465
+ - [ ] Correct `ModulesNameEnum` is selected.
466
+ - [ ] SDK container has a stable `id`.
467
+ - [ ] `BACKEND_API_URL` points to the correct backend.
468
+ - [ ] `onError` is configured.
469
+ - [ ] Required session callbacks are supplied.
470
+
471
+ ### Backend
472
+
473
+ - [ ] Nuvei authorization is stored securely.
474
+ - [ ] Nuvei credentials are never exposed to the browser.
475
+ - [ ] `/api/nuvei/auth/session` is implemented.
476
+ - [ ] Refresh endpoint is implemented when required.
477
+ - [ ] Destroy/logout endpoint is implemented when required.
478
+ - [ ] `emailAddress`, `partnerId`, and `isGuest` are populated from the appropriate application context.
479
+ - [ ] Correct Nuvei environment endpoints are configured.
480
+
481
+ ### Environment
482
+
483
+ - [ ] Development environment is verified.
484
+ - [ ] UAT environment is verified.
485
+ - [ ] Production configuration is separated from development configuration.
486
+ - [ ] Backend API uses HTTPS in production.
487
+
488
+ ---
489
+
490
+ # 16. Common Errors
491
+
492
+ ## SDK does not render
493
+
494
+ Check:
495
+
496
+ ```tsx
497
+ if (!containerRef.current) {
498
+ return;
112
499
  }
113
500
  ```
114
501
 
115
- ### 🔐 Authentication
502
+ and confirm that the container exists:
503
+
504
+ ```tsx
505
+ <div id="nuvei-websdk-container" ref={containerRef} />
506
+ ```
507
+
508
+ ---
116
509
 
117
- By default, the SDK **handles authentication automatically**. No authentication configuration is required from the consuming application.
510
+ ## Session request fails
118
511
 
119
- If the consuming application wants to manage authentication itself, it can provide authentication callbacks. These callbacks **override the SDK's default authentication behavior**.
512
+ Check the browser network request:
120
513
 
121
- ```typescript
122
- const instance = await NuveiSDK.mount({
123
- componentName: moduleName,
124
- container: containerRef.current,
514
+ ```text
515
+ POST /api/nuvei/auth/session
516
+ ```
125
517
 
126
- config: {
127
- environment: "preprod",
128
- },
518
+ Then verify:
129
519
 
130
- fetchClientSession: getSessionId,
131
- refreshClientSession: refreshSessionId,
132
- destroySession: handleSessionDestroy,
520
+ 1. Backend is running.
521
+ 2. `BACKEND_API_URL` is correct.
522
+ 3. Backend can reach Nuvei.
523
+ 4. `NUVEI_AUTHORIZATION` is valid.
524
+ 5. Required session parameters are populated.
525
+ 6. The backend returns valid JSON.
526
+
527
+ ---
528
+
529
+ ## SDK mount fails
530
+
531
+ Check the SDK error callback:
532
+
533
+ ```tsx
534
+ onError: (error) => {
535
+ console.error("NuveiWebSDK Error:", error);
536
+ };
537
+ ```
538
+
539
+ Also check the promise rejection:
540
+
541
+ ```tsx
542
+ .catch((error) => {
543
+ console.error(
544
+ "NuveiWebSDK Mount Error:",
545
+ error
546
+ );
133
547
  });
134
548
  ```
135
549
 
136
- ### ⚙️ Configuration
550
+ ---
551
+
552
+ # 17. Minimal Integration
553
+
554
+ If you only need the simplest integration, the essential flow is:
555
+
556
+ ```tsx
557
+ const fetchClientSession = async () => {
558
+ const response = await fetch(`${backendApiUrl}/api/nuvei/auth/session`, {
559
+ method: "POST",
560
+ });
561
+
562
+ if (!response.ok) {
563
+ throw new Error(`Session error: ${response.status}`);
564
+ }
565
+
566
+ return response.json();
567
+ };
568
+
569
+ useEffect(() => {
570
+ if (!containerRef.current) {
571
+ return;
572
+ }
573
+
574
+ NuveiSDK.mount({
575
+ container: containerRef.current.id,
576
+ componentName: ModulesNameEnum.ONBOARDING_MERCHANT,
577
+ fetchClientSession,
578
+
579
+ config: {
580
+ environment: "dev2",
581
+ },
582
+ });
583
+ }, []);
584
+ ```
585
+
586
+ This is the recommended starting point for a basic React integration.
137
587
 
138
- | Property | Required | Description |
139
- | ---------------------- | -------- | --------------------------------------------------- |
140
- | `componentName` | Yes | Micro Frontend component to load |
141
- | `container` | Yes | DOM element where the component will be mounted |
142
- | `config.environment` | Yes | SDK environment (`local`, `dev`, `preprod`, `prod`) |
143
- | `fetchClientSession` | No | Custom authentication/session callback |
144
- | `refreshClientSession` | No | Custom session refresh callback |
145
- | `destroySession` | No | Custom session destroy/logout callback |
146
- | `config.requestId` | No | Onboarding request ID; falls back to the host URL |
147
- | `config.documentRequestId` | No | Document Utility request ID; falls back to the host URL |
588
+ ---
148
589
 
149
- > **Note:** Authentication callbacks are optional. If they are not provided, the SDK uses its built-in authentication flow.
590
+ The frontend remains responsible for rendering the WebSDK, while the backend handles the Nuvei session lifecycle and secure communication with Nuvei
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuvei-connect/nc-websdk",
3
- "version": "1.0.10",
3
+ "version": "1.12.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",