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.
531 lines (530 loc) • 27.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasePaymentOperation = void 0;
const BCEnvironment_1 = require("../../base/config/BCEnvironment");
const PaymentMethodType_1 = require("../../constants/enums/PaymentMethodType");
const CheckoutPayment_1 = require("../../modules/payments/CheckoutPayment");
const ClearPayPament_1 = require("../../modules/payments/ClearPayPament");
const KlarnaPayment_1 = require("../../modules/payments/KlarnaPayment");
const PayPalPayment_1 = require("../../modules/payments/PayPalPayment");
const StripePayment_1 = require("../../modules/payments/StripePayment");
const ApplePayPayment_1 = require("../../modules/payments/ApplePayPayment");
const JuspayPayment_1 = require("../../modules/payments/JuspayPayment");
const ElavonPayment_1 = require("../../modules/payments/ElavonPayment");
const OmniCapitalPayment_1 = require("../../modules/payments/OmniCapitalPayment");
const NuveiPayment_1 = require("../../modules/payments/NuveiPayment");
/**
* Abstract class {BasePaymentOperation} is the base class for all payment operations
* and defines the concrete methods for specific payment operations of all the gateway providers.
*
* Payment operations are responsible for creating payment orders, retrieving payment methods, processing payments and more.
* This class provides the following methods that can be overridden by the concrete implementation classes:
*
* - {createOneTimePaymentOrder}: Creates a one time payment order.
* - {getPaymentMethods}: Retrieves the payment methods for the current customer.
* - {processPayment}: Processes a payment.
* - {processPaymentHook}: Processes a payment hook.
*
* @abstract
* @category Payment Operation
*/
class BasePaymentOperation {
/**
* Creates a one time payment order.
* Specific to {Klarna}, creates a one time payment order.
*
* This method retrieves the payment provider and checks if it is of type `KLARNA`.
* If so, it calls the `createOneTimePaymentOrder` method of `KlarnaPayment` with the provided data.
* If the payment provider is not `KLARNA`, it returns null.
*
*
* API Reference - https://docs.klarna.com/klarna-payments/integrate-with-klarna-payments/step-3-create-an-order/create-a-one-time-payment-order/
*
* @param data - The payment intent data required by the payment provider.
* @returns A promise that resolves to the result of the payment intent creation
* or an object with error details if an error occurs.
*/
async createOneTimePaymentOrder(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.KLARNA) {
return await new KlarnaPayment_1.KlarnaPayment().createOneTimePaymentOrder(data);
}
return null;
}
/**
* Validates the payment session using the Apple Pay SDK.
* Specific to {ApplePay}, validates the payment session.
*
* This method retrieves the payment provider and checks if it is of type `CHECKOUT_APPLE_PAY`.
* If so, it calls the `validatePaymentSession` method of `ApplePayPayment` with the provided data.
* If the payment provider is not `CHECKOUT_APPLE_PAY`, it returns null.
*
* API Reference - https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/providing_merchant_validation
*
* @param data The Apple Pay session validation data.
* @returns The result of the payment session validation. If the validation is successful, the result is the validated session object. Otherwise, the result is an error object with the error message.
*/
async validatePaymentSession(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.CHECKOUT_APPLE_PAY) {
return await new ApplePayPayment_1.ApplePayPayment().validatePaymentSession(data);
}
return null;
}
/**
* Requests a token from the appropriate payment provider.
* Specific to {Checkout}, Exchange card details for a reference token that can be used later to request a card payment. Tokens are single use and expire after 15 minutes.
*
* This method retrieves the payment provider and checks if it is of type `CHECKOUT`.
* If so, it calls the `requestToken` method of `CheckoutPayment` with the provided data.
* If the payment provider is not `CHECKOUT`, it returns null.
*
* API Reference - https://api-reference.checkout.com/#operation/requestAToken
*
* @param data {Object} - The data required for requesting a token.
* @returns A promise that resolves to the result of the token request
* or null if the payment provider is not `CHECKOUT`.
*/
async requestToken(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.CHECKOUT) {
return await new CheckoutPayment_1.CheckoutPayment().requestToken(data);
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.ELAVON) {
return await new ElavonPayment_1.ElavonPayment().requestToken(data);
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI || paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI_GOOGLE_PAY) {
return await new NuveiPayment_1.NuveiPayment().requestToken(data);
}
return null;
}
/**
* Creates a payment context for the current payment provider.
* Specific to {Checkout}, creates a payment context.
*
* The method attempts to retrieve a payment provider object and then calls its `createPaymentContext` method
* with the provided data. If successful, it returns the result of the payment context creation. Otherwise, it catches any errors that occur during the process and returns an error object.
*
* API Reference - https://api-reference.checkout.com/#operation/requestAPaymentContext
*
* @param data {Object} - The payment context data required by the payment provider.
* @returns A promise that resolves to the result of the payment context creation or an error object in case of a failure.
*/
async createPaymentContext(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.CHECKOUT) {
return await new CheckoutPayment_1.CheckoutPayment().createPaymentContext(data);
}
return null;
}
/**
* Creates a session for a customer using the appropriate payment provider.
* Specific to {Checkout}, Creates a Klarna session for a customer.
*
* The method attempts to retrieve a payment provider object and then calls its `createSession` method
* with the provided data. If successful, it returns the result of the session creation. Otherwise, it catches any errors that occur during the process and returns an error object.
*
* API Reference - https://www.checkout.com/docs/previous/payments/payment-methods/invoice-and-pay-later/klarna#Create_a_session
*
* @param data {Object} - The session data required by the payment provider.
* @returns A promise that resolves to the result of the session creation or an error object in case of a failure.
*/
async createSession(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.CHECKOUT) {
return await new CheckoutPayment_1.CheckoutPayment().createSession(data);
}
return null;
}
/**
* Retrieves the payment context details for the current payment provider.
* Specific to {Checkout},Returns all the Payment Context details.
*
* This method should be implemented by subclasses to fetch payment context
* details using the provided data. It throws an error if not implemented.
*
* API Reference - https://api-reference.checkout.com/#operation/getPaymentContext
*
* @param data - The payment context data required by the payment provider.
* @returns A promise that resolves to the result of the payment context details request
* or an object with error details if an error occurs.
*/
async getPaymentContext(data) {
throw new Error("Method not implemented.");
}
/**
* Retrieves the payment methods available for the current payment provider.
* Specific to {Juspay}, Retrieves the payment methods available for the merchant from Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `getPaymentMethods` method
* with the provided data. If successful, it returns the result of the payment methods request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/payment-methods
*
* @param data - The payment method data required by the payment provider.
* @returns A promise that resolves to the result of the payment methods request
* or null if the payment provider is not {Juspay}.
*/
async getPaymentMethods(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().getPaymentMethods(data);
}
return null;
}
/**
* Retrieves the customer details from the current payment provider.
* Specific to {Juspay}, Retrieves the customer details from Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `getCustomer` method
* with the provided data. If successful, it returns the result of the customer details request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/getcustomer
*
* @param data - The customer ID required by the payment provider.
* @returns A promise that resolves to the result of the customer details request
* or null if the payment provider is not {Juspay}.
*/
async getCustomer(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().getCustomer(data);
}
return null;
}
/**
* Creates a customer in the current payment provider.
* Specific to {Juspay}, creates a customer in Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `createCustomer` method
* with the provided data. If successful, it returns the result of the create customer request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/createcustomer
*
* @param data - The customer data required by the payment provider.
* @returns A promise that resolves to the result of the create customer request
* or null if the payment provider is not {Juspay}.
*/
async createCustomer(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().createCustomer(data);
}
return null;
}
/**
* Creates an order in the current payment provider.
* Specific to {Juspay}, creates an order in Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `createOrder` method
* with the provided data. If successful, it returns the result of the create order request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/create-order-api
*
* @param data - The order data required by the payment provider.
* @returns A promise that resolves to the result of the create order request
* or null if the payment provider is not {Juspay}.
*/
async createOrder(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().createOrder(data);
}
return null;
}
/**
* Updates an order in the current payment provider.
* Specific to {Juspay}, updates an order in Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `updateOrder` method
* with the provided data. If successful, it returns the result of the update order request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/update-order-api
*
* @param data - The order data required by the payment provider.
* @returns A promise that resolves to the result of the update order request
* or null if the payment provider is not {Juspay}.
*/
async updateOrder(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().updateOrder(data);
}
return null;
}
/**
* Retrieves the card information for the current payment provider.
* Specific to {Juspay}, Retrieves the card information for the current payment provider.
*
* The method attempts to retrieve a payment provider object and then calls its `getCardInfo` method
* with the provided data. If successful, it returns the result of the card information request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/card-info
*
* @param data - The card data required by the payment provider.
* @returns A promise that resolves to the result of the card information request
* or null if the payment provider is not {Juspay}.
*/
async getCardInfo(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().getCardInfo(data);
}
return null;
}
/**
* Tokenizes a card using the current payment provider.
* Specific to {Juspay}, exchanges card details for a token.
*
* The method attempts to retrieve a payment provider object and then calls its `tokenizeCard` method
* with the provided data. If successful, it returns the result of the tokenization request. Otherwise, it returns null.
*
* API Reference - https://juspay.io/in/docs/api-reference/docs/express-checkout/tokenize
*
* @param data - The card data required by the payment provider.
* @returns A promise that resolves to the result of the tokenization request
* or null if the payment provider is not {Juspay}.
*/
async tokenizeCard(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().tokenizeCard(data);
}
return null;
}
/**
* Saves a card using the current payment provider.
* Specific to {Juspay}, Saves a card to Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `saveCard` method
* with the provided data. If successful, it returns the result of the save card request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/add-card
*
* @param data - The card data required by the payment provider.
* @returns A promise that resolves to the result of the save card request
* or null if the payment provider is not {Juspay}.
*/
async saveCard(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().saveCard(data);
}
return null;
}
/**
* Deletes a card using the current payment provider.
* Specific to {Juspay}, Deletes a card from Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `deleteCard` method
* with the provided data. If successful, it returns the result of the delete card request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/delete-card
*
* @param data - The card data required by the payment provider.
* @returns A promise that resolves to the result of the delete card request
* or null if the payment provider is not {Juspay}.
*/
async deleteCard(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().deleteCard(data);
}
return null;
}
/**
* Retrieves the cards associated with the current payment provider.
* Specific to {Juspay}, Retrieves the cards associated with the merchant from Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `getCards` method
* with the provided data. If successful, it returns the result of the cards request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/list-stored-cards
*
* @param data - The card data required by the payment provider.
* @returns A promise that resolves to the result of the cards request
* or null if the payment provider is not {Juspay}.
*/
async getCards(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().getCards(data);
}
return null;
}
/**
* Verifies a VPA using the current payment provider.
* Specific to {Juspay}, verifies a VPA with Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `verifyVPA` method
* with the provided data. If successful, it returns the result of the verification request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/verify-vpa
*
* @param data - The VPA data required by the payment provider.
* @returns A promise that resolves to the result of the verification request
* or null if the payment provider is not {Juspay}.
*/
async verifyVPA(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().verifyVPA(data);
}
return null;
}
/**
* Retrieves the offers associated with the current payment provider.
* Specific to {Juspay}, Retrieves the offers associated with the merchant from Juspay.
*
* The method attempts to retrieve a payment provider object and then calls its `getOffers` method
* with the provided data. If successful, it returns the result of the offers request. Otherwise, it returns null.
*
* API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/offer-list
*
* @param data - The offer data required by the payment provider.
* @returns A promise that resolves to the result of the offers request
* or null if the payment provider is not {Juspay}.
*/
async getOffers(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
return await new JuspayPayment_1.JuspayPayment().getOffers(data);
}
return null;
}
/**
* Gets detailed information about a specific transaction.
* Can query by either transactionId or clientUniqueId.
* If multiple transactions share the same clientUniqueId, only the most recent is returned.
*
* API Reference - https://docs.nuvei.com/api/main/indexMain_v1_0.html?json#getTransactionDetails
*
* @param {Object} params The query parameters
* @param {string} params.transactionId - The Gateway transaction ID (conditional - either this or clientUniqueId required)
* @param {string} params.clientUniqueId - The unique transaction ID in merchant system (conditional - either this or transactionId required)
* @returns {Promise<IGetTransactionDetailsResponse>} A promise resolving to the transaction details
*/
async getTransactionDetails(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI || paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI_GOOGLE_PAY) {
return await new NuveiPayment_1.NuveiPayment().getTransactionDetails(data);
}
return null;
}
/**
* Gets Google Pay merchant info JWT for secure domain registration.
* This JWT solution allows Nuvei to dynamically enable unlimited web domains
* without having to register each one in Google Pay and Wallet consoles.
* The merchant receives a registered domain and JWT for each transaction.
*
* API Reference - https://docs.nuvei.com/api/advanced/indexAdvanced.html?json#googlePayMerchantInfoJwt
*
* @param {Object} params The parameters for getting merchant info JWT
* @param {string} params.sessionToken The session token from openOrder
* @param {string} params.merchantOrigin The merchant's domain origin
* @returns {Promise<IGetGooglePayMerchantInfoJwtResponse>} A promise resolving to the merchant info with JWT
*/
async requestGooglePayToken(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI_GOOGLE_PAY) {
return await new NuveiPayment_1.NuveiPayment().requestGooglePayToken(data);
}
return null;
}
/**
* Gets the list of registered Google Pay domains for the merchant.
* This endpoint retrieves all domains that are currently registered for Google Pay processing.
*
* API Reference - https://docs.nuvei.com/api/advanced/indexAdvanced.html?json#getRegisteredGooglePayDomains
*
* @param {Object} params The parameters for retrieving registered domains
* @param {string[]} params.domainNames - Optional array of specific domains to query. If not provided, returns all registered domains.
* @returns {Promise<IGetRegisteredGooglePayDomainsResponse>} A promise resolving to the response with the list of registered domains
*
* @example
* // Get all registered domains
* const result = await transaction.getRegisteredGooglePayDomains();
* console.log('Registered domains:', result.domainNames);
*
* @example
* // Query specific domains
* const result = await transaction.getRegisteredGooglePayDomains({
* domainNames: ["www.example.com", "mobile.example.com"]
* });
* console.log('Queried domains:', result.domainNames);
*/
async getRegisteredDomains(data) {
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI_GOOGLE_PAY) {
return await new NuveiPayment_1.NuveiPayment().getRegisteredDomains(data);
}
return null;
}
/**
* Retrieves the payment provider type from the configuration.
*
* This method accesses the current configuration to determine the
* payment provider being used. It logs the configuration details for
* debugging purposes and returns the provider's system name in
* lowercase form.
*
* @protected
* @returns {PaymentMethodType} The payment provider type in lowercase.
*/
getPaymentProvider() {
var _a;
const config = BCEnvironment_1.BCEnvironment.getConfig();
console.log("getObject() config", config);
/*Logger.logPayment({
data: { data: config },
message: `${config?.systemName} | GetPaymentProvider`,
}, {})*/
return (_a = config === null || config === void 0 ? void 0 : config.systemName) === null || _a === void 0 ? void 0 : _a.toLowerCase();
}
/**
* Retrieves an instance of the payment provider based on the configured payment method type.
*
* This method determines the appropriate payment provider by calling `getPaymentProvider`.
* It then creates and returns an instance of the corresponding payment provider class.
* Supported payment methods include PayPal, Checkout, ClearPay, Klarna, Stripe, and Juspay.
*
* @returns {IPaymentProvider} An instance of a payment provider corresponding to the
* configured payment method type.
*/
getObject() {
let obj;
const paymentProvider = this.getPaymentProvider();
if (paymentProvider === PaymentMethodType_1.PaymentMethodType.PAYPAL) {
obj = new PayPalPayment_1.PayPalPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.CHECKOUT) {
obj = new CheckoutPayment_1.CheckoutPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.CLEAR_PAY) {
obj = new ClearPayPament_1.ClearPayPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.KLARNA) {
obj = new KlarnaPayment_1.KlarnaPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.STRIPE) {
obj = new StripePayment_1.StripePayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.JUSPAY) {
obj = new JuspayPayment_1.JuspayPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.ELAVON) {
obj = new ElavonPayment_1.ElavonPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.OPAYO) {
obj = new ElavonPayment_1.ElavonPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.OMNICAPITAL) {
obj = new OmniCapitalPayment_1.OmniCapitalPayment();
}
else if (paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI || paymentProvider === PaymentMethodType_1.PaymentMethodType.NUVEI_GOOGLE_PAY) {
obj = new NuveiPayment_1.NuveiPayment();
}
return obj;
}
}
exports.BasePaymentOperation = BasePaymentOperation;