UNPKG

@tyagoveras/node-mde

Version:

Biblioteca para consultar notas destinadas e enviar evento de manifestação do destinatário

64 lines (62 loc) 2.84 kB
let { CA, DISTRIBUICAO_MDFE } = require("../env"), DistribuicaoMdfeSchema = require("../schemas").DistribuicaoMdfeSchema, SefazService = require("../services/sefaz-service"), { Gzip, Xml } = require("../util"); class DistribuicaoMdfeHelper { static async enviarConsulta(e, s) { var t = DISTRIBUICAO_MDFE[s.tpAmb], e = { method: "POST", data: e }, a = { ...s.requestOptions || {}, headers: { ...s.requestOptions && s.requestOptions.headers, "Content-Type": 'application/soap+xml; charset=utf-8; action="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeDistribuicaoDFe/mdfeDistDFeInteresse"' } }; return await new SefazService({ baseURL: t, ca: CA, cert: s.cert, key: s.key, tpAmb: s.tpAmb, requestOptions: a, httpsOptions: s.httpsOptions }).request(e); } static montarRequest(e) { var s = DistribuicaoMdfeSchema.montarSchema(e), t = Xml.jsonToXml(s), s = s.mdfeDistDFeInteresse.mdfeDadosMsg.distDFeInt["@_versao"] || "1.00"; return '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">' + `<soap12:Header>${`<mdfeCabecMsg xmlns="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeDistribuicaoDFe"><cUF>${e.cUFAutor}</cUF><versaoDados>${s}</versaoDados></mdfeCabecMsg>`}</soap12:Header>` + `<soap12:Body>${t}</soap12:Body>` + "</soap12:Envelope>"; } static async montarResponse(e) { var s = {}, e = Xml.xmlToJson(e), { "soap:Envelope": { "soap:Body": { mdfeDistDFeInteresseResponse: { mdfeDistDFeInteresseResult: { retDistDFeInt: e = {} } = {} } = {} } = {} } = {} } = (e.error && (s.error = e.error), e), { loteDistDFeInt: t = {} } = e, t = (t.docZip ? Array.isArray(t.docZip) || (t.docZip = [ t.docZip ]) : t.docZip = [], await Promise.all(t.docZip.map(async e => { var s = await Gzip.unzip(e.value); return { xml: s, json: Xml.xmlToJson(s), nsu: e["@_NSU"], schema: e["@_schema"] }; }))); return s.tpAmb = e.tpAmb || "", s.verAplic = e.verAplic || "", s.cStat = e.cStat || "", s.xMotivo = e.xMotivo || "", s.dhResp = e.dhResp || "", s.ultNSU = e.ultNSU || "", s.maxNSU = e.maxNSU || "", s.docZip = t, s; } } module.exports = Object.freeze(DistribuicaoMdfeHelper);