UNPKG

@dueheads/citi-bank-boleto-sdk

Version:

SDK para registro de boletos na API do Citibank via mTLS.

84 lines (83 loc) 3.44 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __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.ApiRequest = void 0; const axios_1 = __importDefault(require("axios")); const https = __importStar(require("https")); const fs = __importStar(require("fs")); class ApiRequest { constructor(config) { this.config = config; this.baseURL = config.baseURL || 'https://citigroupsoauat.citigroup.com/comercioeletronico/registerboleto/RegisterBoletoSOAP'; // Criação do agente HTTPS com os certificados para mTLS this.httpsAgent = new https.Agent({ cert: fs.readFileSync(this.config.certPath), key: fs.readFileSync(this.config.keyPath), rejectUnauthorized: this.config.rejectUnauthorized ?? true, }); } /** * Envia a requisição POST com o payload XML para a API do Citibank. * @param xmlPayload - A string XML gerada pelo XMLBuilder. */ async post(xmlPayload) { const axiosConfig = { method: 'post', url: this.baseURL, headers: { 'Content-Type': 'application/xml' }, httpsAgent: this.httpsAgent, timeout: this.config.timeout || 30000, data: xmlPayload, }; try { const response = await axios_1.default.request(axiosConfig); return response.data; } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { // Se o erro for um HTTP 500, a API retorna um XML de SOAP Fault. [cite: 29] // Retornamos esse body para o ResponseHandler tratar. error.message = `API Error: Status ${error.response.status} - Body: ${error.response.data}`; return error.response.data; } // Para outros erros (timeout, DNS, etc.), relançamos a exceção. throw error; } } } exports.ApiRequest = ApiRequest;