node-red-contrib-afya-ksef 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mchuc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,214 @@
1
- # Temporary Holding Version
1
+ # node-red-contrib-afya-ksef
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![license: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)
4
+
5
+ Node-RED node for **KSeF** (Krajowy System e-Faktur – the Polish National e-Invoice System, API 2.0):
6
+ it reads this month's invoices – **sales = revenue** and **purchases = costs** – tells you **which invoices
7
+ are new**, with their **KSeF numbers**, and gives a ready SMS text. An optional web page shows both journals with
8
+ totals and a **“Do przelewu”** list with the bank accounts of your suppliers – every number has a **copy** button.
9
+ Log in with a **KSeF token** or a **KSeF certificate** (`.crt` + `.key`). No dependencies.
10
+
11
+ **[English](#english) · [Polski](#polski)** · part of [dobry.doctor](https://dobry.doctor/)
12
+
13
+ ---
14
+
15
+ ## English
16
+
17
+ ```
18
+ [inject every 1 h] → [ksef invoices] ─┬─► [switch: payload.tekstPrzychody not empty] → [SMS: new revenue]
19
+ └─► [switch: payload.tekstKoszty not empty] → [SMS: new cost]
20
+ ```
21
+
22
+ Example SMS (Polish, as it goes to Polish users):
23
+
24
+ ```
25
+ Przychód 1 234,50 zł netto – Firma Pacjenta S.A., FV FV/12/11/2026, KSeF 5265877635-20261103-0100000000AA-01
26
+ Koszt 5 068,22 zł – Hurtownia Medyczna Sp. z o.o., termin 16.11.2026, FV HM/77/2026, KSeF 3225081610-20261102-0200000000CC-03
27
+ ```
28
+
29
+ ### Install
30
+
31
+ Requires Node.js ≥ 18.17 and Node-RED ≥ 3.
32
+
33
+ ```bash
34
+ cd ~/.node-red
35
+ npm install node-red-contrib-afya-ksef
36
+ # or straight from GitHub:
37
+ npm install github:mchuc/node-red-contrib-afya-ksef
38
+ ```
39
+
40
+ Restart Node-RED. The node **ksef invoices** appears in the **AFYA** palette group.
41
+
42
+ ### Log in to KSeF
43
+
44
+ | Method | What you need |
45
+ |---|---|
46
+ | **KSeF token** | Generate it in *Aplikacja Podatnika KSeF* → *Tokeny*. Reading invoices needs only the permission *przeglądanie faktur* (`InvoiceRead`). The node encrypts `token|timestamp` with the Ministry's public key (RSA-OAEP SHA-256), as KSeF requires. |
47
+ | **KSeF certificate** | A certificate of type *Uwierzytelnianie* from *Aplikacja Podatnika KSeF*: the `.crt` file, the `.key` file and the password you set for the key. Upload both files in the edit dialog. The node signs the KSeF login request (`AuthTokenRequest`, XAdES-BES, RSA or EC) itself. |
48
+
49
+ Both are stored as Node-RED *credentials* and never exported with the flow. The access token is refreshed with
50
+ the refresh token, so a full login happens only about once a week.
51
+ A `.p12` / `.pfx` file has to be split first:
52
+ `openssl pkcs12 -in cert.p12 -clcerts -nokeys -out cert.crt` and `openssl pkcs12 -in cert.p12 -nocerts -out cert.key`.
53
+
54
+ ### Configuration
55
+
56
+ | Field | Description |
57
+ |---|---|
58
+ | Labels | Language of the edit dialog: *system language* (Polish for a Polish system, English otherwise), *English* or *Polski*. Only the descriptions change. |
59
+ | Environment | *Production* (real invoices), *Demo* or *Test*. |
60
+ | Company NIP | The company the invoices belong to (login context). Checked against the NIP checksum. |
61
+ | Log in with | *KSeF token* or *KSeF certificate* (see above). *Identify by*: certificate subject (NIP/PESEL) or certificate fingerprint. |
62
+ | Amounts | *net*, *gross* or *both* (net, VAT, gross) – in the tables, totals and `%kwota%`. |
63
+ | Month by | *issue date* – an invoice belongs to the month it was issued in; *date received in KSeF* – the month the KSeF number was given (for costs, this is the legal date of receipt). |
64
+ | Also check the previous month | On (default): invoices dated last month that arrive late are caught too. |
65
+ | Send info about a new sales / purchase invoice | Texts in `payload.tekstPrzychody` and `payload.tekstKoszty`, each with its own template. |
66
+ | Without Polish letters | ą→a, ł→l and typographic dashes/quotes → ASCII, so one SMS holds 160 instead of 70 characters. |
67
+ | Append the month totals | Adds `\| 11.2026: przychody …, koszty …, wynik …` to every line. |
68
+ | Transfers | Reads the bank account, due date, payment form, split payment (MPP) and “paid” flag from each purchase invoice XML (once per invoice). *Transfer amount*: gross (or *do zapłaty* from the invoice) or net. *Copied amount*: `1518.44` (dot) or `1518,44` (comma). |
69
+ | Invoice page | Page path, title, password and/or access token (see below). |
70
+ | Variable | Optional: the last `msg.payload` also in a **global** or **flow** context variable. |
71
+
72
+ Placeholders: `%kwota%` `%netto%` `%vat%` `%brutto%` `%kontrahent%` `%nip%` `%numer%` `%ksef%` `%data%` `%typ%`
73
+ `%waluta%` `%miesiac%` `%suma%` `%firma%`, and for purchase invoices `%przelew%` (transfer amount) `%konto%` `%termin%`.
74
+
75
+ Defaults: `Przychód %kwota% – %kontrahent%, FV %numer%, KSeF %ksef%` and `Koszt %kwota% – %kontrahent%, FV %numer%, KSeF %ksef%`.
76
+
77
+ ### KSeF limits
78
+
79
+ KSeF allows **20 invoice-list queries per hour** per company. One check makes 2 queries per month (sales +
80
+ purchases) – 4 with the previous month – so **inject every hour** (never more often than every 15 minutes).
81
+ After `429 Too Many Requests` the node waits as long as KSeF asks and does not call KSeF in between.
82
+ Invoice XML downloads (for the transfer data) have their own limit of 64 per hour; the node downloads at most 12
83
+ per check, new invoices first, and every invoice only once.
84
+
85
+ ### Invoice page
86
+
87
+ With *Show a web page* the node serves `http://<node-red>:1880/afya/ksef` (under `httpNodeRoot`):
88
+
89
+ * company and NIP, tiles **revenue / costs / result** for the month (net, gross or both),
90
+ * two tables like a journal – **Przychody** (sales) and **Koszty** (purchases): date, invoice number,
91
+ counterparty with NIP, **KSeF number** with a *kopiuj* (copy) button, amounts and a total row,
92
+ * **Do przelewu** under the costs: recipient, **bank account**, **amount**, **title** (invoice number), each with a
93
+ *kopiuj* button; due date (overdue in red), paid invoices dimmed, split payment (MPP) with the VAT amount,
94
+ factor account, payment link; a wrong account checksum is flagged,
95
+ * clicking an amount in the tables copies it as `1518.44` (or `1518,44`),
96
+ * arrows switch between the stored months (up to 13), badges for new invoices (last 48 h) and corrections,
97
+ * works on a phone and in dark mode, refreshes itself every 15 minutes.
98
+
99
+ Copying works over plain `http://` in the local network too (where the browser blocks the clipboard API).
100
+
101
+ Access – exactly like the Librus page: a **password** (login form, `HttpOnly` cookie for 30 days, changing the
102
+ password logs everyone out) and/or an **access token** (`…/afya/ksef?token=…`, no login – keep the link like a
103
+ password). Without either the page stays off; 10 wrong tries in 15 minutes block the address for a while.
104
+ Put the page behind HTTPS / VPN when it is reachable from the internet – it shows financial data.
105
+
106
+ ### Output (`msg.payload`)
107
+
108
+ | Field | Meaning |
109
+ |---|---|
110
+ | `ok` | `true` – read successfully; on error `{ ok: false, czas, blad, status }` |
111
+ | `nowe`, `nowePrzychody`, `noweKoszty` | invoices that were not there at the previous check |
112
+ | `tekstPrzychody`, `tekstKoszty`, `tekst` | ready SMS texts; empty when nothing is new |
113
+ | `przychody`, `koszty` | this month's journal: `ksef` (KSeF number), `numer` (invoice number), `data`, `otrzymana`, `kontrahent`, `nipKontrahenta`, `netto`, `vat`, `brutto`, `vatPln`, `waluta`, `typ`, `korekta`, `miesiac`; costs also `platnosc` (`konta`, `termin`, `forma`, `zaplacono`, `mpp`, …) |
114
+ | `sumy` | `{ przychody: { PLN: { ile, netto, vat, brutto } }, koszty: {…}, wynik: {…} }` |
115
+ | `miesiace` | the checked months with their journals and totals |
116
+ | `firma`, `nip`, `miesiac`, `srodowisko`, `kwoty`, `typDaty` | context of the check |
117
+ | `pierwszyOdczyt` | `true` on the very first check – invoices are only remembered |
118
+ | `zapytan`, `pobranoFaktur` | KSeF queries made, invoice XML files downloaded |
119
+ | `certyfikat` | with certificate login: subject, validity, fingerprint |
120
+
121
+ ### SMS test
122
+
123
+ Send `msg.test = true`: when nothing is new, the latest sales and purchase invoice are reported as new.
124
+
125
+ ### Tests
126
+
127
+ ```bash
128
+ npm install
129
+ npm test
130
+ ```
131
+
132
+ The tests run against a fake KSeF that checks what KSeF checks: the RSA-OAEP encrypted token, the XAdES
133
+ signature (verified with `xml-crypto` for RSA and by hand for ECDSA), Bearer tokens, the query filters and the
134
+ invoice XML.
135
+
136
+ ### Credits
137
+
138
+ Based on the official KSeF API 2.0 documentation ([CIRFMF/ksef-docs](https://github.com/CIRFMF/ksef-docs)) and
139
+ the Ministry's C# client (XAdES signature layout). Unofficial – not affiliated with the Ministry of Finance.
140
+ Marcin Chuć · MIT · [dobry.doctor](https://dobry.doctor/)
141
+
142
+ ---
143
+
144
+ ## Polski
145
+
146
+ Węzeł Node-RED do **KSeF** (Krajowy System e-Faktur, API 2.0): czyta faktury bieżącego miesiąca (i poprzedniego)
147
+ – **sprzedaż = przychody**, **zakupy = koszty** – i podaje, **które faktury są nowe**, z **numerami KSeF**.
148
+ Gotowy tekst SMS, a do tego strona z dwiema tabelami jak w dzienniku i listą **Do przelewu** z kontami
149
+ dostawców – każdy numer z przyciskiem **kopiuj**. Logowanie **tokenem KSeF** albo **certyfikatem KSeF**.
150
+
151
+ ### Instalacja
152
+
153
+ ```bash
154
+ cd ~/.node-red
155
+ npm install node-red-contrib-afya-ksef
156
+ # albo prosto z GitHuba:
157
+ npm install github:mchuc/node-red-contrib-afya-ksef
158
+ ```
159
+
160
+ W Dockerze (`nodered`, katalog `/data`): `docker exec -it nodered sh -c "cd /data && npm install node-red-contrib-afya-ksef"`
161
+ i restart kontenera. Węzeł **ksef invoices** jest w grupie **AFYA**.
162
+
163
+ ### Logowanie do KSeF
164
+
165
+ * **Token KSeF** – w *Aplikacji Podatnika KSeF* → *Tokeny*; do odczytu faktur wystarczy uprawnienie
166
+ *przeglądanie faktur*. Tokeny mają działać także po 2026 r.
167
+ * **Certyfikat KSeF** typu *Uwierzytelnianie* – pliki `.crt` i `.key` z Aplikacji Podatnika i hasło klucza.
168
+ Wgrywasz oba pliki w oknie konfiguracji; węzeł sam podpisuje żądanie logowania (XAdES).
169
+
170
+ Token, certyfikat, klucz i hasła są zapisywane jako dane uwierzytelniające Node-RED – nie trafiają do eksportu flow.
171
+
172
+ ### Konfiguracja
173
+
174
+ * **Kwoty**: netto, brutto albo oba (netto, VAT, brutto) – w tabelach, sumach i `%kwota%`.
175
+ * **Miesiąc wg**: daty wystawienia albo daty otrzymania w KSeF (nadania numeru – dla kosztów to data otrzymania faktury).
176
+ * **Sprawdzaj też poprzedni miesiąc** – łapie faktury z poprzedniego miesiąca, które przyszły później.
177
+ * **Nowy przychód / nowy koszt** – osobne szablony SMS: `payload.tekstPrzychody`, `payload.tekstKoszty`
178
+ (oba razem w `payload.tekst`).
179
+ * **Przelewy** – z XML faktur zakupu: konto, bank, termin, forma płatności, split payment, „zapłacono”.
180
+ **Kwota przelewu**: brutto (albo „do zapłaty” z faktury) lub netto. **Kopiowana kwota**: z kropką `1518.44`
181
+ albo z przecinkiem `1518,44`.
182
+
183
+ Pola w szablonach: `%kwota%` `%netto%` `%vat%` `%brutto%` `%kontrahent%` `%nip%` `%numer%` `%ksef%` `%data%` `%typ%`
184
+ `%waluta%` `%miesiac%` `%suma%` `%firma%`, a dla kosztów także `%przelew%` `%konto%` `%termin%`.
185
+
186
+ ### Limity KSeF
187
+
188
+ KSeF pozwala na **20 zapytań o listę faktur na godzinę**. Jedno sprawdzenie to 2 zapytania na miesiąc (4 z poprzednim
189
+ miesiącem) – **inject co godzinę**, najczęściej co 15 minut. Po `429` węzeł czeka tyle, ile każe KSeF.
190
+ XML faktur (dane do przelewu): najwyżej 12 przy jednym sprawdzeniu, każda faktura tylko raz.
191
+
192
+ ### Strona z fakturami
193
+
194
+ `http://<node-red>:1880/afya/ksef` – kafle *przychody / koszty / wynik*, tabele **Przychody** i **Koszty**
195
+ (data, numer faktury, kontrahent z NIP-em, **numer KSeF z przyciskiem kopiuj**, kwoty, wiersz „Razem”) i pod nimi
196
+ **Do przelewu**: odbiorca, **konto**, **kwota**, **tytuł** – wszystko z przyciskiem *kopiuj*; termin (po terminie na
197
+ czerwono), zapłacone wyszarzone, split payment z kwotą VAT, konto faktora. Kliknięcie kwoty w tabeli kopiuje ją jako
198
+ `1518.44`. Kopiowanie działa też przez zwykłe `http://` w sieci lokalnej. Strzałki – poprzednie miesiące (do 13).
199
+
200
+ Dostęp jak w Librusie: **hasło** (logowanie na 30 dni) i/lub **token** w linku `?token=…`; bez nich strona jest
201
+ wyłączona, 10 błędnych prób w 15 minut blokuje adres na chwilę. Wystawiając stronę do internetu – tylko przez HTTPS / VPN.
202
+
203
+ ### Przykład
204
+
205
+ **Menu → Import → Examples → node-red-contrib-afya-ksef → ksef-sms**: co godzinę 7–21, nowy przychód i nowy koszt
206
+ osobnymi SMS-ami (przychody do właściciela, koszty do właściciela i księgowej), bramka SMS po HTTP z tokenem
207
+ w nagłówku, cały wynik do MQTT `afya/ksef/dane`. Pierwsze sprawdzenie tylko zapamiętuje faktury.
208
+
209
+ ### Testy
210
+
211
+ `npm install && npm test` – testy na atrapie KSeF, która sprawdza szyfrowanie tokenu, podpis XAdES (RSA i EC),
212
+ tokeny Bearer, filtry zapytań i XML faktur.
213
+
214
+ <small>Marcin Chuć · MIT · [dobry.doctor](https://dobry.doctor/) · nieoficjalne, niezwiązane z Ministerstwem Finansów</small>