@angro/nav-connector

中文文档
6.1.1发布于 2024/01/10GPL-3.0CommonJS近 30 天下载 5433

Node.js module which provides an interface for communicating with NAV online invoice service.

原始使用文档
文档目录16 节
  1. Installation
  2. Example
  3. API
  4. NavConnector
  5. navConnector.manageInvoice()
  6. navConnector.manageAnnulment()
  7. navConnector.queryTransactionStatus()
  8. navConnector.testConnection()
  9. navConnector.queryInvoiceData()
  10. navConnector.queryInvoiceDigest()
  11. navConnector.queryTaxpayer()
  12. Error handling
  13. Tests
  14. TODO
  15. Maintainers
  16. License

nav-connector

codecov npm (scoped) license nav nav-interface

Node.js module which provides an interface for communicating with NAV online invoice service.

This module was developed in order to satisfy the following specification:
Online invoice interface specification

Installation

Tested with version 12.18.3 of Node.js.

$ npm install @angro/nav-connector

Example

const NavConnector = require('@angro/nav-connector');

/* Your technical user's data. */
const technicalUser = {
  login: 'login123',
  password: 'password',
  taxNumber: '12345678',
  signatureKey: 'signatureKey',
  exchangeKey: 'exchangeKey',
};

const softwareData = {
  softwareId: '123456789123456789',
  softwareName: 'string',
  softwareOperation: 'LOCAL_SOFTWARE',
  softwareMainVersion: 'string',
  softwareDevName: 'string',
  softwareDevContact: 'string',
  softwareDevCountryCode: 'HU',
  softwareDevTaxNumber: 'string',
};

const baseURL = 'https://api-test.onlineszamla.nav.gov.hu/invoiceService/v3/';

/* Create the nav connector interface. */
const navConnector = new NavConnector({ technicalUser, softwareData, baseURL });

(async function sendInvoice() {
  try {
    /* On app start You can test connection to the NAV service and user given data validity.
       testConnection() will throw if a tokenExchangeRequest operation is not successful. */
    await navConnector.testConnection();

    /* Send invoice to the NAV service.
       invoiceOperations is the InvoiceOperationListType in the specification. */
    const invoiceOperations = {
      compressedContent: false,
      invoiceOperation: [
        {
          index: 1,
          operation: 'CREATE',
          invoice: 'invoice xml in base64 encoding',
        },
      ],
    };

    const transactionId = await navConnector.manageInvoice(invoiceOperations);

    /* Check previously sent invoice processing status.
       processingResults is the ProcessingResultListType in the specification. */
    const processingResults = await navConnector.queryTransactionStatus({
      transactionId,
    });

    /* Check processingResults.length.
        If the array is empty then transactionId was invalid. */
    if (processingResults.length) {
      /* Handle invoice status responses. */
    }
  } catch (error) {
    /* Handle errors. See bellow for details. */
  }
})();

API

NavConnector

Class representing the implementation of the NAV online invoice data service specification.

/**
 * Create a navConnector.
 * @param {Object} params Constructor params.
 * @param {Object} params.technicalUser Technical user data.
 * @param {Object} params.softwareData Software data.
 * @param {String} [params.baseURL=https://api.onlineszamla.nav.gov.hu/invoiceService/v3/] Axios baseURL.
 * @param {number} [params.timeout=70000] Axios default timeout integer in milliseconds.
 */
const navConnector = new NavConnector({ technicalUser, softwareData });

Axios timeout option is needed because during NAV service outages, requests may never timeout if axios timeout option is not set.
According to the NAV online invoice service documentation the request timeout is set to 5000 ms on the service side but in practice there may be no timeout or there can be a gateway timeout after 60 seconds. The timeout is set to 70000 milliseconds (70 sec) in axios as default.
You can fine tune this value but its strongly suggested to keep it above 60 seconds to avoid dropped responses.

const navConnector = new NavConnector({
  technicalUser,
  softwareData,
  timeout: 70000,
});

navConnector.manageInvoice()

Method to send a single or multiple invoices to the NAV service. The method returns the transaction id of the operation which can be used later to get the status of the invoice processing status of this request.

/**
 * Send request to NAV service to manage invoices.
 * @async
 * @param {Object} invoiceOperations Request object for xml conversion and send.
 * @returns {Promise<string>} Manage invoice operation transaction id.
 */
const transactionId = await navConnector.manageInvoice(invoiceOperations);

Example for invoiceOperations parameter:

const invoiceOperations = {
  compressedContent: false,
  invoiceOperation: [
    {
      index: 1,
      invoiceOperation: 'CREATE',
      invoiceData: 'invoice xml in base64 encoding',
      electronicInvoiceHash: 'SHA3-512'
    },
    {
      index: 2,
      invoiceOperation: 'STORNO',
      invoiceData: 'invoice xml in base64 encoding',
    },
  ],
};

