-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathutils.js
More file actions
171 lines (157 loc) · 5.46 KB
/
utils.js
File metadata and controls
171 lines (157 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import objectEntries from 'core-js-pure/stable/object/entries';
import arrayFrom from 'core-js-pure/stable/array/from';
import { isIosWebview, isAndroidWebview } from '@krakenjs/belter/src';
import { request, memoize, ppDebug } from '../../../../utils';
import validate from '../../../../library/zoid/message/validation';
export const getContent = memoize(
({
currency,
amount,
payerId,
clientId,
merchantId,
customerId,
buyerCountry,
language,
ignoreCache,
deviceID,
version,
env,
stageTag,
integrationType,
channel,
contextualComponents,
devTouchpoint,
disableSetCookie,
features,
buttonSessionId
}) => {
const query = objectEntries({
currency,
amount,
payer_id: payerId,
client_id: clientId,
merchant_id: merchantId,
customer_id: customerId,
buyer_country: buyerCountry,
language,
ignore_cache: ignoreCache,
deviceID,
version,
env,
stageTag,
integrationType,
channel,
contextual_components: contextualComponents,
devTouchpoint,
disableSetCookie,
features,
buttonSessionId
})
.filter(([, val]) => Boolean(val))
.reduce(
(acc, [key, val]) =>
`${acc}&${key}=${encodeURIComponent(typeof val === 'object' ? JSON.stringify(val) : val)}`,
''
)
.slice(1);
ppDebug('Updating modal with new props...', { inZoid: true });
return request('GET', `${window.location.origin}/credit-presentment/modalContent?${query}`).then(
({ data }) => data
);
}
);
/**
* Checks if target is lander. If true, lander-specific styles will be used.
* @returns boolean
*/
export const isLander = __MESSAGES__.__TARGET__ === 'LANDER';
const { userAgent } = window.navigator;
export const isIframe = window.top !== window || isIosWebview(userAgent) || isAndroidWebview(userAgent);
export function setupTabTrap() {
// Disable tab trap functionality for modal lander
if (isLander) {
return;
}
const focusableElementsString =
"a[href], button, input, textarea, select, details, [tabindex]:not([tabindex='-1'])";
function trapTabKey(e) {
// Check for TAB key press
if (e.keyCode === 9 && !document.querySelector('.modal-closed')) {
const tabArray = arrayFrom(document.querySelectorAll(focusableElementsString)).filter(
node => window.getComputedStyle(node).visibility === 'visible'
);
// SHIFT + TAB
if (e.shiftKey && document.activeElement === tabArray[0]) {
e.preventDefault();
tabArray[tabArray.length - 1].focus();
} else if (document.activeElement === tabArray[tabArray.length - 1]) {
e.preventDefault();
tabArray[0].focus();
}
// auto scroll into view for focused element
setTimeout(() => {
document.activeElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
}, 0);
}
}
window.addEventListener('keydown', trapTabKey);
}
export function formatDateByCountry(country) {
const currentDate = new Date();
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
if (country === 'US') {
return currentDate.toLocaleDateString('en-US', options);
}
return currentDate.toLocaleDateString('en-GB', options);
}
export function createUUID() {
// crypto.randomUUID() is only available in HTTPS secure environments and modern browsers
if (typeof crypto !== 'undefined' && crypto && crypto.randomUUID instanceof Function) {
return crypto.randomUUID();
}
const validChars = '0123456789abcdefghijklmnopqrstuvwxyz';
const stringLength = 32;
let randomId = '';
for (let index = 0; index < stringLength; index++) {
const randomIndex = Math.floor(Math.random() * validChars.length);
randomId += validChars.charAt(randomIndex);
}
return randomId;
}
export function validateProps(updatedProps) {
const validatedProps = {};
Object.entries(updatedProps).forEach(entry => {
const [k, v] = entry;
if (k === 'offerType') {
validatedProps.offer = validate.offer({ props: { offer: v } });
} else {
validatedProps[k] = validate[k]({ props: { [k]: v } });
}
});
return validatedProps;
}
export function sendEventAck(eventId) {
// skip this step if running in test env because jest's target windows don't support postMessage
if (window.process?.env?.NODE_ENV === 'test') {
return;
}
// target window selection depends on if checkout window is in popup or modal iframe
let targetWindow;
const popupCheck = window.parent === window;
if (popupCheck) {
targetWindow = window.opener;
} else {
targetWindow = window.parent;
}
targetWindow.postMessage(
{
// PostMessenger stops reposting an event when it receives an eventName which matches the id in the message it sent and type 'ack'
eventName: eventId,
type: 'ack',
eventPayload: { ok: true },
id: createUUID()
},
'*'
);
}