forked from paypal/paypal-messaging-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk.js
More file actions
290 lines (256 loc) · 8.45 KB
/
sdk.js
File metadata and controls
290 lines (256 loc) · 8.45 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* eslint-disable eslint-comments/disable-enable-pair, no-else-return */
import arrayFrom from 'core-js-pure/stable/array/from';
import { getStorage as getBelterStorage } from '@krakenjs/belter/src';
import { SDK_QUERY_KEYS, SDK_SETTINGS } from '@paypal/sdk-constants/src';
import {
getClientID,
getMerchantID,
getSDKScript,
getEnv as getSDKEnv,
getFundingEligibility,
getSDKMeta,
getSDKAttributes,
getSDKQueryParam,
getCSPNonce,
getNamespace as getSDKNamespace,
getDefaultNamespace as getDefaultSDKNamespace,
getSessionID as getSDKSessionID,
getStorageID as getSDKStorageID,
getStorageState as getSDKStorageState,
getPayPalDomain as getSDKPayPalDomain,
getDisableSetCookie as getSDKDisableCookie,
getPageType as getSDKPageType
} from '@paypal/sdk-client/src';
import { TAG } from './constants';
export function getDisableSetCookie() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKDisableCookie();
} else {
return false;
}
}
export function getNativeModal() {
if (typeof __MESSAGES__.__NATIVE_MODAL__ !== 'undefined') {
return __MESSAGES__.__NATIVE_MODAL__;
}
return false;
}
// SDK helper functions with standalone build polyfills
export function getEnv() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKEnv();
} else {
return __ENV__;
}
}
export function getMerchantConfig() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
// TODO: remove getFundingEligibility call and try catch after globals swap
try {
return __MESSAGING_GLOBALS__?.merchantProfile?.hash;
} catch {
return getFundingEligibility()?.paylater?.merchantConfigHash;
}
} else {
return undefined;
}
}
export function getAccount() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
// TODO: Should we pass both up if they exist so that nodeweb can create a partner context?
return getMerchantID().join(',') || `client-id:${getClientID()}`;
} else {
return undefined;
}
}
export function getPageType() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKPageType();
} else {
return undefined;
}
}
export function getNonce() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getCSPNonce();
} else {
return undefined;
}
}
// Partner accounts should always integrate using client id so no need to prefix it with 'client-id:'
export function getPartnerAccount() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getMerchantID()[0] && `client-id:${getClientID()}`;
} else {
return undefined;
}
}
const { currentScript } = typeof document !== 'undefined' ? document : {};
export function getScript() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKScript();
} else {
return (
currentScript ||
document.querySelector('script[src$="messaging.js"]') ||
document.querySelector('script[src$="merchant.js"]')
);
}
}
export function getCurrency() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
// Returns 'currency' query param without default to USD
return getSDKQueryParam(SDK_QUERY_KEYS.CURRENCY);
} else {
return undefined;
}
}
export function getMeta() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKMeta();
} else {
return undefined;
}
}
export function getScriptAttributes() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKAttributes();
} else {
return {};
}
}
export function getDefaultNamespace() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getDefaultSDKNamespace();
} else {
return 'paypal';
}
}
export function getNamespace() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKNamespace();
} else {
return getScript()?.getAttribute('data-pp-namespace') || getDefaultNamespace();
}
}
export function getLibraryVersion() {
return __MESSAGES__.__VERSION__;
}
export function isZoidComponent() {
// Merchants may use `zoid` to place our components inside an IFrame
// so we ensure that we check for the tags of our components
return Object.values(TAG).some(tag => window.name.startsWith(`__zoid__${tag.replace(/-/g, '_')}`));
}
export function getStorage() {
return getBelterStorage({ name: getNamespace() });
}
// Use SDK methods when available, otherwise manually fetch storage via belter
// see: https://github.com/paypal/paypal-sdk-client/blob/master/src/session.js
export function getSessionID() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKSessionID();
} else {
return getStorage().getSessionID();
}
}
// Retrieves storageID. NOTE: Creates new ID if not already in local storage.
export function getOrCreateDeviceID() {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKStorageID();
} else {
return getStorage().getID();
}
}
export function updateStorage(values) {
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKStorageState(storage => Object.assign(storage, values));
} else {
return getStorage().getState(storage => Object.assign(storage, values));
}
}
// Check if the current script is in the process of being destroyed since
// the MutationObservers can fire before the SDK destroy lifecycle hook
export const isScriptBeingDestroyed = () => {
if (__MESSAGES__.__TARGET__ === 'SDK') {
const currentSdkScript = getScript();
// URL API not supported in IE11, but anchor tags have parsed href properties
const location = document.createElement('a');
location.href = currentSdkScript.src;
// Ensure that there are currently no other SDK scripts that might be in the process of destroying this script
return arrayFrom(document.querySelectorAll(`script[src*="${location.host}/sdk/js"]`)).some(
script =>
script !== currentSdkScript &&
script.getAttribute(SDK_SETTINGS.NAMESPACE) === currentSdkScript.getAttribute(SDK_SETTINGS.NAMESPACE)
);
} else {
return false;
}
};
export function getPayPalDomain() {
if (getEnv() !== 'production' && getEnv() !== 'sandbox') {
const testEnviroment = window.__TEST_ENV__ ?? __MESSAGES__.__TEST_ENV__;
if (testEnviroment) {
return testEnviroment;
}
// eslint-disable-next-line security/detect-unsafe-regex
if (window.location.origin.match(/\.paypal\.com(:\d+)?$/)) {
return window.location.origin;
}
}
if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKPayPalDomain();
} else {
const domain = __MESSAGES__.__DOMAIN__[`__${getEnv().toUpperCase()}__`];
if (domain) {
return domain;
}
throw new Error('Missing PayPal Domain');
}
}
export function getStageTag() {
if (__MESSAGES__.__STAGE_TAG__) {
if (__MESSAGES__.__STAGE_TAG__ === 'local') {
return window.location.origin;
} else {
return __MESSAGES__.__STAGE_TAG__;
}
} else {
return undefined;
}
}
export function getDevTouchpoint() {
const devTouchpoint = window.__DEV_TOUCHPOINT__ ?? __MESSAGES__.__DEV_TOUCHPOINT__;
if (devTouchpoint && getEnv() !== 'production' && getEnv() !== 'sandbox') {
return true;
} else {
return undefined; // Prevent the zoid query param
}
}
// Combine static features and input features into single string
export function getFeatures(featureProps) {
const staticFeatures = [];
if (getNativeModal()) {
staticFeatures.push('native-modal');
}
let newFeatures = featureProps || '';
if (staticFeatures.length > 0) {
newFeatures += newFeatures ? `,${staticFeatures.join(',')}` : staticFeatures.join(',');
}
return featureProps === undefined && newFeatures === '' ? undefined : newFeatures;
}
// open mini-browser with message lander url
export function getURIPopup(lander, label) {
try {
// eslint-disable-next-line compat/compat
const parsed = new URL(lander);
const isHttp = parsed.protocol === 'https:';
const isPayPalDomain = /\.paypal\.com$/i.test(parsed.hostname);
if (isHttp && isPayPalDomain) {
return window.open(lander, label, 'width=460,height=900');
} else {
return null;
}
} catch (e) {
return null;
}
}