Skip to content

Commit

Permalink
Merge pull request #84 from kmee/add/mdfe_assina_raiz
Browse files Browse the repository at this point in the history
[ADD] MDFe signature changes
  • Loading branch information
rvalyi authored Oct 5, 2023
2 parents 78ec810 + 31402d0 commit d87c97f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions nfelib/nfe/ws/edoc_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Copyright 2023 - TODAY Akretion - Raphaël Valyi <raphael.valyi@akretion.com>

from dataclasses import is_dataclass
import base64
from lxml import etree


from erpbrasil.assinatura.assinatura import Assinatura

Expand Down Expand Up @@ -70,7 +73,7 @@ def _post(self, raiz, url, operacao, classe):
xml_string, xml_etree = self._generateds_to_string_etree(raiz)
with self._transmissao.cliente(url):
retorno = self._transmissao.enviar(operacao, xml_etree)
if is_dataclass(raiz):
if is_dataclass(classe):
return analisar_retorno_raw_xsdata(
operacao, raiz, xml_string, retorno, classe
)
Expand Down Expand Up @@ -107,4 +110,17 @@ class MDeAdapter(DocumentoElectronicoAdapter, MDe):


class MDFeAdapter(DocumentoElectronicoAdapter, MDFe):
pass
NAMESPACES = {
"mdfe": "http://www.portalfiscal.inf.br/mdfe",
"ds": "http://www.w3.org/2000/09/xmldsig#",
}

def assina_raiz(self, raiz, id, getchildren=False):
xml_assinado = super(MDFeAdapter, self).assina_raiz(raiz, id, getchildren)

xml_assinado = etree.fromstring(xml_assinado)
# MDFe requires certificate to be base64 encoded
cert_tag = xml_assinado.find('.//ds:X509Certificate', namespaces=self.NAMESPACES)
cert_tag.text = base64.b64encode(cert_tag.text.encode())

return etree.tostring(xml_assinado).decode()

0 comments on commit d87c97f

Please sign in to comment.