From 70816097e9959996e996246b4a929015076f524a Mon Sep 17 00:00:00 2001 From: Danimar Ribeiro Date: Wed, 27 Jun 2018 17:35:35 -0300 Subject: [PATCH] [IMP] Implementing NFe 4.0 - Cleaning up code and improve readability --- pytrustnfe/Servidores.py | 16 ++++---- pytrustnfe/client.py | 31 --------------- pytrustnfe/nfe/__init__.py | 45 +++++++++++++++------- pytrustnfe/nfe/templates/NFeDistribuicaoDFe.xml | 30 ++++++--------- pytrustnfe/nfe/templates/RecepcaoEvento.xml | 2 +- .../nfe/templates/RecepcaoEventoManifesto.xml | 2 +- 6 files changed, 52 insertions(+), 74 deletions(-) diff --git a/pytrustnfe/Servidores.py b/pytrustnfe/Servidores.py index e6a5bad..2328f14 100644 --- a/pytrustnfe/Servidores.py +++ b/pytrustnfe/Servidores.py @@ -56,6 +56,7 @@ SIGLA_ESTADO = { '28': 'SE', '35': 'SP', '17': 'TO', + '91': 'AN' } @@ -250,17 +251,15 @@ SVC_RS = { AN = { AMBIENTE_PRODUCAO: { 'servidor': 'www1.nfe.fazenda.gov.br', - WS_NFE_RECEPCAO_EVENTO: 'RecepcaoEvento/RecepcaoEvento.asmx', - WS_DFE_DISTRIBUICAO: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx', - WS_DOWNLOAD_NFE: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx', - WS_NFE_RECEPCAO_EVENTO: 'RecepcaoEvento/RecepcaoEvento.asmx', + WS_DFE_DISTRIBUICAO: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx?wsdl', + WS_DOWNLOAD_NFE: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx?wsdl', + WS_NFE_RECEPCAO_EVENTO: 'NFeRecepcaoEvento4/NFeRecepcaoEvento4.asmx?wsdl', }, AMBIENTE_HOMOLOGACAO: { 'servidor': 'hom.nfe.fazenda.gov.br', - WS_NFE_RECEPCAO_EVENTO: 'RecepcaoEvento/RecepcaoEvento.asmx', - WS_DFE_DISTRIBUICAO: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx', - WS_DOWNLOAD_NFE: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx', - WS_NFE_RECEPCAO_EVENTO: 'RecepcaoEvento/RecepcaoEvento.asmx', + WS_DFE_DISTRIBUICAO: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx?wsdl', + WS_DOWNLOAD_NFE: 'NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx?wsdl', + WS_NFE_RECEPCAO_EVENTO: 'NFeRecepcaoEvento4/NFeRecepcaoEvento4.asmx?Wsdl', }, } @@ -626,4 +625,5 @@ ESTADO_WS = { 'SE': SVRS, 'SP': UFSP, 'TO': SVRS, + 'AN': AN, } diff --git a/pytrustnfe/client.py b/pytrustnfe/client.py index e6e67e3..7ed1c5a 100644 --- a/pytrustnfe/client.py +++ b/pytrustnfe/client.py @@ -2,11 +2,9 @@ # © 2016 Danimar Ribeiro, Trustcode # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -import urllib3 import requests import suds.client import suds_requests -from requests.packages.urllib3.exceptions import InsecureRequestWarning def get_authenticated_client(base_url, cert, key): @@ -33,32 +31,3 @@ def get_client(base_url): cache=cache, transport=suds_requests.RequestsTransport(session) ) - - -class HttpClient(object): - - def __init__(self, url, cert_path, key_path): - self.url = url - self.cert_path = cert_path - self.key_path = key_path - - def _headers(self, action, send_raw): - if send_raw: - return { - 'Content-type': 'text/xml; charset=utf-8;', - 'SOAPAction': "http://www.portalfiscal.inf.br/nfe/wsdl/%s" % action, - 'Accept': 'application/soap+xml; charset=utf-8', - } - - return { - 'Content-type': 'application/soap+xml; charset=utf-8;', - 'SOAPAction': 'http://www.portalfiscal.inf.br/nfe/wsdl/%s' % action, - } - - def post_soap(self, xml_soap, cabecalho, send_raw): - header = self._headers(cabecalho.soap_action, send_raw) - urllib3.disable_warnings(category=InsecureRequestWarning) - res = requests.post(self.url, data=xml_soap.encode('utf-8'), - cert=(self.cert_path, self.key_path), - verify=False, headers=header) - return res.text diff --git a/pytrustnfe/nfe/__init__.py b/pytrustnfe/nfe/__init__.py index c9ecdff..c067c83 100644 --- a/pytrustnfe/nfe/__init__.py +++ b/pytrustnfe/nfe/__init__.py @@ -165,7 +165,7 @@ def _send(certificado, method, **kwargs): return { 'sent_xml': xml_send, 'received_xml': response, - 'object': obj.Body.nfeResultMsg + 'object': obj.Body.getchildren()[0] } @@ -253,23 +253,13 @@ def recepcao_evento_carta_correcao(certificado, **kwargs): # Assinar def xml_recepcao_evento_manifesto(certificado, **kwargs): # Assinar - return _render(certificado, 'RecepcaoEventoManifesto', True, **kwargs) + return _render(certificado, 'RecepcaoEvento', True, **kwargs) def recepcao_evento_manifesto(certificado, **kwargs): # Assinar if "xml" not in kwargs: kwargs['xml'] = xml_recepcao_evento_manifesto(certificado, **kwargs) - return _send(certificado, 'RecepcaoEventoManifesto', **kwargs) - - -def xml_recepcao_evento_epec(certificado, **kwargs): # Assinar - return _render(certificado, 'RecepcaoEventoEPEC', True, **kwargs) - - -def recepcao_evento_epec(certificado, **kwargs): # Assinar - if "xml" not in kwargs: - kwargs['xml'] = xml_recepcao_evento_epec(certificado, **kwargs) - return _send(certificado, 'RecepcaoEventoEPEC', **kwargs) + return _send(certificado, 'RecepcaoEvento', **kwargs) def xml_consulta_distribuicao_nfe(certificado, **kwargs): # Assinar @@ -279,7 +269,34 @@ def xml_consulta_distribuicao_nfe(certificado, **kwargs): # Assinar def consulta_distribuicao_nfe(certificado, **kwargs): if "xml" not in kwargs: kwargs['xml'] = xml_consulta_distribuicao_nfe(certificado, **kwargs) - return _send(certificado, 'NFeDistribuicaoDFe', **kwargs) + xml_send = kwargs["xml"] + base_url = localizar_url( + 'NFeDistribuicaoDFe', kwargs['estado'], kwargs['modelo'], + kwargs['ambiente']) + + cert, key = extract_cert_and_key_from_pfx( + certificado.pfx, certificado.password) + cert, key = save_cert_key(cert, key) + + session = Session() + session.cert = (cert, key) + session.verify = False + transport = Transport(session=session) + + xml = etree.fromstring(xml_send) + xml_um = etree.fromstring('AN1.00') + client = Client(base_url, transport=transport) + + port = next(iter(client.wsdl.port_types)) + first_operation = next(iter(client.wsdl.port_types[port].operations)) + with client.options(raw_response=True): + response = client.service[first_operation](nfeDadosMsg=xml, _soapheaders=[xml_um]) + response, obj = sanitize_response(response.text) + return { + 'sent_xml': xml_send, + 'received_xml': response, + 'object': obj.Body.nfeDistDFeInteresseResponse.nfeDistDFeInteresseResult + } def xml_download_nfe(certificado, **kwargs): # Assinar diff --git a/pytrustnfe/nfe/templates/NFeDistribuicaoDFe.xml b/pytrustnfe/nfe/templates/NFeDistribuicaoDFe.xml index 9e4d774..cd3fc38 100644 --- a/pytrustnfe/nfe/templates/NFeDistribuicaoDFe.xml +++ b/pytrustnfe/nfe/templates/NFeDistribuicaoDFe.xml @@ -1,19 +1,11 @@ - - - - - - {{ ambiente }} - {{ estado }} - {{ cnpj_cpf }} - - {{ ultimo_nsu }} - - - {{ chave_nfe }} - - - - - - + + {{ ambiente }} + {{ estado }} + {{ cnpj_cpf }} + + {{ ultimo_nsu }} + + + {{ chave_nfe }} + + diff --git a/pytrustnfe/nfe/templates/RecepcaoEvento.xml b/pytrustnfe/nfe/templates/RecepcaoEvento.xml index b49a043..58a3a67 100644 --- a/pytrustnfe/nfe/templates/RecepcaoEvento.xml +++ b/pytrustnfe/nfe/templates/RecepcaoEvento.xml @@ -15,7 +15,7 @@ {{ evento.descEvento }} {{ evento.nProt }} {{ evento.xJust|normalize|escape }} - {{ evento.xCondUso }}. + {{ evento.xCondUso }} diff --git a/pytrustnfe/nfe/templates/RecepcaoEventoManifesto.xml b/pytrustnfe/nfe/templates/RecepcaoEventoManifesto.xml index 0d77fa3..d592678 100644 --- a/pytrustnfe/nfe/templates/RecepcaoEventoManifesto.xml +++ b/pytrustnfe/nfe/templates/RecepcaoEventoManifesto.xml @@ -1,7 +1,7 @@ {{ lote }} - + 91 {{ ambiente }} {{ manifesto.cnpj_empresa }}