@openade/fe
Version:
Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)
142 lines • 5.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SDICOOPClient = void 0;
const http_service_1 = require("./http.service");
const xml_service_1 = require("./xml.service");
class SDICOOPClient {
constructor(config) {
this.config = {
timeout: 30000,
debug: false,
...config,
};
this.xmlService = new xml_service_1.XmlService();
this.httpService = new http_service_1.HttpService();
}
async sendInvoice(filename, xmlContent) {
if (this.config.debug) {
console.log(`[SDICOOP] Sending invoice: ${filename}`);
}
try {
const result = await this.sendViaSdiCoop(filename, xmlContent);
if (this.config.debug) {
console.log(`[SDICOOP] Response: IdentificativoSdI=${result.identificativoSdI}`);
}
return {
success: true,
identifcativoSdI: result.identificativoSdI,
};
}
catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
}
async queryInvoiceStatus(identificativoSdI) {
if (this.config.debug) {
console.log(`[SDICOOP] Querying status for: ${identificativoSdI}`);
}
try {
const soapRequest = this.createStatusQueryRequest(identificativoSdI);
const response = await this.httpService.request({
url: this.config.endpoint,
method: 'POST',
headers: {
'Content-Type': 'text/xml; charset=utf-8',
SOAPAction: 'http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0/richiestaStatoFattura',
},
body: soapRequest,
timeout: this.config.timeout,
});
if (response.status !== 200) {
throw new Error(`SDI request failed with status ${response.status}`);
}
const parsed = await this.xmlService.parse(response.data);
return this.parseStatusResponse(parsed);
}
catch (error) {
return {
status: 'error',
details: error instanceof Error ? error.message : 'Unknown error',
};
}
}
async sendViaSdiCoop(filename, xmlContent) {
const soapRequest = this.createInvoiceRequest(filename, xmlContent);
const response = await this.httpService.request({
url: this.config.endpoint,
method: 'POST',
headers: {
'Content-Type': 'text/xml; charset=utf-8',
SOAPAction: 'http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0/riceviFatture',
},
body: soapRequest,
timeout: this.config.timeout,
});
if (response.status !== 200) {
throw new Error(`SDI request failed with status ${response.status}`);
}
const parsed = await this.xmlService.parse(response.data);
return this.parseInvoiceResponse(parsed);
}
createInvoiceRequest(filename, xmlContent) {
return `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0">
<soap:Header/>
<soap:Body>
<ns:riceviFatture>
<ns:fileSdI>
<ns:nomeFile>${filename}</ns:nomeFile>
<ns:contenuto>${Buffer.from(xmlContent).toString('base64')}</ns:contenuto>
</ns:fileSdI>
</ns:riceviFatture>
</soap:Body>
</soap:Envelope>`;
}
createStatusQueryRequest(identificativoSdI) {
return `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0">
<soap:Header/>
<soap:Body>
<ns:richiestaStatoFattura>
<ns:identificativoSdI>${identificativoSdI}</ns:identificativoSdI>
</ns:richiestaStatoFattura>
</soap:Body>
</soap:Envelope>`;
}
parseInvoiceResponse(parsed) {
const soapBody = parsed['soap:Envelope']?.['soap:Body'];
const response = soapBody?.['ns:riceviFattureResponse'];
if (!response) {
throw new Error('Invalid SOAP response format');
}
const identificativoSdI = response['ns:identificativoSdI'];
if (!identificativoSdI) {
throw new Error('Missing IdentificativoSdI in response');
}
return { identificativoSdI };
}
parseStatusResponse(parsed) {
const soapBody = parsed['soap:Envelope']?.['soap:Body'];
const response = soapBody?.['ns:richiestaStatoFatturaResponse'];
if (!response) {
return { status: 'error', details: 'Invalid response format' };
}
const stato = response['ns:stato'];
const descrizione = response['ns:descrizione'];
switch (stato) {
case 'INVIATA':
return { status: 'pending', details: descrizione };
case 'CONSEGNATA':
return { status: 'delivered', details: descrizione };
case 'SCARTATA':
return { status: 'rejected', details: descrizione };
default:
return { status: 'pending', details: descrizione };
}
}
}
exports.SDICOOPClient = SDICOOPClient;
//# sourceMappingURL=sdicoop.client.js.map