Take note You have to compress the invoice by yourself before using the manageInvoice method.

const invoiceOperations = {
  compressedContent: true,
  invoiceOperation: [
    {
      index: 1,
      invoiceOperation: 'CREATE',
      invoiceData: 'compressed invoice xml in base64 encoding',
      electronicInvoiceHash: 'SHA3-512'
    },
  ],
};

navConnector.manageAnnulment()

Method to send a single or multiple invoice annulments to the NAV service. The method returns the transaction id of the operation which can be used later to get the status of the invoice processing status of this request.

/**
 * Send request to NAV service to manage annulment invoices.
 * @async
 * @param {Object} annulmentOperations Request object for xml conversion and send.
 * @returns {Promise<string>} Manage invoice operation transaction id.
 */
const transactionId = await navConnector.manageAnnulment(annulmentOperations);

Example for annulmentOperations parameter:

const annulmentOperations = {
  annulmentOperation: [
    {
      index: 1,
      annulmentOperation: 'ANNUL',
      invoiceAnnulment: 'invoice xml in base64 encoding',
    },
    {
      index: 2,
      annulmentOperation: 'ANNUL',
      invoiceAnnulment: 'invoice xml in base64 encoding',
    },
  ],
};

navConnector.queryTransactionStatus()

Method to get the processing status of previously send invoices. The resolved return value is the ProcessingResultListType of the specification.

/**
 * Get the result of a previously sent manage invoice request.
 * @async
 * @param {Object} params Function params.
 * @param {string} params.transactionId Manage invoice operation transaction id.
 * @param {boolean} [params.returnOriginalRequest=false] Flag for api response to contain the original invoice.
 * @returns {Promise<Array>} processingResults
 */
const processingResults = await navConnector.queryTransactionStatus({
  transactionId,
  returnOriginalRequest: true,
});

processingResults.forEach(processingResult => {
  const {
    index,
    invoiceStatus,
    originalRequest,
    compressedContentIndicator,
    businessValidationMessages,
    technicalValidationMessages,
  } = processingResult;

  /* Property businessValidationMessages and technicalValidationMessages
     are always normalized to arrays even if there were no validation messages. */
});

This function does type conversion for number and boolean typed values in the response according to the NAV service documentation.

navConnector.testConnection()

Method to test connection, user auth data and keys validity with a tokenExchangeRequest.

/**
 * Test connection, user auth data and keys validity with a tokenExchangeRequest.
 * @async
 * @throws {Object} Will throw an error if there was a network expectation
 * or any user given auth data or key is invalid.
 */
try {
  await navConnector.testConnection();
} catch (error) {
  /* Log the error. */
}

navConnector.queryInvoiceData()

Method to query previously sent invoices with invoice number.

/**
 * Query previously sent invoices with invoice number
 * @async
 * @param {Object} params Function params.
 * @param {Object} params.invoiceQuery Query single invoice with invoice number.
 * @returns {Promise<Object>} response
 */

Query by invoice number

const invoiceQuery = {
  invoiceNumber: 'invoiceNumber',
  invoiceDirection: 'OUTBOUND',
};

const response = await navConnector.queryInvoiceData({
  invoiceQuery,
});

const { invoiceData, auditData, compressedContentIndicator } = response;

/* If no invoice was found with the given query then queryResult is undefined. */
if (!invoiceData) {
  return;
}

const { invoiceResult, invoiceDigestList } = queryResult;

This function does type conversion for number and boolean typed values in the response according to the NAV service documentation.

navConnector.queryInvoiceDigest()

Method to query previously sent invoices with query params.

/**
 * Query previously sent invoices with query params.
 * @async
 * @param {Object} params Function params.
 * @param {number} params.page Integer page to query.
 * @param {string} params.invoiceDirection inbound or outbound request type
 * @param {Object} params.queryParams Query multiple invoices with params.
 * @param {string} queryParams.dateFrom - REQUIRED valid date string to search from
 * @param {string} queryParams.dateTo - REQUIRED valid date string to search to
 * @param {string} queryParams.taxNumber - OPTIONAL Tax number of the invoice supplier or customer
 * @param {string} queryParams.groupMemberTaxNumber - OPTIONAL Tax number of group member for the invoice supplier or customer
 * @param {string} queryParams.name - OPTIONAL Left side text matching for the invoice supplier or customer search parameter
 * @param {string} queryParams.invoiceCategory - OPTIONAL Invoice category type
 * @param {string} queryParams.paymentMethod - OPTIONAL Payment method
 * @param {string} queryParams.invoiceAppearance - OPTIONAL Appearance of the invoice
 * @param {string} queryParams.source - OPTIONAL Data report source
 * @param {string} queryParams.currency - OPTIONAL Invoice currency
 * @param {string} queryParams.transactionId - OPTIONAL The searched transaction ID
 * @param {number} queryParams.index - OPTIONAL Index of the searched invoice within the transaction
 * @param {string} queryParams.invoiceOperation - OPTIONAL Invoice operation search parameter
 * @returns {Promise<Object>} response
 */

