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.

121 lines (120 loc) 6.91 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initiateDirectApiTransaction = initiateDirectApiTransaction; exports.sendCardholderData = sendCardholderData; exports.checkDirectApiStatus = checkDirectApiStatus; // src/directApi.ts const axios_1 = __importDefault(require("axios")); const utils_1 = require("./utils"); const TRANSACTION_INIT_ENDPOINT = "/transaction"; // Send a POST request to /transaction to initiate a transaction. [cite: 30] /** * Initiates a transaction for Direct API Integration. * This is the first step where you get pSign and transactionParams. * @param baseUrl The base URL for the Nass Payment Gateway API (Portal part, for initial transaction request). * @param accessToken The access token obtained from authentication. * @param transactionDetails Details of the transaction to initiate. * @returns A Promise that resolves to the initial transaction response. The response will contain pSign and transactionParams (including nonce and timestamp), which are required for Direct API Integration. [cite: 32] * @throws {Error} If the initiation fails. */ function initiateDirectApiTransaction(baseUrl, accessToken, transactionDetails) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield axios_1.default.post(`${baseUrl}${TRANSACTION_INIT_ENDPOINT}`, transactionDetails, { headers: { Authorization: `Bearer ${accessToken}`, // The access token must be included in the Authorization header [cite: 29] }, }); // The request body must include details such as orderId, amount, currency, and transactionType. [cite: 31] return response.data; } catch (error) { throw (0, utils_1.handleAxiosError)(error); } }); } /** * Sends cardholder data for authorization in Direct API Integration. * After receiving the response from /transaction, the merchant system must send a POST request to the transaction processing URL (tURL). [cite: 33] * @param transactionProcessingUrl The direct transaction processing URL (tURL). * @param pSign The signature from the initial transaction response. [cite: 34] * @param nonce A unique identifier for the transaction. [cite: 34] * @param cardDetails Cardholder details (PAN, expiry date, CVV). [cite: 34] * @param orderDetails Order Number, Amount and all other parameters from the initial request. [cite: 35] * @returns A Promise that resolves to the authorization response (likely an HTML string for redirection). * @throws {Error} If sending cardholder data fails. */ function sendCardholderData(transactionProcessingUrl, 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* () { // The system will send back an html [cite: 37] const requestBody = { card: cardDetails.pan, // "card": "4761349999000047" [cite: 36] expMonth: cardDetails.expMonth, // "expMonth": "12" [cite: 36] expYear: cardDetails.expYear, // "expYear": "30" [cite: 36] currency: orderDetails.currency, // "currency": "368" [cite: 36] name: cardDetails.cardholderName, // "name": "John Doe" [cite: 36] trtype: orderDetails.transactionType, // "trtype": "1" [cite: 36] terminal: orderDetails.terminalId, // "terminal": "00053402" [cite: 36] desc: orderDetails.orderDesc, // "desc": "NOKIA 3310" [cite: 36] cvc2_rc: cardDetails.cvc2ReasonCode, // "cvc2_rc": "0" [cite: 36] orderId: orderDetails.orderId, // "orderId": "000009" [cite: 36] timestamp: orderDetails.timestamp, // "timestamp": "20250302101621" [cite: 36] nonce: nonce, // "nonce": "a6ddb5e5ab7908ff9b347382cd07a3d3" [cite: 36] p_sign: pSign, // "p_sign": "9083..." [cite: 36] amount: orderDetails.amount, // "amount": "23000" [cite: 36] cvc2: cardDetails.cvc2, // "cvc2": "356" [cite: 36] backRefUrl: orderDetails.backRefUrl, // "backRefUrl": "https://www.sample.com/shop/reply" [cite: 36] }; try { // The system will send back an html which if it's rendered it will directly redirect back to the merchant website [cite: 37] const response = yield axios_1.default.post(transactionProcessingUrl, requestBody, { headers: { "Content-Type": "application/json", }, }); return response.data; // This is the HTML response for redirection } catch (error) { throw (0, utils_1.handleAxiosError)(error); } }); } /** * 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 baseUrl The base URL for the Nass Payment Gateway API (Portal part, for status check). * @param accessToken The access token obtained from authentication. * @param orderId The unique order identifier. * @returns A Promise that resolves to the transaction status response. * @throws {Error} If checking the status fails. */ function checkDirectApiStatus(baseUrl, accessToken, orderId) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield axios_1.default.get(`${baseUrl}/transaction/${orderId}/checkStatus`, { // GET ${baseUrl}/transaction/${Orderld}/checkStatus [cite: 44] headers: { Authorization: `Bearer ${accessToken}`, // The access token must be included in the Authorization header [cite: 29] }, }); return response.data; } catch (error) { throw (0, utils_1.handleAxiosError)(error); } }); }