@12-apps/fiscal 1.0.0 → 1.1.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/README.md +152 -7
- package/dist/chunk-27B4HNO6.js +574 -0
- package/dist/chunk-27B4HNO6.js.map +1 -0
- package/dist/chunk-3SDXLSG4.js +24 -0
- package/dist/chunk-3SDXLSG4.js.map +1 -0
- package/dist/chunk-ALABGVPO.js +247 -0
- package/dist/chunk-ALABGVPO.js.map +1 -0
- package/dist/chunk-FOFIN5M7.js +14 -0
- package/dist/chunk-FOFIN5M7.js.map +1 -0
- package/dist/chunk-HQ2NXSTX.js +85 -0
- package/dist/chunk-HQ2NXSTX.js.map +1 -0
- package/dist/chunk-PNTKG3DZ.js +47 -0
- package/dist/chunk-PNTKG3DZ.js.map +1 -0
- package/dist/chunk-RRFKYVLZ.js +192 -0
- package/dist/chunk-RRFKYVLZ.js.map +1 -0
- package/dist/chunk-V5ZQAAJT.js +210 -0
- package/dist/chunk-V5ZQAAJT.js.map +1 -0
- package/dist/chunk-X6MRDUYH.js +21 -0
- package/dist/chunk-X6MRDUYH.js.map +1 -0
- package/dist/chunk-XQGW5HUG.js +30 -0
- package/dist/chunk-XQGW5HUG.js.map +1 -0
- package/dist/context-ChnWR4Kb.d.ts +455 -0
- package/dist/danfe/en-US.d.ts +12 -0
- package/dist/danfe/en-US.js +51 -0
- package/dist/danfe/en-US.js.map +1 -0
- package/dist/danfe/index.d.ts +50 -0
- package/dist/danfe/index.js +18 -0
- package/dist/danfe/index.js.map +1 -0
- package/dist/danfe/pt-BR.d.ts +11 -0
- package/dist/danfe/pt-BR.js +51 -0
- package/dist/danfe/pt-BR.js.map +1 -0
- package/dist/documents/index.d.ts +169 -0
- package/dist/documents/index.js +1228 -0
- package/dist/documents/index.js.map +1 -0
- package/dist/engine/index.d.ts +3 -246
- package/dist/http.d.ts +73 -0
- package/dist/http.js +16 -0
- package/dist/http.js.map +1 -0
- package/dist/issuer/index.d.ts +43 -0
- package/dist/issuer/index.js +131 -0
- package/dist/issuer/index.js.map +1 -0
- package/dist/jobs.d.ts +74 -0
- package/dist/jobs.js +22 -0
- package/dist/jobs.js.map +1 -0
- package/dist/labels-BNvflT0h.d.ts +44 -0
- package/dist/manifest/index.d.ts +30 -16
- package/dist/manifest/index.js +11 -17
- package/dist/manifest/index.js.map +1 -1
- package/dist/manifest/server.d.ts +57 -0
- package/dist/manifest/server.js +25 -0
- package/dist/manifest/server.js.map +1 -0
- package/dist/notifications.d.ts +93 -0
- package/dist/notifications.js +41 -0
- package/dist/notifications.js.map +1 -0
- package/dist/port-DpRFK56U.d.ts +238 -0
- package/dist/sale/index.d.ts +243 -0
- package/dist/sale/index.js +1 -0
- package/dist/sale/index.js.map +1 -0
- package/dist/service-KTjH7AxW.d.ts +139 -0
- package/dist/sidecar/index.d.ts +216 -0
- package/dist/sidecar/index.js +8 -0
- package/dist/sidecar/index.js.map +1 -0
- package/dist/types-B1b8LIWX.d.ts +247 -0
- package/package.json +63 -5
- package/prisma/fiscal.prisma +158 -0
- package/prisma/migrations/20261113220000_fiscal_documents/migration.sql +201 -0
- package/sidecar/.dockerignore +5 -0
- package/sidecar/Dockerfile +68 -0
- package/sidecar/README.md +149 -0
- package/sidecar/composer.json +30 -0
- package/sidecar/composer.lock +2224 -0
- package/sidecar/docker/apache.conf +14 -0
- package/sidecar/docker/icp-brasil.pem +8031 -0
- package/sidecar/docker/mpm.conf +17 -0
- package/sidecar/docker/openssl.cnf +16 -0
- package/sidecar/docker/php.ini +19 -0
- package/sidecar/public/index.php +15 -0
- package/sidecar/schema/sale-for-document.schema.json +401 -0
- package/sidecar/src/ErrorClass.php +31 -0
- package/sidecar/src/Http/Request.php +48 -0
- package/sidecar/src/Http/Response.php +32 -0
- package/sidecar/src/Http/Router.php +59 -0
- package/sidecar/src/Kernel.php +99 -0
- package/sidecar/src/Module.php +31 -0
- package/sidecar/src/Nfe/Classifier.php +89 -0
- package/sidecar/src/Nfe/Credentials.php +52 -0
- package/sidecar/src/Nfe/DocumentMapper.php +383 -0
- package/sidecar/src/Nfe/DocumentSchema.php +39 -0
- package/sidecar/src/Nfe/Input.php +125 -0
- package/sidecar/src/Nfe/Model.php +17 -0
- package/sidecar/src/Nfe/NfeModule.php +62 -0
- package/sidecar/src/Nfe/NfeService.php +504 -0
- package/sidecar/src/Nfe/Sefaz.php +49 -0
- package/sidecar/src/Nfe/SefazReply.php +81 -0
- package/sidecar/src/Nfe/Transport/CurlTransport.php +198 -0
- package/sidecar/src/Nfe/Transport/SoapAdapter.php +57 -0
- package/sidecar/src/Nfe/Transport/Transport.php +20 -0
- package/sidecar/src/Nfe/Transport/TransportFailure.php +42 -0
- package/sidecar/src/RequestLog.php +54 -0
- package/sidecar/src/ServiceError.php +69 -0
- package/src/danfe/en-US.ts +53 -0
- package/src/danfe/index.ts +11 -0
- package/src/danfe/labels.ts +49 -0
- package/src/danfe/pt-BR.ts +52 -0
- package/src/danfe/render.ts +265 -0
- package/src/danfe/xml.ts +45 -0
- package/src/documents/context.ts +260 -0
- package/src/documents/errors.ts +41 -0
- package/src/documents/helpers.ts +128 -0
- package/src/documents/index.ts +39 -0
- package/src/documents/lines.ts +63 -0
- package/src/documents/machine-steps.ts +260 -0
- package/src/documents/machine.ts +128 -0
- package/src/documents/memory.ts +138 -0
- package/src/documents/nfe-proc.ts +51 -0
- package/src/documents/ports.ts +130 -0
- package/src/documents/profile-input.ts +168 -0
- package/src/documents/profile.ts +98 -0
- package/src/documents/service-profile.ts +104 -0
- package/src/documents/service.ts +296 -0
- package/src/documents/settle.ts +121 -0
- package/src/documents/signing.ts +61 -0
- package/src/documents/sql-rows.ts +180 -0
- package/src/documents/sql-store.ts +242 -0
- package/src/documents/sql.ts +18 -0
- package/src/documents/steps.ts +173 -0
- package/src/documents/tests/integration/pglite.ts +73 -0
- package/src/documents/types.ts +193 -0
- package/src/http.ts +264 -0
- package/src/issuer/index.ts +9 -0
- package/src/issuer/port.ts +109 -0
- package/src/issuer/sidecar.ts +171 -0
- package/src/jobs.ts +115 -0
- package/src/manifest/index.ts +25 -16
- package/src/manifest/server.ts +20 -0
- package/src/notifications.ts +120 -0
- package/src/sale/index.ts +8 -0
- package/src/sale/types.ts +292 -0
- package/src/sidecar/index.ts +9 -0
- package/src/sidecar/types.ts +243 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Apache prefork: one PHP process per request, and at most four of them.
|
|
2
|
+
#
|
|
3
|
+
# A 990-item NF-e (the layout's maximum) built, signed and validated peaks at
|
|
4
|
+
# 36 MB of PHP memory (measured 2026-11-13); php.ini caps a request at 128M, so
|
|
5
|
+
# four workers bound the service at 4 x 128M = 512M in the worst case and about
|
|
6
|
+
# 4 x 40M in practice, a slice a 1-2 GB droplet without swap can give it beside
|
|
7
|
+
# the API and the worker. Four is also enough concurrency: a burst of 60 NFC-e a
|
|
8
|
+
# minute at roughly one second of SEFAZ latency each keeps one or two busy; the
|
|
9
|
+
# rest wait in the listen backlog instead of growing the process count.
|
|
10
|
+
<IfModule mpm_prefork_module>
|
|
11
|
+
StartServers 2
|
|
12
|
+
MinSpareServers 1
|
|
13
|
+
MaxSpareServers 3
|
|
14
|
+
MaxRequestWorkers 4
|
|
15
|
+
ServerLimit 4
|
|
16
|
+
MaxConnectionsPerChild 500
|
|
17
|
+
</IfModule>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# OpenSSL 3 with the legacy provider beside the default one, so an A1 .pfx
|
|
2
|
+
# encrypted with RC2-40 or 3DES (most issued before 2023) can be opened.
|
|
3
|
+
openssl_conf = openssl_init
|
|
4
|
+
|
|
5
|
+
[openssl_init]
|
|
6
|
+
providers = provider_sect
|
|
7
|
+
|
|
8
|
+
[provider_sect]
|
|
9
|
+
default = default_sect
|
|
10
|
+
legacy = legacy_sect
|
|
11
|
+
|
|
12
|
+
[default_sect]
|
|
13
|
+
activate = 1
|
|
14
|
+
|
|
15
|
+
[legacy_sect]
|
|
16
|
+
activate = 1
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
; The fiscal service's PHP settings, on top of php.ini-production.
|
|
2
|
+
; Nothing reaches a log but what the service writes itself; nothing reaches disk.
|
|
3
|
+
display_errors = Off
|
|
4
|
+
display_startup_errors = Off
|
|
5
|
+
log_errors = On
|
|
6
|
+
error_log = /dev/stderr
|
|
7
|
+
expose_php = Off
|
|
8
|
+
; A stack trace never carries an argument (a password, a certificate).
|
|
9
|
+
zend.exception_ignore_args = On
|
|
10
|
+
zend.exception_string_param_max_len = 0
|
|
11
|
+
; JSON bodies only: no multipart, so PHP never spools an upload to /tmp.
|
|
12
|
+
file_uploads = Off
|
|
13
|
+
enable_post_data_reading = On
|
|
14
|
+
post_max_size = 8M
|
|
15
|
+
memory_limit = 128M
|
|
16
|
+
max_execution_time = 120
|
|
17
|
+
session.auto_start = 0
|
|
18
|
+
opcache.enable = 1
|
|
19
|
+
opcache.validate_timestamps = 0
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* The front controller. Every request builds its own kernel, so a certificate
|
|
7
|
+
* read for one call is garbage the moment the call answers.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
use Fiscal\Sidecar\Http\Request;
|
|
11
|
+
use Fiscal\Sidecar\Kernel;
|
|
12
|
+
|
|
13
|
+
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
14
|
+
|
|
15
|
+
Kernel::production()->handle(Request::fromGlobals())->send();
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "SaleForDocument",
|
|
4
|
+
"description": "The neutral document shape the fiscal service accepts: one sale, host-agnostic, to be issued as an NF-e (model 55) or an NFC-e (model 65). The model is the request path's, never this document's. Money is integer cents, rates integer basis points (1 bp = 0.01%), quantities at most four decimals. Mirrors the TypeScript type SaleForDocument in @12-apps/fiscal/sale; a test keeps the two in step.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"additionalProperties": false,
|
|
7
|
+
"required": [
|
|
8
|
+
"version",
|
|
9
|
+
"series",
|
|
10
|
+
"number",
|
|
11
|
+
"keyCode",
|
|
12
|
+
"issuedAt",
|
|
13
|
+
"operationNature",
|
|
14
|
+
"purpose",
|
|
15
|
+
"destination",
|
|
16
|
+
"presence",
|
|
17
|
+
"finalConsumer",
|
|
18
|
+
"intermediary",
|
|
19
|
+
"issuer",
|
|
20
|
+
"buyer",
|
|
21
|
+
"lines",
|
|
22
|
+
"freight",
|
|
23
|
+
"payments",
|
|
24
|
+
"changeCents",
|
|
25
|
+
"additionalInfo",
|
|
26
|
+
"technicalResponsible"
|
|
27
|
+
],
|
|
28
|
+
"properties": {
|
|
29
|
+
"version": { "const": 1 },
|
|
30
|
+
"series": { "type": "integer", "minimum": 0, "maximum": 999 },
|
|
31
|
+
"number": { "type": "integer", "minimum": 1, "maximum": 999999999 },
|
|
32
|
+
"keyCode": {
|
|
33
|
+
"description": "cNF: the 8-digit code inside the access key. The caller draws it once and keeps it, so a retried call yields the same key.",
|
|
34
|
+
"type": "string",
|
|
35
|
+
"pattern": "^[0-9]{8}$"
|
|
36
|
+
},
|
|
37
|
+
"issuedAt": {
|
|
38
|
+
"description": "dhEmi: ISO 8601 date-time with its UTC offset, e.g. 2026-11-20T12:30:00-03:00.",
|
|
39
|
+
"type": "string",
|
|
40
|
+
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[+-][0-9]{2}:[0-9]{2}$"
|
|
41
|
+
},
|
|
42
|
+
"operationNature": { "type": "string", "minLength": 1, "maxLength": 60 },
|
|
43
|
+
"purpose": { "enum": ["NORMAL", "COMPLEMENTARY", "ADJUSTMENT", "RETURN"] },
|
|
44
|
+
"destination": { "enum": ["INTERNAL", "INTERSTATE", "FOREIGN"] },
|
|
45
|
+
"presence": {
|
|
46
|
+
"enum": ["IN_PERSON", "INTERNET", "TELEPHONE", "HOME_DELIVERY", "IN_PERSON_OFF_PREMISES", "OTHER"]
|
|
47
|
+
},
|
|
48
|
+
"finalConsumer": { "type": "boolean" },
|
|
49
|
+
"intermediary": {
|
|
50
|
+
"oneOf": [
|
|
51
|
+
{ "type": "null" },
|
|
52
|
+
{
|
|
53
|
+
"type": "object",
|
|
54
|
+
"additionalProperties": false,
|
|
55
|
+
"required": ["cnpj", "userId"],
|
|
56
|
+
"properties": {
|
|
57
|
+
"cnpj": { "$ref": "#/definitions/cnpj" },
|
|
58
|
+
"userId": { "type": "string", "minLength": 2, "maxLength": 60 }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
"issuer": { "$ref": "#/definitions/issuer" },
|
|
64
|
+
"buyer": { "oneOf": [{ "type": "null" }, { "$ref": "#/definitions/buyer" }] },
|
|
65
|
+
"lines": {
|
|
66
|
+
"type": "array",
|
|
67
|
+
"minItems": 1,
|
|
68
|
+
"maxItems": 990,
|
|
69
|
+
"items": { "$ref": "#/definitions/line" }
|
|
70
|
+
},
|
|
71
|
+
"freight": {
|
|
72
|
+
"enum": ["BY_ISSUER", "BY_RECIPIENT", "BY_THIRD_PARTY", "OWN_BY_ISSUER", "OWN_BY_RECIPIENT", "NONE"]
|
|
73
|
+
},
|
|
74
|
+
"payments": {
|
|
75
|
+
"type": "array",
|
|
76
|
+
"minItems": 1,
|
|
77
|
+
"maxItems": 100,
|
|
78
|
+
"items": { "$ref": "#/definitions/payment" }
|
|
79
|
+
},
|
|
80
|
+
"changeCents": { "$ref": "#/definitions/cents" },
|
|
81
|
+
"additionalInfo": {
|
|
82
|
+
"type": "object",
|
|
83
|
+
"additionalProperties": false,
|
|
84
|
+
"required": ["forTaxpayer", "forTaxAuthority"],
|
|
85
|
+
"properties": {
|
|
86
|
+
"forTaxpayer": { "$ref": "#/definitions/optionalText5000" },
|
|
87
|
+
"forTaxAuthority": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 1, "maxLength": 2000 }] }
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"technicalResponsible": {
|
|
91
|
+
"oneOf": [
|
|
92
|
+
{ "type": "null" },
|
|
93
|
+
{
|
|
94
|
+
"type": "object",
|
|
95
|
+
"additionalProperties": false,
|
|
96
|
+
"required": ["cnpj", "contactName", "email", "phone"],
|
|
97
|
+
"properties": {
|
|
98
|
+
"cnpj": { "$ref": "#/definitions/cnpj" },
|
|
99
|
+
"contactName": { "type": "string", "minLength": 2, "maxLength": 60 },
|
|
100
|
+
"email": { "type": "string", "minLength": 6, "maxLength": 60 },
|
|
101
|
+
"phone": { "type": "string", "pattern": "^[0-9]{6,14}$" }
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"definitions": {
|
|
108
|
+
"cents": { "type": "integer", "minimum": 0 },
|
|
109
|
+
"basisPoints": { "type": "integer", "minimum": 0, "maximum": 1000000 },
|
|
110
|
+
"cnpj": {
|
|
111
|
+
"description": "14 characters: the 2026 alphanumeric CNPJ (12 letters or digits, then 2 check digits) or a numeric one.",
|
|
112
|
+
"type": "string",
|
|
113
|
+
"pattern": "^[A-Z0-9]{12}[0-9]{2}$"
|
|
114
|
+
},
|
|
115
|
+
"uf": {
|
|
116
|
+
"enum": [
|
|
117
|
+
"AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE",
|
|
118
|
+
"PI", "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO"
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
"optionalText5000": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 1, "maxLength": 5000 }] },
|
|
122
|
+
"address": {
|
|
123
|
+
"type": "object",
|
|
124
|
+
"additionalProperties": false,
|
|
125
|
+
"required": [
|
|
126
|
+
"street",
|
|
127
|
+
"number",
|
|
128
|
+
"complement",
|
|
129
|
+
"district",
|
|
130
|
+
"municipalityIbge",
|
|
131
|
+
"municipalityName",
|
|
132
|
+
"uf",
|
|
133
|
+
"postalCode",
|
|
134
|
+
"phone"
|
|
135
|
+
],
|
|
136
|
+
"properties": {
|
|
137
|
+
"street": { "type": "string", "minLength": 2, "maxLength": 60 },
|
|
138
|
+
"number": { "type": "string", "minLength": 1, "maxLength": 60 },
|
|
139
|
+
"complement": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 1, "maxLength": 60 }] },
|
|
140
|
+
"district": { "type": "string", "minLength": 2, "maxLength": 60 },
|
|
141
|
+
"municipalityIbge": { "type": "string", "pattern": "^[0-9]{7}$" },
|
|
142
|
+
"municipalityName": { "type": "string", "minLength": 2, "maxLength": 60 },
|
|
143
|
+
"uf": { "$ref": "#/definitions/uf" },
|
|
144
|
+
"postalCode": { "oneOf": [{ "type": "null" }, { "type": "string", "pattern": "^[0-9]{8}$" }] },
|
|
145
|
+
"phone": { "oneOf": [{ "type": "null" }, { "type": "string", "pattern": "^[0-9]{6,14}$" }] }
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
"issuer": {
|
|
149
|
+
"type": "object",
|
|
150
|
+
"additionalProperties": false,
|
|
151
|
+
"required": ["cnpj", "stateRegistration", "legalName", "tradeName", "taxRegime", "address"],
|
|
152
|
+
"properties": {
|
|
153
|
+
"cnpj": { "$ref": "#/definitions/cnpj" },
|
|
154
|
+
"stateRegistration": { "type": "string", "pattern": "^([0-9]{2,14}|ISENTO)$" },
|
|
155
|
+
"legalName": { "type": "string", "minLength": 2, "maxLength": 60 },
|
|
156
|
+
"tradeName": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 1, "maxLength": 60 }] },
|
|
157
|
+
"taxRegime": { "enum": ["SIMPLES", "SIMPLES_EXCESS", "NORMAL", "MEI"] },
|
|
158
|
+
"address": {
|
|
159
|
+
"allOf": [
|
|
160
|
+
{ "$ref": "#/definitions/address" },
|
|
161
|
+
{ "properties": { "postalCode": { "type": "string" } } }
|
|
162
|
+
]
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
"buyerDocument": {
|
|
167
|
+
"oneOf": [
|
|
168
|
+
{
|
|
169
|
+
"type": "object",
|
|
170
|
+
"additionalProperties": false,
|
|
171
|
+
"required": ["kind", "number"],
|
|
172
|
+
"properties": { "kind": { "const": "CPF" }, "number": { "type": "string", "pattern": "^[0-9]{11}$" } }
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
"type": "object",
|
|
176
|
+
"additionalProperties": false,
|
|
177
|
+
"required": ["kind", "number"],
|
|
178
|
+
"properties": { "kind": { "const": "CNPJ" }, "number": { "$ref": "#/definitions/cnpj" } }
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
"type": "object",
|
|
182
|
+
"additionalProperties": false,
|
|
183
|
+
"required": ["kind", "number"],
|
|
184
|
+
"properties": {
|
|
185
|
+
"kind": { "const": "FOREIGN" },
|
|
186
|
+
"number": { "type": "string", "pattern": "^([!-ÿ]{0}|[!-ÿ]{5,20})$" }
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
]
|
|
190
|
+
},
|
|
191
|
+
"buyer": {
|
|
192
|
+
"type": "object",
|
|
193
|
+
"additionalProperties": false,
|
|
194
|
+
"required": ["document", "name", "stateRegistrationStatus", "stateRegistration", "email", "address"],
|
|
195
|
+
"properties": {
|
|
196
|
+
"document": { "$ref": "#/definitions/buyerDocument" },
|
|
197
|
+
"name": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 2, "maxLength": 60 }] },
|
|
198
|
+
"stateRegistrationStatus": { "enum": ["CONTRIBUTOR", "EXEMPT", "NON_CONTRIBUTOR"] },
|
|
199
|
+
"stateRegistration": { "oneOf": [{ "type": "null" }, { "type": "string", "pattern": "^[0-9]{2,14}$" }] },
|
|
200
|
+
"email": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 1, "maxLength": 60 }] },
|
|
201
|
+
"address": { "oneOf": [{ "type": "null" }, { "$ref": "#/definitions/address" }] }
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
"origin": { "type": "integer", "minimum": 0, "maximum": 8 },
|
|
205
|
+
"icms": {
|
|
206
|
+
"oneOf": [
|
|
207
|
+
{
|
|
208
|
+
"type": "object",
|
|
209
|
+
"additionalProperties": false,
|
|
210
|
+
"required": ["regime", "origin", "csosn"],
|
|
211
|
+
"properties": {
|
|
212
|
+
"regime": { "const": "SIMPLES" },
|
|
213
|
+
"origin": { "$ref": "#/definitions/origin" },
|
|
214
|
+
"csosn": { "enum": ["102", "103", "300", "400", "500"] }
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
"type": "object",
|
|
219
|
+
"additionalProperties": false,
|
|
220
|
+
"required": ["regime", "origin", "csosn", "creditRateBp", "creditCents"],
|
|
221
|
+
"properties": {
|
|
222
|
+
"regime": { "const": "SIMPLES" },
|
|
223
|
+
"origin": { "$ref": "#/definitions/origin" },
|
|
224
|
+
"csosn": { "const": "101" },
|
|
225
|
+
"creditRateBp": { "$ref": "#/definitions/basisPoints" },
|
|
226
|
+
"creditCents": { "$ref": "#/definitions/cents" }
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
"type": "object",
|
|
231
|
+
"additionalProperties": false,
|
|
232
|
+
"required": ["regime", "origin", "cst", "baseCents", "rateBp", "amountCents"],
|
|
233
|
+
"properties": {
|
|
234
|
+
"regime": { "const": "NORMAL" },
|
|
235
|
+
"origin": { "$ref": "#/definitions/origin" },
|
|
236
|
+
"cst": { "const": "00" },
|
|
237
|
+
"baseCents": { "$ref": "#/definitions/cents" },
|
|
238
|
+
"rateBp": { "$ref": "#/definitions/basisPoints" },
|
|
239
|
+
"amountCents": { "$ref": "#/definitions/cents" }
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
"type": "object",
|
|
244
|
+
"additionalProperties": false,
|
|
245
|
+
"required": ["regime", "origin", "cst"],
|
|
246
|
+
"properties": {
|
|
247
|
+
"regime": { "const": "NORMAL" },
|
|
248
|
+
"origin": { "$ref": "#/definitions/origin" },
|
|
249
|
+
"cst": { "enum": ["40", "41", "50", "60"] }
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
]
|
|
253
|
+
},
|
|
254
|
+
"contribution": {
|
|
255
|
+
"description": "PIS or COFINS. CST 01/02 carry the base, the rate and the amount; 04 to 09 carry none (send zeros); 49 to 99 carry all three.",
|
|
256
|
+
"type": "object",
|
|
257
|
+
"additionalProperties": false,
|
|
258
|
+
"required": ["cst", "baseCents", "rateBp", "amountCents"],
|
|
259
|
+
"properties": {
|
|
260
|
+
"cst": {
|
|
261
|
+
"enum": [
|
|
262
|
+
"01", "02", "04", "05", "06", "07", "08", "09", "49", "50", "51", "52", "53", "54", "55", "56",
|
|
263
|
+
"60", "61", "62", "63", "64", "65", "66", "67", "70", "71", "72", "73", "74", "75", "98", "99"
|
|
264
|
+
]
|
|
265
|
+
},
|
|
266
|
+
"baseCents": { "$ref": "#/definitions/cents" },
|
|
267
|
+
"rateBp": { "$ref": "#/definitions/basisPoints" },
|
|
268
|
+
"amountCents": { "$ref": "#/definitions/cents" }
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
"ibsCbsFigures": {
|
|
272
|
+
"description": "The figures of @12-apps/fiscal/engine's LineTax: vBC and, per tax, the nominal rate and the amount; reductionBp applies to all three rates.",
|
|
273
|
+
"type": "object",
|
|
274
|
+
"additionalProperties": false,
|
|
275
|
+
"required": [
|
|
276
|
+
"baseCents",
|
|
277
|
+
"reductionBp",
|
|
278
|
+
"cbsRateBp",
|
|
279
|
+
"cbsCents",
|
|
280
|
+
"ibsUfRateBp",
|
|
281
|
+
"ibsUfCents",
|
|
282
|
+
"ibsMunRateBp",
|
|
283
|
+
"ibsMunCents"
|
|
284
|
+
],
|
|
285
|
+
"properties": {
|
|
286
|
+
"baseCents": { "$ref": "#/definitions/cents" },
|
|
287
|
+
"reductionBp": { "type": "integer", "minimum": 0, "maximum": 10000 },
|
|
288
|
+
"cbsRateBp": { "$ref": "#/definitions/basisPoints" },
|
|
289
|
+
"cbsCents": { "$ref": "#/definitions/cents" },
|
|
290
|
+
"ibsUfRateBp": { "$ref": "#/definitions/basisPoints" },
|
|
291
|
+
"ibsUfCents": { "$ref": "#/definitions/cents" },
|
|
292
|
+
"ibsMunRateBp": { "$ref": "#/definitions/basisPoints" },
|
|
293
|
+
"ibsMunCents": { "$ref": "#/definitions/cents" }
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
"ibsCbs": {
|
|
297
|
+
"type": "object",
|
|
298
|
+
"additionalProperties": false,
|
|
299
|
+
"required": ["cst", "classCode", "figures"],
|
|
300
|
+
"properties": {
|
|
301
|
+
"cst": { "type": "string", "pattern": "^[0-9]{3}$" },
|
|
302
|
+
"classCode": { "type": "string", "pattern": "^[0-9]{6}$" },
|
|
303
|
+
"figures": { "oneOf": [{ "type": "null" }, { "$ref": "#/definitions/ibsCbsFigures" }] }
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
"line": {
|
|
307
|
+
"type": "object",
|
|
308
|
+
"additionalProperties": false,
|
|
309
|
+
"required": [
|
|
310
|
+
"id",
|
|
311
|
+
"code",
|
|
312
|
+
"gtin",
|
|
313
|
+
"description",
|
|
314
|
+
"ncm",
|
|
315
|
+
"cest",
|
|
316
|
+
"cfop",
|
|
317
|
+
"unit",
|
|
318
|
+
"quantity",
|
|
319
|
+
"valueCents",
|
|
320
|
+
"discountCents",
|
|
321
|
+
"approximateTaxCents",
|
|
322
|
+
"additionalInfo",
|
|
323
|
+
"icms",
|
|
324
|
+
"pis",
|
|
325
|
+
"cofins",
|
|
326
|
+
"ibsCbs"
|
|
327
|
+
],
|
|
328
|
+
"properties": {
|
|
329
|
+
"id": { "type": "string", "minLength": 1, "maxLength": 120 },
|
|
330
|
+
"code": { "type": "string", "minLength": 1, "maxLength": 60 },
|
|
331
|
+
"gtin": { "oneOf": [{ "type": "null" }, { "type": "string", "pattern": "^([0-9]{8}|[0-9]{12,14})$" }] },
|
|
332
|
+
"description": { "type": "string", "minLength": 1, "maxLength": 120 },
|
|
333
|
+
"ncm": { "type": "string", "pattern": "^([0-9]{2}|[0-9]{8})$" },
|
|
334
|
+
"cest": { "oneOf": [{ "type": "null" }, { "type": "string", "pattern": "^[0-9]{7}$" }] },
|
|
335
|
+
"cfop": { "type": "string", "pattern": "^[1-7][0-9]{3}$" },
|
|
336
|
+
"unit": { "type": "string", "minLength": 1, "maxLength": 6 },
|
|
337
|
+
"quantity": { "type": "number", "minimum": 0.0001, "maximum": 99999999999 },
|
|
338
|
+
"valueCents": {
|
|
339
|
+
"description": "vProd: every unit, before the line's discount.",
|
|
340
|
+
"type": "integer",
|
|
341
|
+
"minimum": 1
|
|
342
|
+
},
|
|
343
|
+
"discountCents": { "$ref": "#/definitions/cents" },
|
|
344
|
+
"approximateTaxCents": { "oneOf": [{ "type": "null" }, { "$ref": "#/definitions/cents" }] },
|
|
345
|
+
"additionalInfo": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 1, "maxLength": 500 }] },
|
|
346
|
+
"icms": { "$ref": "#/definitions/icms" },
|
|
347
|
+
"pis": { "$ref": "#/definitions/contribution" },
|
|
348
|
+
"cofins": { "$ref": "#/definitions/contribution" },
|
|
349
|
+
"ibsCbs": { "oneOf": [{ "type": "null" }, { "$ref": "#/definitions/ibsCbs" }] }
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
"payment": {
|
|
353
|
+
"type": "object",
|
|
354
|
+
"additionalProperties": false,
|
|
355
|
+
"required": ["method", "amountCents", "card", "description"],
|
|
356
|
+
"properties": {
|
|
357
|
+
"method": {
|
|
358
|
+
"enum": [
|
|
359
|
+
"CASH",
|
|
360
|
+
"CHECK",
|
|
361
|
+
"CREDIT_CARD",
|
|
362
|
+
"DEBIT_CARD",
|
|
363
|
+
"STORE_CREDIT",
|
|
364
|
+
"FOOD_VOUCHER",
|
|
365
|
+
"MEAL_VOUCHER",
|
|
366
|
+
"GIFT_VOUCHER",
|
|
367
|
+
"FUEL_VOUCHER",
|
|
368
|
+
"BANK_SLIP",
|
|
369
|
+
"BANK_DEPOSIT",
|
|
370
|
+
"PIX_DYNAMIC",
|
|
371
|
+
"TRANSFER",
|
|
372
|
+
"LOYALTY",
|
|
373
|
+
"PIX_STATIC",
|
|
374
|
+
"NO_PAYMENT",
|
|
375
|
+
"OTHER"
|
|
376
|
+
]
|
|
377
|
+
},
|
|
378
|
+
"amountCents": { "$ref": "#/definitions/cents" },
|
|
379
|
+
"card": {
|
|
380
|
+
"oneOf": [
|
|
381
|
+
{ "type": "null" },
|
|
382
|
+
{
|
|
383
|
+
"type": "object",
|
|
384
|
+
"additionalProperties": false,
|
|
385
|
+
"required": ["integrated", "acquirerCnpj", "brand", "authorizationCode"],
|
|
386
|
+
"properties": {
|
|
387
|
+
"integrated": { "type": "boolean" },
|
|
388
|
+
"acquirerCnpj": { "oneOf": [{ "type": "null" }, { "$ref": "#/definitions/cnpj" }] },
|
|
389
|
+
"brand": { "oneOf": [{ "type": "null" }, { "type": "string", "pattern": "^[0-9]{2}$" }] },
|
|
390
|
+
"authorizationCode": {
|
|
391
|
+
"oneOf": [{ "type": "null" }, { "type": "string", "minLength": 1, "maxLength": 128 }]
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
]
|
|
396
|
+
},
|
|
397
|
+
"description": { "oneOf": [{ "type": "null" }, { "type": "string", "minLength": 2, "maxLength": 60 }] }
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* How a failed call is classified. The first four are the issuer port's own
|
|
9
|
+
* classes; INVALID is a malformed request, which no retry fixes; INTERNAL is a
|
|
10
|
+
* defect of the service itself, which a retry would only repeat.
|
|
11
|
+
*/
|
|
12
|
+
enum ErrorClass: string
|
|
13
|
+
{
|
|
14
|
+
case REJECTED = 'REJECTED';
|
|
15
|
+
case RETRYABLE = 'RETRYABLE';
|
|
16
|
+
case CONTINGENCY = 'CONTINGENCY';
|
|
17
|
+
case AUTH = 'AUTH';
|
|
18
|
+
case INVALID = 'INVALID';
|
|
19
|
+
case INTERNAL = 'INTERNAL';
|
|
20
|
+
|
|
21
|
+
public function httpStatus(): int
|
|
22
|
+
{
|
|
23
|
+
return match ($this) {
|
|
24
|
+
self::INVALID => 400,
|
|
25
|
+
self::AUTH => 401,
|
|
26
|
+
self::REJECTED => 422,
|
|
27
|
+
self::RETRYABLE, self::CONTINGENCY => 503,
|
|
28
|
+
self::INTERNAL => 500,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar\Http;
|
|
6
|
+
|
|
7
|
+
use Fiscal\Sidecar\ServiceError;
|
|
8
|
+
use JsonException;
|
|
9
|
+
|
|
10
|
+
/** One HTTP request, as the kernel sees it: the method, the path and the raw body. */
|
|
11
|
+
final class Request
|
|
12
|
+
{
|
|
13
|
+
public function __construct(
|
|
14
|
+
public readonly string $method,
|
|
15
|
+
public readonly string $path,
|
|
16
|
+
private readonly string $body,
|
|
17
|
+
) {
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static function fromGlobals(): self
|
|
21
|
+
{
|
|
22
|
+
$uri = (string) ($_SERVER['REQUEST_URI'] ?? '/');
|
|
23
|
+
$path = (string) parse_url($uri, PHP_URL_PATH);
|
|
24
|
+
return new self(
|
|
25
|
+
strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')),
|
|
26
|
+
$path === '' ? '/' : $path,
|
|
27
|
+
(string) file_get_contents('php://input'),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The body as a JSON object.
|
|
33
|
+
*
|
|
34
|
+
* @return array<string, mixed>
|
|
35
|
+
*/
|
|
36
|
+
public function json(): array
|
|
37
|
+
{
|
|
38
|
+
try {
|
|
39
|
+
$decoded = json_decode($this->body, true, 64, JSON_THROW_ON_ERROR);
|
|
40
|
+
} catch (JsonException) {
|
|
41
|
+
throw ServiceError::invalid('The body is not JSON.');
|
|
42
|
+
}
|
|
43
|
+
if (!is_array($decoded) || array_is_list($decoded)) {
|
|
44
|
+
throw ServiceError::invalid('The body must be a JSON object.');
|
|
45
|
+
}
|
|
46
|
+
return $decoded;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar\Http;
|
|
6
|
+
|
|
7
|
+
/** A JSON answer. */
|
|
8
|
+
final class Response
|
|
9
|
+
{
|
|
10
|
+
/** @param array<string, mixed> $body */
|
|
11
|
+
public function __construct(
|
|
12
|
+
public readonly int $status,
|
|
13
|
+
public readonly array $body,
|
|
14
|
+
) {
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public function encoded(): string
|
|
18
|
+
{
|
|
19
|
+
return (string) json_encode(
|
|
20
|
+
$this->body,
|
|
21
|
+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public function send(): void
|
|
26
|
+
{
|
|
27
|
+
http_response_code($this->status);
|
|
28
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
29
|
+
header('Cache-Control: no-store');
|
|
30
|
+
echo $this->encoded();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar\Http;
|
|
6
|
+
|
|
7
|
+
use Fiscal\Sidecar\ServiceError;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Exact-path routing. A library module registers its own paths (sped-nfe's
|
|
11
|
+
* under `/nfe`, `/nfce` and `/inutilize`); a second library registers its own
|
|
12
|
+
* prefix beside them, and a path registered twice is refused at boot.
|
|
13
|
+
*/
|
|
14
|
+
final class Router
|
|
15
|
+
{
|
|
16
|
+
/** @var array<string, array<string, callable(Request): Response>> */
|
|
17
|
+
private array $routes = [];
|
|
18
|
+
|
|
19
|
+
/** @param callable(Request): Response $handler */
|
|
20
|
+
public function add(string $method, string $path, callable $handler): void
|
|
21
|
+
{
|
|
22
|
+
if (isset($this->routes[$path][$method])) {
|
|
23
|
+
throw new \LogicException("Route registered twice: $method $path");
|
|
24
|
+
}
|
|
25
|
+
$this->routes[$path][$method] = $handler;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @return list<string> every `METHOD /path`, for the route test */
|
|
29
|
+
public function table(): array
|
|
30
|
+
{
|
|
31
|
+
$table = [];
|
|
32
|
+
foreach ($this->routes as $path => $methods) {
|
|
33
|
+
foreach (array_keys($methods) as $method) {
|
|
34
|
+
$table[] = "$method $path";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
sort($table);
|
|
38
|
+
return $table;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public function dispatch(Request $request): Response
|
|
42
|
+
{
|
|
43
|
+
$methods = $this->routes[$request->path] ?? null;
|
|
44
|
+
if ($methods === null) {
|
|
45
|
+
return new Response(404, ['error' => self::plainError('No such endpoint.')]);
|
|
46
|
+
}
|
|
47
|
+
$handler = $methods[$request->method] ?? null;
|
|
48
|
+
if ($handler === null) {
|
|
49
|
+
return new Response(405, ['error' => self::plainError('Method not allowed.')]);
|
|
50
|
+
}
|
|
51
|
+
return $handler($request);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @return array<string, mixed> */
|
|
55
|
+
private static function plainError(string $message): array
|
|
56
|
+
{
|
|
57
|
+
return (new ServiceError(\Fiscal\Sidecar\ErrorClass::INVALID, $message))->toBody()['error'];
|
|
58
|
+
}
|
|
59
|
+
}
|