UNPKG

payme-sdk

Version:

A Node.js SDK to integrate with PAYME API

136 lines (135 loc) 5.18 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExternalDataService = void 0; const axios_1 = __importDefault(require("axios")); const dotenv = __importStar(require("dotenv")); const https = __importStar(require("https")); class ExternalDataService { constructor() { this.url_payment = process.env.BASE_URL_PAYMENT; this.url_onboard = process.env.BASE_URL_ONBOARDING; this.url_billing = process.env.BASE_URL_BILLING; this.httpsAgent = new https.Agent({ rejectUnauthorized: false, // (NOTE: this will disable client verification) }); this.api_billing = axios_1.default.create({ baseURL: this.url_billing, httpsAgent: this.httpsAgent, headers: { 'Content-Type': 'application/json', }, }); this.api_onboarding = axios_1.default.create({ baseURL: this.url_onboard, httpsAgent: this.httpsAgent, headers: { 'Content-Type': 'application/json', }, }); this.api_payment = axios_1.default.create({ baseURL: this.url_payment, httpsAgent: this.httpsAgent, headers: { 'Content-Type': 'application/json', }, }); dotenv.config({ path: '.env' }); this.token = ''; } getToken() { return this.token; } setToken(token) { this.token = token; } async getDataFromBilling(path) { try { console.log('token', this.token); this.api_billing.defaults.headers.common['Authorization'] = `Bearer ${this.token}`; const response = await this.api_billing.get(path); return response.data; } catch (error) { console.log('Failed to fetch data from Onboarding', error); } } async postDataFromOnboarding(path, body) { try { this.api_onboarding.defaults.headers.common['Authorization'] = `Bearer ${this.token}`; console.log('body', body); const response = await this.api_onboarding.post(path, body, { headers: { Authorization: `Bearer ${this.token}` }, }); const data = response.data; console.log(`Successfully fetched and cached data from Onboarding ${path}`); return data; } catch (error) { console.log('Failed to fetch data from Onboarding', error); } } async getDataFromOnboarding(path) { try { this.api_onboarding.defaults.headers.common['Authorization'] = `Bearer ${this.token}`; const response = await this.api_onboarding.get(path); const data = response.data; return data; } catch (error) { console.log('Failed to fetch data from Onboarding', error); } } async getDataFromPayment(path) { try { this.api_payment.defaults.headers.common['Authorization'] = `Bearer ${this.token}`; const response = await this.api_payment.get(path); const data = response.data; return data; } catch (error) { console.log('Failed to fetch data from Onboarding', error); } } async postDataFromPayment(path, body) { try { this.api_payment.defaults.headers.common['Authorization'] = `Bearer ${this.token}`; console.log('body', body); const response = await this.api_payment.post(path, body, { headers: { Authorization: `Bearer ${this.token}` }, }); const data = response.data; console.log(`Successfully post data from Payment ${path}`); return data; } catch (error) { console.log('Failed to fetch data from Onboarding', error); } } } exports.ExternalDataService = ExternalDataService;