node-sped-nfe 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +2 -0
  2. package/dist/utils/eventos.d.ts +5 -1
  3. package/dist/utils/eventos.js +1459 -9
  4. package/dist/utils/extras.d.ts +2 -0
  5. package/dist/utils/extras.js +58 -0
  6. package/dist/utils/make.js +13 -123
  7. package/dist/utils/tools.d.ts +6 -3
  8. package/dist/utils/tools.js +103 -26
  9. package/exemplos/consulta.js +19 -0
  10. package/exemplos/nfce.js +165 -0
  11. package/{testes → exemplos}/nfe.js +11 -5
  12. package/exemplos/status.js +19 -0
  13. package/package.json +10 -6
  14. package/schemas/leiauteNFe_v4.00.xsd +5 -5
  15. package/src/utils/eventos.ts +1466 -11
  16. package/src/utils/extras.ts +59 -0
  17. package/src/utils/make.ts +13 -124
  18. package/src/utils/tools.ts +125 -33
  19. package/dist/tsconfig.tsbuildinfo +0 -1
  20. package/dist/utils/sefaz.d.ts +0 -1
  21. package/dist/utils/sefaz.js +0 -64
  22. package/dist/utils/xmllint.d.ts +0 -4
  23. package/dist/utils/xmllint.js +0 -234520
  24. package/saida.txt +0 -0
  25. package/src/utils/schemas/consReciNFe_v4.00.xsd +0 -9
  26. package/src/utils/schemas/consSitNFe_v4.00.xsd +0 -9
  27. package/src/utils/schemas/consStatServ_v4.00.xsd +0 -9
  28. package/src/utils/schemas/enviNFe_v4.00.xsd +0 -9
  29. package/src/utils/schemas/inutNFe_v4.00.xsd +0 -9
  30. package/src/utils/schemas/leiauteConsSitNFe_v4.00.xsd +0 -502
  31. package/src/utils/schemas/leiauteConsStatServ_v4.00.xsd +0 -98
  32. package/src/utils/schemas/leiauteInutNFe_v4.00.xsd +0 -193
  33. package/src/utils/schemas/leiauteNFe_v4.00.xsd +0 -7412
  34. package/src/utils/schemas/nfe_v4.00.xsd +0 -9
  35. package/src/utils/schemas/procInutNFe_v4.00.xsd +0 -9
  36. package/src/utils/schemas/procNFe_v4.00.xsd +0 -9
  37. package/src/utils/schemas/retConsReciNFe_v4.00.xsd +0 -9
  38. package/src/utils/schemas/retConsSitNFe_v4.00.xsd +0 -9
  39. package/src/utils/schemas/retConsStatServ_v4.00.xsd +0 -9
  40. package/src/utils/schemas/retEnviNFe_v4.00.xsd +0 -9
  41. package/src/utils/schemas/retInutNFe_v4.00.xsd +0 -9
  42. package/src/utils/schemas/tiposBasico_v4.00.xsd +0 -598
  43. package/src/utils/schemas/xmldsig-core-schema_v1.01.xsd +0 -98
  44. package/testes/assinar.js +0 -16
  45. package/testes/nfe.json +0 -194
  46. package/testes/nfe_teste.json +0 -45
@@ -0,0 +1,59 @@
1
+ const cUF2UF: any = {
2
+ "11": "RO",
3
+ "12": "AC",
4
+ "13": "AM",
5
+ "14": "RR",
6
+ "15": "PA",
7
+ "16": "AP",
8
+ "17": "TO",
9
+ "21": "MA",
10
+ "22": "PI",
11
+ "23": "CE",
12
+ "24": "RN",
13
+ "25": "PB",
14
+ "26": "PE",
15
+ "27": "AL",
16
+ "28": "SE",
17
+ "29": "BA",
18
+ "31": "MG",
19
+ "32": "ES",
20
+ "33": "RJ",
21
+ "35": "SP",
22
+ "41": "PR",
23
+ "42": "SC",
24
+ "43": "RS",
25
+ "50": "MS",
26
+ "51": "MT",
27
+ "52": "GO",
28
+ "53": "DF"
29
+ },
30
+ UF2cUF: any = {
31
+ "RO": "11",
32
+ "AC": "12",
33
+ "AM": "13",
34
+ "RR": "14",
35
+ "PA": "15",
36
+ "AP": "16",
37
+ "TO": "17",
38
+ "MA": "21",
39
+ "PI": "22",
40
+ "CE": "23",
41
+ "RN": "24",
42
+ "PB": "25",
43
+ "PE": "26",
44
+ "AL": "27",
45
+ "SE": "28",
46
+ "BA": "29",
47
+ "MG": "31",
48
+ "ES": "32",
49
+ "RJ": "33",
50
+ "SP": "35",
51
+ "PR": "41",
52
+ "SC": "42",
53
+ "RS": "43",
54
+ "MS": "50",
55
+ "MT": "51",
56
+ "GO": "52",
57
+ "DF": "53"
58
+ };
59
+ export { cUF2UF, UF2cUF }
package/src/utils/make.ts CHANGED
@@ -1,5 +1,7 @@
1
1
 
