-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (48 loc) · 2.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {GerarXml,tratarRetorno,cancelamento} from "./EmissaoXml.js"
import axios from 'axios';
import * as convert from "xml-js"
import { default as FormData } from "form-data"
export function EnviarEmissao(url,username,password,jsonDeEntrada){
return new Promise((resolve,reject)=>{
const formData = new FormData();
GerarXml(jsonDeEntrada).then((xml)=>{
const xmlBuffer = Buffer.from(xml, 'utf-8');
formData.append('xml', xmlBuffer, "nota.xml");
axios.post(url,formData, {
headers: {
'Authorization': 'Basic ' + Buffer.from(username + ":" + password).toString('base64'),
'Content-Type': 'multipart/form-data'
}
}
).then(function (retorno) {
const converter = convert.xml2json(retorno.data,{compact: true,spaces:4})
resolve(tratarRetorno(JSON.parse(converter).retorno))
})
.catch(function (err) {
reject(err)
});
}).catch(reject)
})
}
export function EnviarCancelamento(url,username,password,jsonDeEntrada){
return new Promise((resolve,reject)=>{
const formData = new FormData();
cancelamento(jsonDeEntrada).then((xml)=>{
const xmlBuffer = Buffer.from(xml, 'utf-8');
formData.append('xml', xmlBuffer, "nota.xml");
axios.post(url,formData, {
headers: {
'Authorization': 'Basic ' + Buffer.from(username + ":" + password).toString('base64'),
'Content-Type': 'multipart/form-data'
}
}
).then(function (retorno) {
const converter = convert.xml2json(retorno.data,{compact: true,spaces:4})
resolve(tratarRetorno(JSON.parse(converter).retorno))
})
.catch(function (err) {
reject(err)
});
}).catch(reject)
})
}