simple-paypal-sdk
Version:
Simple PayPal SDK for Node.js.
115 lines (114 loc) • 4.69 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var import_lite_request = __toESM(require("lite-request"), 1);
var import_memory_cache = __toESM(require("memory-cache"), 1);
var import_https_proxy_agent = require("https-proxy-agent");
var import_socks_proxy_agent = require("socks-proxy-agent");
const cache = new import_memory_cache.default.Cache();
class PayPal {
constructor(options = {}) {
this.clientId = options.clientId || options.client_id;
this.clientSecret = options.clientSecret || options.client_secret;
this.environment = options.environment || "sandbox";
if (!this.clientId || !this.clientSecret) {
throw new TypeError("No clientId or clientSecret");
}
const proxy = options.proxy;
if (proxy) {
if (typeof proxy === "string") {
if (proxy.startsWith("http://")) {
this.agent = new import_https_proxy_agent.HttpsProxyAgent(proxy);
} else if (proxy.startsWith("socks://")) {
this.agent = new import_socks_proxy_agent.SocksProxyAgent(proxy);
}
} else if (typeof proxy === "object") {
if (!["http", "socks"].includes(proxy.protocol)) {
throw new TypeError('proxy.protocol must be one of ["http", "socks"]');
}
this.agent = proxy.protocol === "http" ? new import_https_proxy_agent.HttpsProxyAgent(
proxy.username && proxy.password ? `http://${proxy.username}:${proxy.password}@${proxy.host}:${proxy.port}` : `http://${proxy.host}:${proxy.port}`
) : new import_socks_proxy_agent.SocksProxyAgent(
proxy.username && proxy.password ? `socks://${proxy.username}:${proxy.password}@${proxy.host}:${proxy.port}` : `socks://${proxy.host}:${proxy.port}`
);
}
}
}
_getURL(url) {
if (url.startsWith("https://")) {
return url;
}
url = url.replace(/^\//, "");
return this.environment === "sandbox" ? `https://api.sandbox.paypal.com/${url}` : `https://api.paypal.com/${url}`;
}
async _authenticate() {
const url = `${this._getURL("v1/oauth2/token")}`;
const key = `paypal:${this.environment}:${this.clientId}:${this.clientSecret}:accessToken`;
const accessToken = cache.get(key);
if (accessToken) {
return accessToken;
}
const response = await (0, import_lite_request.default)({
method: "POST",
url,
headers: {
Authorization: `Basic ${Buffer.from(this.clientId + ":" + this.clientSecret).toString("base64")}`
},
form: {
grant_type: "client_credentials"
},
json: true,
agent: this.agent
});
const data = response.data;
cache.put(key, data.access_token, data.expires_in * 1e3 - 3e4);
return data.access_token;
}
async execute({ method = "GET", url, headers = {}, body }) {
const accessToken = await this._authenticate();
const payload = {
method,
url: this._getURL(url),
headers: Object.assign({
Authorization: `Bearer ${accessToken}`
}, headers),
json: true,
agent: this.agent
};
if (body) {
payload.body = body;
}
const response = await (0, import_lite_request.default)(payload);
return response.data;
}
}
var index_default = PayPal;