2
2
  import { XMLParser, XMLBuilder, XMLValidator } from "fast-xml-parser";
3
+ import { urlEventos } from "./eventos.js"
4
+ import { cUF2UF } from "./extras.js"
3
5
 
4
6
  //Classe da nota fiscal
5
7
  class Make {
@@ -128,6 +130,7 @@ class Make {
128
130
  tagProd(obj: any) {
129
131
  //Abrir tag de imposto
130
132
  for (let cont = 0; cont < obj.length; cont++) {
133
+
131
134
  if (obj[cont]['@nItem'] === undefined) {
132
135
  obj[cont] = { '@nItem': cont + 1, prod: obj[cont], imposto: {} };
133
136
  } else {
@@ -135,6 +138,9 @@ class Make {
135
138
  delete obj[cont].prod['@nItem'];
136
139
  }
137
140
 
141
+ //Primeiro item + NFCe + Homologação
142
+ if (cont == 0 && this.#NFe.infNFe.ide.mod == 65 && this.#NFe.infNFe.ide.tpAmb == 2) obj[cont].prod.xProd = "NOTA FISCAL EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL";
143
+
138
144
  obj[cont].prod.qCom = (obj[cont].prod.qCom * 1).toFixed(4)
139
145
  obj[cont].prod.vUnCom = (obj[cont].prod.vUnCom * 1).toFixed(10)
140
146
  obj[cont].prod.vProd = (obj[cont].prod.vProd * 1).toFixed(2)
@@ -634,11 +640,13 @@ class Make {
634
640
  if (this.#NFe.infNFe[`@Id`] == null) this.#NFe.infNFe[`@Id`] = `NFe${this.#gerarChaveNFe()}`;
635
641
 
636
642
  //Adicionar QrCode
637
- if (this.#NFe.infNFe.ide.mod == 55) {
638
- /*this.#NFe.infNFeSupl = {
639
- qrCode: "",
640
- urlChave: ""
641
- }*/
643
+ if (this.#NFe.infNFe.ide.mod == 65) {
644
+ //Como ja temos cUF, usamo dados do extras para convere em UF e achar os dados de url!
645
+ let tempUF = urlEventos(cUF2UF[this.#NFe.infNFe.ide.cUF], this.#NFe.infNFe['@versao']);
646
+ this.#NFe.infNFeSupl = {
647
+ qrCode: tempUF.mod65[this.#NFe.infNFe.ide.tpAmb == 1 ? 'producao' : 'homologacao'].NFeConsultaQR,
648
+ urlChave: tempUF.mod65[this.#NFe.infNFe.ide.tpAmb == 1 ? 'producao' : 'homologacao'].urlChave
649
+ }
642
650
  }
643
651
 
644
652
  let tempBuild = new XMLBuilder({
@@ -648,125 +656,6 @@ class Make {
648
656
  return tempBuild.build({ NFe: this.#NFe });
649
657
  }
650
658
 
651
- #getInfoQRCodeByUF(uf: any, amb: any) {
652
- if (this.#NFe.infNFe.ide.tpAmb) {
653
- switch (this.#NFe.infNFe.ide.cUF) {
654
- case 'AC':
655
- return { urlChave: 'www.sefaznet.ac.gov.br/nfce/consulta', urlQRCode: 'http://www.sefaznet.ac.gov.br/nfce/qrcode' };
656
- case 'AL':
657
- return { urlChave: 'www.sefaz.al.gov.br/nfce/consulta', urlQRCode: 'http://nfce.sefaz.al.gov.br/QRCode/consultarNFCe.jsp' };
658
- case 'AP':
659
- return { urlChave: 'www.sefaz.ap.gov.br/nfce/consulta', urlQRCode: 'https://www.sefaz.ap.gov.br/nfce/nfcep.php' };
660
- case 'AM':
661
- return { urlChave: 'www.sefaz.am.gov.br/nfce/consulta', urlQRCode: 'http://sistemas.sefaz.am.gov.br/nfceweb/consultarNFCe.jsp' };
662
- case 'BA':
663
- return { urlChave: 'www.sefaz.ba.gov.br/nfce/consulta', urlQRCode: 'http://nfe.sefaz.ba.gov.br/servicos/nfce/modulos/geral/NFCEC_consulta_chave_acesso.aspx' };
664
- case 'CE':
665
- return { urlChave: 'www.sefaz.ce.gov.br/nfce/consulta', urlQRCode: 'http://nfce.sefaz.ce.gov.br/pages/ShowNFCe.html' };
666
- case 'DF':
667
- return { urlChave: 'www.fazenda.df.gov.br/nfce/consulta', urlQRCode: 'http://dec.fazenda.df.gov.br/ConsultarNFCe.aspx' };
668
- case 'ES':
669
- return { urlChave: 'www.sefaz.es.gov.br/nfce/consulta', urlQRCode: 'http://app.sefaz.es.gov.br/ConsultaNFCe/qrcode.aspx' };
670
- case 'GO':
671
- return { urlChave: 'www.sefaz.go.gov.br/nfce/consulta', urlQRCode: 'http://nfe.sefaz.go.gov.br/nfeweb/sites/nfce/danfeNFCe' };
672
- case 'MA':
673
- return { urlChave: 'www.sefaz.ma.gov.br/nfce/consulta', urlQRCode: 'http://www.nfce.sefaz.ma.gov.br/portal/consultarNFCe.jsp' };
674
- case 'MG':
675
- return { urlChave: 'http://nfce.fazenda.mg.gov.br/portalnfce', urlQRCode: 'https://nfce.fazenda.mg.gov.br/portalnfce/sistema/qrcode.xhtml' };
676
- case 'MS':
677
- return { urlChave: 'http://www.dfe.ms.gov.br/nfce/consulta', urlQRCode: 'http://www.dfe.ms.gov.br/nfce/qrcode' };
678
- case '51': //MT
679
- return { urlChave: 'http://www.sefaz.mt.gov.br/nfce/consultanfce', urlQRCode: 'http://www.sefaz.mt.gov.br/nfce/consultanfce' };
680
- case 'PA':
681
- return { urlChave: 'www.sefa.pa.gov.br/nfce/consulta', urlQRCode: 'https://appnfc.sefa.pa.gov.br/portal/view/consultas/nfce/nfceForm.seam' };
682
- case 'PB':
683
- return { urlChave: 'www.receita.pb.gov.br/nfce/consulta', urlQRCode: 'http://www.receita.pb.gov.br/nfce' };
684
- case 'PE':
685
- return { urlChave: 'nfce.sefaz.pe.gov.br/nfce/consulta', urlQRCode: 'http://nfce.sefaz.pe.gov.br/nfce-web/consultarNFCe' };
686
- case 'PI':
687
- return { urlChave: 'www.sefaz.pi.gov.br/nfce/consulta', urlQRCode: 'http://www.sefaz.pi.gov.br/nfce/qrcode' };
688
- case 'PR':
689
- return { urlChave: 'http://www.fazenda.pr.gov.br/nfce/consulta', urlQRCode: 'http://www.fazenda.pr.gov.br/nfce/qrcode/' };
690
- case 'RJ':
691
- return { urlChave: 'www.fazenda.rj.gov.br/nfce/consulta', urlQRCode: 'http://www4.fazenda.rj.gov.br/consultaNFCe/QRCode' };
692
- case 'RN':
693
- return { urlChave: 'www.set.rn.gov.br/nfce/consulta', urlQRCode: 'http://nfce.set.rn.gov.br/consultarNFCe.aspx' };
694
- case 'RO':
695
- return { urlChave: 'www.sefin.ro.gov.br/nfce/consulta', urlQRCode: 'http://www.nfce.sefin.ro.gov.br/consultanfce/consulta.jsp' };
696
- case 'RS':
697
- return { urlChave: 'www.sefaz.rs.gov.br/nfce/consulta', urlQRCode: 'https://www.sefaz.rs.gov.br/NFCE/NFCE-COM.aspx' };
698
- case 'RR':
699
- return { urlChave: 'www.sefaz.rr.gov.br/nfce/consulta', urlQRCode: 'https://www.sefaz.rr.gov.br/nfce/servlet/qrcode' };
700
- case 'SE':
701
- return { urlChave: 'http://www.nfce.se.gov.br/nfce/consulta', urlQRCode: 'http://www.nfce.se.gov.br/portal/consultarNFCe.jsp' };
702
- case 'SP':
703
- return { urlChave: 'https://www.nfce.fazenda.sp.gov.br/consulta', urlQRCode: 'https://www.nfce.fazenda.sp.gov.br/qrcode' };
704
- case 'TO':
705
- return { urlChave: 'www.sefaz.to.gov.br/nfce/consulta', urlQRCode: 'http://www.sefaz.to.gov.br/nfce/qrcode' };
706
- default:
707
- throw new Error('URL do QRCode não encontrada pelo UF (' + uf + ') informado.');
708
- }
709
- }
710
- else {
711
- switch (this.#NFe.infNFe.ide.cUF) {
712
- case 'AC':
713
- return { urlChave: 'www.sefaznet.ac.gov.br/nfce/consulta', urlQRCode: 'http://hml.sefaznet.ac.gov.br/nfce/qrcode' };
714
- case 'AL':
715
- return { urlChave: 'www.sefaz.al.gov.br/nfce/consulta', urlQRCode: 'http://nfce.sefaz.al.gov.br/QRCode/consultarNFCe.jsp' };
716
- case 'AP':
717
- return { urlChave: 'www.sefaz.ap.gov.br/nfce/consulta', urlQRCode: 'https://www.sefaz.ap.gov.br/nfcehml/nfce.php' };
718
- case 'AM':
719
- return { urlChave: 'https://sistemas.sefaz.am.gov.br/nfceweb-hom/formConsulta.do', urlQRCode: 'https://sistemas.sefaz.am.gov.br/nfceweb-hom/consultarNFCe.jsp' };
720
- case 'BA':
721
- return { urlChave: 'http://hinternet.sefaz.ba.gov.br/nfce/consulta', urlQRCode: 'http://hnfe.sefaz.ba.gov.br/servicos/nfce/modulos/geral/NFCEC_consulta_chave_acesso.aspx' };
722
- case 'CE':
723
- return { urlChave: 'www.sefaz.ce.gov.br/nfce/consulta', urlQRCode: 'http://nfceh.sefaz.ce.gov.br/pages/ShowNFCe.html' };
724
- case 'DF':
725
- return { urlChave: 'www.fazenda.df.gov.br/nfce/consulta', urlQRCode: 'http://dec.fazenda.df.gov.br/ConsultarNFCe.aspx' };
726
- case 'ES':
727
- return { urlChave: 'www.sefaz.es.gov.br/nfce/consulta', urlQRCode: 'http://homologacao.sefaz.es.gov.br/ConsultaNFCe/qrcode.aspx' };
728
- case 'GO':
729
- return { urlChave: 'http://www.nfce.go.gov.br/post/ver/214413/consulta-nfc-e-homologacao', urlQRCode: 'http://homolog.sefaz.go.gov.br/nfeweb/sites/nfce/danfeNFCe' };
730
- case 'MA':
731
- return { urlChave: 'www.sefaz.ma.gov.br/nfce/consulta', urlQRCode: 'http://www.hom.nfce.sefaz.ma.gov.br/portal/consultarNFCe.jsp' };
732
- case 'MG':
733
- return { urlChave: 'http://hnfce.fazenda.mg.gov.br/portalnfce', urlQRCode: 'https://nfce.fazenda.mg.gov.br/portalnfce/sistema/qrcode.xhtml' };
734
- case 'MS':
735
- return { urlChave: 'http://www.dfe.ms.gov.br/nfce/consulta', urlQRCode: 'http://www.dfe.ms.gov.br/nfce/qrcode' };
736
- case '51': //MT
737
- return { urlChave: 'http://homologacao.sefaz.mt.gov.br/nfce/consultanfce', urlQRCode: 'http://homologacao.sefaz.mt.gov.br/nfce/consultanfce' };
738
- case 'PA':
739
- return { urlChave: 'www.sefa.pa.gov.br/nfce/consulta', urlQRCode: 'https://appnfc.sefa.pa.gov.br/portal-homologacao/view/consultas/nfce/nfceForm.seam' };
740
- case 'PB':
741
- return { urlChave: 'www.receita.pb.gov.br/nfcehom', urlQRCode: 'http://www.receita.pb.gov.br/nfcehom' };
742
- case 'PE':
743
- return { urlChave: 'nfce.sefaz.pe.gov.br/nfce/consulta', urlQRCode: 'http://nfcehomolog.sefaz.pe.gov.br/nfce-web/consultarNFCe' };
744
- case 'PI':
745
- return { urlChave: 'www.sefaz.pi.gov.br/nfce/consulta', urlQRCode: 'http://www.sefaz.pi.gov.br/nfce/qrcode' };
746
- case 'PR':
747
- return { urlChave: 'http://www.fazenda.pr.gov.br/nfce/consulta', urlQRCode: 'http://www.fazenda.pr.gov.br/nfce/qrcode/' };
748
- case 'RJ':
749
- return { urlChave: 'www.fazenda.rj.gov.br/nfce/consulta', urlQRCode: 'http://www4.fazenda.rj.gov.br/consultaNFCe/QRCode' };
750
- case 'RN':
751
- return { urlChave: 'www.set.rn.gov.br/nfce/consulta', urlQRCode: 'http://hom.nfce.set.rn.gov.br/consultarNFCe.aspx' };
752
- case 'RO':
753
- return { urlChave: 'www.sefin.ro.gov.br/nfce/consulta', urlQRCode: 'http://www.nfce.sefin.ro.gov.br/consultanfce/consulta.jsp' };
754
- case 'RS':
755
- return { urlChave: 'www.sefaz.rs.gov.br/nfce/consulta', urlQRCode: 'https://www.sefaz.rs.gov.br/NFCE/NFCE-COM.aspx' };
756
- case 'RR':
757
- return { urlChave: 'www.sefaz.rr.gov.br/nfce/consulta', urlQRCode: 'http://200.174.88.103:8080/nfce/servlet/qrcode' };
758
- case 'SE':
759
- return { urlChave: 'http://www.hom.nfe.se.gov.br/nfce/consulta', urlQRCode: 'http://www.hom.nfe.se.gov.br/portal/consultarNFCe.jsp' };
760
- case 'SP':
761
- return { urlChave: 'https://www.homologacao.nfce.fazenda.sp.gov.br/consulta', urlQRCode: 'https://www.homologacao.nfce.fazenda.sp.gov.br/qrcode' };
762
- case 'TO':
763
- return { urlChave: 'http://homologacao.sefaz.to.gov.br/nfce/consulta.jsf', urlQRCode: 'http://homologacao.sefaz.to.gov.br/nfce/qrcode' };
764
- default:
765
- throw new Error('URL do QRCode não encontrada pelo UF (' + uf + ') informado.');
766
- }
767
- }
768
- }
769
-
770
659
  #calICMSTot(obj: any) {
771
660
  Object.keys(obj).map(key => {
772
661
  if (this.#ICMSTot[key] !== undefined) {
@@ -1,13 +1,15 @@
1
- import { XMLParser, XMLBuilder, XMLValidator } from "fast-xml-parser";
2
- import pem from "pem";
1
+ import { XMLParser, XMLBuilder } from "fast-xml-parser";
3
2
  import https from "https";
4
3
  import { spawnSync, SpawnSyncReturns } from "child_process"
5
4
  import tmp from "tmp"
6
5
  import crypto from "crypto";
7
- import { urlServicos } from "./eventos.js"
6
+ import { urlEventos } from "./eventos.js"
8
7
  import fs from "fs"
9
8
  import path from 'path';
10
9
  import { fileURLToPath } from 'url';
10
+ import pem from 'pem';
11
+
12
+
11
13
 
12
14
  const __filename = fileURLToPath(import.meta.url);
13
15
  const __dirname = path.dirname(__filename);
@@ -36,11 +38,14 @@ class Tools {
36
38
  #config: {
37
39
  mod: string;
38
40
  xmllint: string;
39
- cUF: string;
41
+ UF: string;
40
42
  tpAmb: number;
43
+ CSC: string;
44
+ CSCid: string;
45
+ versao: string;
41
46
  };
42
47
 
43
- constructor(config = { mod: "", xmllint: 'xmllint', cUF: '51', tpAmb: 2 }, certificado = { pfx: "", senha: "" }) {
48
+ constructor(config = { mod: "", xmllint: 'xmllint', UF: '', tpAmb: 2, CSC: "", CSCid: "", versao: "4.00" }, certificado = { pfx: "", senha: "" }) {
44
49
  //Configurar certificado
45
50
  this.#config = config;
46
51
  this.#cert = certificado;
@@ -55,10 +60,6 @@ class Tools {
55
60
  });
56
61
  }
57
62
 
58
- async teste() {
59
- return await this.#certTools()
60
- }
61
-
62
63
  sefazEnviaLote(xml: string, data: any = { idLote: 1, indSinc: 0, compactar: false }): Promise<string> {
63
64
  return new Promise(async (resvol, reject) => {
64
65
  if (typeof data.idLote == "undefined") data.idLote = 1;
@@ -88,9 +89,9 @@ class Tools {
88
89
  },
89
90
  }
90
91
  let xmlLote = await this.json2xml(jsonXmlLote);
91
- fs.writeFileSync("testes/nfe_guara_sign_lote.xml", xmlLote, { encoding: "utf8" });
92
92
  try {
93
- const req = https.request(urlServicos[`${this.#config.cUF}`][`mod_${this.#config.mod}`].NFeAutorizacao[(this.#config.tpAmb == 1 ? "producao" : "homologacao")], {
93
+ let tempUF = urlEventos(this.#config.UF, this.#config.versao);
94
+ const req = https.request(tempUF[`mod${this.#config.mod}`][(this.#config.tpAmb == 1 ? "producao" : "homologacao")].NFeAutorizacao, {
94
95
  ...{
95
96
  method: 'POST',
96
97
  headers: {
@@ -175,8 +176,14 @@ class Tools {
175
176
  "@xmlns": "http://www.w3.org/2000/09/xmldsig#"
176
177
  }
177
178
  }
179
+
180
+
181
+ if (xml.NFe.infNFe.ide.mod == 65) {
182
+ xml.NFe.infNFeSupl.qrCode = this.#gerarQRCodeNFCe(xml.NFe, "2", this.#config.CSCid, this.#config.CSC)
183
+ }
184
+
178
185
  this.json2xml(xml).then(async res => {
179
- this.#xmlValido(res);
186
+ this.#xmlValido(res, `nfe_${this.#config.versao}`);
180
187
  resvol(res);
181
188
  }).catch(err => {
182
189
  reject(err)
@@ -184,6 +191,20 @@ class Tools {
184
191
  })
185
192
  }
186
193
 
194
+ #gerarQRCodeNFCe(NFe: any, versaoQRCode: string = "2", idCSC: string, CSC: string) {
195
+ let s = '|',
196
+ concat,
197
+ hash;
198
+ if (NFe.infNFe.ide.tpEmis == 1) {
199
+ concat = [NFe.infNFe['@Id'].replace("NFe", ""), versaoQRCode, NFe.infNFe.ide.tpAmb, Number(idCSC)].join(s);
200
+ } else {
201
+ let hexDigestValue = Buffer.from(NFe.Signature.SignedInfo.Reference.DigestValue).toString('hex');
202
+ concat = [NFe.infNFe['@Id'].replace("NFe", ""), versaoQRCode, NFe.infNFe.ide.tpAmb, NFe.infNFe.ide.dhEmi, NFe.infNFe.total.ICMSTot.vNF, hexDigestValue, Number(idCSC)].join(s);
203
+ }
204
+ hash = crypto.createHash('sha1').update(concat + CSC).digest('hex');
205
+ return NFe.infNFeSupl.qrCode + '?p=' + concat + s + hash;
206
+ }
207
+
187
208
  async xml2json(xml: string): Promise<object> {
188
209
  return new Promise((resvol, reject) => {
189
210
  resvol(this.#xmlTools.XMLParser.parse(xml))
@@ -198,32 +219,105 @@ class Tools {
198
219
 
199
220
  async getCertificado() {
200
221
  return new Promise(async (resvol, reject) => {
201
- await this.#certTools().then(resvol).catch(reject)
222
+ this.#certTools().then(resvol).catch(reject)
202
223
  })
203
224
  }
204
225
 
226
+ consultarNFe(chNFe: string): Promise<string> {
227
+ return new Promise(async (resolve, reject) => {
228
+ if (!chNFe || chNFe.length !== 44) {
229
+ return reject("consultarNFe(chNFe) -> chave inválida!");
230
+ }
231
+
232
+ if (typeof this.#config.UF === "undefined") throw "consultarNFe({...UF}) -> não definido!";
233
+ if (typeof this.#config.tpAmb === "undefined") throw "consultarNFe({...tpAmb}) -> não definido!";
234
+ if (typeof this.#config.mod === "undefined") throw "consultarNFe({...mod}) -> não definido!";
235
+
236
+ let consSitNFe = {
237
+ "@xmlns": "http://www.portalfiscal.inf.br/nfe",
238
+ "@versao": "4.00",
239
+ "tpAmb": this.#config.tpAmb,
240
+ "xServ": "CONSULTAR",
241
+ "chNFe": chNFe
242
+ };
243
+
244
+ let xmlObj = {
245
+ "soap:Envelope": {
246
+ "@xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
247
+ "@xmlns:nfe": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeConsultaProtocolo4",
248
+ "soap:Body": {
249
+ "nfe:nfeDadosMsg": {
250
+ "consSitNFe": consSitNFe
251
+ }
252
+ }
253
+ }
254
+ };
255
+
256
+ try {
257
+ const builder = new XMLBuilder({
258
+ ignoreAttributes: false,
259
+ attributeNamePrefix: "@"
260
+ });
261
+
262
+ // Validação do XML interno (opcional)
263
+ this.#xmlValido(builder.build({ consSitNFe }), `consSitNFe_v${this.#config.versao}`);
264
+
265
+ const xml = builder.build(xmlObj);
266
+
267
+ let tempUF = urlEventos(this.#config.UF, this.#config.versao);
268
+
269
+ const url = tempUF[`mod${this.#config.mod}`][(this.#config.tpAmb == 1 ? "producao" : "homologacao")].NFeConsultaProtocolo;
270
+
271
+ const req = https.request(url, {
272
+ method: 'POST',
273
+ headers: {
274
+ 'Content-Type': 'application/soap+xml; charset=utf-8',
275
+ 'Content-Length': xml.length,
276
+ },
277
+ rejectUnauthorized: false,
278
+ ...await this.#certTools()
279
+ }, (res) => {
280
+ let data = '';
281
+ res.on('data', (chunk) => data += chunk);
282
+ res.on('end', () => resolve(data));
283
+ });
284
+
285
+ req.on('error', (err) => reject(err));
286
+
287
+ req.write(xml);
288
+ req.end();
289
+ } catch (err) {
290
+ reject(err);
291
+ }
292
+ });
293
+ }
294
+
205
295
  //Consulta status sefaz
206
- async sefazStatus() {
296
+ async sefazStatus(): Promise<string> {
207
297
  return new Promise(async (resvol, reject) => {
208
- await this.#certTools();
209
298
 
210
- if (typeof this.#config.cUF == "undefined") throw "sefazStatus({...cUF}) -> não definido!";
299
+ if (typeof this.#config.UF == "undefined") throw "sefazStatus({...UF}) -> não definido!";
211
300
  if (typeof this.#config.tpAmb == "undefined") throw "sefazStatus({...tpAmb}) -> não definido!";
212
301
  if (typeof this.#config.mod == "undefined") throw "sefazStatus({...mod}) -> não definido!";
213
302
 
303
+ let tempUF = urlEventos(this.#config.UF, this.#config.versao);
304
+
305
+ //Separado para validar o corpo da consulta
306
+ let consStatServ = {
307
+ "@versao": "4.00",
308
+ "@xmlns": "http://www.portalfiscal.inf.br/nfe",
309
+ "tpAmb": this.#config.tpAmb,
310
+ "cUF": tempUF.cUF,
311
+ "xServ": "STATUS"
312
+ }
313
+
214
314
  let xmlObj = {
215
315
  "soap:Envelope": {
216
316
  "@xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
217
317
  "@xmlns:nfe": "http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4",
218
318
  "soap:Body": {
219
319
  "nfe:nfeDadosMsg": {
220
- "consStatServ": {
221
- "@versao": "4.00",
222
- "@xmlns": "http://www.portalfiscal.inf.br/nfe",
223
- "tpAmb": this.#config.tpAmb,
224
- "cUF": this.#config.cUF,
225
- "xServ": "STATUS"
226
- }
320
+ consStatServ
227
321
  }
228
322
  }
229
323
  }
@@ -234,9 +328,12 @@ class Tools {
234
328
  ignoreAttributes: false,
235
329
  attributeNamePrefix: "@"
236
330
  });
237
- let xml = tempBuild.build(xmlObj);
238
331
 
239
- const req = https.request(urlServicos[`${this.#config.cUF}`][`mod_${this.#config.mod}`].NfeStatusServico[(this.#config.tpAmb == 1 ? "producao" : "homologacao")], {
332
+ //Validação
333
+ this.#xmlValido(tempBuild.build({ consStatServ }), `consStatServ_v${this.#config.versao}`);
334
+ let tempUF = urlEventos(this.#config.UF, this.#config.versao);
335
+ let xml = tempBuild.build(xmlObj);
336
+ const req = https.request(tempUF[`mod${this.#config.mod}`][(this.#config.tpAmb == 1 ? "producao" : "homologacao")].NFeStatusServico, {
240
337
  ...{
241
338
  method: 'POST',
242
339
  headers: {
@@ -245,7 +342,7 @@ class Tools {
245
342
  },
246
343
  rejectUnauthorized: false
247
344
  },
248
- ...this.#pem
345
+ ...await this.#certTools()
249
346
  }, (res) => {
250
347
  let data = '';
251
348
 
@@ -272,11 +369,10 @@ class Tools {
272
369
 
273
370
 
274
371
  //Validar XML da NFe, somente apos assinar
275
- async #xmlValido(xml: string) {
372
+ async #xmlValido(xml: string, xsd: string) {
276
373
  const xmlFile = tmp.fileSync({ mode: 0o644, prefix: 'xml-', postfix: '.xml' });
277
374
  fs.writeFileSync(xmlFile.name, xml, { encoding: 'utf8' });
278
-
279
- const schemaPath = path.resolve(__dirname, '../../schemas/nfe_v4.00.xsd');
375
+ const schemaPath = path.resolve(__dirname, `../../schemas/${xsd}.xsd`);
280
376
  const verif: SpawnSyncReturns<string> = spawnSync(
281
377
  this.#config.xmllint,
282
378
  ['--noout', '--schema', schemaPath, xmlFile.name],
@@ -291,13 +387,9 @@ class Tools {
291
387
  } else if (!verif.stderr.includes(".xml validates")) {
292
388
  throw new Error(verif.stderr);
293
389
  }
294
-
295
390
  return 1;
296
391
  }
297
392
 
298
-
299
-
300
- //Extrair dados do certificado pem
301
393
  #certTools(): Promise<object> {
302
394
  return new Promise(async (resvol, reject) => {
303
395
  if (this.#pem.key != "") resvol(this.#pem);
@@ -1 +0,0 @@
1
- {"root":["../src/index.ts","../src/utils/eventos.ts","../src/utils/make.ts","../src/utils/tools.ts"],"version":"5.8.3"}
@@ -1 +0,0 @@
1
- export {};
@@ -1,64 +0,0 @@
1
- import axios from "axios";
2
- import https from "https";
3
- class Instance {
4
- constructor(opts) {
5
- const { baseURL, ca, cert, key } = opts;
6
- const AgentOptions = Object.assign({
7
- cert: cert,
8
- key: key,
9
- ca: ca,
10
- }, { ...opts.httpsOptions });
11
- const httpsAgent = new https.Agent(AgentOptions);
12
- const requestOptions = Object.assign({
13
- baseURL: baseURL,
14
- headers: {
15
- 'User-Agent': `node-nfe/1.0`,
16
- 'Content-Type': 'application/soap+xml; charset=utf-8',
17
- },
18
- httpsAgent: httpsAgent,
19
- timeout: 60000,
20
- }, { ...opts.requestOptions });
21
- const instance = axios.create({
22
- ...requestOptions,
23
- });
24
- this.instance = instance;
25
- }
26
- /**
27
- * @returns {Promise<{status: number, data: string}>}
28
- */
29
- async request(config) {
30
- try {
31
- const response = await this.instance(config);
32
- const { status, data } = response;
33
- return { status, data };
34
- }
35
- catch (error) {
36
- if (error.response) {
37
- const { status, data } = error.response;
38
- return { status, data };
39
- }
40
- else if (error.request) {
41
- if (error.code === 'ECONNABORTED') {
42
- const retorno = {
43
- status: 504,
44
- data: `<error>${error.message || error}</error>`,
45
- };
46
- return retorno;
47
- }
48
- const retorno = {
49
- status: 502,
50
- data: `<error>${error.message || error}</error>`,
51
- };
52
- return retorno;
53
- }
54
- else {
55
- const retorno = {
56
- status: 500,
57
- data: `<error>${error.message || error}</error>`,
58
- };
59
- return retorno;
60
- }
61
- }
62
- }
63
- }
64
- module.exports = Instance;
@@ -1,4 +0,0 @@
1
- declare const validateXML: (options: any) => {
2
- errors: any;
3
- };
4
- export { validateXML };