@bitize/bitmde 0.19.0 → 0.20.0
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.
- package/CHANGELOG.md +25 -0
- package/README.md +252 -2
- package/dist/apis/distribuicaoNFSe-api.d.ts +176 -0
- package/dist/apis/manifestacaoNFSe-api.d.ts +130 -0
- package/dist/controllers/distribuicaoNFSe-controller.d.ts +50 -0
- package/dist/controllers/index.d.ts +76 -0
- package/dist/controllers/manifestacaoNFSe-controller.d.ts +38 -0
- package/dist/env/index.d.ts +4 -1
- package/dist/env/nfse.d.ts +32 -0
- package/dist/helpers/distribuicaoNFSe-helper.d.ts +107 -0
- package/dist/helpers/index.d.ts +146 -0
- package/dist/helpers/manifestacaoNFSe-helper.d.ts +80 -0
- package/dist/helpers/retornoNfse-helper.d.ts +44 -0
- package/dist/index.d.ts +7 -1
- package/dist/schemas/index.d.ts +17 -0
- package/dist/schemas/manifestacaoNFSe-schema.d.ts +33 -0
- package/dist/util/assinatura.d.ts +3 -1
- package/dist/util/gzip.d.ts +7 -0
- package/dist/util/index.d.ts +2 -1
- package/dist/validators/chaveNfse-validator.d.ts +14 -0
- package/dist/validators/cnpj-validator.d.ts +19 -0
- package/dist/validators/index.d.ts +50 -0
- package/dist/validators/motivoRejeicao-validator.d.ts +23 -0
- package/dist/validators/nsuNfse-validator.d.ts +20 -0
- package/lib/apis/distribuicaoNFSe-api.js +59 -0
- package/lib/apis/manifestacaoNFSe-api.js +69 -0
- package/lib/controllers/distribuicaoNFSe-controller.js +16 -0
- package/lib/controllers/index.js +3 -1
- package/lib/controllers/manifestacaoNFSe-controller.js +25 -0
- package/lib/env/index.js +8 -1
- package/lib/env/nfse.js +24 -0
- package/lib/env/version.js +1 -1
- package/lib/helpers/distribuicaoNFSe-helper.js +128 -0
- package/lib/helpers/index.js +5 -2
- package/lib/helpers/manifestacaoNFSe-helper.js +105 -0
- package/lib/helpers/retornoNfse-helper.js +31 -0
- package/lib/index.js +7 -1
- package/lib/schemas/index.js +2 -1
- package/lib/schemas/manifestacaoNFSe-schema.js +19 -0
- package/lib/util/assinatura.js +2 -2
- package/lib/util/gzip.js +8 -0
- package/lib/validators/chaveNfse-validator.js +16 -0
- package/lib/validators/cnpj-validator.js +21 -0
- package/lib/validators/index.js +5 -1
- package/lib/validators/motivoRejeicao-validator.js +28 -0
- package/lib/validators/nsuNfse-validator.js +18 -0
- package/package.json +6 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare const _exports: typeof ChaveNfseValidator;
|
|
2
|
+
export = _exports;
|
|
3
|
+
declare class ChaveNfseValidator {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param {string} chave
|
|
7
|
+
*/
|
|
8
|
+
constructor(chave: string);
|
|
9
|
+
chave: string;
|
|
10
|
+
error: string;
|
|
11
|
+
isValid(): boolean;
|
|
12
|
+
getValues(): string;
|
|
13
|
+
getError(): string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare const _exports: typeof CnpjValidator;
|
|
2
|
+
export = _exports;
|
|
3
|
+
declare class CnpjValidator {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} config
|
|
7
|
+
* @param {string} config.cnpj
|
|
8
|
+
*/
|
|
9
|
+
constructor(config: {
|
|
10
|
+
cnpj: string;
|
|
11
|
+
});
|
|
12
|
+
cnpj: string;
|
|
13
|
+
error: string;
|
|
14
|
+
isValid(): boolean;
|
|
15
|
+
getValues(): {
|
|
16
|
+
cnpj: string;
|
|
17
|
+
};
|
|
18
|
+
getError(): string;
|
|
19
|
+
}
|
|
@@ -33,6 +33,15 @@ declare const validator: Readonly<{
|
|
|
33
33
|
getError(): string;
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
|
+
ChaveNfseValidator: {
|
|
37
|
+
new (chave: string): {
|
|
38
|
+
chave: string;
|
|
39
|
+
error: string;
|
|
40
|
+
isValid(): boolean;
|
|
41
|
+
getValues(): string;
|
|
42
|
+
getError(): string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
36
45
|
ChaveValidator: {
|
|
37
46
|
new (chave: string): {
|
|
38
47
|
chave: string;
|
|
@@ -58,6 +67,19 @@ declare const validator: Readonly<{
|
|
|
58
67
|
getError(): string;
|
|
59
68
|
};
|
|
60
69
|
};
|
|
70
|
+
CnpjValidator: {
|
|
71
|
+
new (config: {
|
|
72
|
+
cnpj: string;
|
|
73
|
+
}): {
|
|
74
|
+
cnpj: string;
|
|
75
|
+
error: string;
|
|
76
|
+
isValid(): boolean;
|
|
77
|
+
getValues(): {
|
|
78
|
+
cnpj: string;
|
|
79
|
+
};
|
|
80
|
+
getError(): string;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
61
83
|
EventoValidator: {
|
|
62
84
|
new (evento: {
|
|
63
85
|
chNFe: string;
|
|
@@ -108,6 +130,34 @@ declare const validator: Readonly<{
|
|
|
108
130
|
getError(): string;
|
|
109
131
|
};
|
|
110
132
|
};
|
|
133
|
+
MotivoRejeicaoValidator: {
|
|
134
|
+
new (motivo?: {
|
|
135
|
+
cMotivo: "1" | "2" | "3" | "4" | "5" | "9";
|
|
136
|
+
xMotivo?: string;
|
|
137
|
+
}): {
|
|
138
|
+
cMotivo: "1" | "2" | "3" | "4" | "5" | "9";
|
|
139
|
+
xMotivo: string;
|
|
140
|
+
error: string;
|
|
141
|
+
isValid(): boolean;
|
|
142
|
+
getValues(): {
|
|
143
|
+
cMotivo: "1" | "2" | "3" | "4" | "5" | "9";
|
|
144
|
+
xMotivo: string;
|
|
145
|
+
};
|
|
146
|
+
getError(): string;
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
NsuNfseValidator: {
|
|
150
|
+
new (nsu: string | number, opcoes?: {
|
|
151
|
+
positivo?: boolean;
|
|
152
|
+
}): {
|
|
153
|
+
nsu: string | number;
|
|
154
|
+
positivo: boolean;
|
|
155
|
+
error: string;
|
|
156
|
+
isValid(): boolean;
|
|
157
|
+
getValues(): string | number;
|
|
158
|
+
getError(): string;
|
|
159
|
+
};
|
|
160
|
+
};
|
|
111
161
|
NsuValidator: {
|
|
112
162
|
new (nsu: string): {
|
|
113
163
|
nsu: string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare const _exports: typeof MotivoRejeicaoValidator;
|
|
2
|
+
export = _exports;
|
|
3
|
+
declare class MotivoRejeicaoValidator {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} [motivo]
|
|
7
|
+
* @param {'1' | '2' | '3' | '4' | '5' | '9'} motivo.cMotivo
|
|
8
|
+
* @param {string} [motivo.xMotivo]
|
|
9
|
+
*/
|
|
10
|
+
constructor(motivo?: {
|
|
11
|
+
cMotivo: "1" | "2" | "3" | "4" | "5" | "9";
|
|
12
|
+
xMotivo?: string;
|
|
13
|
+
});
|
|
14
|
+
cMotivo: "1" | "2" | "3" | "4" | "5" | "9";
|
|
15
|
+
xMotivo: string;
|
|
16
|
+
error: string;
|
|
17
|
+
isValid(): boolean;
|
|
18
|
+
getValues(): {
|
|
19
|
+
cMotivo: "1" | "2" | "3" | "4" | "5" | "9";
|
|
20
|
+
xMotivo: string;
|
|
21
|
+
};
|
|
22
|
+
getError(): string;
|
|
23
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare const _exports: typeof NsuNfseValidator;
|
|
2
|
+
export = _exports;
|
|
3
|
+
declare class NsuNfseValidator {
|
|
4
|
+
/**
|
|
5
|
+
* No ADN o NSU é inteiro: sem o zero-pad do leiaute da NF-e.
|
|
6
|
+
*
|
|
7
|
+
* @param {string | number} nsu
|
|
8
|
+
* @param {Object} [opcoes]
|
|
9
|
+
* @param {boolean} [opcoes.positivo] Recusa o NSU `0`.
|
|
10
|
+
*/
|
|
11
|
+
constructor(nsu: string | number, opcoes?: {
|
|
12
|
+
positivo?: boolean;
|
|
13
|
+
});
|
|
14
|
+
nsu: string | number;
|
|
15
|
+
positivo: boolean;
|
|
16
|
+
error: string;
|
|
17
|
+
isValid(): boolean;
|
|
18
|
+
getValues(): string | number;
|
|
19
|
+
getError(): string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
let DistribuicaoNFSeController = require("../controllers").DistribuicaoNFSeController, {
|
|
2
|
+
AmbienteValidator,
|
|
3
|
+
CertificadoValidator,
|
|
4
|
+
ChaveNfseValidator,
|
|
5
|
+
CnpjValidator,
|
|
6
|
+
NsuNfseValidator
|
|
7
|
+
} = require("../validators");
|
|
8
|
+
|
|
9
|
+
class DistribuicaoNFSe {
|
|
10
|
+
constructor(r) {
|
|
11
|
+
var {
|
|
12
|
+
requestOptions: e = {},
|
|
13
|
+
httpsOptions: t = {}
|
|
14
|
+
} = r.options || {}, i = new CertificadoValidator(r), o = new AmbienteValidator(r), r = new CnpjValidator(r);
|
|
15
|
+
if (!i.isValid()) throw new Error(i.getError());
|
|
16
|
+
if (!o.isValid()) throw new Error(o.getError());
|
|
17
|
+
if (!r.isValid()) throw new Error(r.getError());
|
|
18
|
+
var {
|
|
19
|
+
cert: i,
|
|
20
|
+
key: a
|
|
21
|
+
} = i.getValues(), o = o.getValues().tpAmb, r = r.getValues().cnpj;
|
|
22
|
+
this.config = Object.freeze({
|
|
23
|
+
cnpj: r,
|
|
24
|
+
tpAmb: o,
|
|
25
|
+
cert: i,
|
|
26
|
+
key: a,
|
|
27
|
+
requestOptions: Object.freeze(e),
|
|
28
|
+
httpsOptions: Object.freeze(t)
|
|
29
|
+
}), Object.freeze(this);
|
|
30
|
+
}
|
|
31
|
+
consultaChNFSe(r) {
|
|
32
|
+
var e, r = new ChaveNfseValidator(r);
|
|
33
|
+
if (r.isValid()) return e = r.getValues(), e = {
|
|
34
|
+
...this.config,
|
|
35
|
+
chNFSe: e
|
|
36
|
+
}, DistribuicaoNFSeController.enviar(e);
|
|
37
|
+
throw new Error(r.getError());
|
|
38
|
+
}
|
|
39
|
+
consultaNSU(r) {
|
|
40
|
+
var e, r = new NsuNfseValidator(r, {
|
|
41
|
+
positivo: !0
|
|
42
|
+
});
|
|
43
|
+
if (r.isValid()) return e = r.getValues(), e = {
|
|
44
|
+
...this.config,
|
|
45
|
+
nsu: e
|
|
46
|
+
}, DistribuicaoNFSeController.enviar(e);
|
|
47
|
+
throw new Error(r.getError());
|
|
48
|
+
}
|
|
49
|
+
consultaUltNSU(r) {
|
|
50
|
+
var e, r = new NsuNfseValidator(r);
|
|
51
|
+
if (r.isValid()) return e = r.getValues(), e = {
|
|
52
|
+
...this.config,
|
|
53
|
+
ultNSU: e
|
|
54
|
+
}, DistribuicaoNFSeController.enviar(e);
|
|
55
|
+
throw new Error(r.getError());
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = DistribuicaoNFSe;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
let ManifestacaoNFSeController = require("../controllers").ManifestacaoNFSeController, {
|
|
2
|
+
EVENTOS_NFSE,
|
|
3
|
+
VERSION
|
|
4
|
+
} = require("../env"), Data = require("../util").Data, {
|
|
5
|
+
AmbienteValidator,
|
|
6
|
+
CertificadoValidator,
|
|
7
|
+
ChaveNfseValidator,
|
|
8
|
+
CnpjValidator,
|
|
9
|
+
MotivoRejeicaoValidator,
|
|
10
|
+
ZoneValidator
|
|
11
|
+
} = require("../validators");
|
|
12
|
+
|
|
13
|
+
function montarOpts(e, r, t) {
|
|
14
|
+
return {
|
|
15
|
+
...e,
|
|
16
|
+
chNFSe: r,
|
|
17
|
+
evento: EVENTOS_NFSE[t],
|
|
18
|
+
verAplic: "bitmde/" + VERSION,
|
|
19
|
+
dhEvento: Data.toFormat(new Date(), e.timezone)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class ManifestacaoNFSe {
|
|
24
|
+
constructor(e) {
|
|
25
|
+
var {
|
|
26
|
+
requestOptions: r = {},
|
|
27
|
+
httpsOptions: t = {}
|
|
28
|
+
} = e.options || {}, o = new CertificadoValidator(e), a = new AmbienteValidator(e), i = new CnpjValidator(e), e = new ZoneValidator(e);
|
|
29
|
+
if (!o.isValid()) throw new Error(o.getError());
|
|
30
|
+
if (!a.isValid()) throw new Error(a.getError());
|
|
31
|
+
if (!i.isValid()) throw new Error(i.getError());
|
|
32
|
+
if (!e.isValid()) throw new Error(e.getError());
|
|
33
|
+
var {
|
|
34
|
+
cert: o,
|
|
35
|
+
key: n
|
|
36
|
+
} = o.getValues(), a = a.getValues().tpAmb, i = i.getValues().cnpj, e = e.getValues().timezone;
|
|
37
|
+
this.config = Object.freeze({
|
|
38
|
+
cnpj: i,
|
|
39
|
+
tpAmb: a,
|
|
40
|
+
timezone: e,
|
|
41
|
+
cert: o,
|
|
42
|
+
key: n,
|
|
43
|
+
requestOptions: Object.freeze(r),
|
|
44
|
+
httpsOptions: Object.freeze(t)
|
|
45
|
+
}), Object.freeze(this);
|
|
46
|
+
}
|
|
47
|
+
confirmar(e) {
|
|
48
|
+
var r, e = new ChaveNfseValidator(e);
|
|
49
|
+
if (e.isValid()) return r = montarOpts(this.config, e.getValues(), 203202),
|
|
50
|
+
ManifestacaoNFSeController.enviar(r);
|
|
51
|
+
throw new Error(e.getError());
|
|
52
|
+
}
|
|
53
|
+
rejeitar(e, r) {
|
|
54
|
+
e = new ChaveNfseValidator(e);
|
|
55
|
+
if (!e.isValid()) throw new Error(e.getError());
|
|
56
|
+
var t, o, r = new MotivoRejeicaoValidator(r);
|
|
57
|
+
if (r.isValid()) return {
|
|
58
|
+
cMotivo: t,
|
|
59
|
+
xMotivo: o
|
|
60
|
+
} = r.getValues(), e = {
|
|
61
|
+
...montarOpts(this.config, e.getValues(), 203206),
|
|
62
|
+
cMotivo: t,
|
|
63
|
+
xMotivo: o
|
|
64
|
+
}, ManifestacaoNFSeController.enviar(e);
|
|
65
|
+
throw new Error(r.getError());
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = ManifestacaoNFSe;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
let {
|
|
2
|
+
DistribuicaoNFSeHelper,
|
|
3
|
+
RetornoNfseHelper
|
|
4
|
+
} = require("../helpers");
|
|
5
|
+
|
|
6
|
+
class DistribuicaoNFSeController {
|
|
7
|
+
static async enviar(e) {
|
|
8
|
+
var r = DistribuicaoNFSeHelper.montarRequest(e), r = await DistribuicaoNFSeHelper.enviarConsulta(r, e), e = await DistribuicaoNFSeHelper.montarResponse(r.data, e.cnpj);
|
|
9
|
+
return RetornoNfseHelper.montarRetorno({
|
|
10
|
+
json: e,
|
|
11
|
+
retorno: r
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = Object.freeze(DistribuicaoNFSeController);
|
package/lib/controllers/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
let DistribuicaoController = require("./distribuicaoDFe-controller"), RecepcaoController = require("./recepcaoEvento-controller"), controller = Object.freeze({
|
|
1
|
+
let DistribuicaoController = require("./distribuicaoDFe-controller"), DistribuicaoNFSeController = require("./distribuicaoNFSe-controller"), ManifestacaoNFSeController = require("./manifestacaoNFSe-controller"), RecepcaoController = require("./recepcaoEvento-controller"), controller = Object.freeze({
|
|
2
2
|
DistribuicaoController: DistribuicaoController,
|
|
3
|
+
DistribuicaoNFSeController: DistribuicaoNFSeController,
|
|
4
|
+
ManifestacaoNFSeController: ManifestacaoNFSeController,
|
|
3
5
|
RecepcaoController: RecepcaoController
|
|
4
6
|
});
|
|
5
7
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
let {
|
|
2
|
+
ManifestacaoNFSeHelper,
|
|
3
|
+
RetornoNfseHelper
|
|
4
|
+
} = require("../helpers");
|
|
5
|
+
|
|
6
|
+
class ManifestacaoNFSeController {
|
|
7
|
+
static async enviar(e) {
|
|
8
|
+
var {
|
|
9
|
+
reqXml: a,
|
|
10
|
+
config: r
|
|
11
|
+
} = await ManifestacaoNFSeHelper.montarRequest(e), r = await ManifestacaoNFSeHelper.enviarEvento(r, e), {
|
|
12
|
+
json: e,
|
|
13
|
+
falha: o
|
|
14
|
+
} = await ManifestacaoNFSeHelper.montarResponse(r.data);
|
|
15
|
+
return RetornoNfseHelper.montarRetorno({
|
|
16
|
+
json: e,
|
|
17
|
+
falha: o,
|
|
18
|
+
retorno: r,
|
|
19
|
+
servico: "SEFIN",
|
|
20
|
+
reqXml: a
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = Object.freeze(ManifestacaoNFSeController);
|
package/lib/env/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
let {
|
|
2
2
|
CA,
|
|
3
3
|
CA_PADRAO
|
|
4
|
-
} = require("./ca"), DISTRIBUICAO = require("./distribuicao").DISTRIBUICAO, EVENTOS = require("./evento").EVENTOS,
|
|
4
|
+
} = require("./ca"), DISTRIBUICAO = require("./distribuicao").DISTRIBUICAO, EVENTOS = require("./evento").EVENTOS, {
|
|
5
|
+
EVENTOS_NFSE,
|
|
6
|
+
NFSE_ADN,
|
|
7
|
+
NFSE_SEFIN
|
|
8
|
+
} = require("./nfse"), RECEPCAO = require("./recepcao").RECEPCAO, CODIGOS_UF = require("./uf").CODIGOS_UF, VERSION = require("./version").VERSION, ZONES = require("./zone").ZONES;
|
|
5
9
|
|
|
6
10
|
module.exports = {
|
|
7
11
|
CA: CA,
|
|
@@ -9,6 +13,9 @@ module.exports = {
|
|
|
9
13
|
CODIGOS_UF: CODIGOS_UF,
|
|
10
14
|
DISTRIBUICAO: DISTRIBUICAO,
|
|
11
15
|
EVENTOS: EVENTOS,
|
|
16
|
+
EVENTOS_NFSE: EVENTOS_NFSE,
|
|
17
|
+
NFSE_ADN: NFSE_ADN,
|
|
18
|
+
NFSE_SEFIN: NFSE_SEFIN,
|
|
12
19
|
RECEPCAO: RECEPCAO,
|
|
13
20
|
VERSION: VERSION,
|
|
14
21
|
ZONES: ZONES
|
package/lib/env/nfse.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
let NFSE_ADN = {
|
|
2
|
+
1: "https://adn.nfse.gov.br/contribuintes",
|
|
3
|
+
2: "https://adn.producaorestrita.nfse.gov.br/contribuintes"
|
|
4
|
+
}, NFSE_SEFIN = {
|
|
5
|
+
1: "https://sefin.nfse.gov.br/SefinNacional",
|
|
6
|
+
2: "https://sefin.producaorestrita.nfse.gov.br/SefinNacional"
|
|
7
|
+
}, EVENTOS_NFSE = {
|
|
8
|
+
203202: {
|
|
9
|
+
codigo: "203202",
|
|
10
|
+
elemento: "e203202",
|
|
11
|
+
xDesc: "Manifestação de NFS-e - Confirmação do Tomador"
|
|
12
|
+
},
|
|
13
|
+
203206: {
|
|
14
|
+
codigo: "203206",
|
|
15
|
+
elemento: "e203206",
|
|
16
|
+
xDesc: "Manifestação de NFS-e - Rejeição do Tomador"
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
EVENTOS_NFSE: EVENTOS_NFSE,
|
|
22
|
+
NFSE_ADN: NFSE_ADN,
|
|
23
|
+
NFSE_SEFIN: NFSE_SEFIN
|
|
24
|
+
};
|
package/lib/env/version.js
CHANGED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
let {
|
|
2
|
+
CA_PADRAO,
|
|
3
|
+
NFSE_ADN
|
|
4
|
+
} = require("../env"), SefazService = require("../services/sefaz-service"), {
|
|
5
|
+
Gzip,
|
|
6
|
+
Xml
|
|
7
|
+
} = require("../util"), PAPEIS = [ [ "toma", "tomador" ], [ "prest", "prestador" ], [ "interm", "intermediario" ] ];
|
|
8
|
+
|
|
9
|
+
function ler(e, t) {
|
|
10
|
+
return t.reduce((e, t) => e && "object" == typeof e ? e[t] : void 0, e);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function texto(e) {
|
|
14
|
+
return null == e ? "" : String(e);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function normalizarMensagens(e) {
|
|
18
|
+
return Array.isArray(e) ? e.filter(e => e && "object" == typeof e).map(e => ({
|
|
19
|
+
codigo: texto(e.Codigo),
|
|
20
|
+
descricao: texto(e.Descricao),
|
|
21
|
+
complemento: texto(e.Complemento)
|
|
22
|
+
})) : [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class DistribuicaoNFSeHelper {
|
|
26
|
+
static async enviarConsulta(e, t) {
|
|
27
|
+
return await new SefazService({
|
|
28
|
+
baseURL: NFSE_ADN[t.tpAmb],
|
|
29
|
+
ca: CA_PADRAO,
|
|
30
|
+
cert: t.cert,
|
|
31
|
+
key: t.key,
|
|
32
|
+
tpAmb: t.tpAmb,
|
|
33
|
+
requestOptions: t.requestOptions,
|
|
34
|
+
httpsOptions: t.httpsOptions
|
|
35
|
+
}).request(e);
|
|
36
|
+
}
|
|
37
|
+
static montarRequest(e) {
|
|
38
|
+
var t, r = {
|
|
39
|
+
Accept: "application/json"
|
|
40
|
+
};
|
|
41
|
+
return void 0 !== e.chNFSe ? {
|
|
42
|
+
method: "GET",
|
|
43
|
+
url: `/NFSe/${e.chNFSe}/Eventos`,
|
|
44
|
+
headers: r,
|
|
45
|
+
responseType: "text"
|
|
46
|
+
} : {
|
|
47
|
+
method: "GET",
|
|
48
|
+
url: "/DFe/" + ((t = void 0 !== e.nsu) ? (BigInt(e.nsu) - BigInt(1)).toString() : e.ultNSU),
|
|
49
|
+
params: {
|
|
50
|
+
lote: !t,
|
|
51
|
+
cnpjConsulta: e.cnpj
|
|
52
|
+
},
|
|
53
|
+
headers: r,
|
|
54
|
+
responseType: "text"
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
static async montarResponse(e, t) {
|
|
58
|
+
let r;
|
|
59
|
+
try {
|
|
60
|
+
r = JSON.parse(e);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
if (!r || "object" != typeof r || Array.isArray(r) || !r.StatusProcessamento) return null;
|
|
65
|
+
var o, e = Array.isArray(r.LoteDFe) ? r.LoteDFe : [], a = [], s = [];
|
|
66
|
+
for (o of await Promise.all(e.map(e => DistribuicaoNFSeHelper.abrirArquivoXml(e, t)))) (o.error ? s : a).push(o);
|
|
67
|
+
return {
|
|
68
|
+
statusProcessamento: texto(r.StatusProcessamento),
|
|
69
|
+
tipoAmbiente: texto(r.TipoAmbiente),
|
|
70
|
+
versaoAplicativo: texto(r.VersaoAplicativo),
|
|
71
|
+
dataHoraProcessamento: texto(r.DataHoraProcessamento),
|
|
72
|
+
alertas: normalizarMensagens(r.Alertas),
|
|
73
|
+
erros: normalizarMensagens(r.Erros),
|
|
74
|
+
documentos: a,
|
|
75
|
+
documentosErros: s
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
static async abrirArquivoXml(e, t) {
|
|
79
|
+
var e = e && "object" == typeof e ? e : {}, r = texto(e.NSU), o = texto(e.ChaveAcesso), a = texto(e.TipoDocumento);
|
|
80
|
+
if (!e.ArquivoXml) return {
|
|
81
|
+
nsu: r,
|
|
82
|
+
chaveAcesso: o,
|
|
83
|
+
tipoDocumento: a,
|
|
84
|
+
error: "ArquivoXml sem conteúdo."
|
|
85
|
+
};
|
|
86
|
+
let s;
|
|
87
|
+
try {
|
|
88
|
+
s = await Gzip.unzip(e.ArquivoXml);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
return {
|
|
91
|
+
nsu: r,
|
|
92
|
+
chaveAcesso: o,
|
|
93
|
+
tipoDocumento: a,
|
|
94
|
+
error: "Falha ao descompactar o ArquivoXml: " + e.message
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
let i;
|
|
98
|
+
try {
|
|
99
|
+
i = Xml.xmlToJson(s);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
return {
|
|
102
|
+
nsu: r,
|
|
103
|
+
chaveAcesso: o,
|
|
104
|
+
tipoDocumento: a,
|
|
105
|
+
error: "Falha ao interpretar o XML do ArquivoXml: " + e.message
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
nsu: r,
|
|
110
|
+
chaveAcesso: o,
|
|
111
|
+
tipoDocumento: a,
|
|
112
|
+
tipoEvento: texto(e.TipoEvento),
|
|
113
|
+
dataHoraGeracao: texto(e.DataHoraGeracao),
|
|
114
|
+
papel: DistribuicaoNFSeHelper.calcularPapel(a, i, t),
|
|
115
|
+
xml: s,
|
|
116
|
+
json: i
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
static calcularPapel(e, t, r) {
|
|
120
|
+
if ("NFSE" === e) {
|
|
121
|
+
var o, a, s = ler(t, [ "NFSe", "infNFSe", "DPS", "infDPS" ]);
|
|
122
|
+
for ([ o, a ] of PAPEIS) if (ler(s, [ o, "CNPJ" ]) === r) return a;
|
|
123
|
+
}
|
|
124
|
+
return "";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = Object.freeze(DistribuicaoNFSeHelper);
|
package/lib/helpers/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
let DistribuicaoHelper = require("./distribuicaoDFe-helper"), RecepcaoHelper = require("./recepcaoEvento-helper"), RetornoHelper = require("./retorno-helper"), helper = Object.freeze({
|
|
1
|
+
let DistribuicaoHelper = require("./distribuicaoDFe-helper"), DistribuicaoNFSeHelper = require("./distribuicaoNFSe-helper"), ManifestacaoNFSeHelper = require("./manifestacaoNFSe-helper"), RecepcaoHelper = require("./recepcaoEvento-helper"), RetornoHelper = require("./retorno-helper"), RetornoNfseHelper = require("./retornoNfse-helper"), helper = Object.freeze({
|
|
2
2
|
DistribuicaoHelper: DistribuicaoHelper,
|
|
3
|
+
DistribuicaoNFSeHelper: DistribuicaoNFSeHelper,
|
|
4
|
+
ManifestacaoNFSeHelper: ManifestacaoNFSeHelper,
|
|
3
5
|
RecepcaoHelper: RecepcaoHelper,
|
|
4
|
-
RetornoHelper: RetornoHelper
|
|
6
|
+
RetornoHelper: RetornoHelper,
|
|
7
|
+
RetornoNfseHelper: RetornoNfseHelper
|
|
5
8
|
});
|
|
6
9
|
|
|
7
10
|
module.exports = helper;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
let {
|
|
2
|
+
CA_PADRAO,
|
|
3
|
+
NFSE_SEFIN
|
|
4
|
+
} = require("../env"), ManifestacaoNFSeSchema = require("../schemas").ManifestacaoNFSeSchema, SefazService = require("../services/sefaz-service"), {
|
|
5
|
+
Assinatura,
|
|
6
|
+
Gzip,
|
|
7
|
+
Xml
|
|
8
|
+
} = require("../util");
|
|
9
|
+
|
|
10
|
+
function texto(e) {
|
|
11
|
+
return null == e ? "" : String(e);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function normalizarMensagens(e) {
|
|
15
|
+
return (Array.isArray(e) ? e : e ? [ e ] : []).filter(e => e && "object" == typeof e).map(e => ({
|
|
16
|
+
codigo: texto(e.codigo),
|
|
17
|
+
descricao: texto(e.descricao),
|
|
18
|
+
complemento: texto(e.complemento)
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class ManifestacaoNFSeHelper {
|
|
23
|
+
static async enviarEvento(e, t) {
|
|
24
|
+
return await new SefazService({
|
|
25
|
+
baseURL: NFSE_SEFIN[t.tpAmb],
|
|
26
|
+
ca: CA_PADRAO,
|
|
27
|
+
cert: t.cert,
|
|
28
|
+
key: t.key,
|
|
29
|
+
tpAmb: t.tpAmb,
|
|
30
|
+
requestOptions: t.requestOptions,
|
|
31
|
+
httpsOptions: t.httpsOptions
|
|
32
|
+
}).request(e);
|
|
33
|
+
}
|
|
34
|
+
static async montarRequest(e) {
|
|
35
|
+
var t = ManifestacaoNFSeSchema.montarSchema(e), t = Xml.jsonToXml(t), t = '<?xml version="1.0" encoding="UTF-8"?>' + Assinatura.assinarXml(e.cert, e.key, t, "infPedReg"), a = await Gzip.zip(t);
|
|
36
|
+
return {
|
|
37
|
+
reqXml: t,
|
|
38
|
+
config: {
|
|
39
|
+
method: "POST",
|
|
40
|
+
url: `/nfse/${e.chNFSe}/eventos`,
|
|
41
|
+
data: JSON.stringify({
|
|
42
|
+
pedidoRegistroEventoXmlGZipB64: a
|
|
43
|
+
}),
|
|
44
|
+
headers: {
|
|
45
|
+
Accept: "application/json",
|
|
46
|
+
"Content-Type": "application/json"
|
|
47
|
+
},
|
|
48
|
+
responseType: "text"
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
static async montarResponse(e) {
|
|
53
|
+
let t;
|
|
54
|
+
try {
|
|
55
|
+
t = JSON.parse(e);
|
|
56
|
+
} catch (e) {
|
|
57
|
+
return {
|
|
58
|
+
json: null
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
var a, r;
|
|
62
|
+
return t && "object" == typeof t && !Array.isArray(t) && ("eventoXmlGZipB64" in t || "erro" in t || "erros" in t) ? (e = {
|
|
63
|
+
tipoAmbiente: texto(t.tipoAmbiente),
|
|
64
|
+
versaoAplicativo: texto(t.versaoAplicativo),
|
|
65
|
+
dataHoraProcessamento: texto(t.dataHoraProcessamento),
|
|
66
|
+
alertas: normalizarMensagens(t.alertas),
|
|
67
|
+
erros: normalizarMensagens(t.erros || t.erro),
|
|
68
|
+
evento: {}
|
|
69
|
+
}, "eventoXmlGZipB64" in t && ({
|
|
70
|
+
evento: a,
|
|
71
|
+
falha: r
|
|
72
|
+
} = await ManifestacaoNFSeHelper.abrirEvento(t.eventoXmlGZipB64), e.evento = a,
|
|
73
|
+
r) ? {
|
|
74
|
+
json: e,
|
|
75
|
+
falha: r
|
|
76
|
+
} : {
|
|
77
|
+
json: e
|
|
78
|
+
}) : {
|
|
79
|
+
json: null
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
static async abrirEvento(e) {
|
|
83
|
+
var t = "Falha ao abrir o evento retornado: ";
|
|
84
|
+
if (!e) return {
|
|
85
|
+
evento: {},
|
|
86
|
+
falha: t + "eventoXmlGZipB64 sem conteúdo."
|
|
87
|
+
};
|
|
88
|
+
try {
|
|
89
|
+
var a = await Gzip.unzip(e);
|
|
90
|
+
return {
|
|
91
|
+
evento: {
|
|
92
|
+
xml: a,
|
|
93
|
+
json: Xml.xmlToJson(a)
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
} catch (e) {
|
|
97
|
+
return {
|
|
98
|
+
evento: {},
|
|
99
|
+
falha: t + e.message
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = Object.freeze(ManifestacaoNFSeHelper);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
let RESPOSTA_INESPERADA = {
|
|
2
|
+
ADN: "Resposta inesperada do ADN",
|
|
3
|
+
SEFIN: "Resposta inesperada da SEFIN"
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
class RetornoNfseHelper {
|
|
7
|
+
static montarRetorno(r) {
|
|
8
|
+
var {
|
|
9
|
+
json: r,
|
|
10
|
+
retorno: e,
|
|
11
|
+
servico: o = "ADN",
|
|
12
|
+
reqXml: s,
|
|
13
|
+
falha: a
|
|
14
|
+
} = r, t = e.status, n = {
|
|
15
|
+
data: r || {}
|
|
16
|
+
}, s = (void 0 !== s && (n.reqXml = s), n.resBody = e.data || "", n.status = t,
|
|
17
|
+
RESPOSTA_INESPERADA[o] + ` (HTTP ${t}).`);
|
|
18
|
+
return e.transportError ? ({
|
|
19
|
+
code: o,
|
|
20
|
+
message: e
|
|
21
|
+
} = e.transportError, n.data = {}, n.error = `Falha de transporte (${o}): ` + e,
|
|
22
|
+
n.transportError = {
|
|
23
|
+
code: o,
|
|
24
|
+
message: e
|
|
25
|
+
}) : r ? "NENHUM_DOCUMENTO_LOCALIZADO" !== r.statusProcessamento && (o = Array.isArray(r.erros) ? r.erros : [],
|
|
26
|
+
2 !== Math.floor(t / 100) || "REJEICAO" === r.statusProcessamento || 0 < o.length ? n.error = o.length ? o.map(r => r.codigo + ": " + r.descricao).join("; ") : s : a && (n.error = a)) : n.error = s,
|
|
27
|
+
n;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = Object.freeze(RetornoNfseHelper);
|