-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
181 lines (166 loc) · 4.81 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const Nightmare = require('nightmare');
let nightmare = null;
Nightmare.action('show',
(name, options, parent, win, renderer, done) => {
parent.respondTo('show', (inactive, done) => {
if (inactive) {
win.showInactive();
} else {
win.show();
}
done();
});
done();
},
function (inactive, done) {
this.child.call('show', inactive, done);
});
Nightmare.action('hide',
(name, options, parent, win, renderer, done) => {
parent.respondTo('hide', (debugMode, done) => {
if (!debugMode) {
win.hide();
}
done();
});
done();
},
function (debugMode, done) {
this.child.call('hide', debugMode, done);
});
require('nightmare-upload')(Nightmare);
// TODO: module.exports = (rfc, password, mode, rango) => {
module.exports = (rfc, password, rango, debug) => {
if (debug) {
nightmare = Nightmare({
show: true
});
} else {
nightmare = Nightmare();
}
let mode = 'emitidas';
validaciones(rfc, password, mode, rango);
if (mode === 'recibidas') {
mode = '#ctl00_MainContent_RdoTipoBusquedaReceptor';
}
if (mode === 'emitidas') {
mode = '#ctl00_MainContent_RdoTipoBusquedaEmisor';
}
const satUrl = 'https://portalcfdi.facturaelectronica.sat.gob.mx/';
const nightmarePromise = new Promise((resolve, reject) => {
nightmare
.goto(satUrl)
.wait(5000)
.click('#buttonFiel')
.wait('#submit')
.upload('#fileCertificate', 'C:\\Users\\mauricio.m\\Documents\\00001000000411530467.cer')
.upload('#filePrivateKey', 'C:\\Users\\mauricio.m\\Documents\\Claveprivada_FIEL_MAPM940720QI5_20180714_110105.key')
.insert('#privateKeyPassword', 'Tomas135')
.click('#submit')
// .wait(30000)
// .wait('#rfc')
// .insert('#rfc', rfc)
// .insert('#password', password)
// // El span aparece en el home despues de iniciar sesión
.wait('#ctl00_LblRfcAutenticado')
// Verificar que el RFC proporcionado sea el que inicio sesión
.evaluate(rfc => {
if (rfc !== (document.querySelector('#ctl00_LblRfcAutenticado').innerText.split(':')[1]).split(' ')[1]) {
throw new Error('RFC PROPORCIONADO NO COINCIDE CON EL QUE INICIO SESION');
}
}, rfc)
.hide(debug)
.click(mode)
.click('#ctl00_MainContent_BtnBusqueda')
.wait('#ctl00_MainContent_RdoFechas')
.click('#ctl00_MainContent_RdoFechas')
.wait(1500)
.evaluate(rango => {
function parseDate(date) {
if (!(date instanceof Date)) {
date = new Date(date);
}
let dd = date.getDate();
let mm = date.getMonth() + 1;
const yyyy = date.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
return dd + '/' + mm + '/' + yyyy;
}
updateDateField('ctl00$MainContent$CldFechaInicial2$Calendario_text', parseDate(rango.inicio));
updateDateField('ctl00$MainContent$CldFechaFinal2$Calendario_text', parseDate(rango.final));
}, rango)
.wait(500)
.select('#ctl00_MainContent_DdlEstadoComprobante', 1)
.wait(500)
.click('#ctl00_MainContent_BtnBusqueda')
.wait(5000)
.evaluate(() => {
const promises = [];
for (const facturaElement of document.getElementsByName('BtnDescarga')) {
promises.push(
new Promise((resolve, reject) => {
resolve(window.location.href.replace(window.location.pathname, ''));
})
);
// Promises.push(
// new Promise((resolve, reject) => {
// const regex = /'.*?'/g;
// const downloadFunction = facturaElement.getAttribute('onclick');
// let blobUrl = regex.exec(downloadFunction)[0].slice(1, -1);
// blobUrl = (window.location.href.replace(window.location.pathname, '')) + '/' + blobUrl;
// const xhr = new XMLHttpRequest();
// xhr.open('GET', blobUrl, true);
// xhr.onload = () => {
// if (xhr.readyState === 4) {
// if (xhr.status === 200) {
// resolve(xhr.responseText);
// } else {
// console.error(xhr.statusText);
// reject(xhr.statusText);
// }
// }
// };
// xhr.send();
// })
// );
}
return Promise.all(promises);
})
.end(xmls => {
resolve(xmls);
})
.catch(e => {
reject(e);
});
});
return nightmarePromise;
};
function validaciones(rfc, password, mode, rango) {
if (!rfc) {
throw new Error('RFC es requerido');
}
if (!password) {
throw new Error('La contraseña es requerida');
}
if (mode !== 'recibidas' && mode !== 'emitidas') {
throw new Error(`Modo inválido, recibí ${mode}`);
}
if (!rango.inicio) {
throw new Error('Falta la fecha inicial en el rango');
}
if (!rango.final) {
throw new Error('Falta la fecha final en el rango');
}
if (!(rango.inicio instanceof Date)) {
throw new TypeError('La fecha inicial no es de tipo Date');
}
if (!(rango.final instanceof Date)) {
throw new TypeError('La fecha final no es de tipo Date');
}
}