falcotura-atv-sdk
Version:
Librería (SDK) de Javascript/NodeJS para acceder al API de Administración Tributaria Virtual (ATV) del Ministerio de Hacienda.
73 lines • 3.43 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateDocumentCommand = void 0;
const billDocToAtv_1 = require("../../mappers/billDocToAtv");
const genXML_1 = require("../../../lib/genXML");
const CreateDocFactory_1 = require("../../core/CreateDocFactory");
const dateUtils_1 = require("../../../utils/dateUtils");
const options = {
prod: {
serviceUrl: 'https://api.comprobanteselectronicos.go.cr/recepcion/v1/recepcion'
},
stg: {
serviceUrl: 'https://api-sandbox.comprobanteselectronicos.go.cr/recepcion/v1/recepcion'
}
};
class CreateDocumentCommand {
constructor(scope) {
this.serviceUrl = options[scope.mode].serviceUrl;
this.createDoc = new CreateDocFactory_1.CreateDocFactory();
}
execute(dto) {
return __awaiter(this, void 0, void 0, function* () {
const documentName = dto.document.documentName || 'FacturaElectronica'; // TODO NotaDebitoElectronica
const document = this.createDoc.createDocument(dto.document);
const atvDocument = (0, billDocToAtv_1.mapDocumentToAtvFormat)(documentName, document);
const xml = yield (0, genXML_1.genXML)(documentName, atvDocument, dto.signatureOptions);
const command = yield this.createDocumentCommand(document, xml, dto.token);
return {
command,
extraData: {
xml,
document: atvDocument
}
};
});
}
createDocumentCommand(document, xml, token) {
return __awaiter(this, void 0, void 0, function* () {
return {
url: this.serviceUrl,
method: 'post',
data: Object.assign(Object.assign({ clave: document.clave, fecha: (0, dateUtils_1.toCostaRicaISOString)(document.issueDate), emisor: {
tipoIdentificacion: document.emitter.identifierType,
numeroIdentificacion: document.emitter.identifierId
} }, (document.receiver && {
receptor: {
tipoIdentificacion: document.receiver.identifierType,
numeroIdentificacion: document.receiver.identifierId
}
})), { comprobanteXml: this.encodeXML(xml) }),
headers: {
Authorization: 'bearer ' + token,
'Content-Type': 'application/json'
}
};
});
}
encodeXML(xmlStr) {
const buffer = Buffer.from(xmlStr);
return buffer.toString('base64');
}
}
exports.CreateDocumentCommand = CreateDocumentCommand;
//# sourceMappingURL=index.js.map