@paypal/paypal-js
Version:
Loading wrapper and TypeScript types for the PayPal JS SDK
100 lines (92 loc) • 4.1 kB
JavaScript
/*!
* paypal-js v9.0.1 (2025-10-03T16:05:19.915Z)
* Copyright 2020-present, PayPal, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
function insertScriptElement(_a) {
var url = _a.url, attributes = _a.attributes, onSuccess = _a.onSuccess, onError = _a.onError;
var newScript = createScriptElement(url, attributes);
newScript.onerror = onError;
newScript.onload = onSuccess;
document.head.insertBefore(newScript, document.head.firstElementChild);
}
function createScriptElement(url, attributes) {
if (attributes === void 0) { attributes = {}; }
var newScript = document.createElement("script");
newScript.src = url;
Object.keys(attributes).forEach(function (key) {
newScript.setAttribute(key, attributes[key]);
if (key === "data-csp-nonce") {
newScript.setAttribute("nonce", attributes["data-csp-nonce"]);
}
});
return newScript;
}
var version = "9.0.1";
function loadCoreSdkScript(options) {
if (options === void 0) { options = {}; }
validateArguments(options);
var environment = options.environment, debug = options.debug;
var baseURL = environment === "production"
? "https://www.paypal.com"
: "https://www.sandbox.paypal.com";
var url = new URL("/web-sdk/v6/core", baseURL);
if (debug) {
url.searchParams.append("debug", "true");
}
return new Promise(function (resolve, reject) {
insertScriptElement({
url: url.toString(),
onSuccess: function () {
if (!window.paypal) {
return reject("The window.paypal global variable is not available");
}
return resolve(window.paypal);
},
onError: function () {
var defaultError = new Error("The script \"".concat(url, "\" failed to load. Check the HTTP status code and response body in DevTools to learn more."));
return reject(defaultError);
},
});
});
}
function validateArguments(options) {
if (typeof options !== "object" || options === null) {
throw new Error("Expected an options object");
}
var environment = options.environment;
if (environment &&
environment !== "production" &&
environment !== "sandbox") {
throw new Error('The "environment" option must be either "production" or "sandbox"');
}
}
export { loadCoreSdkScript, version };