@paypal/paypal-js
Advanced tools
Comparing version 2.1.9 to 3.0.0
/*! | ||
* paypal-js v2.1.9 (2021-02-15T22:49:36.078Z) | ||
* paypal-js v3.0.0 (2021-03-01T22:43:43.710Z) | ||
* Copyright 2020-present, PayPal, Inc. All rights reserved. | ||
@@ -25,3 +25,3 @@ * | ||
if (objectSize(currentScript.dataset) !== objectSize(nextScript.dataset)) { | ||
if (Object.keys(currentScript.dataset).length !== Object.keys(nextScript.dataset).length) { | ||
return null; | ||
@@ -32,3 +32,3 @@ } | ||
forEachObjectKey(currentScript.dataset, function (key) { | ||
Object.keys(currentScript.dataset).forEach(function (key) { | ||
if (currentScript.dataset[key] !== nextScript.dataset[key]) { | ||
@@ -58,13 +58,24 @@ isExactMatch = false; | ||
var processedOptions = { | ||
var processedMerchantIDAttributes = processMerchantID(options["merchant-id"], options["data-merchant-id"]); | ||
var newOptions = Object.assign(options, processedMerchantIDAttributes); | ||
var _Object$keys$filter$r = Object.keys(newOptions).filter(function (key) { | ||
return typeof newOptions[key] !== "undefined" && newOptions[key] !== null && newOptions[key] !== ""; | ||
}).reduce(function (accumulator, key) { | ||
var value = newOptions[key].toString(); | ||
if (key.substring(0, 5) === "data-") { | ||
accumulator.dataAttributes[key] = value; | ||
} else { | ||
accumulator.queryParams[key] = value; | ||
} | ||
return accumulator; | ||
}, { | ||
queryParams: {}, | ||
dataAttributes: {} | ||
}; | ||
forEachObjectKey(options, function (key) { | ||
var keyType = key.substring(0, 5) === "data-" ? "dataAttributes" : "queryParams"; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
}), | ||
queryParams = _Object$keys$filter$r.queryParams, | ||
dataAttributes = _Object$keys$filter$r.dataAttributes; | ||
processedOptions[keyType][key] = options[key].toString(); | ||
}); | ||
var queryParams = processedOptions.queryParams, | ||
dataAttributes = processedOptions.dataAttributes; | ||
return { | ||
@@ -77,3 +88,3 @@ url: "".concat(sdkBaseURL, "?").concat(objectToQueryString(queryParams)), | ||
var queryString = ""; | ||
forEachObjectKey(params, function (key) { | ||
Object.keys(params).forEach(function (key) { | ||
if (queryString.length !== 0) queryString += "&"; | ||
@@ -89,3 +100,3 @@ queryString += key + "=" + params[key]; | ||
newScript.src = url; | ||
forEachObjectKey(dataAttributes, function (key) { | ||
Object.keys(dataAttributes).forEach(function (key) { | ||
newScript.setAttribute(key, dataAttributes[key]); | ||
@@ -98,40 +109,44 @@ | ||
return newScript; | ||
} // uses es3 to avoid requiring polyfills for Array.prototype.forEach and Object.keys | ||
} | ||
function processMerchantID(merchantID, dataMerchantID) { | ||
var newMerchantID = ""; | ||
var newDataMerchantID = ""; | ||
function forEachObjectKey(obj, callback) { | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
callback(key); | ||
if (Array.isArray(merchantID)) { | ||
if (merchantID.length > 1) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = merchantID.toString(); | ||
} else { | ||
newMerchantID = merchantID.toString(); | ||
} | ||
} else if (typeof merchantID === "string" && merchantID.length > 0) { | ||
newMerchantID = merchantID; | ||
} else if (typeof dataMerchantID === "string" && dataMerchantID.length > 0) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = dataMerchantID; | ||
} | ||
} | ||
function objectSize(obj) { | ||
var size = 0; | ||
forEachObjectKey(obj, function () { | ||
return size++; | ||
}); | ||
return size; | ||
return { | ||
"merchant-id": newMerchantID, | ||
"data-merchant-id": newDataMerchantID | ||
}; | ||
} | ||
var loadingPromise; | ||
var isLoading = false; | ||
/** | ||
* Load the Paypal JS SDK script asynchronously. | ||
* | ||
* @param {Object} options - used to configure query parameters and data attributes for the JS SDK. | ||
* @param {PromiseConstructor} [PromisePonyfill=window.Promise] - optional Promise Constructor ponyfill. | ||
* @return {Promise<Object>} paypalObject - reference to the global window PayPal object. | ||
*/ | ||
function loadScript(options, PromisePonyfill) { | ||
if (!(options instanceof Object)) { | ||
throw new Error("Invalid arguments. Expected an object to be passed into loadScript()."); | ||
} | ||
validateArguments(options, PromisePonyfill); | ||
if (typeof PromisePonyfill === "undefined") { | ||
// default to using window.Promise as the Promise implementation | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Failed to load the PayPal JS SDK script because Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
PromisePonyfill = getDefaultPromiseImplementation(); | ||
} | ||
PromisePonyfill = Promise; | ||
} // resolve with the existing promise when the script is loading | ||
if (isLoading) return loadingPromise; | ||
return loadingPromise = new PromisePonyfill(function (resolve, reject) { | ||
return new PromisePonyfill(function (resolve, reject) { | ||
// resolve with null when running in Node | ||
@@ -142,7 +157,11 @@ if (typeof window === "undefined") return resolve(null); | ||
url = _processOptions.url, | ||
dataAttributes = _processOptions.dataAttributes; // resolve with the existing global paypal object when a script with the same src already exists | ||
dataAttributes = _processOptions.dataAttributes; | ||
var namespace = dataAttributes["data-namespace"] || "paypal"; | ||
var existingWindowNamespace = getPayPalWindowNamespace(namespace); // resolve with the existing global paypal namespace when a script with the same params already exists | ||
if (findScript(url, dataAttributes) && window.paypal) return resolve(window.paypal); | ||
isLoading = true; | ||
if (findScript(url, dataAttributes) && existingWindowNamespace) { | ||
return resolve(existingWindowNamespace); | ||
} | ||
insertScriptElement({ | ||
@@ -152,8 +171,7 @@ url: url, | ||
onSuccess: function onSuccess() { | ||
isLoading = false; | ||
if (window.paypal) return resolve(window.paypal); | ||
return reject(new Error("The window.paypal global variable is not available.")); | ||
var newWindowNamespace = getPayPalWindowNamespace(namespace); | ||
if (newWindowNamespace) return resolve(newWindowNamespace); | ||
return reject(new Error("The window.".concat(namespace, " global variable is not available."))); | ||
}, | ||
onError: function onError() { | ||
isLoading = false; | ||
return reject(new Error("The script \"".concat(url, "\" didn't load correctly."))); | ||
@@ -165,4 +183,31 @@ } | ||
var version = "2.1.9"; | ||
function validateArguments(options, PromisePonyfill) { | ||
if (typeof options !== "object" || options === null) { | ||
throw new Error("Invalid arguments. Expected options to be an object."); | ||
} | ||
if (typeof PromisePonyfill === "undefined") { | ||
return; | ||
} | ||
if (typeof PromisePonyfill !== "function") { | ||
throw new Error("Invalid arguments. Expected PromisePolyfill to be a function."); | ||
} | ||
} | ||
function getDefaultPromiseImplementation() { | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
return Promise; | ||
} | ||
function getPayPalWindowNamespace(namespace) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return window[namespace]; | ||
} | ||
var version = "3.0.0"; | ||
exports.loadScript = loadScript; | ||
@@ -169,0 +214,0 @@ exports.version = version; |
/*! | ||
* paypal-js v2.1.9 (2021-02-15T22:49:36.078Z) | ||
* paypal-js v3.0.0 (2021-03-01T22:43:43.710Z) | ||
* Copyright 2020-present, PayPal, Inc. All rights reserved. | ||
@@ -17,2 +17,2 @@ * | ||
*/ | ||
var paypalLoadScript=function(t){"use strict";function r(t){var r="";return n(t,(function(e){0!==r.length&&(r+="&"),r+=e+"="+t[e]})),r}function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e=document.createElement("script");return e.src=t,n(r,(function(t){e.setAttribute(t,r[t]),"data-csp-nonce"===t&&e.setAttribute("nonce",r["data-csp-nonce"])})),e}function n(t,r){for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&r(e)}function a(t){var r=0;return n(t,(function(){return r++})),r}var o,i=!1;return t.loadScript=function(t,u){if(!(t instanceof Object))throw new Error("Invalid arguments. Expected an object to be passed into loadScript().");if(void 0===u){if("undefined"==typeof Promise)throw new Error("Failed to load the PayPal JS SDK script because Promise is undefined. To resolve the issue, use a Promise polyfill.");u=Promise}return i?o:o=new u((function(o,u){if("undefined"==typeof window)return o(null);var c=function(t){var e="https://www.paypal.com/sdk/js";t.sdkBaseURL&&(e=t.sdkBaseURL,delete t.sdkBaseURL);var a={queryParams:{},dataAttributes:{}};n(t,(function(r){var e="data-"===r.substring(0,5)?"dataAttributes":"queryParams";a[e][r]=t[r].toString()}));var o=a.queryParams,i=a.dataAttributes;return{url:"".concat(e,"?").concat(r(o)),dataAttributes:i}}(t),s=c.url,d=c.dataAttributes;if(function(t,r){var o=document.querySelector('script[src="'.concat(t,'"]'));if(null===o)return null;var i=e(t,r);if(a(o.dataset)!==a(i.dataset))return null;var u=!0;return n(o.dataset,(function(t){o.dataset[t]!==i.dataset[t]&&(u=!1)})),u?o:null}(s,d)&&window.paypal)return o(window.paypal);i=!0,function(t){var r=t.url,n=t.dataAttributes,a=t.onSuccess,o=t.onError,i=e(r,n);i.onerror=o,i.onload=a,document.head.insertBefore(i,document.head.firstElementChild)}({url:s,dataAttributes:d,onSuccess:function(){return i=!1,window.paypal?o(window.paypal):u(new Error("The window.paypal global variable is not available."))},onError:function(){return i=!1,u(new Error('The script "'.concat(s,"\" didn't load correctly.")))}})}))},t.version="2.1.9",Object.defineProperty(t,"__esModule",{value:!0}),t}({});paypalLoadScript=paypalLoadScript.loadScript; | ||
var paypalLoadScript=function(t){"use strict";function e(t){var e="https://www.paypal.com/sdk/js";t.sdkBaseURL&&(e=t.sdkBaseURL,delete t.sdkBaseURL);var n=function(t,e){var r="",n="";Array.isArray(t)?t.length>1?(r="*",n=t.toString()):r=t.toString():"string"==typeof t&&t.length>0?r=t:"string"==typeof e&&e.length>0&&(r="*",n=e);return{"merchant-id":r,"data-merchant-id":n}}(t["merchant-id"],t["data-merchant-id"]),a=Object.assign(t,n),o=Object.keys(a).filter((function(t){return void 0!==a[t]&&null!==a[t]&&""!==a[t]})).reduce((function(t,e){var r=a[e].toString();return"data-"===e.substring(0,5)?t.dataAttributes[e]=r:t.queryParams[e]=r,t}),{queryParams:{},dataAttributes:{}}),i=o.queryParams,c=o.dataAttributes;return{url:"".concat(e,"?").concat(r(i)),dataAttributes:c}}function r(t){var e="";return Object.keys(t).forEach((function(r){0!==e.length&&(e+="&"),e+=r+"="+t[r]})),e}function n(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=document.createElement("script");return r.src=t,Object.keys(e).forEach((function(t){r.setAttribute(t,e[t]),"data-csp-nonce"===t&&r.setAttribute("nonce",e["data-csp-nonce"])})),r}function a(t){return window[t]}return t.loadScript=function(t,r){return function(t,e){if("object"!=typeof t||null===t)throw new Error("Invalid arguments. Expected options to be an object.");if(void 0===e)return;if("function"!=typeof e)throw new Error("Invalid arguments. Expected PromisePolyfill to be a function.")}(t,r),void 0===r&&(r=function(){if("undefined"==typeof Promise)throw new Error("Promise is undefined. To resolve the issue, use a Promise polyfill.");return Promise}()),new r((function(r,o){if("undefined"==typeof window)return r(null);var i=e(t),c=i.url,u=i.dataAttributes,s=u["data-namespace"]||"paypal",d=a(s);if(function(t,e){var r=document.querySelector('script[src="'.concat(t,'"]'));if(null===r)return null;var a=n(t,e);if(Object.keys(r.dataset).length!==Object.keys(a.dataset).length)return null;var o=!0;return Object.keys(r.dataset).forEach((function(t){r.dataset[t]!==a.dataset[t]&&(o=!1)})),o?r:null}(c,u)&&d)return r(d);!function(t){var e=t.url,r=t.dataAttributes,a=t.onSuccess,o=t.onError,i=n(e,r);i.onerror=o,i.onload=a,document.head.insertBefore(i,document.head.firstElementChild)}({url:c,dataAttributes:u,onSuccess:function(){var t=a(s);return t?r(t):o(new Error("The window.".concat(s," global variable is not available.")))},onError:function(){return o(new Error('The script "'.concat(c,"\" didn't load correctly.")))}})}))},t.version="3.0.0",Object.defineProperty(t,"__esModule",{value:!0}),t}({});paypalLoadScript=paypalLoadScript.loadScript; |
/*! | ||
* paypal-js v2.1.9 (2021-02-15T22:49:36.078Z) | ||
* paypal-js v3.0.0 (2021-03-01T22:43:43.710Z) | ||
* Copyright 2020-present, PayPal, Inc. All rights reserved. | ||
@@ -22,3 +22,3 @@ * | ||
if (objectSize(currentScript.dataset) !== objectSize(nextScript.dataset)) { | ||
if (Object.keys(currentScript.dataset).length !== Object.keys(nextScript.dataset).length) { | ||
return null; | ||
@@ -29,3 +29,3 @@ } | ||
forEachObjectKey(currentScript.dataset, function (key) { | ||
Object.keys(currentScript.dataset).forEach(function (key) { | ||
if (currentScript.dataset[key] !== nextScript.dataset[key]) { | ||
@@ -55,13 +55,24 @@ isExactMatch = false; | ||
var processedOptions = { | ||
var processedMerchantIDAttributes = processMerchantID(options["merchant-id"], options["data-merchant-id"]); | ||
var newOptions = Object.assign(options, processedMerchantIDAttributes); | ||
var _Object$keys$filter$r = Object.keys(newOptions).filter(function (key) { | ||
return typeof newOptions[key] !== "undefined" && newOptions[key] !== null && newOptions[key] !== ""; | ||
}).reduce(function (accumulator, key) { | ||
var value = newOptions[key].toString(); | ||
if (key.substring(0, 5) === "data-") { | ||
accumulator.dataAttributes[key] = value; | ||
} else { | ||
accumulator.queryParams[key] = value; | ||
} | ||
return accumulator; | ||
}, { | ||
queryParams: {}, | ||
dataAttributes: {} | ||
}; | ||
forEachObjectKey(options, function (key) { | ||
var keyType = key.substring(0, 5) === "data-" ? "dataAttributes" : "queryParams"; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
}), | ||
queryParams = _Object$keys$filter$r.queryParams, | ||
dataAttributes = _Object$keys$filter$r.dataAttributes; | ||
processedOptions[keyType][key] = options[key].toString(); | ||
}); | ||
var queryParams = processedOptions.queryParams, | ||
dataAttributes = processedOptions.dataAttributes; | ||
return { | ||
@@ -74,3 +85,3 @@ url: "".concat(sdkBaseURL, "?").concat(objectToQueryString(queryParams)), | ||
var queryString = ""; | ||
forEachObjectKey(params, function (key) { | ||
Object.keys(params).forEach(function (key) { | ||
if (queryString.length !== 0) queryString += "&"; | ||
@@ -86,3 +97,3 @@ queryString += key + "=" + params[key]; | ||
newScript.src = url; | ||
forEachObjectKey(dataAttributes, function (key) { | ||
Object.keys(dataAttributes).forEach(function (key) { | ||
newScript.setAttribute(key, dataAttributes[key]); | ||
@@ -95,40 +106,44 @@ | ||
return newScript; | ||
} // uses es3 to avoid requiring polyfills for Array.prototype.forEach and Object.keys | ||
} | ||
function processMerchantID(merchantID, dataMerchantID) { | ||
var newMerchantID = ""; | ||
var newDataMerchantID = ""; | ||
function forEachObjectKey(obj, callback) { | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
callback(key); | ||
if (Array.isArray(merchantID)) { | ||
if (merchantID.length > 1) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = merchantID.toString(); | ||
} else { | ||
newMerchantID = merchantID.toString(); | ||
} | ||
} else if (typeof merchantID === "string" && merchantID.length > 0) { | ||
newMerchantID = merchantID; | ||
} else if (typeof dataMerchantID === "string" && dataMerchantID.length > 0) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = dataMerchantID; | ||
} | ||
} | ||
function objectSize(obj) { | ||
var size = 0; | ||
forEachObjectKey(obj, function () { | ||
return size++; | ||
}); | ||
return size; | ||
return { | ||
"merchant-id": newMerchantID, | ||
"data-merchant-id": newDataMerchantID | ||
}; | ||
} | ||
var loadingPromise; | ||
var isLoading = false; | ||
/** | ||
* Load the Paypal JS SDK script asynchronously. | ||
* | ||
* @param {Object} options - used to configure query parameters and data attributes for the JS SDK. | ||
* @param {PromiseConstructor} [PromisePonyfill=window.Promise] - optional Promise Constructor ponyfill. | ||
* @return {Promise<Object>} paypalObject - reference to the global window PayPal object. | ||
*/ | ||
function loadScript(options, PromisePonyfill) { | ||
if (!(options instanceof Object)) { | ||
throw new Error("Invalid arguments. Expected an object to be passed into loadScript()."); | ||
} | ||
validateArguments(options, PromisePonyfill); | ||
if (typeof PromisePonyfill === "undefined") { | ||
// default to using window.Promise as the Promise implementation | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Failed to load the PayPal JS SDK script because Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
PromisePonyfill = getDefaultPromiseImplementation(); | ||
} | ||
PromisePonyfill = Promise; | ||
} // resolve with the existing promise when the script is loading | ||
if (isLoading) return loadingPromise; | ||
return loadingPromise = new PromisePonyfill(function (resolve, reject) { | ||
return new PromisePonyfill(function (resolve, reject) { | ||
// resolve with null when running in Node | ||
@@ -139,7 +154,11 @@ if (typeof window === "undefined") return resolve(null); | ||
url = _processOptions.url, | ||
dataAttributes = _processOptions.dataAttributes; // resolve with the existing global paypal object when a script with the same src already exists | ||
dataAttributes = _processOptions.dataAttributes; | ||
var namespace = dataAttributes["data-namespace"] || "paypal"; | ||
var existingWindowNamespace = getPayPalWindowNamespace(namespace); // resolve with the existing global paypal namespace when a script with the same params already exists | ||
if (findScript(url, dataAttributes) && window.paypal) return resolve(window.paypal); | ||
isLoading = true; | ||
if (findScript(url, dataAttributes) && existingWindowNamespace) { | ||
return resolve(existingWindowNamespace); | ||
} | ||
insertScriptElement({ | ||
@@ -149,8 +168,7 @@ url: url, | ||
onSuccess: function onSuccess() { | ||
isLoading = false; | ||
if (window.paypal) return resolve(window.paypal); | ||
return reject(new Error("The window.paypal global variable is not available.")); | ||
var newWindowNamespace = getPayPalWindowNamespace(namespace); | ||
if (newWindowNamespace) return resolve(newWindowNamespace); | ||
return reject(new Error("The window.".concat(namespace, " global variable is not available."))); | ||
}, | ||
onError: function onError() { | ||
isLoading = false; | ||
return reject(new Error("The script \"".concat(url, "\" didn't load correctly."))); | ||
@@ -162,4 +180,31 @@ } | ||
var version = "2.1.9"; | ||
function validateArguments(options, PromisePonyfill) { | ||
if (typeof options !== "object" || options === null) { | ||
throw new Error("Invalid arguments. Expected options to be an object."); | ||
} | ||
if (typeof PromisePonyfill === "undefined") { | ||
return; | ||
} | ||
if (typeof PromisePonyfill !== "function") { | ||
throw new Error("Invalid arguments. Expected PromisePolyfill to be a function."); | ||
} | ||
} | ||
function getDefaultPromiseImplementation() { | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
return Promise; | ||
} | ||
function getPayPalWindowNamespace(namespace) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return window[namespace]; | ||
} | ||
var version = "3.0.0"; | ||
export { loadScript, version }; |
/*! | ||
* paypal-js v2.1.9 (2021-02-15T22:49:36.078Z) | ||
* paypal-js v3.0.0 (2021-03-01T22:43:43.710Z) | ||
* Copyright 2020-present, PayPal, Inc. All rights reserved. | ||
@@ -347,3 +347,4 @@ * | ||
// check if the new script has the same number of data attributes | ||
if (objectSize(currentScript.dataset) !== objectSize(nextScript.dataset)) { | ||
if (Object.keys(currentScript.dataset).length !== | ||
Object.keys(nextScript.dataset).length) { | ||
return null; | ||
@@ -353,3 +354,3 @@ } | ||
// check if the data attribute values are the same | ||
forEachObjectKey(currentScript.dataset, (key) => { | ||
Object.keys(currentScript.dataset).forEach((key) => { | ||
if (currentScript.dataset[key] !== nextScript.dataset[key]) { | ||
@@ -373,12 +374,23 @@ isExactMatch = false; | ||
} | ||
const processedOptions = { | ||
const processedMerchantIDAttributes = processMerchantID(options["merchant-id"], options["data-merchant-id"]); | ||
const newOptions = Object.assign(options, processedMerchantIDAttributes); | ||
const { queryParams, dataAttributes } = Object.keys(newOptions) | ||
.filter((key) => { | ||
return (typeof newOptions[key] !== "undefined" && | ||
newOptions[key] !== null && | ||
newOptions[key] !== ""); | ||
}) | ||
.reduce((accumulator, key) => { | ||
const value = newOptions[key].toString(); | ||
if (key.substring(0, 5) === "data-") { | ||
accumulator.dataAttributes[key] = value; | ||
} | ||
else { | ||
accumulator.queryParams[key] = value; | ||
} | ||
return accumulator; | ||
}, { | ||
queryParams: {}, | ||
dataAttributes: {}, | ||
}; | ||
forEachObjectKey(options, (key) => { | ||
const keyType = key.substring(0, 5) === "data-" ? "dataAttributes" : "queryParams"; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
processedOptions[keyType][key] = options[key].toString(); | ||
}); | ||
const { queryParams, dataAttributes } = processedOptions; | ||
return { | ||
@@ -391,3 +403,3 @@ url: `${sdkBaseURL}?${objectToQueryString(queryParams)}`, | ||
let queryString = ""; | ||
forEachObjectKey(params, (key) => { | ||
Object.keys(params).forEach((key) => { | ||
if (queryString.length !== 0) | ||
@@ -402,3 +414,3 @@ queryString += "&"; | ||
newScript.src = url; | ||
forEachObjectKey(dataAttributes, (key) => { | ||
Object.keys(dataAttributes).forEach((key) => { | ||
newScript.setAttribute(key, dataAttributes[key]); | ||
@@ -411,33 +423,41 @@ if (key === "data-csp-nonce") { | ||
} | ||
// uses es3 to avoid requiring polyfills for Array.prototype.forEach and Object.keys | ||
function forEachObjectKey(obj, callback) { | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
callback(key); | ||
function processMerchantID(merchantID, dataMerchantID) { | ||
let newMerchantID = ""; | ||
let newDataMerchantID = ""; | ||
if (Array.isArray(merchantID)) { | ||
if (merchantID.length > 1) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = merchantID.toString(); | ||
} | ||
else { | ||
newMerchantID = merchantID.toString(); | ||
} | ||
} | ||
else if (typeof merchantID === "string" && merchantID.length > 0) { | ||
newMerchantID = merchantID; | ||
} | ||
else if (typeof dataMerchantID === "string" && | ||
dataMerchantID.length > 0) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = dataMerchantID; | ||
} | ||
return { | ||
"merchant-id": newMerchantID, | ||
"data-merchant-id": newDataMerchantID, | ||
}; | ||
} | ||
function objectSize(obj) { | ||
let size = 0; | ||
forEachObjectKey(obj, () => size++); | ||
return size; | ||
} | ||
let loadingPromise; | ||
let isLoading = false; | ||
function loadScript(options, PromisePonyfill) { | ||
if (!(options instanceof Object)) { | ||
throw new Error("Invalid arguments. Expected an object to be passed into loadScript()."); | ||
} | ||
/** | ||
* Load the Paypal JS SDK script asynchronously. | ||
* | ||
* @param {Object} options - used to configure query parameters and data attributes for the JS SDK. | ||
* @param {PromiseConstructor} [PromisePonyfill=window.Promise] - optional Promise Constructor ponyfill. | ||
* @return {Promise<Object>} paypalObject - reference to the global window PayPal object. | ||
*/ | ||
function loadScript$1(options, PromisePonyfill) { | ||
validateArguments(options, PromisePonyfill); | ||
if (typeof PromisePonyfill === "undefined") { | ||
// default to using window.Promise as the Promise implementation | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Failed to load the PayPal JS SDK script because Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
PromisePonyfill = Promise; | ||
PromisePonyfill = getDefaultPromiseImplementation(); | ||
} | ||
// resolve with the existing promise when the script is loading | ||
if (isLoading) | ||
return loadingPromise; | ||
return (loadingPromise = new PromisePonyfill((resolve, reject) => { | ||
return new PromisePonyfill((resolve, reject) => { | ||
// resolve with null when running in Node | ||
@@ -447,6 +467,8 @@ if (typeof window === "undefined") | ||
const { url, dataAttributes } = processOptions(options); | ||
// 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; | ||
const namespace = dataAttributes["data-namespace"] || "paypal"; | ||
const existingWindowNamespace = getPayPalWindowNamespace(namespace); | ||
// resolve with the existing global paypal namespace when a script with the same params already exists | ||
if (findScript(url, dataAttributes) && existingWindowNamespace) { | ||
return resolve(existingWindowNamespace); | ||
} | ||
insertScriptElement({ | ||
@@ -456,22 +478,42 @@ url, | ||
onSuccess: () => { | ||
isLoading = false; | ||
if (window.paypal) | ||
return resolve(window.paypal); | ||
return reject(new Error("The window.paypal global variable is not available.")); | ||
const newWindowNamespace = getPayPalWindowNamespace(namespace); | ||
if (newWindowNamespace) | ||
return resolve(newWindowNamespace); | ||
return reject(new Error(`The window.${namespace} global variable is not available.`)); | ||
}, | ||
onError: () => { | ||
isLoading = false; | ||
return reject(new Error(`The script "${url}" didn't load correctly.`)); | ||
}, | ||
}); | ||
})); | ||
}); | ||
} | ||
function validateArguments(options, PromisePonyfill) { | ||
if (typeof options !== "object" || options === null) { | ||
throw new Error("Invalid arguments. Expected options to be an object."); | ||
} | ||
if (typeof PromisePonyfill === "undefined") { | ||
return; | ||
} | ||
if (typeof PromisePonyfill !== "function") { | ||
throw new Error("Invalid arguments. Expected PromisePolyfill to be a function."); | ||
} | ||
} | ||
function getDefaultPromiseImplementation() { | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
return Promise; | ||
} | ||
function getPayPalWindowNamespace(namespace) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return window[namespace]; | ||
} | ||
function loadScript$1(options) { | ||
return loadScript(options, Promise$1); | ||
function loadScript(options) { | ||
return loadScript$1(options, Promise$1); | ||
} | ||
// replaced with the package.json version at build time | ||
const version = "2.1.9"; | ||
const version = "3.0.0"; | ||
exports.loadScript = loadScript$1; | ||
exports.loadScript = loadScript; | ||
exports.version = version; | ||
@@ -478,0 +520,0 @@ |
/*! | ||
* paypal-js v2.1.9 (2021-02-15T22:49:36.078Z) | ||
* paypal-js v3.0.0 (2021-03-01T22:43:43.710Z) | ||
* Copyright 2020-present, PayPal, Inc. All rights reserved. | ||
@@ -17,2 +17,2 @@ * | ||
*/ | ||
var paypalLoadScript=function(t){"use strict";var e=setTimeout;function n(t){return Boolean(t&&void 0!==t.length)}function r(){}function o(t){if(!(this instanceof o))throw new TypeError("Promises must be constructed via new");if("function"!=typeof t)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],l(t,this)}function i(t,e){for(;3===t._state;)t=t._value;0!==t._state?(t._handled=!0,o._immediateFn((function(){var n=1===t._state?e.onFulfilled:e.onRejected;if(null!==n){var r;try{r=n(t._value)}catch(t){return void u(e.promise,t)}a(e.promise,r)}else(1===t._state?a:u)(e.promise,t._value)}))):t._deferreds.push(e)}function a(t,e){try{if(e===t)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var n=e.then;if(e instanceof o)return t._state=3,t._value=e,void c(t);if("function"==typeof n)return void l((r=n,i=e,function(){r.apply(i,arguments)}),t)}t._state=1,t._value=e,c(t)}catch(e){u(t,e)}var r,i}function u(t,e){t._state=2,t._value=e,c(t)}function c(t){2===t._state&&0===t._deferreds.length&&o._immediateFn((function(){t._handled||o._unhandledRejectionFn(t._value)}));for(var e=0,n=t._deferreds.length;e<n;e++)i(t,t._deferreds[e]);t._deferreds=null}function s(t,e,n){this.onFulfilled="function"==typeof t?t:null,this.onRejected="function"==typeof e?e:null,this.promise=n}function l(t,e){var n=!1;try{t((function(t){n||(n=!0,a(e,t))}),(function(t){n||(n=!0,u(e,t))}))}catch(t){if(n)return;n=!0,u(e,t)}}function f(t){let e="";return p(t,(n=>{0!==e.length&&(e+="&"),e+=n+"="+t[n]})),e}function d(t,e={}){const n=document.createElement("script");return n.src=t,p(e,(t=>{n.setAttribute(t,e[t]),"data-csp-nonce"===t&&n.setAttribute("nonce",e["data-csp-nonce"])})),n}function p(t,e){for(const n in t)Object.prototype.hasOwnProperty.call(t,n)&&e(n)}function h(t){let e=0;return p(t,(()=>e++)),e}let y;o.prototype.catch=function(t){return this.then(null,t)},o.prototype.then=function(t,e){var n=new this.constructor(r);return i(this,new s(t,e,n)),n},o.prototype.finally=function(t){var e=this.constructor;return this.then((function(n){return e.resolve(t()).then((function(){return n}))}),(function(n){return e.resolve(t()).then((function(){return e.reject(n)}))}))},o.all=function(t){return new o((function(e,r){if(!n(t))return r(new TypeError("Promise.all accepts an array"));var o=Array.prototype.slice.call(t);if(0===o.length)return e([]);var i=o.length;function a(t,n){try{if(n&&("object"==typeof n||"function"==typeof n)){var u=n.then;if("function"==typeof u)return void u.call(n,(function(e){a(t,e)}),r)}o[t]=n,0==--i&&e(o)}catch(t){r(t)}}for(var u=0;u<o.length;u++)a(u,o[u])}))},o.allSettled=function(t){return new this((function(e,n){if(!t||void 0===t.length)return n(new TypeError(typeof t+" "+t+" is not iterable(cannot read property Symbol(Symbol.iterator))"));var r=Array.prototype.slice.call(t);if(0===r.length)return e([]);var o=r.length;function i(t,n){if(n&&("object"==typeof n||"function"==typeof n)){var a=n.then;if("function"==typeof a)return void a.call(n,(function(e){i(t,e)}),(function(n){r[t]={status:"rejected",reason:n},0==--o&&e(r)}))}r[t]={status:"fulfilled",value:n},0==--o&&e(r)}for(var a=0;a<r.length;a++)i(a,r[a])}))},o.resolve=function(t){return t&&"object"==typeof t&&t.constructor===o?t:new o((function(e){e(t)}))},o.reject=function(t){return new o((function(e,n){n(t)}))},o.race=function(t){return new o((function(e,r){if(!n(t))return r(new TypeError("Promise.race accepts an array"));for(var i=0,a=t.length;i<a;i++)o.resolve(t[i]).then(e,r)}))},o._immediateFn="function"==typeof setImmediate&&function(t){setImmediate(t)}||function(t){e(t,0)},o._unhandledRejectionFn=function(t){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",t)};let v=!1;function w(t,e){if(!(t instanceof Object))throw new Error("Invalid arguments. Expected an object to be passed into loadScript().");if(void 0===e){if("undefined"==typeof Promise)throw new Error("Failed to load the PayPal JS SDK script because Promise is undefined. To resolve the issue, use a Promise polyfill.");e=Promise}return v?y:y=new e(((e,n)=>{if("undefined"==typeof window)return e(null);const{url:r,dataAttributes:o}=function(t){let e="https://www.paypal.com/sdk/js";t.sdkBaseURL&&(e=t.sdkBaseURL,delete t.sdkBaseURL);const n={queryParams:{},dataAttributes:{}};p(t,(e=>{const r="data-"===e.substring(0,5)?"dataAttributes":"queryParams";n[r][e]=t[e].toString()}));const{queryParams:r,dataAttributes:o}=n;return{url:`${e}?${f(r)}`,dataAttributes:o}}(t);if(function(t,e){const n=document.querySelector(`script[src="${t}"]`);if(null===n)return null;const r=d(t,e);if(h(n.dataset)!==h(r.dataset))return null;let o=!0;return p(n.dataset,(t=>{n.dataset[t]!==r.dataset[t]&&(o=!1)})),o?n:null}(r,o)&&window.paypal)return e(window.paypal);v=!0,function({url:t,dataAttributes:e,onSuccess:n,onError:r}){const o=d(t,e);o.onerror=r,o.onload=n,document.head.insertBefore(o,document.head.firstElementChild)}({url:r,dataAttributes:o,onSuccess:()=>(v=!1,window.paypal?e(window.paypal):n(new Error("The window.paypal global variable is not available."))),onError:()=>(v=!1,n(new Error(`The script "${r}" didn't load correctly.`)))})}))}return t.loadScript=function(t){return w(t,o)},t.version="2.1.9",Object.defineProperty(t,"__esModule",{value:!0}),t}({});paypalLoadScript=paypalLoadScript.loadScript; | ||
var paypalLoadScript=function(t){"use strict";var e=setTimeout;function n(t){return Boolean(t&&void 0!==t.length)}function r(){}function o(t){if(!(this instanceof o))throw new TypeError("Promises must be constructed via new");if("function"!=typeof t)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],f(t,this)}function i(t,e){for(;3===t._state;)t=t._value;0!==t._state?(t._handled=!0,o._immediateFn((function(){var n=1===t._state?e.onFulfilled:e.onRejected;if(null!==n){var r;try{r=n(t._value)}catch(t){return void u(e.promise,t)}a(e.promise,r)}else(1===t._state?a:u)(e.promise,t._value)}))):t._deferreds.push(e)}function a(t,e){try{if(e===t)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var n=e.then;if(e instanceof o)return t._state=3,t._value=e,void c(t);if("function"==typeof n)return void f((r=n,i=e,function(){r.apply(i,arguments)}),t)}t._state=1,t._value=e,c(t)}catch(e){u(t,e)}var r,i}function u(t,e){t._state=2,t._value=e,c(t)}function c(t){2===t._state&&0===t._deferreds.length&&o._immediateFn((function(){t._handled||o._unhandledRejectionFn(t._value)}));for(var e=0,n=t._deferreds.length;e<n;e++)i(t,t._deferreds[e]);t._deferreds=null}function s(t,e,n){this.onFulfilled="function"==typeof t?t:null,this.onRejected="function"==typeof e?e:null,this.promise=n}function f(t,e){var n=!1;try{t((function(t){n||(n=!0,a(e,t))}),(function(t){n||(n=!0,u(e,t))}))}catch(t){if(n)return;n=!0,u(e,t)}}function l(t){let e="https://www.paypal.com/sdk/js";t.sdkBaseURL&&(e=t.sdkBaseURL,delete t.sdkBaseURL);const n=function(t,e){let n="",r="";Array.isArray(t)?t.length>1?(n="*",r=t.toString()):n=t.toString():"string"==typeof t&&t.length>0?n=t:"string"==typeof e&&e.length>0&&(n="*",r=e);return{"merchant-id":n,"data-merchant-id":r}}(t["merchant-id"],t["data-merchant-id"]),r=Object.assign(t,n),{queryParams:o,dataAttributes:i}=Object.keys(r).filter((t=>void 0!==r[t]&&null!==r[t]&&""!==r[t])).reduce(((t,e)=>{const n=r[e].toString();return"data-"===e.substring(0,5)?t.dataAttributes[e]=n:t.queryParams[e]=n,t}),{queryParams:{},dataAttributes:{}});return{url:`${e}?${d(o)}`,dataAttributes:i}}function d(t){let e="";return Object.keys(t).forEach((n=>{0!==e.length&&(e+="&"),e+=n+"="+t[n]})),e}function p(t,e={}){const n=document.createElement("script");return n.src=t,Object.keys(e).forEach((t=>{n.setAttribute(t,e[t]),"data-csp-nonce"===t&&n.setAttribute("nonce",e["data-csp-nonce"])})),n}function h(t,e){return function(t,e){if("object"!=typeof t||null===t)throw new Error("Invalid arguments. Expected options to be an object.");if(void 0===e)return;if("function"!=typeof e)throw new Error("Invalid arguments. Expected PromisePolyfill to be a function.")}(t,e),void 0===e&&(e=function(){if("undefined"==typeof Promise)throw new Error("Promise is undefined. To resolve the issue, use a Promise polyfill.");return Promise}()),new e(((e,n)=>{if("undefined"==typeof window)return e(null);const{url:r,dataAttributes:o}=l(t),i=o["data-namespace"]||"paypal",a=y(i);if(function(t,e){const n=document.querySelector(`script[src="${t}"]`);if(null===n)return null;const r=p(t,e);if(Object.keys(n.dataset).length!==Object.keys(r.dataset).length)return null;let o=!0;return Object.keys(n.dataset).forEach((t=>{n.dataset[t]!==r.dataset[t]&&(o=!1)})),o?n:null}(r,o)&&a)return e(a);!function({url:t,dataAttributes:e,onSuccess:n,onError:r}){const o=p(t,e);o.onerror=r,o.onload=n,document.head.insertBefore(o,document.head.firstElementChild)}({url:r,dataAttributes:o,onSuccess:()=>{const t=y(i);return t?e(t):n(new Error(`The window.${i} global variable is not available.`))},onError:()=>n(new Error(`The script "${r}" didn't load correctly.`))})}))}function y(t){return window[t]}o.prototype.catch=function(t){return this.then(null,t)},o.prototype.then=function(t,e){var n=new this.constructor(r);return i(this,new s(t,e,n)),n},o.prototype.finally=function(t){var e=this.constructor;return this.then((function(n){return e.resolve(t()).then((function(){return n}))}),(function(n){return e.resolve(t()).then((function(){return e.reject(n)}))}))},o.all=function(t){return new o((function(e,r){if(!n(t))return r(new TypeError("Promise.all accepts an array"));var o=Array.prototype.slice.call(t);if(0===o.length)return e([]);var i=o.length;function a(t,n){try{if(n&&("object"==typeof n||"function"==typeof n)){var u=n.then;if("function"==typeof u)return void u.call(n,(function(e){a(t,e)}),r)}o[t]=n,0==--i&&e(o)}catch(t){r(t)}}for(var u=0;u<o.length;u++)a(u,o[u])}))},o.allSettled=function(t){return new this((function(e,n){if(!t||void 0===t.length)return n(new TypeError(typeof t+" "+t+" is not iterable(cannot read property Symbol(Symbol.iterator))"));var r=Array.prototype.slice.call(t);if(0===r.length)return e([]);var o=r.length;function i(t,n){if(n&&("object"==typeof n||"function"==typeof n)){var a=n.then;if("function"==typeof a)return void a.call(n,(function(e){i(t,e)}),(function(n){r[t]={status:"rejected",reason:n},0==--o&&e(r)}))}r[t]={status:"fulfilled",value:n},0==--o&&e(r)}for(var a=0;a<r.length;a++)i(a,r[a])}))},o.resolve=function(t){return t&&"object"==typeof t&&t.constructor===o?t:new o((function(e){e(t)}))},o.reject=function(t){return new o((function(e,n){n(t)}))},o.race=function(t){return new o((function(e,r){if(!n(t))return r(new TypeError("Promise.race accepts an array"));for(var i=0,a=t.length;i<a;i++)o.resolve(t[i]).then(e,r)}))},o._immediateFn="function"==typeof setImmediate&&function(t){setImmediate(t)}||function(t){e(t,0)},o._unhandledRejectionFn=function(t){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",t)};return t.loadScript=function(t){return h(t,o)},t.version="3.0.0",Object.defineProperty(t,"__esModule",{value:!0}),t}({});paypalLoadScript=paypalLoadScript.loadScript; |
/*! | ||
* paypal-js v2.1.9 (2021-02-15T22:49:36.078Z) | ||
* paypal-js v3.0.0 (2021-03-01T22:43:43.710Z) | ||
* Copyright 2020-present, PayPal, Inc. All rights reserved. | ||
@@ -26,3 +26,3 @@ * | ||
if (objectSize(currentScript.dataset) !== objectSize(nextScript.dataset)) { | ||
if (Object.keys(currentScript.dataset).length !== Object.keys(nextScript.dataset).length) { | ||
return null; | ||
@@ -33,3 +33,3 @@ } | ||
forEachObjectKey(currentScript.dataset, function (key) { | ||
Object.keys(currentScript.dataset).forEach(function (key) { | ||
if (currentScript.dataset[key] !== nextScript.dataset[key]) { | ||
@@ -59,13 +59,24 @@ isExactMatch = false; | ||
var processedOptions = { | ||
var processedMerchantIDAttributes = processMerchantID(options["merchant-id"], options["data-merchant-id"]); | ||
var newOptions = Object.assign(options, processedMerchantIDAttributes); | ||
var _Object$keys$filter$r = Object.keys(newOptions).filter(function (key) { | ||
return typeof newOptions[key] !== "undefined" && newOptions[key] !== null && newOptions[key] !== ""; | ||
}).reduce(function (accumulator, key) { | ||
var value = newOptions[key].toString(); | ||
if (key.substring(0, 5) === "data-") { | ||
accumulator.dataAttributes[key] = value; | ||
} else { | ||
accumulator.queryParams[key] = value; | ||
} | ||
return accumulator; | ||
}, { | ||
queryParams: {}, | ||
dataAttributes: {} | ||
}; | ||
forEachObjectKey(options, function (key) { | ||
var keyType = key.substring(0, 5) === "data-" ? "dataAttributes" : "queryParams"; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
}), | ||
queryParams = _Object$keys$filter$r.queryParams, | ||
dataAttributes = _Object$keys$filter$r.dataAttributes; | ||
processedOptions[keyType][key] = options[key].toString(); | ||
}); | ||
var queryParams = processedOptions.queryParams, | ||
dataAttributes = processedOptions.dataAttributes; | ||
return { | ||
@@ -78,3 +89,3 @@ url: "".concat(sdkBaseURL, "?").concat(objectToQueryString(queryParams)), | ||
var queryString = ""; | ||
forEachObjectKey(params, function (key) { | ||
Object.keys(params).forEach(function (key) { | ||
if (queryString.length !== 0) queryString += "&"; | ||
@@ -90,3 +101,3 @@ queryString += key + "=" + params[key]; | ||
newScript.src = url; | ||
forEachObjectKey(dataAttributes, function (key) { | ||
Object.keys(dataAttributes).forEach(function (key) { | ||
newScript.setAttribute(key, dataAttributes[key]); | ||
@@ -99,40 +110,44 @@ | ||
return newScript; | ||
} // uses es3 to avoid requiring polyfills for Array.prototype.forEach and Object.keys | ||
} | ||
function processMerchantID(merchantID, dataMerchantID) { | ||
var newMerchantID = ""; | ||
var newDataMerchantID = ""; | ||
function forEachObjectKey(obj, callback) { | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
callback(key); | ||
if (Array.isArray(merchantID)) { | ||
if (merchantID.length > 1) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = merchantID.toString(); | ||
} else { | ||
newMerchantID = merchantID.toString(); | ||
} | ||
} else if (typeof merchantID === "string" && merchantID.length > 0) { | ||
newMerchantID = merchantID; | ||
} else if (typeof dataMerchantID === "string" && dataMerchantID.length > 0) { | ||
newMerchantID = "*"; | ||
newDataMerchantID = dataMerchantID; | ||
} | ||
} | ||
function objectSize(obj) { | ||
var size = 0; | ||
forEachObjectKey(obj, function () { | ||
return size++; | ||
}); | ||
return size; | ||
return { | ||
"merchant-id": newMerchantID, | ||
"data-merchant-id": newDataMerchantID | ||
}; | ||
} | ||
var loadingPromise; | ||
var isLoading = false; | ||
/** | ||
* Load the Paypal JS SDK script asynchronously. | ||
* | ||
* @param {Object} options - used to configure query parameters and data attributes for the JS SDK. | ||
* @param {PromiseConstructor} [PromisePonyfill=window.Promise] - optional Promise Constructor ponyfill. | ||
* @return {Promise<Object>} paypalObject - reference to the global window PayPal object. | ||
*/ | ||
function loadScript(options, PromisePonyfill) { | ||
if (!(options instanceof Object)) { | ||
throw new Error("Invalid arguments. Expected an object to be passed into loadScript()."); | ||
} | ||
validateArguments(options, PromisePonyfill); | ||
if (typeof PromisePonyfill === "undefined") { | ||
// default to using window.Promise as the Promise implementation | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Failed to load the PayPal JS SDK script because Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
PromisePonyfill = getDefaultPromiseImplementation(); | ||
} | ||
PromisePonyfill = Promise; | ||
} // resolve with the existing promise when the script is loading | ||
if (isLoading) return loadingPromise; | ||
return loadingPromise = new PromisePonyfill(function (resolve, reject) { | ||
return new PromisePonyfill(function (resolve, reject) { | ||
// resolve with null when running in Node | ||
@@ -143,7 +158,11 @@ if (typeof window === "undefined") return resolve(null); | ||
url = _processOptions.url, | ||
dataAttributes = _processOptions.dataAttributes; // resolve with the existing global paypal object when a script with the same src already exists | ||
dataAttributes = _processOptions.dataAttributes; | ||
var namespace = dataAttributes["data-namespace"] || "paypal"; | ||
var existingWindowNamespace = getPayPalWindowNamespace(namespace); // resolve with the existing global paypal namespace when a script with the same params already exists | ||
if (findScript(url, dataAttributes) && window.paypal) return resolve(window.paypal); | ||
isLoading = true; | ||
if (findScript(url, dataAttributes) && existingWindowNamespace) { | ||
return resolve(existingWindowNamespace); | ||
} | ||
insertScriptElement({ | ||
@@ -153,8 +172,7 @@ url: url, | ||
onSuccess: function onSuccess() { | ||
isLoading = false; | ||
if (window.paypal) return resolve(window.paypal); | ||
return reject(new Error("The window.paypal global variable is not available.")); | ||
var newWindowNamespace = getPayPalWindowNamespace(namespace); | ||
if (newWindowNamespace) return resolve(newWindowNamespace); | ||
return reject(new Error("The window.".concat(namespace, " global variable is not available."))); | ||
}, | ||
onError: function onError() { | ||
isLoading = false; | ||
return reject(new Error("The script \"".concat(url, "\" didn't load correctly."))); | ||
@@ -166,5 +184,32 @@ } | ||
var version = "2.1.9"; | ||
function validateArguments(options, PromisePonyfill) { | ||
if (typeof options !== "object" || options === null) { | ||
throw new Error("Invalid arguments. Expected options to be an object."); | ||
} | ||
if (typeof PromisePonyfill === "undefined") { | ||
return; | ||
} | ||
if (typeof PromisePonyfill !== "function") { | ||
throw new Error("Invalid arguments. Expected PromisePolyfill to be a function."); | ||
} | ||
} | ||
function getDefaultPromiseImplementation() { | ||
if (typeof Promise === "undefined") { | ||
throw new Error("Promise is undefined. To resolve the issue, use a Promise polyfill."); | ||
} | ||
return Promise; | ||
} | ||
function getPayPalWindowNamespace(namespace) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return window[namespace]; | ||
} | ||
var version = "3.0.0"; | ||
exports.loadScript = loadScript; | ||
exports.version = version; |
{ | ||
"name": "@paypal/paypal-js", | ||
"version": "2.1.9", | ||
"version": "3.0.0", | ||
"description": "Client-side loader for the PayPal JS SDK", | ||
@@ -45,14 +45,14 @@ "main": "dist/paypal.node.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.12.16", | ||
"@babel/preset-env": "^7.12.16", | ||
"@babel/preset-typescript": "^7.12.16", | ||
"@babel/core": "^7.13.8", | ||
"@babel/preset-env": "^7.13.8", | ||
"@babel/preset-typescript": "^7.13.0", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-commonjs": "^17.1.0", | ||
"@rollup/plugin-node-resolve": "^11.2.0", | ||
"@rollup/plugin-replace": "^2.3.4", | ||
"@rollup/plugin-replace": "^2.4.1", | ||
"@rollup/plugin-typescript": "^8.2.0", | ||
"@types/jest": "^26.0.20", | ||
"@types/promise-polyfill": "^6.0.3", | ||
"@typescript-eslint/eslint-plugin": "^4.15.0", | ||
"@typescript-eslint/parser": "^4.15.0", | ||
"@typescript-eslint/eslint-plugin": "^4.15.2", | ||
"@typescript-eslint/parser": "^4.15.2", | ||
"babel-jest": "^26.6.3", | ||
@@ -62,10 +62,10 @@ "eslint": "^7.20.0", | ||
"filesize": "^6.1.0", | ||
"husky": "^5.0.9", | ||
"husky": "^5.1.1", | ||
"jest": "^26.6.3", | ||
"jest-puppeteer": "^4.4.0", | ||
"lint-staged": "^10.5.4", | ||
"pinst": "^2.1.4", | ||
"pinst": "^2.1.6", | ||
"prettier": "2.2.1", | ||
"puppeteer": "^7.1.0", | ||
"rollup": "^2.39.0", | ||
"puppeteer": "^8.0.0", | ||
"rollup": "^2.40.0", | ||
"rollup-plugin-filesize": "^9.1.0", | ||
@@ -72,0 +72,0 @@ "rollup-plugin-terser": "^7.0.2", |
@@ -62,5 +62,9 @@ # paypal-js | ||
loadScript({ "client-id": "test" }).then((paypal) => { | ||
paypal.Buttons().render("#your-container-element"); | ||
}); | ||
loadScript({ "client-id": "test" }) | ||
.then((paypal) => { | ||
paypal.Buttons().render("#your-container-element"); | ||
}) | ||
.catch(err) => { | ||
console.error("failed to load the PayPal JS SDK script", err); | ||
}); | ||
``` | ||
@@ -110,2 +114,39 @@ | ||
#### Merchant ID Array | ||
The `merchant-id` option accepts an array to simplify the implementation for Multi-Seller Payments. With this approach the caller doesn't have to worry about managing the two different merchant id values (`data-merchant-id` and `merchant-id`). | ||
**Here's an example with multiple `merchant-id` values:** | ||
```js | ||
loadScript({ | ||
"client-id": "YOUR_CLIENT_ID", | ||
"merchant-id": ["123", "456", "789"], | ||
}); | ||
``` | ||
Which will load the following `<script>` and use `merchant-id=*` to properly configure the edge cache: | ||
```html | ||
<script | ||
data-merchant-id="123,456,789" | ||
src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&merchant-id=*" | ||
></script> | ||
``` | ||
**Here's an example with one `merchant-id` value:** | ||
```js | ||
loadScript({ | ||
"client-id": "YOUR_CLIENT_ID", | ||
"merchant-id": ["123"], | ||
}); | ||
``` | ||
When there's only one, the merchant-id is passed in using the query string. | ||
```html | ||
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&merchant-id=123"></script> | ||
``` | ||
#### sdkBaseURL | ||
@@ -112,0 +153,0 @@ |
interface PayPalScriptQueryParameters { | ||
"client-id": string; | ||
"merchant-id"?: string; | ||
// loadScript() supports an array and will convert it | ||
// to the correct merchant-id and data-merchant-id string values | ||
"merchant-id"?: string[] | string; | ||
currency?: string; | ||
@@ -25,2 +27,3 @@ intent?: string; | ||
"data-merchant-id"?: string; | ||
"data-namespace"?: string; | ||
} | ||
@@ -31,4 +34,5 @@ | ||
PayPalScriptDataAttributes { | ||
[key: string]: string | boolean | undefined; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[key: string]: any; | ||
sdkBaseURL?: string; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85748
1411
224