SefazNfe.NFCe (sefaz_nfe v0.5.0)

Copy Markdown View Source

The NFC-e (model 65) pieces that are not plain transport: infNFeSupl and the on-line QR code, version 3.00 (NT 2025.001).

QR code 3.00

Version 3.00 dropped the CSC, its id token and the hash, so an on-line QR code is a pure function of the document:

{qr_url}?p={chNFe}|3|{tpAmb}

qr_url is always the emitting UF's own address (SefazNfe.Endpoints.qr_code_url/2), also where SVRS authorizes. A host that still builds version 2 with a CSC puts its own infNFeSupl in the XML, and put_supl/3 leaves such a document exactly as it is.

QR code 2.00 (CSC)

Opt in with qr_code_version: 2, csc: and csc_id: (put_supl/4). The text follows the "Manual de Especificacoes Tecnicas do DANFE NFC-e e QR Code" v5, formatted as NFePHP's QRCode::get200 does:

on-line:  {qr_url}?p={chNFe}|2|{tpAmb}|{cIdToken}|{hash}
off-line: {qr_url}?p={chNFe}|2|{tpAmb}|{dd}|{vNF}|{hex(DigestValue)}|{cIdToken}|{hash}

hash is the upper-case hex SHA-1 of everything before it plus the CSC. cIdToken is the integer, so leading zeros are dropped; dd is the day of dhEmi as written in the document, two digits; vNF has two decimals; and the digest is the hex of the ASCII text of the base64 DigestValue in SignedInfo, which is why an off-line version 2 needs a signed document. Version 2 off-line needs only the CSC, no certificate signature, so for version 2 (and only for it) tpEmis 9 is supported.

The CSC is a secret. It is never logged, never part of an error tuple and never inspected by this library; do not put it in logs or telemetry on the host side either.

Off-line (tpEmis 9), version 3

Not supported: the off-line QR code embeds the issue day, the total, the recipient and the document's own digest value, and is only meaningful together with the contingency flow (AD-005, planned for Phase 1). Without an infNFeSupl of the host's own it is {:error, {:unsupported, :offline_qr_code}} rather than a QR code SEFAZ would reject as a schema failure.

Placement and escaping

infNFeSupl is a child of NFe, after infNFe and before Signature. It is not covered by the signature, whose only Reference is infNFe's Id, so it is spliced into the already signed bytes immediately before <Signature, by byte offset (:binary.match/2) — never by grapheme, which an accent earlier in the document would shift (AD-011, CHANGELOG 0.1.1). The text is XML-escaped (&, <, >): the |, ? and = the QR code is made of need none, but a published address may carry an ampersand and qrCode is element text, not CDATA.

Summary

Functions

Makes sure the NFC-e xml carries an infNFeSupl.

The version 3.00 on-line QR code text.

Functions

put_supl(xml, uf, environment, opts \\ [])

@spec put_supl(
  String.t(),
  String.t() | atom(),
  SefazNfe.Endpoints.environment(),
  keyword()
) ::
  {:ok, String.t()} | {:error, term()}

Makes sure the NFC-e xml carries an infNFeSupl.

uf is the emitting UF, which selects the QR code and urlChave addresses, and environment selects between the production and homologation tables. The key and tpAmb come from the document itself.

Options

  • :qr_code_version - 3 (default) or 2.
  • :csc and :csc_id - required for version 2 and ignored for 3. The CSC is a string; its id is an integer or a numeric string. Either missing gives {:error, {:missing_csc, fields}} naming the fields, never values.

Works on a signed or an unsigned document; the element goes before <Signature when there is one, and before </NFe> otherwise.

qr_code(qr_url, key, tp_amb)

@spec qr_code(String.t(), String.t(), String.t()) :: String.t()

The version 3.00 on-line QR code text.

?p= is appended unless the address already carries it.