Query by parameters

const queryParams = {
  dateFrom: '2017-12-28',
  dateTo: '2017-12-28',
};

const response = await navConnector.queryInvoiceDigest({
  invoiceDirection: 'OUTBOUND',
  page: 1,
  queryParams,
});

const { currentPage, availablePage, queryResult } = response;

/* If no invoice was found with the given query then queryResult is undefined. */
if (!queryResult) {
  return;
}

const { invoiceDigestList } = queryResult;

This function does type conversion for number and boolean typed values in the response according to the NAV service documentation.

navConnector.queryTaxpayer()

Method to get taxpayer information by tax number.
It resolves to an object containing taxpayerValidity and taxpayerData properties.
Keep in mind the taxpayerData property is not returned by the NAV service if the tax number is non existent.

/**
 * Get taxpayer information by tax number.
 * @param {string} taxNumber Taxpayer tax number to get information for.
 * @returns {Promise<Object>} Taxpayer information with taxpayerValidity, taxpayerData, incorporation fields.
 */
const { taxpayerValidity, taxpayerData, incorporation } = await navConnector.queryTaxpayer(
  '12345678'
);

if (!taxpayerValidity) {
  /* Taxpayer is non existent or inactive. */
} else if (taxpayerData) {
  /* The taxpayerData property was returned by the service. */
}

This function does type conversion for boolean typed values in the response according to the NAV service documentation.

Error handling

All methods can throw expectation and You can fine tune how to log these error, handle them or retry the request if possible.

try {
  await navConnector.testConnection();
} catch (error) {
  /* Axios error instance.*/
  const { response, request } = error;

  if (response) {
    if (response.status === 504) {
      /* Gateway timeout, retryable. */
    } else if (response.data.result.errorCode === 'OPERATION_FAILED') {
      /* Server side error, retryable. */
    } else {
      /* Log the error and fix the request then resend it. */
    }
  } else if (request) {
    /* http.ClientRequest instance.
       Possible network error. Retryable. */
  } else {
    /* Something happened in setting up the request that triggered an Error.
       Log the error and try to fix the problem then resend the request. */
  }
}

The error.response.data object is always normalized to the following format:

{
  result: {
    funcCode: 'funcCode',
    errorCode: 'errorCode',
    message: 'message',
  },
  technicalValidationMessages: [
    {
      validationResultCode: 'validationResultCode',
      validationErrorCode: 'validationErrorCode',
      message: 'message',
    },
    {
      validationResultCode: 'validationResultCode',
      validationErrorCode: 'validationErrorCode',
      message: 'message',
    },
  ],
}

Take note properties funcCode, errorCode and message can be undefined and technicalValidationMessages length can be zero but response.data and result are always an object and technicalValidationMessages is always an array.

Tests

Copy the file named .env.example and rename it to .env in the root of the repository and replace placeholder values with your own technical user's data.
⚠️Never edit the .env.example file and never commit your sensitive data!

$ npm run test

TODO

These operations are currently not supported by the project:

  • queryInvoiceChainDigest
  • queryInvoiceCheck
  • queryTransactionList

Maintainers

This repository is maintained by ANGRO Nagykereskedelmi Kft.

License

GPL-3.0

版本与发布记录

@angro/nav-connector 共发布 24 个版本,首次发布于 2018/04/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.1.1latest2024/01/102 年前
6.1.02021/12/024 年前
6.0.42021/11/194 年前
6.0.32021/01/295 年前
6.0.22020/12/175 年前
6.0.12020/11/135 年前

关于 @angro/nav-connector 的常见问题

@angro/nav-connector 是什么?

Node.js module which provides an interface for communicating with NAV online invoice service.

如何安装 @angro/nav-connector?

在项目目录运行 npm install @angro/nav-connector,或使用 pnpm add / yarn add / bun add。

@angro/nav-connector 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/angro__nav-connector 是否存在。

@angro/nav-connector 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@angro/nav-connector 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 8.11.2。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@angro/nav-connector 可以商用吗?

@angro/nav-connector 使用 GPL-3.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@angro/nav-connector 的下载量有多少?

根据 npm 官方下载统计,@angro/nav-connector 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,433 次,日均约 181 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@angro/nav-connector 还在维护吗?

最新版本 6.1.1 发布于 2024/01/10(2 年前);至今共发布 24 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/04/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。