# `SefazNfe.SOAP.Envelope`
[🔗](https://github.com/devaction-labs/sefaz_nfe/blob/v0.5.0/lib/sefaz_nfe/soap/envelope.ex#L1)

Builds the SOAP 1.2 envelope the 4.00 webservices expect.

Each service takes its message inside `nfeDadosMsg`, whose namespace is the
service's own WSDL namespace — that is what routes the call on the `.asmx`
endpoint, so it is per service rather than global.

CT-e and MDF-e use the same SOAP 1.2 shape with their own namespaces
(`.../cte/wsdl/<service>`, `.../mdfe/wsdl/<service>`) and their own body
element, `cteDadosMsg` and `mdfeDadosMsg`.

The message itself is passed through untouched. For `SefazNfe.authorize/1`
that matters: the ERP owns the XML and a signed document must reach SEFAZ byte
for byte, or the signature breaks (SEFAZ-04).

# `service`

```elixir
@type service() :: SefazNfe.Endpoints.service()
```

# `action`

```elixir
@spec action(service(), String.t() | nil) ::
  {:ok, String.t()} | {:error, {:unknown_service, term()}}
```

SOAPAction URI for `service`.

`operation` overrides the service's default operation name. Only
`NFeConsultaCadastro` needs it: its authorizers disagree on the name, so it
comes from `SefazNfe.Endpoints.operation/2`. `nil` keeps the default.

# `authorization_result`

```elixir
@spec authorization_result(String.t(), 1 | 2) :: binary()
```

`consReciNFe` for `NFeRetAutorizacao4`.

# `consult_protocol`

```elixir
@spec consult_protocol(String.t(), 1 | 2) :: binary()
```

`consSitNFe` for `NFeConsultaProtocolo4`.

# `consult_taxpayer`

```elixir
@spec consult_taxpayer(String.t(), {:tax_id | :ie, String.t()}) :: binary()
```

`ConsCad` for `NFeConsultaCadastro` (layout 2.00, not 4.00).

`uf` is the UF whose registry is consulted. `filter` is exactly one of
`{:tax_id, id}` (CPF or CNPJ) and `{:ie, ie}`.

MT quirk (from NFePHP): SEFAZ-MT wants the message wrapped in one more
`nfeDadosMsg`, with no namespace of its own, inside the WSDL one. Every other
authorizer takes `ConsCad` directly.

# `dist_dfe`

```elixir
@spec dist_dfe(String.t(), pos_integer(), 1 | 2, {atom(), String.t()}) :: binary()
```

`distDFeInt` for `NFeDistribuicaoDFe`.

The cursor is one of `{:ult_nsu, nsu}` to walk forward, `{:nsu, nsu}` for a
single document, or `{:ch_nfe, key}` to fetch one by access key.

# `send_event`

```elixir
@spec send_event(String.t(), String.t()) :: binary()
```

`envEvento` for `NFeRecepcaoEvento4`, carrying one already signed event.

# `send_nfe`

```elixir
@spec send_nfe(String.t(), String.t(), 0 | 1) :: binary()
```

`enviNFe` for `NFeAutorizacao4`, carrying one already signed NF-e.

`ind_sinc` 1 asks SEFAZ to process the batch in the same call and answer with
the protocol; 0 answers a receipt that `SefazNfe.authorization_result/1` then
consults. The MOC allows up to 50 documents per batch, and v1 sends one.

The signed document is embedded verbatim apart from its XML declaration,
which is not allowed inside another document. Touching anything else would
break the signature.

# `status_service`

```elixir
@spec status_service(pos_integer(), 1 | 2) :: binary()
```

`consStatServ` for `NFeStatusServico4`.

`xServ` is fixed at `STATUS`; it is the only operation the service defines.

# `status_service_cte`

```elixir
@spec status_service_cte(pos_integer(), 1 | 2) :: binary()
```

`consStatServCTe` for `CTeStatusServicoV4`, layout 4.00.

Same fields as the NF-e one (`tpAmb`, `cUF`, `xServ` fixed at `STATUS`) in the
CT-e namespace.

# `status_service_mdfe`

```elixir
@spec status_service_mdfe(1 | 2) :: binary()
```

`consStatServMDFe` for `MDFeStatusServico`, layout 3.00.

Unlike NF-e and CT-e it has no `cUF`: SVRS answers for every UF.

# `tax_id_element`

```elixir
@spec tax_id_element(String.t()) :: binary()
```

A CPF is 11 digits; anything else is treated as a CNPJ.

# `tp_amb`

```elixir
@spec tp_amb(:production | :homologation) :: 1 | 2
```

`tpAmb` for an environment: 1 is production, 2 is homologation.

# `wrap`

```elixir
@spec wrap(service(), iodata()) ::
  {:ok, binary()} | {:error, {:unknown_service, term()}}
```

Wraps `message` for `service`.

The UF services take `nfeDadosMsg` directly in the body. `NFeDistribuicaoDFe`
nests it one level deeper, inside the operation element — sending it flat
makes the Ambiente Nacional answer a null reference from its own .NET stack
rather than a `cStat`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
