UNPKG

bc-payments-sdk

Version:

BetterCommerce's Payments NodeJS SDK is a complete solution for storefront clients that integrate payments. `bc-payments-sdk` is a single point interface for storefront clients for interacting with payment gateways.

287 lines (286 loc) 11.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BCEnvironment = void 0; const Endpoints_1 = require("../../constants/Endpoints"); /** * Class {BCEnvironment} */ class BCEnvironment { /** * Initializes the BCEnvironment object with the required parameters to make API calls. * * @param {string} clientId - The client ID of the BetterCommerce account. * @param {string} sharedSecret - The shared secret of the BetterCommerce account. * @param {Object} [config] - The configuration for payment gateways etc. * @param {string} [baseAuthUrl] - The base URL for the authentication API. * @param {string} [baseApiUrl] - The base URL for the API. * @param {number} [connectTimeout] - The timeout for the connection in milliseconds. * @param {number} [readTimeout] - The timeout for reading the response in milliseconds. * @param {string} [defaultCountry] - The default country to use for the API. * @param {string} [defaultCurrency] - The default currency to use for the API. * @param {string} [defaultLanguage] - The default language to use for the API. * @returns {BCEnvironment} - The initialized BCEnvironment object. */ static init(clientId, sharedSecret, config, baseAuthUrl, baseApiUrl, connectTimeout, readTimeout, defaultCountry, defaultCurrency, defaultLanguage) { /*if (BCEnvironment.thisObj != undefined) { return BCEnvironment.thisObj; } else {*/ BCEnvironment.thisObj = new BCEnvironment(); BCEnvironment.clientId = clientId; BCEnvironment.sharedSecret = sharedSecret; BCEnvironment.baseAuthUrl = baseAuthUrl || Endpoints_1.Endpoints.Base.AUTH_URL; BCEnvironment.baseApiUrl = baseApiUrl || Endpoints_1.Endpoints.Base.API_URL; BCEnvironment.defaultCountry = defaultCountry || "GB"; BCEnvironment.defaultCurrency = defaultCurrency || "GBP"; BCEnvironment.defaultLanguage = defaultLanguage || "en-GB"; BCEnvironment.config = config; BCEnvironment.enableProviderLogging = true; if (baseAuthUrl) { BCEnvironment.baseAuthUrl = baseAuthUrl; } if (baseApiUrl) { BCEnvironment.baseApiUrl = baseApiUrl; } if (connectTimeout) { BCEnvironment.connectTimeout = connectTimeout; } if (readTimeout) { BCEnvironment.readTimeout = readTimeout; } if (defaultCountry) { BCEnvironment.defaultCountry = defaultCountry; } if (defaultCurrency) { BCEnvironment.defaultCurrency = defaultCurrency; } if (defaultLanguage) { BCEnvironment.defaultLanguage = defaultLanguage; } return BCEnvironment.thisObj; //} } /** * Initializes the BCEnvironment object with the required parameters to make API calls. * This method should be used when you already have a valid API token and refresh token. * @param {string} apiToken - The API token to use for authentication. * @param {string} refreshToken - The refresh token to use when the API token expires. * @param {Object} [config] - The configuration for payment gateways etc. * @param {string} [baseAuthUrl] - The base URL for the authentication API. * @param {string} [baseApiUrl] - The base URL for the API. * @param {number} [connectTimeout] - The timeout for the connection in milliseconds. * @param {number} [readTimeout] - The timeout for reading the response in milliseconds. * @param {string} [defaultCountry] - The default country to use for the API. * @param {string} [defaultCurrency] - The default currency to use for the API. * @param {string} [defaultLanguage] - The default language to use for the API. * @returns {BCEnvironment} - The initialized BCEnvironment object. */ static initSession(apiToken, refreshToken, config, baseAuthUrl, baseApiUrl, connectTimeout, readTimeout, defaultCountry, defaultCurrency, defaultLanguage) { BCEnvironment.thisObj = new BCEnvironment(); BCEnvironment.apiToken = apiToken; BCEnvironment.refreshToken = refreshToken; BCEnvironment.baseAuthUrl = baseAuthUrl || Endpoints_1.Endpoints.Base.AUTH_URL; BCEnvironment.baseApiUrl = baseApiUrl || Endpoints_1.Endpoints.Base.API_URL; BCEnvironment.defaultCountry = defaultCountry || "GB"; BCEnvironment.defaultCurrency = defaultCurrency || "GBP"; BCEnvironment.defaultLanguage = defaultLanguage || "en-GB"; BCEnvironment.config = config; BCEnvironment.enableProviderLogging = true; if (baseAuthUrl) { BCEnvironment.baseAuthUrl = baseAuthUrl; } if (baseApiUrl) { BCEnvironment.baseApiUrl = baseApiUrl; } if (connectTimeout) { BCEnvironment.connectTimeout = connectTimeout; } if (readTimeout) { BCEnvironment.readTimeout = readTimeout; } if (defaultCountry) { BCEnvironment.defaultCountry = defaultCountry; } if (defaultCurrency) { BCEnvironment.defaultCurrency = defaultCurrency; } if (defaultLanguage) { BCEnvironment.defaultLanguage = defaultLanguage; } return BCEnvironment.thisObj; } /** * Static class to hold environment variables for BetterCommerce API * and payment providers. * * @remarks * The class is a singleton and should be initialized using the static * method {@link BCEnvironment.init}. * * @example * BCEnvironment.init(clientId, sharedSecret, config); * * @example * const environment = BCEnvironment.init(clientId, sharedSecret, config); * environment.addExtras({ * country: "GB", * currency: "GBP", * language: "en-GB", * }); * * @property {string} clientId - The BetterCommerce client ID. * @property {string} sharedSecret - The BetterCommerce shared secret. * @property {string} baseAuthUrl - The base URL for authentication. * @property {string} baseApiUrl - The base URL for API requests. * @property {number} connectTimeout - The timeout in milliseconds to wait for a connection to be established. * @property {number} readTimeout - The timeout in milliseconds to wait for a response to be read. * @property {string} defaultCountry - The default country code. * @property {string} defaultCurrency - The default currency code. * @property {string} defaultLanguage - The default language code. * @property {object} config - The configuration object. * @property {object} extras - Additional environment variables. */ static addExtras(extras) { if (extras) { BCEnvironment.extras = extras; if (extras === null || extras === void 0 ? void 0 : extras.country) { BCEnvironment.defaultCountry = extras === null || extras === void 0 ? void 0 : extras.country; } if (extras === null || extras === void 0 ? void 0 : extras.currency) { BCEnvironment.defaultCurrency = extras === null || extras === void 0 ? void 0 : extras.currency; } if (extras === null || extras === void 0 ? void 0 : extras.language) { BCEnvironment.defaultLanguage = extras === null || extras === void 0 ? void 0 : extras.language; } } return BCEnvironment.thisObj; } /** * Retrieves the base URL for authentication. * * @return {string} The base URL for authentication. */ static getBaseAuthUrl() { return BCEnvironment.baseAuthUrl; } /** * Retrieves the base URL for API requests. * * @return {string} The base URL for API requests. */ static getBaseApiUrl() { return BCEnvironment.baseApiUrl; } /** * Retrieves the client ID associated with the BetterCommerce account. * * @return {string} The client ID. */ static getClientId() { return BCEnvironment.clientId; } /** * Retrieves the shared secret associated with the BetterCommerce account. * * @return {string} The shared secret. */ static getSharedSecret() { return BCEnvironment.sharedSecret; } /** * Retrieves the API token used for authentication. * * @return {string} The API token. */ static getApiToken() { return BCEnvironment.apiToken; } /** * Retrieves the refresh token used for authentication. * * @return {string} The refresh token. */ static getRefreshToken() { return BCEnvironment.refreshToken; } /** * Retrieves the default country associated with the BetterCommerce account. * * @return {string} The default country. */ static getDefaultCountry() { return BCEnvironment.defaultCountry; } /** * Retrieves the default currency associated with the BetterCommerce account. * * @return {string} The default currency. */ static getDefaultCurrency() { return BCEnvironment.defaultCurrency; } /** * Retrieves the default language associated with the BetterCommerce account. * * @return {string} The default language. */ static getDefaultLanguage() { return BCEnvironment.defaultLanguage; } /** * Retrieves the connection timeout value for the BetterCommerce API. * * @return {number} The connection timeout in milliseconds. */ static getConnectTimeout() { return BCEnvironment.connectTimeout; } /** * Retrieves the read timeout value for the BetterCommerce API. * * @return {number} The read timeout in milliseconds. */ static getReadTimeout() { return BCEnvironment.readTimeout; } /** * Retrieves the configuration object used for payment gateways and other settings. * * @return {Object} The configuration object. */ static getConfig() { return BCEnvironment.config; } /** * Retrieves the additional environment variables. * * @return {Object} The extras object containing additional environment variables. */ static getExtras() { return BCEnvironment.extras; } /** * Retrieves the flag indicating whether provider logging is enabled. * * @return {boolean} True if provider logging is enabled, false otherwise. */ static getEnableProviderLogging() { return BCEnvironment.enableProviderLogging; } /** * Sets the API token for the BetterCommerce API. * * @param {string} apiToken - The API token to set. */ static setApiToken(apiToken) { BCEnvironment.apiToken = apiToken; } /** * Sets the refresh token for the BetterCommerce API. * * @param {string} refreshToken - The refresh token to set. */ static setRefreshToken(refreshToken) { BCEnvironment.refreshToken = refreshToken; } } exports.BCEnvironment = BCEnvironment;