Skip to main content

attach_cancellation

Function attach_cancellation 

Source
pub fn attach_cancellation(
    nfe_proc_xml: &str,
    cancel_event_xml: &str,
) -> Result<String, FiscalError>
Expand description

Attach a cancellation event response to an authorized <nfeProc> XML, marking the NF-e as locally cancelled.

This mirrors the PHP Complements::cancelRegister() method. The function searches the cancel_event_xml for <retEvento> elements whose:

  • cStat is in [135, 136, 155] (valid cancellation statuses)
  • tpEvento is 110111 (cancellation) or 110112 (cancellation by substitution)
  • chNFe matches the access key in the authorized NF-e’s <protNFe>

When a matching <retEvento> is found, it is appended inside the <nfeProc> element (before the closing </nfeProc> tag).

If no matching cancellation event is found, the original NF-e XML is returned unchanged (same behavior as the PHP implementation).

§Arguments

  • nfe_proc_xml - The authorized NF-e XML containing <nfeProc> with <protNFe>.
  • cancel_event_xml - The SEFAZ cancellation event response XML containing <retEvento>.

§Errors

Returns FiscalError::XmlParsing if:

  • The nfe_proc_xml does not contain <protNFe> (not an authorized NF-e)
  • The <protNFe> does not contain <chNFe>