-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.js
41 lines (33 loc) · 1.46 KB
/
main.js
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
import Promise from 'promise-polyfill';
import { findScript, insertScriptElement, processOptions } from './utils';
const SDK_BASE_URL = 'https://www.paypal.com/sdk/js';
let loadingPromise;
let isLoading = false;
export function loadScript(options) {
// resolve with the existing promise when the script is loading
if (isLoading) return loadingPromise;
return loadingPromise = new Promise((resolve, reject) => {
// resolve with null when running in Node
if (typeof window === 'undefined') return resolve(null);
const { queryString, dataAttributes } = processOptions(options);
const url = `${SDK_BASE_URL}?${queryString}`;
// resolve with the existing global paypal object when a script with the same src already exists
if (findScript(url, dataAttributes) && window.paypal) return resolve(window.paypal);
isLoading = true;
insertScriptElement({
url,
dataAttributes,
onSuccess: () => {
isLoading = false;
if (window.paypal) return resolve(window.paypal);
return reject(new Error('The window.paypal global variable is not available.'));
},
onError: () => {
isLoading = false;
return reject(new Error(`The script "${url}" didn't load correctly.`));
}
});
});
}
// replaced with the package.json version at build time
export const version = '__VERSION__';