@devlas/dte-sii 2.12.10 → 2.12.12

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/CafSolicitor.js CHANGED
@@ -224,6 +224,8 @@ class CafSolicitor {
224
224
  // parsear <form action> + hidden fields → submit con esos campos + los propios.
225
225
  const authResponse = await this.session.ensureSession('/cvc_cgi/dte/of_solicita_folios');
226
226
 
227
+ this._saveDebug(debugDir, 'step0-ensureSession.html', authResponse.body || '');
228
+
227
229
  let response;
228
230
  if (this._requiresAuthentication(authResponse.body)) {
229
231
  // Sesión sigue inválida tras ensureSession — el chequeo de más abajo
@@ -235,6 +237,8 @@ class CafSolicitor {
235
237
  response = await this.session.submitForm(formAction, { ...hiddenFields, ...fields });
236
238
  }
237
239
 
240
+ this._saveDebug(debugDir, 'step1-submit.html', response.body || '');
241
+
238
242
  // Guardar sesión para reutilización
239
243
  if (this.sessionPath) {
240
244
  this.session.saveSession(this.sessionPath);
@@ -371,6 +375,19 @@ class CafSolicitor {
371
375
  */
372
376
  async _processMultiStepFlow(response, rut, dv, tipoDte, cantidad, debugDir) {
373
377
  let currentHtml = response.body || '';
378
+ const realFormAction = SiiSession.extractFormAction(currentHtml);
379
+
380
+ // Si el <form> real ya apunta a of_confirma_folio, el SII preseleccionó
381
+ // COD_DOCTO server-side (coincide con el tipoDte enviado en el paso 1) y no
382
+ // hace falta pasar por of_solicita_folios_dcto. Ese endpoint solo se dispara
383
+ // vía JS (changeRegregion) si el usuario cambia el <select> a mano — su sola
384
+ // presencia como string dentro de un <script> no significa que sea el
385
+ // próximo paso real. Ir directo a _processStep3, que ya arma COD_DOCTO/
386
+ // CANT_DOCTOS explícitamente (extractInputValues no lee <select>, así que
387
+ // dejarlo entrar a la rama de abajo pierde el tipo de documento).
388
+ if (realFormAction && realFormAction.includes('of_confirma_folio')) {
389
+ return this._processStep3(response, rut, dv, tipoDte, cantidad, debugDir);
390
+ }
374
391
 
375
392
  // Paso 2: of_solicita_folios_dcto
376
393
  if (currentHtml.includes('of_solicita_folios_dcto')) {
package/SiiSession.js CHANGED
@@ -456,6 +456,22 @@ class SiiSession {
456
456
  }
457
457
  }
458
458
 
459
+ // Pantalla "ESCOJA COMO DESEA INGRESAR" — aparece cuando el certificado está
460
+ // habilitado para representación electrónica de otros RUT. No es un fallo de
461
+ // autenticación (aunque su <title> genérico "Autenticación" coincida con el
462
+ // check de más arriba y por eso el caller podría malinterpretarlo como tal):
463
+ // hay que seguir el link "Continuar" (GET al mismo recurso) para llegar al
464
+ // formulario real.
465
+ if (response.body && response.body.includes('ESCOJA COMO DESEA INGRESAR')) {
466
+ const continuarMatch = response.body.match(/<a\s+href="([^"]+)"[^>]*>\s*Continuar\s*<\/a>/i);
467
+ if (continuarMatch) {
468
+ const continuarUrl = new URL(continuarMatch[1], targetUrl).toString();
469
+ const continuado = await this.request(continuarUrl, { method: 'GET' });
470
+ const continuadoResult = await this.followRedirects(continuado);
471
+ response = continuadoResult.response;
472
+ }
473
+ }
474
+
459
475
  // Si hay redirección a af_anular1
460
476
  if (response.body && response.body.includes('/cvc_cgi/dte/af_anular1')) {
461
477
  const continued = await this.request(targetUrl, { method: 'GET' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devlas/dte-sii",
3
- "version": "2.12.10",
3
+ "version": "2.12.12",
4
4
  "description": "Facturación y boletas electrónicas para el SII de Chile. Genera, timbra, firma y envía DTEs, libros electrónicos y automatiza la certificación.",
5
5
  "main": "index.js",
6
6
  "types": "dte-sii.d.ts",