@12-apps/fiscal 1.0.0 → 1.2.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 +164 -7
- package/dist/chunk-2LBQRYPV.js +21 -0
- package/dist/chunk-2LBQRYPV.js.map +1 -0
- package/dist/chunk-3SDXLSG4.js +24 -0
- package/dist/chunk-3SDXLSG4.js.map +1 -0
- package/dist/chunk-5S2PJHAE.js +85 -0
- package/dist/chunk-5S2PJHAE.js.map +1 -0
- package/dist/chunk-G3V7IYNF.js +192 -0
- package/dist/chunk-G3V7IYNF.js.map +1 -0
- package/dist/chunk-JVQKJNOC.js +14 -0
- package/dist/chunk-JVQKJNOC.js.map +1 -0
- package/dist/chunk-N5DCKYFR.js +610 -0
- package/dist/chunk-N5DCKYFR.js.map +1 -0
- package/dist/chunk-NVRYAAQ4.js +212 -0
- package/dist/chunk-NVRYAAQ4.js.map +1 -0
- package/dist/chunk-PNTKG3DZ.js +47 -0
- package/dist/chunk-PNTKG3DZ.js.map +1 -0
- package/dist/chunk-WTGASIZE.js +247 -0
- package/dist/chunk-WTGASIZE.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 +181 -0
- package/dist/documents/index.js +1234 -0
- package/dist/documents/index.js.map +1 -0
- package/dist/engine/index.d.ts +3 -246
- package/dist/http.d.ts +75 -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 +269 -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 +218 -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 +162 -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 +422 -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 +428 -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 +48 -0
- package/src/documents/helpers.ts +177 -0
- package/src/documents/index.ts +40 -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 +299 -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 +266 -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 +318 -0
- package/src/sidecar/index.ts +9 -0
- package/src/sidecar/types.ts +245 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar;
|
|
6
|
+
|
|
7
|
+
use Fiscal\Sidecar\Http\Request;
|
|
8
|
+
use Fiscal\Sidecar\Http\Response;
|
|
9
|
+
use Fiscal\Sidecar\Http\Router;
|
|
10
|
+
use Fiscal\Sidecar\Nfe\NfeModule;
|
|
11
|
+
use Fiscal\Sidecar\Nfe\Transport\CurlTransport;
|
|
12
|
+
use Fiscal\Sidecar\Nfe\Transport\Transport;
|
|
13
|
+
use Throwable;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The service: a router over the library modules, one request at a time.
|
|
17
|
+
* Built per request by `public/index.php`, so nothing a call brings in (a
|
|
18
|
+
* certificate, above all) can outlive it.
|
|
19
|
+
*/
|
|
20
|
+
final class Kernel
|
|
21
|
+
{
|
|
22
|
+
private readonly Router $router;
|
|
23
|
+
|
|
24
|
+
/** @param list<Module> $modules */
|
|
25
|
+
public function __construct(
|
|
26
|
+
private readonly array $modules,
|
|
27
|
+
private readonly RequestLog $log,
|
|
28
|
+
) {
|
|
29
|
+
$this->router = new Router();
|
|
30
|
+
$this->router->add('GET', '/health', fn (): Response => $this->health());
|
|
31
|
+
foreach ($modules as $module) {
|
|
32
|
+
$module->register($this->router);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The production wiring: every module, talking to SEFAZ over cURL. */
|
|
37
|
+
public static function production(?Transport $transport = null, ?RequestLog $log = null): self
|
|
38
|
+
{
|
|
39
|
+
return new self(self::modules($transport ?? CurlTransport::fromEnvironment()), $log ?? new RequestLog());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The libraries the service runs. A second one (NFS-e) is appended here.
|
|
44
|
+
*
|
|
45
|
+
* @return list<Module>
|
|
46
|
+
*/
|
|
47
|
+
public static function modules(Transport $transport): array
|
|
48
|
+
{
|
|
49
|
+
return [new NfeModule($transport)];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public function router(): Router
|
|
53
|
+
{
|
|
54
|
+
return $this->router;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public function handle(Request $request): Response
|
|
58
|
+
{
|
|
59
|
+
$startedAt = microtime(true);
|
|
60
|
+
try {
|
|
61
|
+
$response = $this->router->dispatch($request);
|
|
62
|
+
$class = isset($response->body['error']['class'])
|
|
63
|
+
? ErrorClass::from($response->body['error']['class'])
|
|
64
|
+
: null;
|
|
65
|
+
$this->log->record($request->method, $request->path, $response->status, $startedAt, $class, null);
|
|
66
|
+
return $response;
|
|
67
|
+
} catch (ServiceError $error) {
|
|
68
|
+
$status = $error->class->httpStatus();
|
|
69
|
+
$this->log->record($request->method, $request->path, $status, $startedAt, $error->class, $error->sefazCode);
|
|
70
|
+
return new Response($status, $error->toBody());
|
|
71
|
+
} catch (Throwable $unexpected) {
|
|
72
|
+
// The exception's message can quote input, so only its type is logged.
|
|
73
|
+
$this->log->record(
|
|
74
|
+
$request->method,
|
|
75
|
+
$request->path,
|
|
76
|
+
500,
|
|
77
|
+
$startedAt,
|
|
78
|
+
ErrorClass::INTERNAL,
|
|
79
|
+
null,
|
|
80
|
+
(new \ReflectionClass($unexpected))->getShortName(),
|
|
81
|
+
);
|
|
82
|
+
// INTERNAL, not RETRYABLE: a defect answers the same way every time,
|
|
83
|
+
// and a retry loop would only repeat it.
|
|
84
|
+
$error = new ServiceError(ErrorClass::INTERNAL, 'The service failed unexpectedly.');
|
|
85
|
+
return new Response(500, $error->toBody());
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private function health(): Response
|
|
90
|
+
{
|
|
91
|
+
$libraries = [];
|
|
92
|
+
$facts = [];
|
|
93
|
+
foreach ($this->modules as $module) {
|
|
94
|
+
$libraries += $module->libraries();
|
|
95
|
+
$facts += $module->health();
|
|
96
|
+
}
|
|
97
|
+
return new Response(200, ['status' => 'ok', 'libraries' => $libraries] + $facts);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar;
|
|
6
|
+
|
|
7
|
+
use Fiscal\Sidecar\Http\Router;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* One document library behind the service. sped-nfe (models 55 and 65) is the
|
|
11
|
+
* first; an NFS-e library is a second module with its own `/nfse/...` paths,
|
|
12
|
+
* added beside it in `Kernel::modules()`.
|
|
13
|
+
*/
|
|
14
|
+
interface Module
|
|
15
|
+
{
|
|
16
|
+
public function register(Router $router): void;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The libraries this module runs, by name, with their installed version.
|
|
20
|
+
*
|
|
21
|
+
* @return array<string, string>
|
|
22
|
+
*/
|
|
23
|
+
public function libraries(): array;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What this module adds to `/health`.
|
|
27
|
+
*
|
|
28
|
+
* @return array<string, scalar>
|
|
29
|
+
*/
|
|
30
|
+
public function health(): array;
|
|
31
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar\Nfe;
|
|
6
|
+
|
|
7
|
+
use Fiscal\Sidecar\ErrorClass;
|
|
8
|
+
use Fiscal\Sidecar\Nfe\Transport\TransportFailure;
|
|
9
|
+
use Fiscal\Sidecar\ServiceError;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* SEFAZ's status codes and the transport's failures, sorted into the issuer
|
|
13
|
+
* port's classes. Everything not listed is a REJECTED with its code: the
|
|
14
|
+
* layout's rejection table is open-ended, and a code the service does not know
|
|
15
|
+
* is still SEFAZ saying no to this document.
|
|
16
|
+
*
|
|
17
|
+
* The codes and their meanings are the MOC table as sped-nfe carries it,
|
|
18
|
+
* `vendor/nfephp-org/sped-nfe/storage/cstat.json`.
|
|
19
|
+
*/
|
|
20
|
+
final class Classifier
|
|
21
|
+
{
|
|
22
|
+
/**
|
|
23
|
+
* The certificate itself refused: the transmitter's (280 invalid, 281
|
|
24
|
+
* validity, 282 no CNPJ, 283 chain, 284 revoked, 285 not ICP-Brasil), the
|
|
25
|
+
* signature's (290–295, the same six), and a CNPJ-base mismatch (213).
|
|
26
|
+
*/
|
|
27
|
+
private const AUTH = [
|
|
28
|
+
'213', '280', '281', '282', '283', '284', '285', '290', '291', '292', '293', '294', '295',
|
|
29
|
+
];
|
|
30
|
+
/** Service stopped, momentarily or with no forecast. */
|
|
31
|
+
private const STOPPED = ['108', '109'];
|
|
32
|
+
/**
|
|
33
|
+
* Ask again: a receipt only (103), a batch still processing (105), SEFAZ
|
|
34
|
+
* failing to reach a revocation list for the transmitter's or the signing
|
|
35
|
+
* certificate (286, 296 — SEFAZ's trouble, not the certificate's), rate
|
|
36
|
+
* limiting (656), an uncatalogued error (999).
|
|
37
|
+
*/
|
|
38
|
+
private const RETRYABLE = ['103', '105', '286', '296', '656', '999'];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param bool $issuing an authorisation request: SEFAZ stopped (108, 109)
|
|
42
|
+
* means contingency there, and only "try again" anywhere else
|
|
43
|
+
*/
|
|
44
|
+
public static function fromStatus(string $status, string $reason, SefazReply $reply, bool $issuing): ServiceError
|
|
45
|
+
{
|
|
46
|
+
$class = match (true) {
|
|
47
|
+
in_array($status, self::AUTH, true) => ErrorClass::AUTH,
|
|
48
|
+
in_array($status, self::STOPPED, true) => $issuing ? ErrorClass::CONTINGENCY : ErrorClass::RETRYABLE,
|
|
49
|
+
in_array($status, self::RETRYABLE, true) => ErrorClass::RETRYABLE,
|
|
50
|
+
default => ErrorClass::REJECTED,
|
|
51
|
+
};
|
|
52
|
+
return new ServiceError($class, "SEFAZ [$status] $reason", $status, [], null, null, $reply->raw);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param bool $offlineAllowed issuing an NFC-e, the one call that can go
|
|
57
|
+
* offline (`tpEmis = 9`) when nothing reached SEFAZ
|
|
58
|
+
*/
|
|
59
|
+
public static function fromTransport(TransportFailure $failure, bool $offlineAllowed): ServiceError
|
|
60
|
+
{
|
|
61
|
+
return match ($failure->kind) {
|
|
62
|
+
TransportFailure::TLS_REFUSED => new ServiceError(
|
|
63
|
+
ErrorClass::AUTH,
|
|
64
|
+
'SEFAZ refused the client certificate (' . $failure->getMessage() . ').',
|
|
65
|
+
),
|
|
66
|
+
TransportFailure::NOT_SENT => new ServiceError(
|
|
67
|
+
$offlineAllowed ? ErrorClass::CONTINGENCY : ErrorClass::RETRYABLE,
|
|
68
|
+
'The request never reached SEFAZ (' . $failure->getMessage() . ').',
|
|
69
|
+
),
|
|
70
|
+
TransportFailure::NO_ANSWER => new ServiceError(
|
|
71
|
+
ErrorClass::RETRYABLE,
|
|
72
|
+
'The request went out and SEFAZ did not answer (' . $failure->getMessage()
|
|
73
|
+
. '); the outcome is unknown, so look the key up before sending it again.',
|
|
74
|
+
outcomeUnknown: true,
|
|
75
|
+
),
|
|
76
|
+
// INTERNAL rather than a new class: the issuer port's four classes plus
|
|
77
|
+
// INVALID (the caller's input) and INTERNAL (this side's defect) cover it,
|
|
78
|
+
// and a wrong web-service address is this side's defect.
|
|
79
|
+
TransportFailure::SEFAZ_HTTP => new ServiceError(
|
|
80
|
+
ErrorClass::INTERNAL,
|
|
81
|
+
'SEFAZ refused the request itself (' . $failure->getMessage() . '); sending it again gets the same answer.',
|
|
82
|
+
),
|
|
83
|
+
default => new ServiceError(
|
|
84
|
+
ErrorClass::RETRYABLE,
|
|
85
|
+
'An answer that is not SEFAZ\'s (' . $failure->getMessage() . '); nothing was processed.',
|
|
86
|
+
),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Fiscal\Sidecar\Nfe;
|
|
6
|
+
|
|
7
|
+
use Fiscal\Sidecar\ErrorClass;
|
|
8
|
+
use Fiscal\Sidecar\ServiceError;
|
|
9
|
+
use NFePHP\Common\Certificate;
|
|
10
|
+
use Throwable;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Opens the A1 handed in with the call, in memory. A wrong password, a file
|
|
14
|
+
* that is not PKCS#12 or an expired certificate is AUTH, and the answer says
|
|
15
|
+
* which without quoting anything it was given.
|
|
16
|
+
*/
|
|
17
|
+
final class Credentials
|
|
18
|
+
{
|
|
19
|
+
/**
|
|
20
|
+
* @param array{pfx: string, password: string} $certificate
|
|
21
|
+
* @param \DateTimeImmutable|null $now the moment the validity is judged at;
|
|
22
|
+
* the current time unless a test asks for another (PHP cannot backdate a
|
|
23
|
+
* certificate, so an expired one is judged at a later moment instead)
|
|
24
|
+
*/
|
|
25
|
+
public static function open(array $certificate, ?\DateTimeImmutable $now = null): Certificate
|
|
26
|
+
{
|
|
27
|
+
try {
|
|
28
|
+
$opened = Certificate::readPfx($certificate['pfx'], $certificate['password']);
|
|
29
|
+
} catch (Throwable) {
|
|
30
|
+
self::drainOpensslErrors();
|
|
31
|
+
throw new ServiceError(
|
|
32
|
+
ErrorClass::AUTH,
|
|
33
|
+
'The certificate could not be opened: the password is wrong or the file is not a PKCS#12 (.pfx).',
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
if ($opened->getValidTo() < ($now ?? new \DateTimeImmutable())) {
|
|
37
|
+
throw new ServiceError(
|
|
38
|
+
ErrorClass::AUTH,
|
|
39
|
+
'The certificate expired on ' . $opened->getValidTo()->format('Y-m-d') . '.',
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return $opened;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** OpenSSL keeps a per-process error queue; a failed read must not leave one for the next call. */
|
|
46
|
+
private static function drainOpensslErrors(): void
|
|
47
|
+
{
|
|
48
|
+
while (openssl_error_string() !== false) {
|
|
49
|
+
// drain
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|