UNPKG

nasspay

Version:

TypeScript/JavaScript SDK for integrating with the Nass Merchant Payment Gateway. Supports both Portal and Direct API integration methods with comprehensive type definitions.

168 lines (167 loc) 9.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UAT_BASE_URL_DIRECT_API_TRANSACTION_PROCESSING = exports.UAT_BASE_URL_PORTAL = void 0; // src/index.ts const auth_1 = require("./auth"); const portal_1 = require("./portal"); const directApi_1 = require("./directApi"); const utils_1 = require("./utils"); class NassPaymentGateway { constructor(config) { this.accessToken = null; this.username = config.username; this.password = config.password; this.environment = config.environment || "UAT"; // 'UAT' or 'PRODUCTION' // Determine base URLs based on environment if (this.environment === "UAT") { this.portalBaseUrl = config.portalBaseUrl || utils_1.UAT_BASE_URL_PORTAL; // UAT Environment: https://uat-gateway.nass.iq:9746/ [cite: 7] this.directApiTransactionUrl = config.directApiTransactionUrl || utils_1.UAT_BASE_URL_DIRECT_API_TRANSACTION_PROCESSING; // Transaction Processing URL (tURL): https://3dsecure.nass.iq/cgi-bin/cgi_json [cite: 9] } else { // Production URL will be provided by your account manager or the support team. [cite: 7] // You must provide these in the config for production if (!config.portalBaseUrl || !config.directApiTransactionUrl) { throw new Error("For PRODUCTION environment, portalBaseUrl and directApiTransactionUrl must be provided in the config."); } this.portalBaseUrl = config.portalBaseUrl; this.directApiTransactionUrl = config.directApiTransactionUrl; } } /** * Authenticates the merchant with the Nass Payment Gateway. * Upon successful authentication, the API returns an access token, which must be included in the Authorization header of all subsequent requests. [cite: 29] * @returns A Promise that resolves to the authentication response containing the access token. */ authenticate() { return __awaiter(this, void 0, void 0, function* () { const authResponse = yield (0, auth_1.authenticate)(this.portalBaseUrl, { username: this.username, password: this.password, }); this.accessToken = authResponse.access_token; // Upon successful authentication, the API returns an access token [cite: 29] return authResponse; }); } // --- Nass Portal Integration Methods --- /** * Initiates a new payment transaction using the Nass Payment Gateway Portal method. * Once authenticated, merchants can initiate transactions. [cite: 11] * @param orderId Unique order identifier generated by the merchant. [cite: 12] * @param orderDesc Order description. [cite: 12] * @param amount Order total amount. [cite: 12] * @param currency Order currency (3-character ISO currency code). [cite: 12] * @param transactionType Transaction type ('1' for sale, etc.). [cite: 12] * @param backRef Frontend Redirect URL. [cite: 12] * @param notifyUrl HTTP Backend URL for sending POS request callbacks. [cite: 12] * @returns A Promise that resolves to the transaction response, including a URL to redirect the customer to complete the payment. * @throws {Error} If not authenticated or if the transaction initiation fails. */ createPortalTransaction(orderId, orderDesc, amount, currency, transactionType, backRef, notifyUrl) { return __awaiter(this, void 0, void 0, function* () { if (!this.accessToken) { throw new Error("Not authenticated. Call authenticate() first."); } const transactionDetails = { orderId, orderDesc, amount, currency, transactionType, backRef, notifyUrl, }; return (0, portal_1.initiatePortalTransaction)(this.portalBaseUrl, this.accessToken, transactionDetails); }); } /** * Checks the status of a transaction initiated via the Nass Portal method. * Merchants can check the status of a transaction within 24 hours of its initiation. [cite: 23] * @param orderId The unique order identifier of the transaction to check. * @returns A Promise that resolves to the transaction status response. * @throws {Error} If not authenticated or if checking the status fails. */ checkPortalTransactionStatus(orderId) { return __awaiter(this, void 0, void 0, function* () { if (!this.accessToken) { throw new Error("Not authenticated. Call authenticate() first."); } return (0, portal_1.checkPortalStatus)(this.portalBaseUrl, this.accessToken, orderId); }); } // --- Direct API Integration Methods --- /** * Initiates the first step of a Direct API transaction. * Sends a POST request to /transaction to initiate a transaction. [cite: 30] * @param orderId Unique order identifier generated by the merchant. [cite: 31] * @param orderDesc Order description. * @param amount Order total amount. [cite: 31] * @param currency Order currency (3-character ISO currency code). [cite: 31] * @param transactionType Transaction type ('1' for sale, etc.). [cite: 31] * @returns A Promise that resolves to the initial transaction response containing `pSign` and `transactionParams` (including `nonce` and `timestamp`), which are required for the next step of Direct API Integration. [cite: 32] * @throws {Error} If not authenticated or if the initiation fails. */ createDirectApiTransaction(orderId, orderDesc, amount, currency, transactionType) { return __awaiter(this, void 0, void 0, function* () { if (!this.accessToken) { throw new Error("Not authenticated. Call authenticate() first."); } const transactionDetails = { orderId, orderDesc, amount, currency, transactionType, }; return (0, directApi_1.initiateDirectApiTransaction)(this.portalBaseUrl, this.accessToken, transactionDetails); }); } /** * Sends cardholder data directly to the payment gateway for authorization (second step of Direct API). * This method requires merchants to securely transmit cardholder data. [cite: 27] * @param pSign The `pSign` received from the `createDirectApiTransaction` response. [cite: 34] * @param nonce The `nonce` received from the `createDirectApiTransaction` response's `transactionParams`. [cite: 34] * @param cardDetails Object containing card number (PAN), expiration month/year, CVV, and optional cardholder name and CVC2 reason code. [cite: 34, 46] * @param orderDetails Object containing order number, amount (in minor units), currency, transaction type, terminal ID, order description, timestamp, and back reference URL. [cite: 35, 46] * @returns A Promise that resolves to an HTML string which, if rendered, will redirect the user back to the merchant's website. [cite: 37] * @throws {Error} If sending cardholder data fails. */ sendDirectApiCardholderData(pSign, // pSign: The signature from the initial transaction response [cite: 34] nonce, // NONCE: A unique identifier for the transaction [cite: 34] cardDetails, // Cardholder details (PAN, expiry date, CVV). [cite: 34] orderDetails) { return __awaiter(this, void 0, void 0, function* () { return (0, directApi_1.sendCardholderData)(this.directApiTransactionUrl, pSign, nonce, cardDetails, orderDetails); }); } /** * Checks the status of a transaction initiated via Direct API Integration. * Merchants can check the status of a transaction within 24 hours of initiation. [cite: 44] * @param orderId The unique order identifier of the transaction to check. * @returns A Promise that resolves to the transaction status response. * @throws {Error} If not authenticated or if checking the status fails. */ checkDirectApiTransactionStatus(orderId) { return __awaiter(this, void 0, void 0, function* () { if (!this.accessToken) { throw new Error("Not authenticated. Call authenticate() first."); } return (0, directApi_1.checkDirectApiStatus)(this.portalBaseUrl, this.accessToken, orderId); }); } } exports.default = NassPaymentGateway; // Export utility constants var utils_2 = require("./utils"); Object.defineProperty(exports, "UAT_BASE_URL_PORTAL", { enumerable: true, get: function () { return utils_2.UAT_BASE_URL_PORTAL; } }); Object.defineProperty(exports, "UAT_BASE_URL_DIRECT_API_TRANSACTION_PROCESSING", { enumerable: true, get: function () { return utils_2.UAT_BASE_URL_DIRECT_API_TRANSACTION_PROCESSING; } });