UNPKG

envio-comprobantes-sri

Version:

Envia comprobantes electronicos al SRI (Ecuador). Recibe un objeto JSON. Lo convierte a XML, lo firma, lo envía al servicio web del SRI y devuelve la respuesta.

51 lines (50 loc) 2.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnvioAKARecepcion_SRI = void 0; const axios_1 = __importDefault(require("axios")); const TablasSRI_1 = require("./Structures/Utils/TablasSRI"); const urls_1 = __importDefault(require("./urls")); // import fs from "fs"; // import path from "path"; const parseWebServiceResponse_1 = __importDefault(require("./Utils/parseWebServiceResponse")); async function EnvioAKARecepcion_SRI(firmado_en_xml, signal, ambiente) { var factura_firmada = Buffer.from(firmado_en_xml, "utf-8").toString("base64"); const xml_envelope = ` <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ec="http://ec.gob.sri.ws.recepcion"> <soapenv:Header/> <soapenv:Body> <ec:validarComprobante> <xml>${factura_firmada}</xml> </ec:validarComprobante> </soapenv:Body> </soapenv:Envelope>`.replace(/\n/g, "").replace(/\t/g, "").replace(/ /g, ""); const endpoint = ambiente === TablasSRI_1.TablaAmbiente.PRODUCCION ? urls_1.default.ENVIO_AKA_RECEPCION_PRODUCCION : urls_1.default.ENVIO_AKA_RECEPCION_PRUEBAS; const response = await axios_1.default.post(endpoint, xml_envelope, { headers: { "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br" }, method: "POST", signal }); if (response.headers.get("Content-Type").indexOf("text/xml") < 0) { throw new Error("The https request response with a non-xml value"); } const xml = (0, parseWebServiceResponse_1.default)(response.data, "Subir_XML", xml_envelope); return { estado: xml.getTextValue("estado"), xml_sent: xml_envelope, xml_received: response.data }; } exports.EnvioAKARecepcion_SRI = EnvioAKARecepcion_SRI; /*function snapshot(xml: string, data: Object) { const dir_path = path.join(process.cwd(), "test-outputs", new Date().valueOf().toString()); fs.mkdirSync(dir_path); fs.writeFileSync(path.join(dir_path, "upload.xml"), xml, "binary"); fs.writeFileSync(path.join(dir_path, "response.json"), JSON.stringify(data), "binary"); }*/