@@ -8,19 +8,29 @@ import { ACH_IFRAME, CARD_IFRAME, CASH_IFRAME, ElementTypes } from './data';
8
8
9
9
const sessionKey = self . crypto . randomUUID ( ) ;
10
10
11
- export const getData = async ( url : string , apiKey : string ) => {
12
- const options : RequestInit = {
13
- method : 'GET' ,
14
- mode : 'cors' ,
15
- cache : 'no-cache' ,
16
- headers : {
17
- 'x-api-key' : apiKey ,
18
- 'x-session-key' : sessionKey ,
19
- } ,
20
- } ;
21
- /* global fetch */
22
- const response = await fetch ( url , options ) ;
23
- return await response . json ( ) ;
11
+ const sessionId = self . crypto . randomUUID ( ) ;
12
+
13
+ export const getData = async ( url : string ,
14
+ apiKey : string ,
15
+ sessionKey : string | null ) => {
16
+ // Validate that the session key is generated by crypto.randomUUID
17
+ // If it is not, set it to null
18
+ if ( sessionKey && ! sessionKey . match ( / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - 4 [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / ) ) {
19
+ sessionKey = null ;
20
+ }
21
+
22
+ const options : RequestInit = {
23
+ method : 'GET' ,
24
+ mode : 'cors' ,
25
+ cache : 'no-cache' ,
26
+ headers : {
27
+ 'x-api-key' : apiKey ,
28
+ 'x-session-key' : sessionKey || sessionId ,
29
+ } ,
30
+ } ;
31
+ /* global fetch */
32
+ const response = await fetch ( url , options ) ;
33
+ return await response . json ( ) ;
24
34
} ;
25
35
26
36
export const PARTNER = process . env . ENV ;
@@ -35,22 +45,23 @@ export const hostedFieldsEndpoint = `https://${ENVIRONMENT}.tags.static.${STAGE}
35
45
export const hostedCheckoutEndpoint = `https://${ ENVIRONMENT } .checkout.${ STAGE } .com` ;
36
46
37
47
export const fetchPtToken = async (
38
- apiKey : string ,
48
+ apiKey : string ,
49
+ sessionKey ?: string | null
39
50
) : Promise <
40
- | {
41
- 'pt-token' : string ;
42
- origin : string ;
43
- challengeOptions : object ;
44
- }
45
- | false
51
+ | {
52
+ 'pt-token' : string ;
53
+ origin : string ;
54
+ challengeOptions : object ;
55
+ }
56
+ | false
46
57
> => {
47
- for ( let i = 0 ; i < 5 ; i ++ ) {
48
- const token = await getData ( transactionEndpoint , apiKey ) ;
49
- if ( token [ 'pt-token' ] ) {
50
- return token ;
58
+ for ( let i = 0 ; i < 5 ; i ++ ) {
59
+ const token = await getData ( transactionEndpoint , apiKey , sessionKey ) ;
60
+ if ( token [ 'pt-token' ] ) {
61
+ return token ;
62
+ }
51
63
}
52
- }
53
- return false ;
64
+ return false ;
54
65
} ;
55
66
56
67
const sendTransactingMessageToField = (
0 commit comments