UNPKG

@curiosity/niubiz

Version:
157 lines (140 loc) 5.12 kB
const request = require('request'); const { NiubizExceptionSecurityToken, NiubizExceptionComunicationSession, NiubizExceptionAuthorization, } = require('./errors'); /** * VisaNet */ class Niubiz { /** * VisaNet object * @param {object} config */ constructor(config) { config = config || {}; this.user = config.user || process.env.NIUBIZ_USER; this.password = config.password || process.env.NIUBIZ_PASSWORD; this.merchantId = config.merchantId || process.env.NIUBIZ_MERCHANT_ID; // defaults const env = config.env || process.env.NIUBIZ_ENV || 'production'; const apiUrlProd = process.env.NIUBIZ_API_URL_PROD || 'https://apiprod.vnforapps.com'; const apiUrlDev = process.env.NIUBIZ_API_URL_DEV || 'https://apitestenv.vnforapps.com'; this.apiUrl = env === 'production' ? apiUrlProd : apiUrlDev; const jsUrlDev = config.jsUrlDev || process.env.NIUBIZ_JS_URL_DEV ||'https://static-content-qas.vnforapps.com/v2/js/checkout.js?qa=true' const jsUrlProd = config.jsUrlProd || process.env.NIUBIZ_JS_URL_PROD ||'https://static-content.vnforapps.com/v2/js/checkout.js' this.jsUrl = env === 'production' ? jsUrlProd : jsUrlDev; this.channel = config.channel || process.env.NIUBIZ_MERCHANT_ID || 'web'; this.currency = config.currency || process.env.NIUBIZ_DEFAULT_CURRENCY || 'PEN'; } /** * Crea el token de seguridad * @returns Promise<string> */ createToken() { return new Promise( (resolve, reject) => { const credentials = Buffer .from(`${this.user}:${this.password}`) .toString('base64'); request({ headers: { 'Authorization': `Basic ${credentials}` }, url: `${this.apiUrl}/api.security/v1/security`, method: 'POST' }, function (err, res, body) { if (!err && res.statusCode == 201) { resolve(body); } else if (!err) { const data = JSON.parse(Buffer.from(body).toString()); reject( new NiubizExceptionSecurityToken('exception', res.statusCode, res.body, data) ); } else{ res = res || {}; reject( new NiubizExceptionSecurityToken('error', res.statusCode, res.body, err) ); } }); }); } /** * Crea la sesion de visanet (datos para el boton) * @param {string} securityToken * @param {object} data cuerpo de la peticion * @returns {Promise<{sessionKey: string, expirationTime: number}>} */ createSession(securityToken, data) { const jsonBody = JSON.stringify(data); console.log(`url is: ${this.apiUrl}/api.ecommerce/v2/ecommerce/token/session/${this.merchantId}`); console.log('body:', jsonBody); return new Promise( (resolve, reject) => { request({ headers: { 'Content-Type': 'application/json', 'Authorization': securityToken, }, encoding: null, body: jsonBody, url: `${this.apiUrl}/api.ecommerce/v2/ecommerce/token/session/${this.merchantId}`, method: 'POST', }, function (err, res, body) { if (!err && res.statusCode == 200) { resolve(JSON.parse(Buffer.from(body).toString())); } else if (!err) { const data = JSON.parse(Buffer.from(body).toString()); reject( new NiubizExceptionComunicationSession('exception', res.statusCode, res.body, data) ); } else{ res = res || {}; reject( new NiubizExceptionComunicationSession('error', res.statusCode, res.body, err) ); } }); }); } /** * * @param {string} securityToken * @param {object} data * @returns {Promise<{header: any, order: any, dataMap: any}>} */ getAuthorization(securityToken, data) { const jsonBody = JSON.stringify(data); return new Promise( (resolve, reject) => { request( { headers: { 'Content-Type': 'application/json', 'Authorization': securityToken }, body: jsonBody, url: `${this.apiUrl}/api.authorization/v3/authorization/ecommerce/${this.merchantId}`, method: 'POST' }, function (err, res, body) { if (!err && res.statusCode == 200) { resolve(JSON.parse(Buffer.from(body).toString())); } else if (!err) { const data = JSON.parse(Buffer.from(body).toString()); reject( new NiubizExceptionAuthorization('exception', res.statusCode, res.body, data) ); } else{ res = res || {}; reject( new NiubizExceptionAuthorization('error', res.statusCode, res.body, err) ); } } ); }); } } module.exports = Niubiz;