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.

591 lines (590 loc) 33.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCardBrand = exports.getCardIssuer = exports.getCardType = exports.getPSPGatewayInfo = exports.getPaymentIdentifier = exports.getPSPInfo = exports.getIsSavePSPInfo = exports.getPSPResponseMsg = exports.getSignature = exports.getAuthCode = exports.getPaymentNo = exports.getOrderNo = exports.getPaymentTransactionOrderId = exports.getPaymentTransactionStatus = exports.getGatewayName = exports.getGatewayId = exports.idToGatewayNameMap = exports.gatewayNameToIdMap = void 0; // Package Imports const PayPalPayment_1 = require("../modules/payments/PayPalPayment"); // Other Imports const parse_util_1 = require("./parse-util"); const constants_1 = require("../constants"); const constants_2 = require("../constants/constants"); const BCEnvironment_1 = require("../base/config/BCEnvironment"); const Logger_1 = require("../modules/better-commerce/Logger"); const PaymentStatus_1 = require("../constants/enums/PaymentStatus"); exports.gatewayNameToIdMap = new Map(Object.entries(constants_1.PaymentMethodType).map(([key, value]) => [ value.toLowerCase(), constants_1.PaymentMethodTypeId[key], ])); exports.idToGatewayNameMap = new Map(Object.entries(constants_1.PaymentMethodTypeId) .filter(([key, value]) => typeof value === 'number') // Filter out string keys (TS enum quirk) .map(([key, value]) => [value, constants_1.PaymentMethodType[key]])); /*export const getGatewayId = (gatewayName: string): number => { const normalizedGatewayName = gatewayName.toLowerCase(); return gatewayNameToIdMap.get(normalizedGatewayName) ?? -1; };*/ /** * Given a gateway name, returns the corresponding gateway ID. * @param gatewayName - The name of the payment gateway. * @returns The gateway ID if found, -1 otherwise. */ const getGatewayId = (gatewayName) => { if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.PAYPAL, true)) { return constants_1.PaymentMethodTypeId.PAYPAL; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.CHECKOUT, true)) { return constants_1.PaymentMethodTypeId.CHECKOUT; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.KLARNA, true)) { return constants_1.PaymentMethodTypeId.KLARNA; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.CLEAR_PAY, true)) { return constants_1.PaymentMethodTypeId.CLEAR_PAY; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.MASTER_CARD, true)) { return constants_1.PaymentMethodTypeId.MASTER_CARD; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.JUSPAY, true)) { return constants_1.PaymentMethodTypeId.JUSPAY; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.STRIPE, true)) { return constants_1.PaymentMethodTypeId.STRIPE; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.COD, true)) { return constants_1.PaymentMethodTypeId.COD; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.ACCOUNT_CREDIT, true)) { return constants_1.PaymentMethodTypeId.ACCOUNT_CREDIT; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.CHEQUE, true)) { return constants_1.PaymentMethodTypeId.CHEQUE; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.CHECKOUT_APPLE_PAY, true)) { return constants_1.PaymentMethodTypeId.CHECKOUT_APPLE_PAY; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.CHECKOUT_KLARNA, true)) { return constants_1.PaymentMethodTypeId.CHECKOUT_KLARNA; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.ELAVON, true)) { return constants_1.PaymentMethodTypeId.ELAVON; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.OPAYO, true)) { return constants_1.PaymentMethodTypeId.OPAYO; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.WALLET, true)) { return constants_1.PaymentMethodTypeId.WALLET; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.OMNICAPITAL, true)) { return constants_1.PaymentMethodTypeId.OMNICAPITAL; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.NUVEI, true)) { return constants_1.PaymentMethodTypeId.NUVEI; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.NUVEI_GOOGLE_PAY, true)) { return constants_1.PaymentMethodTypeId.NUVEI_GOOGLE_PAY; } else if ((0, parse_util_1.matchStrings)(gatewayName, constants_1.PaymentMethodType.NUVEI_APPLE_PAY, true)) { return constants_1.PaymentMethodTypeId.NUVEI_APPLE_PAY; } return -1; }; exports.getGatewayId = getGatewayId; /*export const getGatewayName = (id: number): string | -1 => { return idToGatewayNameMap.get(id) ?? -1; };*/ /** * Given a gateway ID, returns the corresponding gateway name. * @param {number} id the gateway ID. * @returns {string} the gateway name. */ const getGatewayName = (id) => { if (id === constants_1.PaymentMethodTypeId.PAYPAL) { return constants_1.PaymentMethodType.PAYPAL; } else if (id === constants_1.PaymentMethodTypeId.CHECKOUT) { return constants_1.PaymentMethodType.CHECKOUT; } else if (id === constants_1.PaymentMethodTypeId.KLARNA) { return constants_1.PaymentMethodType.KLARNA; } else if (id === constants_1.PaymentMethodTypeId.CLEAR_PAY) { return constants_1.PaymentMethodType.CLEAR_PAY; } else if (id === constants_1.PaymentMethodTypeId.MASTER_CARD) { return constants_1.PaymentMethodType.MASTER_CARD; } else if (id === constants_1.PaymentMethodTypeId.JUSPAY) { return constants_1.PaymentMethodType.JUSPAY; } else if (id === constants_1.PaymentMethodTypeId.STRIPE) { return constants_1.PaymentMethodType.STRIPE; } else if (id === constants_1.PaymentMethodTypeId.COD) { return constants_1.PaymentMethodType.COD; } else if (id === constants_1.PaymentMethodTypeId.ACCOUNT_CREDIT) { return constants_1.PaymentMethodType.ACCOUNT_CREDIT; } else if (id === constants_1.PaymentMethodTypeId.CHEQUE) { return constants_1.PaymentMethodType.CHEQUE; } else if (id === constants_1.PaymentMethodTypeId.CHECKOUT_APPLE_PAY) { return constants_1.PaymentMethodType.CHECKOUT_APPLE_PAY; } else if (id === constants_1.PaymentMethodTypeId.CHECKOUT_KLARNA) { return constants_1.PaymentMethodType.CHECKOUT_KLARNA; } else if (id === constants_1.PaymentMethodTypeId.ELAVON) { return constants_1.PaymentMethodType.ELAVON; } else if (id === constants_1.PaymentMethodTypeId.OPAYO) { return constants_1.PaymentMethodType.OPAYO; } else if (id === constants_1.PaymentMethodTypeId.WALLET) { return constants_1.PaymentMethodType.WALLET; } else if (id === constants_1.PaymentMethodTypeId.OMNICAPITAL) { return constants_1.PaymentMethodType.OMNICAPITAL; } else if (id === constants_1.PaymentMethodTypeId.NUVEI) { return constants_1.PaymentMethodType.NUVEI; } else if (id === constants_1.PaymentMethodTypeId.NUVEI_GOOGLE_PAY) { return constants_1.PaymentMethodType.NUVEI_GOOGLE_PAY; } else if (id === constants_1.PaymentMethodTypeId.NUVEI_APPLE_PAY) { return constants_1.PaymentMethodType.NUVEI_APPLE_PAY; } return -1; }; exports.getGatewayName = getGatewayName; /** * Determines the payment transaction status based on the payment method and transaction data. * * This function evaluates the payment method ID and corresponding transaction data to ascertain * the status of a payment transaction. For the Checkout payment method, it checks the event type * and response summary to categorize the transaction as either charged or failed. For PayPal, it * assesses the event type to determine if the transaction was captured or if authentication failed. * If the conditions for a charged or failed transaction are not met, it defaults to returning a * status of 'NONE'. * * @param methodId - The ID of the payment method, used to determine the applicable gateway logic. * @param data - The transaction data containing details necessary to determine the transaction status. * @returns A string representing the transaction status, such as 'TXN_CHARGED', 'TXN_FAILED', or 'NONE'. */ const getPaymentTransactionStatus = (methodId, data) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { if (((_a = data === null || data === void 0 ? void 0 : data.type) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === ((_b = constants_2.Checkout.EventType.PAYMENT_CAPTURED) === null || _b === void 0 ? void 0 : _b.toLowerCase())) { if (((_c = data === null || data === void 0 ? void 0 : data.data) === null || _c === void 0 ? void 0 : _c.response_summary) === constants_2.Checkout.ResponseSummaryType.APPROVED) { return constants_2.PaymentTransactionStatus.TXN_CHARGED; } else if (((_d = data === null || data === void 0 ? void 0 : data.data) === null || _d === void 0 ? void 0 : _d.response_summary) === constants_2.Checkout.ResponseSummaryType.DECLINED) { return constants_2.PaymentTransactionStatus.TXN_FAILED; } } else if (((_e = data === null || data === void 0 ? void 0 : data.type) === null || _e === void 0 ? void 0 : _e.toLowerCase()) === ((_f = constants_2.Checkout.EventType.PAYMENT_AUTHENTICATION_FAILED) === null || _f === void 0 ? void 0 : _f.toLowerCase())) { return constants_2.PaymentTransactionStatus.TXN_FAILED; } } else if (methodId == constants_1.PaymentMethodTypeId.PAYPAL) { if ((data === null || data === void 0 ? void 0 : data.event_type) === constants_2.Paypal.EventType.PAYMENT_CAPTURED) { return constants_2.PaymentTransactionStatus.TXN_CHARGED; } else if ((data === null || data === void 0 ? void 0 : data.event_type) === constants_2.Paypal.EventType.PAYMENT_AUTHENTICATION_FAILED) { return constants_2.PaymentTransactionStatus.TXN_FAILED; } } else if (methodId == constants_1.PaymentMethodTypeId.OMNICAPITAL) { let paymentStatus = constants_2.PaymentTransactionStatus.NONE; if (constants_2.DEBUG_LOGGING_ENABLED) { // TODO: Debugging Log Logger_1.Logger.logPayment({ data: { methodId, data }, message: `Log | getPaymentTransactionStatus` }, { headers: {}, cookies: {} }); } const status = ((_g = data === null || data === void 0 ? void 0 : data.Status) === null || _g === void 0 ? void 0 : _g.toLowerCase()) || constants_2.Defaults.String.Value; switch (status) { case (_h = PaymentStatus_1.OmniCapital.PaymentStatus.COMPLETE) === null || _h === void 0 ? void 0 : _h.toLowerCase(): paymentStatus = constants_2.PaymentTransactionStatus.TXN_CHARGED; break; case (_j = PaymentStatus_1.OmniCapital.PaymentStatus.DECLINED) === null || _j === void 0 ? void 0 : _j.toLowerCase(): case (_k = PaymentStatus_1.OmniCapital.PaymentStatus.FINANCE_OFFER_WITHDRAWN) === null || _k === void 0 ? void 0 : _k.toLowerCase(): case (_l = PaymentStatus_1.OmniCapital.PaymentStatus.ORDER_CANCELLED) === null || _l === void 0 ? void 0 : _l.toLowerCase(): case (_m = PaymentStatus_1.OmniCapital.PaymentStatus.APPLICATION_LAPSED) === null || _m === void 0 ? void 0 : _m.toLowerCase(): case (_o = PaymentStatus_1.OmniCapital.PaymentStatus.CREDIT_CHECK_DECLINED) === null || _o === void 0 ? void 0 : _o.toLowerCase(): case (_p = PaymentStatus_1.OmniCapital.PaymentStatus.CREDIT_CHECK_PRE_DECLINE) === null || _p === void 0 ? void 0 : _p.toLowerCase(): paymentStatus = constants_2.PaymentTransactionStatus.TXN_FAILED; break; case (_q = PaymentStatus_1.OmniCapital.PaymentStatus.CDS_NOTE_REQUIRED) === null || _q === void 0 ? void 0 : _q.toLowerCase(): case (_r = PaymentStatus_1.OmniCapital.PaymentStatus.AWAITING_FULFILMENT) === null || _r === void 0 ? void 0 : _r.toLowerCase(): case (_s = PaymentStatus_1.OmniCapital.PaymentStatus.ORDER_FULFILLED) === null || _s === void 0 ? void 0 : _s.toLowerCase(): paymentStatus = constants_2.PaymentTransactionStatus.TXN_INITIATED; break; default: console.log('--- OmniCapital no case matched, returning NONE ---'); break; } if (constants_2.DEBUG_LOGGING_ENABLED) { // TODO: Debugging Log Logger_1.Logger.logPayment({ data: { paymentStatus }, message: `Log | getPaymentTransactionStatus` }, { headers: {}, cookies: {} }); } return paymentStatus; } return constants_2.PaymentTransactionStatus.NONE; }; exports.getPaymentTransactionStatus = getPaymentTransactionStatus; /** * Retrieves the order ID from the payment transaction data. * @param methodId - The ID of the payment method. * @param data - The payment transaction data. * @returns A promise that resolves to the order ID as a string. */ const getPaymentTransactionOrderId = async (methodId, data) => { var _a, _b, _c, _d, _e, _f, _g, _h; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return (_b = (_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.metadata) === null || _b === void 0 ? void 0 : _b.udf1; } else if (methodId == constants_1.PaymentMethodTypeId.PAYPAL) { console.log('--- data ---', JSON.stringify(data)); const payPalOrderId = (_e = (_d = (_c = data === null || data === void 0 ? void 0 : data.resource) === null || _c === void 0 ? void 0 : _c.supplementary_data) === null || _d === void 0 ? void 0 : _d.related_ids) === null || _e === void 0 ? void 0 : _e.order_id; console.log('--- payPalOrderId ---', payPalOrderId); if (payPalOrderId) { const { extras } = data; const { clientId, sharedSecret, apiToken, refreshToken, config, authUrl, baseUrl } = extras; if (!clientId && !sharedSecret) { BCEnvironment_1.BCEnvironment.init(clientId, sharedSecret, config, authUrl, baseUrl); } if (!apiToken && !refreshToken) { BCEnvironment_1.BCEnvironment.initSession(apiToken, refreshToken, config, authUrl, baseUrl); } const paypalOrderDetails = await new PayPalPayment_1.PayPalPayment().getOrderDetails(payPalOrderId); console.log('--- paypalOrderDetails ---', JSON.stringify(paypalOrderDetails)); if ((_f = paypalOrderDetails === null || paypalOrderDetails === void 0 ? void 0 : paypalOrderDetails.purchase_units) === null || _f === void 0 ? void 0 : _f.length) { const description = (_g = paypalOrderDetails === null || paypalOrderDetails === void 0 ? void 0 : paypalOrderDetails.purchase_units[0]) === null || _g === void 0 ? void 0 : _g.description; console.log('--- description ---', description); if (description) { return ((_h = parseOrderId(description)) === null || _h === void 0 ? void 0 : _h.trim()) || constants_2.Defaults.String.Value; } } } } else if (methodId == constants_1.PaymentMethodTypeId.OMNICAPITAL) { const retailerUniqueRef = data === null || data === void 0 ? void 0 : data["Identification[RetailerUniqueRef]"]; if (retailerUniqueRef) { const json = (0, parse_util_1.tryParseJson)(retailerUniqueRef); if (json === null || json === void 0 ? void 0 : json.id) { return json === null || json === void 0 ? void 0 : json.id; } } } return Promise.resolve(constants_2.Defaults.String.Value); }; exports.getPaymentTransactionOrderId = getPaymentTransactionOrderId; /*export const getPaymentTransactionOrderNo = async (methodId: number, data: any): Promise<string> => { if (methodId == PaymentMethodTypeId.PAYPAL) { console.log('--- data ---', JSON.stringify(data)) const payPalOrderId = data?.resource?.supplementary_data?.related_ids?.order_id; console.log('--- payPalOrderId ---', payPalOrderId) if (payPalOrderId) { const { extras } = data const { clientId, sharedSecret, config, authUrl, baseUrl } = extras BCEnvironment.init(clientId, sharedSecret, config, authUrl, baseUrl); const paypalOrderDetails = await new PayPalPayment().getOrderDetails(payPalOrderId); console.log('--- paypalOrderDetails ---', JSON.stringify(paypalOrderDetails)) if (paypalOrderDetails?.purchase_units?.length) { const description = paypalOrderDetails?.purchase_units[0]?.description; console.log('--- description ---', description) if (description) { return parseOrderPaymentNo(description)?.trim() || Defaults.String.Value; } } } } return Promise.resolve(Defaults.String.Value); }*/ /** * Retrieves the order number from the given data based on the payment method type. * * @param methodId - The payment method type identifier. * @param data - The data object containing the order number. * @param extras - The extras object containing additional information. * * @returns The order number if found, otherwise the default integer value. */ const getOrderNo = (methodId, data, extras) => { var _a, _b; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return (_a = data === null || data === void 0 ? void 0 : data.metadata) === null || _a === void 0 ? void 0 : _a.udf4.split('-')[0]; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return parseInt((_b = data === null || data === void 0 ? void 0 : data.order_id) === null || _b === void 0 ? void 0 : _b.split('-')[0]); } else if (methodId == constants_1.PaymentMethodTypeId.OMNICAPITAL) { return data === null || data === void 0 ? void 0 : data.orderNo; } return constants_2.Defaults.Int.Value; }; exports.getOrderNo = getOrderNo; /** * Retrieves the payment number from the provided data based on the payment method type. * For CHECKOUT payment method, it retrieves the payment number from the `udf4` field. * For JUSPAY payment method, it retrieves the payment number from the `udf6` field. * @param {number} methodId - The ID of the payment method. * @param {any} data - The data object containing the payment number information. * @param {any} extras - Optional additional data containing configuration or environment information. * @returns {string} The payment number corresponding to the payment method. */ const getPaymentNo = (methodId, data, extras) => { var _a, _b; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return (_a = data === null || data === void 0 ? void 0 : data.metadata) === null || _a === void 0 ? void 0 : _a.udf4.split('-')[1]; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (_b = data === null || data === void 0 ? void 0 : data.udf6) === null || _b === void 0 ? void 0 : _b.split('-')[1]; } else if (methodId == constants_1.PaymentMethodTypeId.OMNICAPITAL) { return data === null || data === void 0 ? void 0 : data.paymentNo; } return constants_2.Defaults.String.Value; }; exports.getPaymentNo = getPaymentNo; /** * Retrieves the authorization code from the provided data. * * This function extracts the authorization code based on the payment method ID. * For the CHECKOUT method, it returns the transaction ID. * For the PAYPAL method, it returns the capture ID from the purchase unit. * For the JUSPAY method, it returns the transaction ID. * * @param {number} methodId - The id of the payment method. * @param {any} data - The data object containing the authorization code information. * @returns {string} The authorization code corresponding to the payment method. * Returns a default string value if no relevant information is found. */ const getAuthCode = (methodId, data) => { var _a, _b, _c; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return data === null || data === void 0 ? void 0 : data.id; } else if (methodId == constants_1.PaymentMethodTypeId.PAYPAL) { //return data?.id; return ((_c = (_b = (_a = data === null || data === void 0 ? void 0 : data.purchase_units) === null || _a === void 0 ? void 0 : _a[0].payments) === null || _b === void 0 ? void 0 : _b.captures) === null || _c === void 0 ? void 0 : _c[0].id) || constants_2.Defaults.String.Value; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return data === null || data === void 0 ? void 0 : data.txn_id; } return constants_2.Defaults.String.Value; }; exports.getAuthCode = getAuthCode; /** * Generates a signature string necessary for payment verification. * * This function constructs a signature based on the payment method ID. * For CHECKOUT, it includes various transaction identifiers in the signature. * For PAYPAL, it returns a JSON string containing token, order ID, payer ID, and gateway. * For JUSPAY, it retrieves the EPG transaction ID from the payment gateway response. * * @param {number} methodId - The ID of the payment method. * @param {any} data - The data object containing information related to the payment. * @param {any} hookData - The hook data object containing additional resource information. * @returns {string} The signature string specific to the payment method. * Returns a default string value if no relevant information is found. */ const getSignature = (methodId, data, hookData) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return `scheme_id=${(data === null || data === void 0 ? void 0 : data.scheme_id) || constants_2.Defaults.String.Value}&acquirer_transaction_id=${((_a = data === null || data === void 0 ? void 0 : data.processing) === null || _a === void 0 ? void 0 : _a.acquirer_transaction_id) || constants_2.Defaults.String.Value}&retrieval_reference_number=${((_b = data === null || data === void 0 ? void 0 : data.processing) === null || _b === void 0 ? void 0 : _b.retrieval_reference_number) || constants_2.Defaults.String.Value}&merchant_category_code=${((_c = data === null || data === void 0 ? void 0 : data.processing) === null || _c === void 0 ? void 0 : _c.merchant_category_code) || constants_2.Defaults.String.Value}&scheme_merchant_id=${((_d = data === null || data === void 0 ? void 0 : data.processing) === null || _d === void 0 ? void 0 : _d.scheme_merchant_id) || constants_2.Defaults.String.Value}`; } else if (methodId == constants_1.PaymentMethodTypeId.PAYPAL) { return JSON.stringify({ token: `${(_e = hookData === null || hookData === void 0 ? void 0 : hookData.resource) === null || _e === void 0 ? void 0 : _e.id}`, orderId: `${data === null || data === void 0 ? void 0 : data.id}`, payerId: `${(_f = data === null || data === void 0 ? void 0 : data.payer) === null || _f === void 0 ? void 0 : _f.payerId}`, gateway: `${constants_1.PaymentMethodTypeId.PAYPAL}`, isCancelled: false }); } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (_j = (_h = (_g = data === null || data === void 0 ? void 0 : data.content) === null || _g === void 0 ? void 0 : _g.txn) === null || _h === void 0 ? void 0 : _h.payment_gateway_response) === null || _j === void 0 ? void 0 : _j.epg_txn_id; } return constants_2.Defaults.String.Value; }; exports.getSignature = getSignature; /** * Retrieves the PSP (Payment Service Provider) response message from the provided data. * * This function constructs a response message based on the payment method ID. * For the CHECKOUT method, it includes the transaction ID and status in the message. * For the PAYPAL method, it includes only the transaction ID in the message. * For the JUSPAY method, it retrieves the status from the transaction content. * * @param {number} methodId - The ID of the payment method. * @param {any} data - The data object containing information related to the payment. * @returns {string} The PSP response message corresponding to the payment method. * Returns a default string value if no relevant information is found. */ const getPSPResponseMsg = (methodId, data) => { var _a, _b; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return `id=${(data === null || data === void 0 ? void 0 : data.id) || constants_2.Defaults.String.Value}&status=${(data === null || data === void 0 ? void 0 : data.status) || constants_2.Defaults.String.Value}`; } else if (methodId == constants_1.PaymentMethodTypeId.PAYPAL) { return `id=${(data === null || data === void 0 ? void 0 : data.id) || constants_2.Defaults.String.Value}`; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (_b = (_a = data === null || data === void 0 ? void 0 : data.content) === null || _a === void 0 ? void 0 : _a.txn) === null || _b === void 0 ? void 0 : _b.status; } return constants_2.Defaults.String.Value; }; exports.getPSPResponseMsg = getPSPResponseMsg; /** * Retrieves a boolean indicating whether the PSP info should be saved. * @param {number} methodId - The id of the payment method. * @param {any} data - The data object containing the PSP info. * @returns {boolean} A boolean indicating whether the PSP info should be saved. */ const getIsSavePSPInfo = (methodId, data) => { var _a; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return true; } else if (methodId == constants_1.PaymentMethodTypeId.PAYPAL) { return false; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { const payGatewayId = data === null || data === void 0 ? void 0 : data.udf4; if (payGatewayId) { return (_a = (0, parse_util_1.stringToBoolean)(payGatewayId.split('_')[1])) !== null && _a !== void 0 ? _a : false; } } return false; }; exports.getIsSavePSPInfo = getIsSavePSPInfo; /** * Retrieves the PSP (Payment Service Provider) information from the provided data. * * This function extracts PSP-specific information based on the payment method ID. * For the CHECKOUT method, it returns a fixed value indicating 3D Secure (3ds). * For the JUSPAY method, it attempts to extract the PSP info from the `udf7` field. * * @param {number} methodId - The ID of the payment method. * @param {any} data - The data object containing information related to the payment. * @returns {string} The PSP information corresponding to the payment method. * Returns a default string value if no relevant information is found. */ const getPSPInfo = (methodId, data) => { var _a; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return `3ds`; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { const payGatewayId = data === null || data === void 0 ? void 0 : data.udf4; if (payGatewayId) { return (data === null || data === void 0 ? void 0 : data.udf7) ? (_a = data === null || data === void 0 ? void 0 : data.udf7) === null || _a === void 0 ? void 0 : _a.split('_')[1] : constants_2.Defaults.String.Value; } } return constants_2.Defaults.String.Value; }; exports.getPSPInfo = getPSPInfo; /** * Retrieves the payment identifier from the provided data. * @param {number} methodId - The id of the payment method. * @param {any} data - The data object containing the payment identifier information. * @returns {string} The payment identifier. */ const getPaymentIdentifier = (methodId, data) => { var _a; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return `3ds`; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (data === null || data === void 0 ? void 0 : data.udf7) ? (_a = data === null || data === void 0 ? void 0 : data.udf7) === null || _a === void 0 ? void 0 : _a.split('_')[0] : constants_2.Defaults.String.Value; } return constants_2.Defaults.String.Value; }; exports.getPaymentIdentifier = getPaymentIdentifier; /** * Retrieves the PSP gateway information from the provided data. * @param {number} methodId - The id of the payment method. * @param {any} data - The data object containing the PSP gateway information. * @returns {string} The PSP gateway information. */ const getPSPGatewayInfo = (methodId, data) => { var _a; if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (_a = data === null || data === void 0 ? void 0 : data.txn_detail) === null || _a === void 0 ? void 0 : _a.gateway; } else if (methodId == constants_1.PaymentMethodTypeId.PAYPAL) { return constants_1.PaymentMethodType.PAYPAL; } return constants_2.Defaults.String.Value; }; exports.getPSPGatewayInfo = getPSPGatewayInfo; /** * Retrieves the card type from the provided data. * @param {number} methodId - The id of the payment method. * @param {any} data - The data object containing the card type information. * @returns {string} The card type name. */ const getCardType = (methodId, data) => { var _a, _b; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return (_a = data === null || data === void 0 ? void 0 : data.source) === null || _a === void 0 ? void 0 : _a.card_type; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (0, parse_util_1.matchStrings)(data === null || data === void 0 ? void 0 : data.udf5, 'card', true) ? (_b = data === null || data === void 0 ? void 0 : data.card) === null || _b === void 0 ? void 0 : _b.card_type : null; } return constants_2.Defaults.String.Value; }; exports.getCardType = getCardType; /** * Retrieves the card issuer from the provided data. * @param {number} methodId - The id of the payment method. * @param {any} data - The data object containing the card issuer information. * @returns {string} The card issuer name. */ const getCardIssuer = (methodId, data) => { var _a, _b; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return (_a = data === null || data === void 0 ? void 0 : data.source) === null || _a === void 0 ? void 0 : _a.card_category; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (0, parse_util_1.matchStrings)(data === null || data === void 0 ? void 0 : data.udf5, 'card', true) ? (_b = data === null || data === void 0 ? void 0 : data.card) === null || _b === void 0 ? void 0 : _b.card_issuer : null; } return constants_2.Defaults.String.Value; }; exports.getCardIssuer = getCardIssuer; /** * Retrieves the card brand from the provided data. * @param {number} methodId - The id of the payment method. * @param {any} data - The data object containing the card brand information. * @returns {string} The card brand name. */ const getCardBrand = (methodId, data) => { var _a, _b; if (methodId == constants_1.PaymentMethodTypeId.CHECKOUT) { return (_a = data === null || data === void 0 ? void 0 : data.source) === null || _a === void 0 ? void 0 : _a.product_type; } else if (methodId == constants_1.PaymentMethodTypeId.JUSPAY) { return (0, parse_util_1.matchStrings)(data === null || data === void 0 ? void 0 : data.udf5, 'card', true) ? (_b = data === null || data === void 0 ? void 0 : data.card) === null || _b === void 0 ? void 0 : _b.card_brand : null; } return constants_2.Defaults.String.Value; }; exports.getCardBrand = getCardBrand; /** * This function takes an input string that contains the order and basket IDs * and extracts them. The IDs are returned as a comma-separated string. * @param {string} input - Input string containing the order and basket IDs. * @returns {string} A comma-separated string containing the order and basket IDs. */ const parseOrderId = (input) => { const matches = (0, parse_util_1.groupMatch)(input === null || input === void 0 ? void 0 : input.trim(), new RegExp(constants_2.RegularExpression.ORDER_BASKET_ID_MATCH)); console.log('--- matches ---', JSON.stringify(matches)); if ((matches === null || matches === void 0 ? void 0 : matches.length) >= 3) { return `${matches[1]},${matches[3]}`; } return constants_2.Defaults.String.Value; };