@hyperse/paypal-node-sdk
Version:
NodeJS SDK for PayPal Checkout APIs
50 lines (49 loc) • 1.64 kB
JavaScript
import paypalhttp from '@paypal/paypalhttp';
const SANDBOX = 'https://api.sandbox.paypal.com';
const LIVE = 'https://api.paypal.com';
const SANDBOX_WEB_URL = 'https://www.sandbox.paypal.com';
const LIVE_WEB_URL = 'https://www.paypal.com';
/**
* Base class for PayPal Environments
* Documentation
*
* @see {@link https://github.com/hyperse-io/paypal-node-sdk/tree/main/src/core/PayPalEnvironment.ts}
*/
export class PayPalEnvironment extends paypalhttp.Environment {
/**
* @param clientId - The client id for this environment
* @param clientSecret - The client secret
* @param baseUrl - The base url to execute requests
* @param webUrl -The web url to authorize user's consent
*/
constructor(clientId, clientSecret, baseUrl, webUrl) {
super(baseUrl);
this.clientId = clientId;
this.clientSecret = clientSecret;
this.webUrl = webUrl;
}
/**
* Authorization header string for basic authentication with the current client id and secret
* @return - The authorization header value
*/
authorizationString() {
const encoded = Buffer.from(`${this.clientId}:${this.clientSecret}`).toString('base64');
return `Basic ${encoded}`;
}
}
/**
* Sandbox Environment
*/
export class SandboxEnvironment extends PayPalEnvironment {
constructor(clientId, clientSecret) {
super(clientId, clientSecret, SANDBOX, SANDBOX_WEB_URL);
}
}
/**
* Live Environment
*/
export class LiveEnvironment extends PayPalEnvironment {
constructor(clientId, clientSecret) {
super(clientId, clientSecret, LIVE, LIVE_WEB_URL);
}
}