UNPKG

trustpayway-sdk

Version:

Official Node.js SDK for interacting with TrustPayWay API (MTN, Orange, etc.)

84 lines (83 loc) 2.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TrustPayWay = void 0; const axios_1 = __importDefault(require("axios")); class TrustPayWay { constructor(config) { this.sessionToken = ""; this.baseUrl = config.baseUrl; this.appToken = config.appToken; this.authToken = config.authToken; this.network = config.network; this.http = axios_1.default.create({ baseURL: this.baseUrl, headers: { 'Content-Type': 'application/json' }, }); } async getToken() { var _a; try { const response = await this.http.post('/api/login', { applicationId: this.appToken }, { headers: { Authorization: `Bearer ${this.authToken}`, }, }); this.sessionToken = response.data.access_token; return this.sessionToken; } catch (err) { console.error('Token Error:', ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) || err.message); throw err; } } async processPayment(params) { var _a, _b; if (!this.sessionToken) { await this.getToken(); } const { amount, currency, phone, description, orderId, notifUrl } = params; try { const response = await this.http.post(`/api/${this.network}/process-payment`, { amount, currency, subscriberMsisdn: phone, description, orderId, notifUrl, }, { headers: { Authorization: `Bearer ${this.sessionToken}`, }, }); return response.data; } catch (err) { console.error('Process Payment Error:', ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) || err.message); return (_b = err.response) === null || _b === void 0 ? void 0 : _b.data; } } async checkPaymentStatus(params) { var _a; if (!this.sessionToken) { await this.getToken(); } const { transactionId } = params; try { const url = `/api/${this.network}/get-status/${transactionId}?auth_token=${this.authToken}`; const response = await this.http.get(url, { headers: { Authorization: `Bearer ${this.sessionToken}`, }, }); return response.data; } catch (err) { console.error('Check Status Error:', ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) || err.message); throw err; } } } exports.TrustPayWay = TrustPayWay;