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.
215 lines (214 loc) • 22.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasePaymentProvider = void 0;
// Package Imports
const bc_paypal_sdk_1 = require("bc-paypal-sdk");
const bc_checkout_sdk_1 = require("bc-checkout-sdk");
const bc_stripe_sdk_1 = require("bc-stripe-sdk");
const bc_clearpay_sdk_1 = require("bc-clearpay-sdk");
const bc_klarna_sdk_1 = require("bc-klarna-sdk");
const bc_apple_pay_sdk_1 = require("bc-apple-pay-sdk");
const bc_juspay_sdk_1 = require("bc-juspay-sdk");
// Other Imports
const constants_1 = require("../../constants/constants");
const BCEnvironment_1 = require("../config/BCEnvironment");
const parse_util_1 = require("../../utils/parse-util");
const PaymentMethodType_1 = require("../../constants/enums/PaymentMethodType");
const Logger_1 = require("../../modules/better-commerce/Logger");
const bc_elavon_sdk_1 = require("bc-elavon-sdk");
const bc_opayo_sdk_1 = require("bc-opayo-sdk");
const bc_omnicapital_sdk_1 = require("bc-omnicapital-sdk");
const bc_nuvei_sdk_1 = require("bc-nuvei-sdk");
/**
* Abstract class {BasePaymentProvider} is the base class for all payment providers.
*
* Payment providers are responsible for providing the concrete implementation of the
* payment provider methods.
*
* This class provides the following methods that can be overridden by the concrete
* implementation classes:
*
* - {initSDK}: Initializes the SDK for the payment provider.
* - {initPaymentIntent}: Initializes the payment intent for the payment provider.
* - {requestToken}: Requests a token from the payment provider.
* - {requestPayment}: Requests a payment from the payment provider.
*
* @abstract
* @category Payment Provider
*/
class BasePaymentProvider {
/**
* Initializes the SDK for the given payment provider.
*
* @protected
* @return {boolean} True if the SDK was initialized successfully, false otherwise.
*/
initSDK() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58;
/**
* Logs the payment activity to the logging service, if enabled.
*
* @param {boolean} providerLoggingEnabled - True if logging is enabled for the provider.
* @param {Object} data - The data associated with the payment.
* @param {string} logMessage - The message to log.
*/
const logActivity = (providerLoggingEnabled, data, logMessage) => {
if (providerLoggingEnabled) {
Logger_1.Logger.logPayment({
data,
message: logMessage,
}, {});
}
};
const providerLoggingEnabled = BCEnvironment_1.BCEnvironment.getEnableProviderLogging();
const config = BCEnvironment_1.BCEnvironment.getConfig();
if ((config === null || config === void 0 ? void 0 : config.systemName) && ((_a = config === null || config === void 0 ? void 0 : config.settings) === null || _a === void 0 ? void 0 : _a.length)) {
const useSandbox = ((_c = (_b = config === null || config === void 0 ? void 0 : config.settings) === null || _b === void 0 ? void 0 : _b.find((x) => x.key === "UseSandbox")) === null || _c === void 0 ? void 0 : _c.value) || constants_1.Defaults.String.Value;
const isSandbox = useSandbox ? (0, parse_util_1.stringToBoolean)(useSandbox) : false;
if (((_d = config === null || config === void 0 ? void 0 : config.systemName) === null || _d === void 0 ? void 0 : _d.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.PAYPAL.toLowerCase()) {
const clientId = ((_f = (_e = config === null || config === void 0 ? void 0 : config.settings) === null || _e === void 0 ? void 0 : _e.find((x) => x.key === "AccountCode")) === null || _f === void 0 ? void 0 : _f.value) || constants_1.Defaults.String.Value;
const appSecret = ((_h = (_g = config === null || config === void 0 ? void 0 : config.settings) === null || _g === void 0 ? void 0 : _g.find((x) => x.key === "Signature")) === null || _h === void 0 ? void 0 : _h.value) || constants_1.Defaults.String.Value;
// Init Env
bc_paypal_sdk_1.PayPalEnvironment.init(clientId, appSecret, isSandbox);
//const logData = { data: `PayPalEnvironment.init(${clientId}, ${appSecret}, ${isSandbox})` };
//logActivity(providerLoggingEnabled, logData, `${config?.systemName} | InitProviderPayment`);
return true;
}
else if (((_j = config === null || config === void 0 ? void 0 : config.systemName) === null || _j === void 0 ? void 0 : _j.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.CHECKOUT.toLowerCase()) {
const clientId = ((_l = (_k = config === null || config === void 0 ? void 0 : config.settings) === null || _k === void 0 ? void 0 : _k.find((x) => x.key === "MotoAccountCode")) === null || _l === void 0 ? void 0 : _l.value) || constants_1.Defaults.String.Value;
const accessSecret = ((_o = (_m = config === null || config === void 0 ? void 0 : config.settings) === null || _m === void 0 ? void 0 : _m.find((x) => x.key === "MotoSignature")) === null || _o === void 0 ? void 0 : _o.value) || constants_1.Defaults.String.Value;
const processingChannelId = ((_q = (_p = config === null || config === void 0 ? void 0 : config.settings) === null || _p === void 0 ? void 0 : _p.find((x) => x.key === "MotoUserName")) === null || _q === void 0 ? void 0 : _q.value) || constants_1.Defaults.String.Value;
// Init Env
bc_checkout_sdk_1.CheckoutEnvironment.initServer(clientId, accessSecret, processingChannelId, isSandbox, {
logActivity: (data) => {
if (providerLoggingEnabled) {
Logger_1.Logger.logPayment(data, {});
}
}
});
//const logData = { data: `CheckoutEnvironment.initServer(${clientId}, ${accessSecret}, ${processingChannelId}, ${isSandbox})` };
//logActivity(providerLoggingEnabled, logData, `${config?.systemName} | InitProviderPayment`);
return true;
}
else if (((_r = config === null || config === void 0 ? void 0 : config.systemName) === null || _r === void 0 ? void 0 : _r.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.STRIPE.toLowerCase()) {
const publicKey = ((_t = (_s = config === null || config === void 0 ? void 0 : config.settings) === null || _s === void 0 ? void 0 : _s.find((x) => x.key === "AccountCode")) === null || _t === void 0 ? void 0 : _t.value) || constants_1.Defaults.String.Value;
const privateKey = ((_v = (_u = config === null || config === void 0 ? void 0 : config.settings) === null || _u === void 0 ? void 0 : _u.find((x) => x.key === "Signature")) === null || _v === void 0 ? void 0 : _v.value) || constants_1.Defaults.String.Value;
// Init Env
bc_stripe_sdk_1.StripeEnvironment.init(publicKey, privateKey);
//const logData = { data: `StripeEnvironment.init(${publicKey}, ${privateKey})` };
//logActivity(providerLoggingEnabled, logData, `${config?.systemName} | InitProviderPayment`);
return true;
}
else if (((_w = config === null || config === void 0 ? void 0 : config.systemName) === null || _w === void 0 ? void 0 : _w.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.KLARNA.toLowerCase()) {
const apiUserName = ((_y = (_x = config === null || config === void 0 ? void 0 : config.settings) === null || _x === void 0 ? void 0 : _x.find((x) => x.key === "AccountCode")) === null || _y === void 0 ? void 0 : _y.value) || constants_1.Defaults.String.Value;
const apiPassword = ((_0 = (_z = config === null || config === void 0 ? void 0 : config.settings) === null || _z === void 0 ? void 0 : _z.find((x) => x.key === "Signature")) === null || _0 === void 0 ? void 0 : _0.value) || constants_1.Defaults.String.Value;
// Init Env
bc_klarna_sdk_1.KlarnaEnvironment.init(apiUserName, apiPassword, isSandbox);
//const logData = { data: `KlarnaEnvironment.init(${apiUserName}, ${apiPassword}, ${isSandbox})` };
//logActivity(providerLoggingEnabled, logData, `${config?.systemName} | InitProviderPayment`);
return true;
}
else if (((_1 = config === null || config === void 0 ? void 0 : config.systemName) === null || _1 === void 0 ? void 0 : _1.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.CLEAR_PAY.toLowerCase()) {
const apiUserName = ((_3 = (_2 = config === null || config === void 0 ? void 0 : config.settings) === null || _2 === void 0 ? void 0 : _2.find((x) => x.key === "AccountCode")) === null || _3 === void 0 ? void 0 : _3.value) || constants_1.Defaults.String.Value;
const apiPassword = ((_5 = (_4 = config === null || config === void 0 ? void 0 : config.settings) === null || _4 === void 0 ? void 0 : _4.find((x) => x.key === "Signature")) === null || _5 === void 0 ? void 0 : _5.value) || constants_1.Defaults.String.Value;
// Init Env
bc_clearpay_sdk_1.ClearPayEnvironment.init(apiUserName, apiPassword, isSandbox);
//const logData = { data: `ClearPayEnvironment.init(${apiUserName}, ${apiPassword}, ${isSandbox})` };
//logActivity(providerLoggingEnabled, logData, `${config?.systemName} | InitProviderPayment`);
return true;
}
else if (((_6 = config === null || config === void 0 ? void 0 : config.systemName) === null || _6 === void 0 ? void 0 : _6.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.CHECKOUT_APPLE_PAY.toLowerCase()) {
const merchantId = ((_8 = (_7 = config === null || config === void 0 ? void 0 : config.settings) === null || _7 === void 0 ? void 0 : _7.find((x) => x.key === "AccountCode")) === null || _8 === void 0 ? void 0 : _8.value) || constants_1.Defaults.String.Value;
const domainName = ((_10 = (_9 = config === null || config === void 0 ? void 0 : config.settings) === null || _9 === void 0 ? void 0 : _9.find((x) => x.key === "MotoUserName")) === null || _10 === void 0 ? void 0 : _10.value) || constants_1.Defaults.String.Value;
const displayName = ((_12 = (_11 = config === null || config === void 0 ? void 0 : config.settings) === null || _11 === void 0 ? void 0 : _11.find((x) => x.key === "MotoPassword")) === null || _12 === void 0 ? void 0 : _12.value) || constants_1.Defaults.String.Value;
const extras = BCEnvironment_1.BCEnvironment.getExtras();
const pemCert = extras === null || extras === void 0 ? void 0 : extras.pemCert;
const keyCert = extras === null || extras === void 0 ? void 0 : extras.keyCert;
// Init Env
bc_apple_pay_sdk_1.ApplePayEnvironment.init(merchantId, domainName, displayName, pemCert, keyCert, isSandbox);
//const logData = { data: `ApplePayEnvironment.init(${merchantId}, ${domainName}, ${displayName}, ${pemCert}, ${keyCert}, ${isSandbox})` };
//logActivity(providerLoggingEnabled, logData, `${config?.systemName} | InitProviderPayment`);
return true;
}
else if (((_13 = config === null || config === void 0 ? void 0 : config.systemName) === null || _13 === void 0 ? void 0 : _13.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.JUSPAY.toLowerCase()) {
const merchantId = ((_15 = (_14 = config === null || config === void 0 ? void 0 : config.settings) === null || _14 === void 0 ? void 0 : _14.find((x) => x.key === "AccountCode")) === null || _15 === void 0 ? void 0 : _15.value) || constants_1.Defaults.String.Value;
const apiKey = ((_17 = (_16 = config === null || config === void 0 ? void 0 : config.settings) === null || _16 === void 0 ? void 0 : _16.find((x) => x.key === "Signature")) === null || _17 === void 0 ? void 0 : _17.value) || constants_1.Defaults.String.Value;
const sandboxUrl = ((_19 = (_18 = config === null || config === void 0 ? void 0 : config.settings) === null || _18 === void 0 ? void 0 : _18.find((x) => x.key === "TestUrl")) === null || _19 === void 0 ? void 0 : _19.value) || constants_1.Defaults.String.Value;
const liveUrl = ((_21 = (_20 = config === null || config === void 0 ? void 0 : config.settings) === null || _20 === void 0 ? void 0 : _20.find((x) => x.key === "ProductionUrl")) === null || _21 === void 0 ? void 0 : _21.value) || constants_1.Defaults.String.Value;
const useSandbox = ((_23 = (_22 = config === null || config === void 0 ? void 0 : config.settings) === null || _22 === void 0 ? void 0 : _22.find((x) => x.key === "UseSandbox")) === null || _23 === void 0 ? void 0 : _23.value) || constants_1.Defaults.String.Value;
const baseUrl = (0, parse_util_1.stringToBoolean)(useSandbox) ? sandboxUrl : liveUrl;
let extras = BCEnvironment_1.BCEnvironment.getExtras();
const returnUrl = `${extras === null || extras === void 0 ? void 0 : extras.origin}${config === null || config === void 0 ? void 0 : config.notificationUrl}`;
// Init Env
extras = Object.assign(Object.assign({}, extras), { returnUrl });
bc_juspay_sdk_1.JuspayEnv.init();
bc_juspay_sdk_1.JuspayEnv.withCredentials(merchantId, apiKey, baseUrl, undefined, undefined, extras);
return true;
}
else if (((_24 = config === null || config === void 0 ? void 0 : config.systemName) === null || _24 === void 0 ? void 0 : _24.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.ELAVON.toLowerCase()) {
const merchantId = ((_26 = (_25 = config === null || config === void 0 ? void 0 : config.settings) === null || _25 === void 0 ? void 0 : _25.find((x) => x.key === "AccountCode")) === null || _26 === void 0 ? void 0 : _26.value) || constants_1.Defaults.String.Value;
const merchantUserId = ((_28 = (_27 = config === null || config === void 0 ? void 0 : config.settings) === null || _27 === void 0 ? void 0 : _27.find((x) => x.key === "Signature")) === null || _28 === void 0 ? void 0 : _28.value) || constants_1.Defaults.String.Value;
const merchantPIN = ((_30 = (_29 = config === null || config === void 0 ? void 0 : config.settings) === null || _29 === void 0 ? void 0 : _29.find((x) => x.key === "MotoAccountCode")) === null || _30 === void 0 ? void 0 : _30.value) || constants_1.Defaults.String.Value;
const vendorId = ((_32 = (_31 = config === null || config === void 0 ? void 0 : config.settings) === null || _31 === void 0 ? void 0 : _31.find((x) => x.key === "MotoSignature")) === null || _32 === void 0 ? void 0 : _32.value) || constants_1.Defaults.String.Value;
// Init Env
bc_elavon_sdk_1.ElavonEnvironment.initServer(merchantId, merchantUserId, merchantPIN, vendorId, isSandbox, {
logActivity: (data) => {
if (providerLoggingEnabled) {
Logger_1.Logger.logPayment(data, {});
}
}
});
//const logData = { data: `CheckoutEnvironment.initServer(${clientId}, ${accessSecret}, ${processingChannelId}, ${isSandbox})` };
//logActivity(providerLoggingEnabled, logData, `${config?.systemName} | InitProviderPayment`);
return true;
}
else if (((_33 = config === null || config === void 0 ? void 0 : config.systemName) === null || _33 === void 0 ? void 0 : _33.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.OPAYO.toLowerCase()) {
const vendorName = ((_35 = (_34 = config === null || config === void 0 ? void 0 : config.settings) === null || _34 === void 0 ? void 0 : _34.find((x) => x.key === "AccountCode")) === null || _35 === void 0 ? void 0 : _35.value) || constants_1.Defaults.String.Value;
const integrationKey = ((_37 = (_36 = config === null || config === void 0 ? void 0 : config.settings) === null || _36 === void 0 ? void 0 : _36.find((x) => x.key === "Signature")) === null || _37 === void 0 ? void 0 : _37.value) || constants_1.Defaults.String.Value;
const integrationPassword = ((_39 = (_38 = config === null || config === void 0 ? void 0 : config.settings) === null || _38 === void 0 ? void 0 : _38.find((x) => x.key === "MotoAccountCode")) === null || _39 === void 0 ? void 0 : _39.value) || constants_1.Defaults.String.Value;
let extras = BCEnvironment_1.BCEnvironment.getExtras();
const returnUrl = `${extras === null || extras === void 0 ? void 0 : extras.origin}${config === null || config === void 0 ? void 0 : config.notificationUrl}`;
extras = Object.assign(Object.assign({}, extras), { returnUrl });
// Init Env
bc_opayo_sdk_1.OpayoEnvironment.init(vendorName, integrationKey, integrationPassword, isSandbox, Object.assign(Object.assign({}, extras), { logActivity: (data) => {
if (providerLoggingEnabled) {
Logger_1.Logger.logPayment(data, {});
}
} }));
return true;
}
else if (((_40 = config === null || config === void 0 ? void 0 : config.systemName) === null || _40 === void 0 ? void 0 : _40.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.OMNICAPITAL.toLowerCase()) {
const installationId = ((_42 = (_41 = config === null || config === void 0 ? void 0 : config.settings) === null || _41 === void 0 ? void 0 : _41.find((x) => x.key === "AccountCode")) === null || _42 === void 0 ? void 0 : _42.value) || constants_1.Defaults.String.Value;
const apiKey = ((_44 = (_43 = config === null || config === void 0 ? void 0 : config.settings) === null || _43 === void 0 ? void 0 : _43.find((x) => x.key === "Signature")) === null || _44 === void 0 ? void 0 : _44.value) || constants_1.Defaults.String.Value;
const username = ((_46 = (_45 = config === null || config === void 0 ? void 0 : config.settings) === null || _45 === void 0 ? void 0 : _45.find((x) => x.key === "MotoUserName")) === null || _46 === void 0 ? void 0 : _46.value) || constants_1.Defaults.String.Value;
const password = ((_48 = (_47 = config === null || config === void 0 ? void 0 : config.settings) === null || _47 === void 0 ? void 0 : _47.find((x) => x.key === "MotoPassword")) === null || _48 === void 0 ? void 0 : _48.value) || constants_1.Defaults.String.Value;
const webFormSubmitUrl = ((_50 = (_49 = config === null || config === void 0 ? void 0 : config.settings) === null || _49 === void 0 ? void 0 : _49.find((x) => x.key === "ProductionUrl")) === null || _50 === void 0 ? void 0 : _50.value) || constants_1.Defaults.String.Value;
let extras = BCEnvironment_1.BCEnvironment.getExtras();
extras = Object.assign(Object.assign({}, extras), { webFormSubmitUrl });
// Init Env
bc_omnicapital_sdk_1.OmniCapitalEnvironment.init(apiKey, installationId, username, password, isSandbox, Object.assign(Object.assign({}, extras), { logActivity: (data) => {
if (providerLoggingEnabled) {
Logger_1.Logger.logPayment(data, {});
}
} }));
return true;
}
else if (((_51 = config === null || config === void 0 ? void 0 : config.systemName) === null || _51 === void 0 ? void 0 : _51.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.NUVEI.toLowerCase() || ((_52 = config === null || config === void 0 ? void 0 : config.systemName) === null || _52 === void 0 ? void 0 : _52.toLowerCase()) === PaymentMethodType_1.PaymentMethodType.NUVEI_GOOGLE_PAY.toLowerCase()) {
const merchantId = ((_54 = (_53 = config === null || config === void 0 ? void 0 : config.settings) === null || _53 === void 0 ? void 0 : _53.find((x) => x.key === "AccountCode")) === null || _54 === void 0 ? void 0 : _54.value) || constants_1.Defaults.String.Value;
const merchantSiteId = ((_56 = (_55 = config === null || config === void 0 ? void 0 : config.settings) === null || _55 === void 0 ? void 0 : _55.find((x) => x.key === "Signature")) === null || _56 === void 0 ? void 0 : _56.value) || constants_1.Defaults.String.Value;
const merchantSecretKey = ((_58 = (_57 = config === null || config === void 0 ? void 0 : config.settings) === null || _57 === void 0 ? void 0 : _57.find((x) => x.key === "MotoSignature")) === null || _58 === void 0 ? void 0 : _58.value) || constants_1.Defaults.String.Value;
const extras = BCEnvironment_1.BCEnvironment.getExtras();
console.log({ merchantId, merchantSiteId, merchantSecretKey, isSandbox, extras });
// Init Env
bc_nuvei_sdk_1.NuveiEnvironment.init(merchantId, merchantSiteId, merchantSecretKey, isSandbox, Object.assign(Object.assign({}, extras), { logActivity: (data) => {
if (providerLoggingEnabled) {
Logger_1.Logger.logPayment(data, {});
}
} }));
return true;
}
}
return false;
}
}
exports.BasePaymentProvider = BasePaymentProvider;