@evervault/react-native 2.4.0 → 2.5.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/build/ThreeDSecure/event.d.ts +11 -0
- package/build/ThreeDSecure/event.d.ts.map +1 -0
- package/build/ThreeDSecure/session.d.ts +4 -4
- package/build/ThreeDSecure/session.d.ts.map +1 -1
- package/build/ThreeDSecure/types.d.ts +15 -3
- package/build/ThreeDSecure/types.d.ts.map +1 -1
- package/build/ThreeDSecure/useThreeDSecure.d.ts +4 -1
- package/build/ThreeDSecure/useThreeDSecure.d.ts.map +1 -1
- package/build/index.cjs.js +138 -80
- package/build/index.cjs.js.map +1 -1
- package/build/index.esm.js +138 -80
- package/package.json +2 -2
- package/src/ThreeDSecure/event.ts +19 -0
- package/src/ThreeDSecure/session.test.ts +219 -24
- package/src/ThreeDSecure/session.ts +76 -24
- package/src/ThreeDSecure/types.ts +17 -4
- package/src/ThreeDSecure/useThreeDSecure.test.tsx +112 -1
- package/src/ThreeDSecure/useThreeDSecure.ts +23 -6
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EV_API_DOMAIN } from "./config";
|
|
2
|
+
import { ThreeDSecureEvent } from "./event";
|
|
2
3
|
import {
|
|
3
4
|
pollSession,
|
|
4
5
|
startSession,
|
|
@@ -24,7 +25,7 @@ describe("stopPolling", () => {
|
|
|
24
25
|
});
|
|
25
26
|
|
|
26
27
|
describe("startSession", () => {
|
|
27
|
-
const
|
|
28
|
+
const options = {
|
|
28
29
|
onSuccess: vi.fn(),
|
|
29
30
|
onFailure: vi.fn(),
|
|
30
31
|
onError: vi.fn(),
|
|
@@ -32,6 +33,10 @@ describe("startSession", () => {
|
|
|
32
33
|
const intervalRef = { current: null };
|
|
33
34
|
const setIsVisible = vi.fn();
|
|
34
35
|
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
vi.clearAllMocks();
|
|
38
|
+
});
|
|
39
|
+
|
|
35
40
|
it("should start a successful session", async () => {
|
|
36
41
|
const session: ThreeDSecureSession = {
|
|
37
42
|
cancel: vi.fn(),
|
|
@@ -39,10 +44,10 @@ describe("startSession", () => {
|
|
|
39
44
|
sessionId: "123",
|
|
40
45
|
};
|
|
41
46
|
|
|
42
|
-
await startSession(session,
|
|
47
|
+
await startSession(session, options, intervalRef, setIsVisible);
|
|
43
48
|
|
|
44
49
|
expect(session.get).toHaveBeenCalled();
|
|
45
|
-
expect(
|
|
50
|
+
expect(options.onSuccess).toHaveBeenCalled();
|
|
46
51
|
});
|
|
47
52
|
|
|
48
53
|
it("should start a failed session", async () => {
|
|
@@ -52,10 +57,10 @@ describe("startSession", () => {
|
|
|
52
57
|
sessionId: "123",
|
|
53
58
|
};
|
|
54
59
|
|
|
55
|
-
await startSession(session,
|
|
60
|
+
await startSession(session, options, intervalRef, setIsVisible);
|
|
56
61
|
|
|
57
62
|
expect(session.get).toHaveBeenCalled();
|
|
58
|
-
expect(
|
|
63
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
59
64
|
new Error("3DS session failed")
|
|
60
65
|
);
|
|
61
66
|
});
|
|
@@ -67,12 +72,98 @@ describe("startSession", () => {
|
|
|
67
72
|
sessionId: "123",
|
|
68
73
|
};
|
|
69
74
|
|
|
70
|
-
await startSession(session,
|
|
75
|
+
await startSession(session, options, intervalRef, setIsVisible);
|
|
71
76
|
|
|
72
77
|
expect(session.get).toHaveBeenCalled();
|
|
73
78
|
expect(setIsVisible).toHaveBeenCalledWith(true);
|
|
74
79
|
});
|
|
75
80
|
|
|
81
|
+
it("should fail the session when failOnChallenge is true and a challenge is required", async () => {
|
|
82
|
+
const session: ThreeDSecureSession = {
|
|
83
|
+
cancel: vi.fn(),
|
|
84
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
85
|
+
sessionId: "123",
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
await startSession(
|
|
89
|
+
session,
|
|
90
|
+
{ ...options, failOnChallenge: true },
|
|
91
|
+
intervalRef,
|
|
92
|
+
setIsVisible
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(session.get).toHaveBeenCalled();
|
|
96
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
97
|
+
new Error("3DS session failed")
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should fail the session when failOnChallenge is a function that returns true and a challenge is required", async () => {
|
|
102
|
+
const session: ThreeDSecureSession = {
|
|
103
|
+
cancel: vi.fn(),
|
|
104
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
105
|
+
sessionId: "123",
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
await startSession(
|
|
109
|
+
session,
|
|
110
|
+
{ ...options, failOnChallenge: () => Promise.resolve(true) },
|
|
111
|
+
intervalRef,
|
|
112
|
+
setIsVisible
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
expect(session.get).toHaveBeenCalled();
|
|
116
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
117
|
+
new Error("3DS session failed")
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should call onRequestChallenge when a challenge is required", async () => {
|
|
122
|
+
const session: ThreeDSecureSession = {
|
|
123
|
+
cancel: vi.fn(),
|
|
124
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
125
|
+
sessionId: "123",
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const onRequestChallenge = vi.fn();
|
|
129
|
+
|
|
130
|
+
await startSession(
|
|
131
|
+
session,
|
|
132
|
+
{ ...options, onRequestChallenge },
|
|
133
|
+
intervalRef,
|
|
134
|
+
setIsVisible
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
expect(session.get).toHaveBeenCalled();
|
|
138
|
+
expect(onRequestChallenge).toHaveBeenCalled();
|
|
139
|
+
expect(options.onFailure).not.toHaveBeenCalled();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("should fail the session when onRequestChallenge is called and default is prevented", async () => {
|
|
143
|
+
const session: ThreeDSecureSession = {
|
|
144
|
+
cancel: vi.fn(),
|
|
145
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
146
|
+
sessionId: "123",
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const onRequestChallenge = vi.fn((event: ThreeDSecureEvent) => {
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
await startSession(
|
|
154
|
+
session,
|
|
155
|
+
{ ...options, onRequestChallenge },
|
|
156
|
+
intervalRef,
|
|
157
|
+
setIsVisible
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
expect(session.get).toHaveBeenCalled();
|
|
161
|
+
expect(onRequestChallenge).toHaveBeenCalled();
|
|
162
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
163
|
+
new Error("3DS session failed")
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
|
|
76
167
|
it("should call onError if the session fails to start", async () => {
|
|
77
168
|
const session: ThreeDSecureSession = {
|
|
78
169
|
cancel: vi.fn(),
|
|
@@ -84,21 +175,21 @@ describe("startSession", () => {
|
|
|
84
175
|
.spyOn(console, "error")
|
|
85
176
|
.mockImplementation(() => {});
|
|
86
177
|
|
|
87
|
-
await startSession(session,
|
|
178
|
+
await startSession(session, options, intervalRef, setIsVisible);
|
|
88
179
|
|
|
89
180
|
expect(session.get).toHaveBeenCalled();
|
|
90
181
|
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
91
182
|
"Error checking session state",
|
|
92
183
|
new Error("Failed to start session")
|
|
93
184
|
);
|
|
94
|
-
expect(
|
|
185
|
+
expect(options.onError).toHaveBeenCalledWith(
|
|
95
186
|
new Error("Failed to check 3DS session state")
|
|
96
187
|
);
|
|
97
188
|
});
|
|
98
189
|
});
|
|
99
190
|
|
|
100
191
|
describe("pollSession", () => {
|
|
101
|
-
const
|
|
192
|
+
const options = {
|
|
102
193
|
onSuccess: vi.fn(),
|
|
103
194
|
onFailure: vi.fn(),
|
|
104
195
|
onError: vi.fn(),
|
|
@@ -106,6 +197,10 @@ describe("pollSession", () => {
|
|
|
106
197
|
const intervalRef = { current: null };
|
|
107
198
|
const setIsVisible = vi.fn();
|
|
108
199
|
|
|
200
|
+
beforeEach(() => {
|
|
201
|
+
vi.clearAllMocks();
|
|
202
|
+
});
|
|
203
|
+
|
|
109
204
|
it("should start an interval", () => {
|
|
110
205
|
const session: ThreeDSecureSession = {
|
|
111
206
|
cancel: vi.fn(),
|
|
@@ -114,7 +209,7 @@ describe("pollSession", () => {
|
|
|
114
209
|
};
|
|
115
210
|
|
|
116
211
|
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
117
|
-
pollSession(session,
|
|
212
|
+
pollSession(session, options, intervalRef, setIsVisible);
|
|
118
213
|
|
|
119
214
|
expect(intervalSpy).toHaveBeenCalledWith(expect.any(Function), 3000);
|
|
120
215
|
});
|
|
@@ -127,11 +222,11 @@ describe("pollSession", () => {
|
|
|
127
222
|
};
|
|
128
223
|
|
|
129
224
|
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
130
|
-
pollSession(session,
|
|
225
|
+
pollSession(session, options, intervalRef, setIsVisible);
|
|
131
226
|
await intervalSpy.mock.calls[0][0]();
|
|
132
227
|
|
|
133
228
|
expect(session.get).toHaveBeenCalled();
|
|
134
|
-
expect(
|
|
229
|
+
expect(options.onSuccess).toHaveBeenCalled();
|
|
135
230
|
});
|
|
136
231
|
|
|
137
232
|
it("should poll failed session", async () => {
|
|
@@ -142,11 +237,11 @@ describe("pollSession", () => {
|
|
|
142
237
|
};
|
|
143
238
|
|
|
144
239
|
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
145
|
-
pollSession(session,
|
|
240
|
+
pollSession(session, options, intervalRef, setIsVisible);
|
|
146
241
|
await intervalSpy.mock.calls[0][0]();
|
|
147
242
|
|
|
148
243
|
expect(session.get).toHaveBeenCalled();
|
|
149
|
-
expect(
|
|
244
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
150
245
|
new Error("3DS session failed")
|
|
151
246
|
);
|
|
152
247
|
});
|
|
@@ -159,13 +254,113 @@ describe("pollSession", () => {
|
|
|
159
254
|
};
|
|
160
255
|
|
|
161
256
|
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
162
|
-
pollSession(session,
|
|
257
|
+
pollSession(session, options, intervalRef, setIsVisible);
|
|
163
258
|
await intervalSpy.mock.calls[0][0]();
|
|
164
259
|
|
|
165
260
|
expect(session.get).toHaveBeenCalled();
|
|
166
261
|
expect(setIsVisible).toHaveBeenCalledWith(true);
|
|
167
262
|
});
|
|
168
263
|
|
|
264
|
+
it("should fail the session when failOnChallenge is true and a challenge is required", async () => {
|
|
265
|
+
const session: ThreeDSecureSession = {
|
|
266
|
+
cancel: vi.fn(),
|
|
267
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
268
|
+
sessionId: "123",
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
272
|
+
const onRequestChallenge = vi.fn();
|
|
273
|
+
pollSession(
|
|
274
|
+
session,
|
|
275
|
+
{ ...options, failOnChallenge: true, onRequestChallenge },
|
|
276
|
+
intervalRef,
|
|
277
|
+
setIsVisible
|
|
278
|
+
);
|
|
279
|
+
await intervalSpy.mock.calls[0][0]();
|
|
280
|
+
|
|
281
|
+
expect(session.get).toHaveBeenCalled();
|
|
282
|
+
expect(onRequestChallenge).not.toHaveBeenCalled();
|
|
283
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
284
|
+
new Error("3DS session failed")
|
|
285
|
+
);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("should fail the session when failOnChallenge is a function that returns true and a challenge is required", async () => {
|
|
289
|
+
const session: ThreeDSecureSession = {
|
|
290
|
+
cancel: vi.fn(),
|
|
291
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
292
|
+
sessionId: "123",
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
296
|
+
const onRequestChallenge = vi.fn();
|
|
297
|
+
pollSession(
|
|
298
|
+
session,
|
|
299
|
+
{
|
|
300
|
+
...options,
|
|
301
|
+
onRequestChallenge,
|
|
302
|
+
failOnChallenge: () => Promise.resolve(true),
|
|
303
|
+
},
|
|
304
|
+
intervalRef,
|
|
305
|
+
setIsVisible
|
|
306
|
+
);
|
|
307
|
+
await intervalSpy.mock.calls[0][0]();
|
|
308
|
+
|
|
309
|
+
expect(session.get).toHaveBeenCalled();
|
|
310
|
+
expect(onRequestChallenge).not.toHaveBeenCalled();
|
|
311
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
312
|
+
new Error("3DS session failed")
|
|
313
|
+
);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("should call onRequestChallenge when a challenge is required", async () => {
|
|
317
|
+
const session: ThreeDSecureSession = {
|
|
318
|
+
cancel: vi.fn(),
|
|
319
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
320
|
+
sessionId: "123",
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
324
|
+
const onRequestChallenge = vi.fn();
|
|
325
|
+
pollSession(
|
|
326
|
+
session,
|
|
327
|
+
{ ...options, onRequestChallenge },
|
|
328
|
+
intervalRef,
|
|
329
|
+
setIsVisible
|
|
330
|
+
);
|
|
331
|
+
await intervalSpy.mock.calls[0][0]();
|
|
332
|
+
|
|
333
|
+
expect(session.get).toHaveBeenCalled();
|
|
334
|
+
expect(onRequestChallenge).toHaveBeenCalled();
|
|
335
|
+
expect(options.onFailure).not.toHaveBeenCalled();
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it("should fail the session when onRequestChallenge is called and default is prevented", async () => {
|
|
339
|
+
const session: ThreeDSecureSession = {
|
|
340
|
+
cancel: vi.fn(),
|
|
341
|
+
get: vi.fn(() => Promise.resolve({ status: "action-required" }) as any),
|
|
342
|
+
sessionId: "123",
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
346
|
+
const onRequestChallenge = vi.fn((event: ThreeDSecureEvent) => {
|
|
347
|
+
event.preventDefault();
|
|
348
|
+
});
|
|
349
|
+
pollSession(
|
|
350
|
+
session,
|
|
351
|
+
{ ...options, onRequestChallenge },
|
|
352
|
+
intervalRef,
|
|
353
|
+
setIsVisible
|
|
354
|
+
);
|
|
355
|
+
await intervalSpy.mock.calls[0][0]();
|
|
356
|
+
|
|
357
|
+
expect(session.get).toHaveBeenCalled();
|
|
358
|
+
expect(onRequestChallenge).toHaveBeenCalled();
|
|
359
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
360
|
+
new Error("3DS session failed")
|
|
361
|
+
);
|
|
362
|
+
});
|
|
363
|
+
|
|
169
364
|
it("should call onError if the session fails to poll", async () => {
|
|
170
365
|
const session: ThreeDSecureSession = {
|
|
171
366
|
cancel: vi.fn(),
|
|
@@ -178,7 +373,7 @@ describe("pollSession", () => {
|
|
|
178
373
|
.mockImplementation(() => {});
|
|
179
374
|
|
|
180
375
|
const intervalSpy = vi.spyOn(global, "setInterval");
|
|
181
|
-
pollSession(session,
|
|
376
|
+
pollSession(session, options, intervalRef, setIsVisible);
|
|
182
377
|
await intervalSpy.mock.calls[0][0]();
|
|
183
378
|
|
|
184
379
|
expect(session.get).toHaveBeenCalled();
|
|
@@ -186,14 +381,14 @@ describe("pollSession", () => {
|
|
|
186
381
|
"Error polling session",
|
|
187
382
|
new Error("Failed to poll session")
|
|
188
383
|
);
|
|
189
|
-
expect(
|
|
384
|
+
expect(options.onError).toHaveBeenCalledWith(
|
|
190
385
|
new Error("Error polling 3DS session")
|
|
191
386
|
);
|
|
192
387
|
});
|
|
193
388
|
});
|
|
194
389
|
|
|
195
390
|
describe("threeDSecureSession", () => {
|
|
196
|
-
const
|
|
391
|
+
const options = {
|
|
197
392
|
onSuccess: vi.fn(),
|
|
198
393
|
onFailure: vi.fn(),
|
|
199
394
|
onError: vi.fn(),
|
|
@@ -205,7 +400,7 @@ describe("threeDSecureSession", () => {
|
|
|
205
400
|
const session = threeDSecureSession({
|
|
206
401
|
sessionId: "123",
|
|
207
402
|
appId: "app_123",
|
|
208
|
-
|
|
403
|
+
options,
|
|
209
404
|
intervalRef,
|
|
210
405
|
setIsVisible,
|
|
211
406
|
});
|
|
@@ -220,7 +415,7 @@ describe("threeDSecureSession", () => {
|
|
|
220
415
|
const session = threeDSecureSession({
|
|
221
416
|
sessionId: "123",
|
|
222
417
|
appId: "app_123",
|
|
223
|
-
|
|
418
|
+
options,
|
|
224
419
|
intervalRef,
|
|
225
420
|
setIsVisible,
|
|
226
421
|
});
|
|
@@ -250,7 +445,7 @@ describe("threeDSecureSession", () => {
|
|
|
250
445
|
const session = threeDSecureSession({
|
|
251
446
|
sessionId: "123",
|
|
252
447
|
appId: "app_123",
|
|
253
|
-
|
|
448
|
+
options,
|
|
254
449
|
intervalRef,
|
|
255
450
|
setIsVisible,
|
|
256
451
|
});
|
|
@@ -275,7 +470,7 @@ describe("threeDSecureSession", () => {
|
|
|
275
470
|
const session = threeDSecureSession({
|
|
276
471
|
sessionId: "123",
|
|
277
472
|
appId: "app_123",
|
|
278
|
-
|
|
473
|
+
options,
|
|
279
474
|
intervalRef,
|
|
280
475
|
setIsVisible,
|
|
281
476
|
});
|
|
@@ -297,7 +492,7 @@ describe("threeDSecureSession", () => {
|
|
|
297
492
|
}
|
|
298
493
|
);
|
|
299
494
|
|
|
300
|
-
expect(
|
|
495
|
+
expect(options.onFailure).toHaveBeenCalledWith(
|
|
301
496
|
new Error("3DS session cancelled by user")
|
|
302
497
|
);
|
|
303
498
|
});
|
|
@@ -306,7 +501,7 @@ describe("threeDSecureSession", () => {
|
|
|
306
501
|
const session = threeDSecureSession({
|
|
307
502
|
sessionId: "123",
|
|
308
503
|
appId: "app_123",
|
|
309
|
-
|
|
504
|
+
options,
|
|
310
505
|
intervalRef,
|
|
311
506
|
setIsVisible,
|
|
312
507
|
});
|
|
@@ -3,8 +3,10 @@ import {
|
|
|
3
3
|
ThreeDSecureSessionsParams,
|
|
4
4
|
ThreeDSecureSession,
|
|
5
5
|
ThreeDSecureSessionResponse,
|
|
6
|
+
ThreeDSecureOptions,
|
|
6
7
|
} from "./types";
|
|
7
8
|
import { EV_API_DOMAIN } from "./config";
|
|
9
|
+
import { ThreeDSecureEvent } from "./event";
|
|
8
10
|
|
|
9
11
|
export function stopPolling(
|
|
10
12
|
intervalRef: React.MutableRefObject<NodeJS.Timeout | null>,
|
|
@@ -20,58 +22,108 @@ export function stopPolling(
|
|
|
20
22
|
|
|
21
23
|
export async function startSession(
|
|
22
24
|
session: ThreeDSecureSession,
|
|
23
|
-
|
|
25
|
+
options: ThreeDSecureOptions | undefined,
|
|
24
26
|
intervalRef: React.MutableRefObject<NodeJS.Timeout | null>,
|
|
25
27
|
setIsVisible: (show: boolean) => void
|
|
26
28
|
) {
|
|
27
29
|
try {
|
|
28
30
|
const sessionState = await session.get();
|
|
29
31
|
|
|
32
|
+
function fail() {
|
|
33
|
+
stopPolling(intervalRef, setIsVisible);
|
|
34
|
+
options?.onFailure?.(new Error("3DS session failed"));
|
|
35
|
+
}
|
|
36
|
+
|
|
30
37
|
switch (sessionState.status) {
|
|
31
|
-
case "success":
|
|
38
|
+
case "success": {
|
|
32
39
|
stopPolling(intervalRef, setIsVisible);
|
|
33
|
-
|
|
40
|
+
options?.onSuccess?.();
|
|
34
41
|
break;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
case "failure": {
|
|
45
|
+
fail();
|
|
38
46
|
break;
|
|
39
|
-
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
case "action-required": {
|
|
50
|
+
const failOnChallenge =
|
|
51
|
+
typeof options?.failOnChallenge === "function"
|
|
52
|
+
? await options.failOnChallenge()
|
|
53
|
+
: options?.failOnChallenge ?? false;
|
|
54
|
+
if (failOnChallenge) {
|
|
55
|
+
fail();
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const event = new ThreeDSecureEvent("requestChallenge", session);
|
|
60
|
+
options?.onRequestChallenge?.(event);
|
|
61
|
+
if (event.defaultPrevented) {
|
|
62
|
+
fail();
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
|
|
40
66
|
setIsVisible(true);
|
|
41
|
-
pollSession(session,
|
|
42
|
-
|
|
43
|
-
default:
|
|
44
|
-
break;
|
|
67
|
+
pollSession(session, options, intervalRef, setIsVisible);
|
|
68
|
+
}
|
|
45
69
|
}
|
|
46
70
|
} catch (error) {
|
|
47
71
|
console.error("Error checking session state", error);
|
|
48
|
-
|
|
72
|
+
options?.onError?.(new Error("Failed to check 3DS session state"));
|
|
49
73
|
}
|
|
50
74
|
}
|
|
51
75
|
|
|
52
76
|
export function pollSession(
|
|
53
77
|
session: ThreeDSecureSession,
|
|
54
|
-
|
|
78
|
+
options: ThreeDSecureOptions | undefined,
|
|
55
79
|
intervalRef: React.MutableRefObject<NodeJS.Timeout | null>,
|
|
56
80
|
setIsVisible: (show: boolean) => void,
|
|
57
81
|
interval = 3000
|
|
58
82
|
) {
|
|
83
|
+
function fail() {
|
|
84
|
+
stopPolling(intervalRef, setIsVisible);
|
|
85
|
+
options?.onFailure?.(new Error("3DS session failed"));
|
|
86
|
+
}
|
|
87
|
+
|
|
59
88
|
intervalRef.current = setInterval(async () => {
|
|
60
89
|
try {
|
|
61
90
|
const pollResponse: ThreeDSecureSessionResponse = await session.get();
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
91
|
+
switch (pollResponse.status) {
|
|
92
|
+
case "success": {
|
|
93
|
+
stopPolling(intervalRef, setIsVisible);
|
|
94
|
+
options?.onSuccess?.();
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
case "failure": {
|
|
99
|
+
fail();
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
case "action-required": {
|
|
104
|
+
const failOnChallenge =
|
|
105
|
+
typeof options?.failOnChallenge === "function"
|
|
106
|
+
? await options.failOnChallenge()
|
|
107
|
+
: options?.failOnChallenge ?? false;
|
|
108
|
+
if (failOnChallenge) {
|
|
109
|
+
fail();
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const event = new ThreeDSecureEvent("requestChallenge", session);
|
|
114
|
+
options?.onRequestChallenge?.(event);
|
|
115
|
+
if (event.defaultPrevented) {
|
|
116
|
+
fail();
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
setIsVisible(true);
|
|
121
|
+
}
|
|
70
122
|
}
|
|
71
123
|
} catch (error) {
|
|
72
124
|
stopPolling(intervalRef, setIsVisible);
|
|
73
125
|
console.error("Error polling session", error);
|
|
74
|
-
|
|
126
|
+
options?.onError?.(new Error("Error polling 3DS session"));
|
|
75
127
|
}
|
|
76
128
|
}, interval);
|
|
77
129
|
}
|
|
@@ -79,7 +131,7 @@ export function pollSession(
|
|
|
79
131
|
export function threeDSecureSession({
|
|
80
132
|
sessionId,
|
|
81
133
|
appId,
|
|
82
|
-
|
|
134
|
+
options,
|
|
83
135
|
intervalRef,
|
|
84
136
|
setIsVisible,
|
|
85
137
|
}: ThreeDSecureSessionsParams): ThreeDSecureSession {
|
|
@@ -116,7 +168,7 @@ export function threeDSecureSession({
|
|
|
116
168
|
}
|
|
117
169
|
);
|
|
118
170
|
|
|
119
|
-
|
|
171
|
+
options?.onFailure?.(new Error("3DS session cancelled by user"));
|
|
120
172
|
stopPolling(intervalRef, setIsVisible);
|
|
121
173
|
} catch (error) {
|
|
122
174
|
console.error("Error cancelling 3DS session", error);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ThreeDSecureEvent } from "./event";
|
|
2
2
|
|
|
3
3
|
export interface ThreeDSecureCallbacks {
|
|
4
4
|
/**
|
|
@@ -6,6 +6,12 @@ export interface ThreeDSecureCallbacks {
|
|
|
6
6
|
*/
|
|
7
7
|
onError?(error: Error): void;
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* The 'requestChallenge' event will be fired if the 3DS authentication process requires a challenge.
|
|
11
|
+
* If you'd like to fail the authentication, you should call `preventDefault` on the passed event.
|
|
12
|
+
*/
|
|
13
|
+
onRequestChallenge?(event: ThreeDSecureEvent): void;
|
|
14
|
+
|
|
9
15
|
/**
|
|
10
16
|
* The 'failure' event will be fired if the 3DS authentication process fails. You should use this event to handle the failure and inform the user and prompt them to try again.
|
|
11
17
|
* If the user cancels the 3DS authentication process this event will be fired.
|
|
@@ -20,6 +26,13 @@ export interface ThreeDSecureCallbacks {
|
|
|
20
26
|
onSuccess?(): void;
|
|
21
27
|
}
|
|
22
28
|
|
|
29
|
+
export interface ThreeDSecureOptions extends ThreeDSecureCallbacks {
|
|
30
|
+
/**
|
|
31
|
+
* If set to `true` (or a function that returns `true`), the authentication will fail if a challenge is required.
|
|
32
|
+
*/
|
|
33
|
+
failOnChallenge?: boolean | (() => Promise<boolean>);
|
|
34
|
+
}
|
|
35
|
+
|
|
23
36
|
export interface ThreeDSecureInitialState {
|
|
24
37
|
session: ThreeDSecureSession | null;
|
|
25
38
|
isVisible: boolean;
|
|
@@ -44,7 +57,7 @@ export interface ThreeDSecureSessionResponse {
|
|
|
44
57
|
|
|
45
58
|
export interface ThreeDSecureSessionsParams {
|
|
46
59
|
appId: string;
|
|
47
|
-
|
|
60
|
+
options?: ThreeDSecureOptions;
|
|
48
61
|
intervalRef: React.MutableRefObject<NodeJS.Timeout | null>;
|
|
49
62
|
sessionId: string;
|
|
50
63
|
setIsVisible: (show: boolean) => void;
|
|
@@ -61,7 +74,7 @@ export interface ThreeDSecureState extends ThreeDSecureInitialState {
|
|
|
61
74
|
* The `start()` function is used to kick off the 3DS authentication process.
|
|
62
75
|
*
|
|
63
76
|
* @param sessionId The 3DS session ID. A 3DS session can be created using the [Evervault API](https://docs.evervault.com/api-reference#createThreeDSSession).
|
|
64
|
-
* @param
|
|
77
|
+
* @param options The options to be used for the 3DS authentication process.
|
|
65
78
|
*/
|
|
66
|
-
start(sessionId: string,
|
|
79
|
+
start(sessionId: string, options?: ThreeDSecureOptions): void;
|
|
67
80
|
}
|