Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Step committed Mar 14, 2021
1 parent 25ae832 commit e4eac93
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
57 changes: 53 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,60 @@

## Использование

...
Установите пакет
```bash
npm i moy-nalog
```


[Подробное описание методов класса](/blob/main/docs/nalogAPIClass.md)
Инициализаци и авторизация
```javascript
const moyNalog = require('moy-nalog')

const nalogAPI = new moyNalog({ username:'23456789', password: 'your_pass' })
```

Отправка информации о доходе
```javascript
nalogAPI.addIncome({ name:'Предоставление информационных услуг', amount: 99.99 }).then( receipt => {
console.log(receipt.id, receipt.data)

// ссылка на картинку с чеком
return receipt.printUrl
})
```

### Примеры
Вызов произвольного метода api (см. network в devtools на сайте lknpd.nalog.ru)
```javascript
const stats = await nalogAPI.call('incomes/summary')
```

Пример расширенного добавления дохода
```javascript
const response = await nalogAPI.call('income', 'POST', {
paymentType: 'CASH',
inn: null,
ignoreMaxTotalIncomeRestriction: false,
client: { contactPhone: null, displayName: null, incomeType: 'FROM_INDIVIDUAL' },

requestTime: nalogAPI.dateToLocalISO(),
operationTime: nalogAPI.dateToLocalISO(new Date('2021-03-08 12:42')),

services: [{
name: 'Предоставление информационных услуг #' + orderId,
amount: 99.99,
quantity: 1
}],

totalAmount: 99.99
})

console.log(response)

```


[Подробное описание методов класса](/docs/nalogAPIClass.md)


## Разработка
...
6 changes: 3 additions & 3 deletions docs/nalogAPIClass.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* [.createDeviceId()](#NalogAPI+createDeviceId)
* [.auth(username, password)](#NalogAPI+auth) ⇒ <code>Promise(object)</code>
* [.getToken()](#NalogAPI+getToken) ⇒ <code>Promise(string)</code>
* [.call(endpoint, payload, method)](#NalogAPI+call) ⇒ <code>Promise(object)</code>
* [.call(endpoint, method, payload)](#NalogAPI+call) ⇒ <code>Promise(object)</code>
* [.addIncome(date, name, amount)](#NalogAPI+addIncome) ⇒ <code>Promise({id,printUrl,jsonUrl,data,approvedReceiptUuid})</code>
* [.userInfo()](#NalogAPI+userInfo) ⇒ <code>Promise(object)</code>

Expand Down Expand Up @@ -73,7 +73,7 @@
**Kind**: instance method of [<code>NalogAPI</code>](#NalogAPI)
<a name="NalogAPI+call"></a>

### nalogAPI.call(endpoint, payload, method) ⇒ <code>Promise(object)</code>
### nalogAPI.call(endpoint, method, payload) ⇒ <code>Promise(object)</code>
Вызов метода api

**Kind**: instance method of [<code>NalogAPI</code>](#NalogAPI)
Expand All @@ -82,8 +82,8 @@
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| endpoint | <code>string</code> | | url метода без слэша в начале (например `user`) |
| method | <code>enum</code> | <code>&#x27;GET&#x27;</code> | |
| payload | <code>object</code> | | данные для отправки в body |
| method | <code>enum</code> | <code>&#x27;POST&#x27;</code> | |

<a name="NalogAPI+addIncome"></a>

Expand Down
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ class NalogAPI {
/**
* Вызов метода api
* @param {string} endpoint - url метода без слэша в начале (например `user`)
* @param {enum} method='GET'
* @param {object} payload - данные для отправки в body
* @param {enum} method='POST'
* @returns {Promise(object)} - json ответа сервера
*/
async call (endpoint, payload, method = 'POST') {
async call (endpoint, method = 'GET', payload = {}) {
const params = {
method: method,
headers: {
Expand Down Expand Up @@ -179,24 +179,14 @@ class NalogAPI {
* @returns {Promise({id,printUrl,jsonUrl,data,approvedReceiptUuid})} - информация о созданном чеке, либо об ошибке
*/
async addIncome ({ date = new Date(), name, quantity = 1, amount }) {
function dateToLocalISO (date = new Date()) {
date = new Date(date)
const off = date.getTimezoneOffset()
const absoff = Math.abs(off)
return (new Date(date.getTime() - off * 60 * 1000).toISOString().substr(0, 19) +
(off > 0 ? '-' : '+') +
(absoff / 60).toFixed(0).padStart(2, '0') + ':' +
(absoff % 60).toString().padStart(2, '0'))
}

const response = await this.call('income', {
const response = await this.call('income', 'POST', {
paymentType: 'CASH',
inn: null,
ignoreMaxTotalIncomeRestriction: false,
client: { contactPhone: null, displayName: null, incomeType: 'FROM_INDIVIDUAL' },

requestTime: dateToLocalISO(),
operationTime: dateToLocalISO(date),
requestTime: this.dateToLocalISO(),
operationTime: this.dateToLocalISO(date),

services: [{
name: name, // 'Предоставление информационных услуг #970/2495',
Expand Down Expand Up @@ -227,7 +217,17 @@ class NalogAPI {
* @returns {Promise(object)}
*/
async userInfo () {
return this.call('user', {}, 'GET')
return this.call('user')
}

dateToLocalISO (date = new Date()) {
date = new Date(date)
const off = date.getTimezoneOffset()
const absoff = Math.abs(off)
return (new Date(date.getTime() - off * 60 * 1000).toISOString().substr(0, 19) +
(off > 0 ? '-' : '+') +
(absoff / 60).toFixed(0).padStart(2, '0') + ':' +
(absoff % 60).toString().padStart(2, '0'))
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moy-nalog",
"version": "1.0.0",
"version": "1.0.1",
"description": "Обёртка для API сервиса lknpd.nalog.ru",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e4eac93

Please sign in to comment.