UNPKG

falcotura-atv-sdk

Version:

Librería (SDK) de Javascript/NodeJS para acceder al API de Administración Tributaria Virtual (ATV) del Ministerio de Hacienda.

115 lines 5.12 kB
"use strict"; 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.CreateReceptorMessageCommand = void 0; const types_1 = require("../../core/types"); const billDocToAtv_1 = require("../../mappers/billDocToAtv"); const genXML_1 = require("../../../lib/genXML"); const ReceptorConsecutive_1 = require("../../core/ReceptorConsecutive"); 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 CreateReceptorMessageCommand { constructor(scope) { this.serviceUrl = options[scope.mode].serviceUrl; } execute(input) { return __awaiter(this, void 0, void 0, function* () { const receptorMessageProps = this.processDocument(input); const atvDocument = (0, billDocToAtv_1.mapReceptorMessageToAtvFormat)(receptorMessageProps); const xml = yield (0, genXML_1.genXML)('MensajeReceptor', atvDocument, input.signatureOptions); const command = yield this.createDocumentCommand(receptorMessageProps, xml, input.token); return { command, extraData: { xml, document: atvDocument } }; }); } createDocumentCommand(receptorMessageProps, xml, token) { return __awaiter(this, void 0, void 0, function* () { return { url: this.serviceUrl, method: 'post', data: { clave: receptorMessageProps.clave, fecha: (0, dateUtils_1.toCostaRicaISOString)(receptorMessageProps.documentIssueDate), emisor: { tipoIdentificacion: receptorMessageProps.emitterIdentifierType, numeroIdentificacion: receptorMessageProps.emitterIdentifier }, receptor: { tipoIdentificacion: receptorMessageProps.receptorIdentifierType, numeroIdentificacion: receptorMessageProps.receptorIdentifier }, comprobanteXml: this.encodeXML(xml), consecutivoReceptor: receptorMessageProps.receptorConcecutive }, headers: { Authorization: 'bearer ' + token, 'Content-Type': 'application/json' } }; }); } encodeXML(xmlStr) { const buffer = Buffer.from(xmlStr); return buffer.toString('base64'); } acceptationStateToDocumentType(aceptationState) { switch (aceptationState) { case types_1.AceptationStates.ACCEPTED: return '05'; case types_1.AceptationStates.PARTIALLY_ACCEPTED: return '06'; default: return '07'; } } processDocument(input) { try { const receptorConsecutive = ReceptorConsecutive_1.ReceptorConsecutive.create({ branch: input.branch, terminal: input.terminal, documentType: this.acceptationStateToDocumentType(input.aceptationState), consecutive: input.consecutive }); return { clave: input.clave, emitterIdentifier: input.emitterIdentifier, emitterIdentifierType: input.emitterIdentifierType, receptorIdentifier: input.receptorIdentifier, receptorIdentifierType: input.receptorIdentifierType, documentIssueDate: input.documentIssueDate, activityCode: input.activityCode, taxCondition: input.taxCondition, totalTaxes: input.totalTaxes, totalSale: input.totalSale, aceptationState: input.aceptationState, aceptationDetailMessage: input.aceptationDetailMessage, receptorConcecutive: receptorConsecutive.value }; } catch (err) { throw new Error(`Error parsing the document ${err}`); } } } exports.CreateReceptorMessageCommand = CreateReceptorMessageCommand; //# sourceMappingURL=index.js.map