Skip to content

Commit 3fa2f08

Browse files
committed
fix: merge with session fix
2 parents 3b741a0 + e29a417 commit 3fa2f08

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

src/common/network.ts

+37-26
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@ import { ACH_IFRAME, CARD_IFRAME, CASH_IFRAME, ElementTypes } from './data';
88

99
const sessionKey = self.crypto.randomUUID();
1010

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-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-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();
2434
};
2535

2636
export const PARTNER = process.env.ENV;
@@ -35,22 +45,23 @@ export const hostedFieldsEndpoint = `https://${ENVIRONMENT}.tags.static.${STAGE}
3545
export const hostedCheckoutEndpoint = `https://${ENVIRONMENT}.checkout.${STAGE}.com`;
3646

3747
export const fetchPtToken = async (
38-
apiKey: string,
48+
apiKey: string,
49+
sessionKey?: string | null
3950
): 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
4657
> => {
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+
}
5163
}
52-
}
53-
return false;
64+
return false;
5465
};
5566

5667
const sendTransactingMessageToField = (

src/common/pay_theory_types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export type PayTheoryPaymentFieldsInput = {
169169
metadata?: { [key: string | number]: string | number | boolean };
170170
placeholders?: PlaceholderObject;
171171
elementIds?: typeof defaultElementIds;
172-
session?: string;
172+
session?: string; // This is used for internal use to connect a button and qr code to a hosted checkout page
173173
feeMode?: typeof MERCHANT_FEE | typeof SERVICE_FEE;
174174
amount?: number;
175175
};

src/components/pay-theory-hosted-field-transactional/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class PayTheoryHostedFieldTransactional extends PayTheoryHostedField {
153153
type: `pt-static:connection_token` | `pt-static:reset_host`,
154154
): Promise<ErrorResponse | ReadyResponse> {
155155
try {
156-
const ptToken = await common.fetchPtToken(this._apiKey!);
156+
const ptToken = await common.fetchPtToken(this._apiKey!, this._session);
157157
if (ptToken) {
158158
this._challengeOptions = ptToken['challengeOptions'];
159159
const transactingIFrame = document.getElementById(

temp-062024

Whitespace-only changes.

0 commit comments

Comments
 (0)