Browse Source

[IMP] Implementing NFe 4.0 - Cleaning up code and improve readability

pull/147/head
Danimar Ribeiro 8 years ago
parent
commit
70816097e9
  1. 16
      pytrustnfe/Servidores.py
  2. 31
      pytrustnfe/client.py
  3. 45
      pytrustnfe/nfe/__init__.py
  4. 30
      pytrustnfe/nfe/templates/NFeDistribuicaoDFe.xml
  5. 2
      pytrustnfe/nfe/templates/RecepcaoEvento.xml
  6. 2
      pytrustnfe/nfe/templates/RecepcaoEventoManifesto.xml

16
pytrustnfe/Servidores.py

@ -56,6 +56,7 @@ SIGLA_ESTADO = {
'28': 'SE', '28': 'SE',
'35': 'SP', '35': 'SP',
'17': 'TO', '17': 'TO',
'91': 'AN'
} }
@ -250,17 +251,15 @@ SVC_RS = {
AN = { AN = {
AMBIENTE_PRODUCAO: { AMBIENTE_PRODUCAO: {
'servidor': 'www1.nfe.fazenda.gov.br', '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: { AMBIENTE_HOMOLOGACAO: {
'servidor': 'hom.nfe.fazenda.gov.br', '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, 'SE': SVRS,
'SP': UFSP, 'SP': UFSP,
'TO': SVRS, 'TO': SVRS,
'AN': AN,
} }

31
pytrustnfe/client.py

@ -2,11 +2,9 @@
# © 2016 Danimar Ribeiro, Trustcode # © 2016 Danimar Ribeiro, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import urllib3
import requests import requests
import suds.client import suds.client
import suds_requests import suds_requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
def get_authenticated_client(base_url, cert, key): def get_authenticated_client(base_url, cert, key):
@ -33,32 +31,3 @@ def get_client(base_url):
cache=cache, cache=cache,
transport=suds_requests.RequestsTransport(session) 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

45
pytrustnfe/nfe/__init__.py

@ -165,7 +165,7 @@ def _send(certificado, method, **kwargs):
return { return {
'sent_xml': xml_send, 'sent_xml': xml_send,
'received_xml': response, '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 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 def recepcao_evento_manifesto(certificado, **kwargs): # Assinar
if "xml" not in kwargs: if "xml" not in kwargs:
kwargs['xml'] = xml_recepcao_evento_manifesto(certificado, **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 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): def consulta_distribuicao_nfe(certificado, **kwargs):
if "xml" not in kwargs: if "xml" not in kwargs:
kwargs['xml'] = xml_consulta_distribuicao_nfe(certificado, **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('<nfeCabecMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/"><cUF>AN</cUF><versaoDados>1.00</versaoDados></nfeCabecMsg>')
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 def xml_download_nfe(certificado, **kwargs): # Assinar

30
pytrustnfe/nfe/templates/NFeDistribuicaoDFe.xml

@ -1,19 +1,11 @@
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<nfeDistDFeInteresse xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe">
<nfeDadosMsg>
<distDFeInt xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.01">
<tpAmb>{{ ambiente }}</tpAmb>
<cUFAutor>{{ estado }}</cUFAutor>
<CNPJ>{{ cnpj_cpf }}</CNPJ>
<distNSU>
<ultNSU>{{ ultimo_nsu }}</ultNSU>
</distNSU>
<consChNFe>
<chNFe>{{ chave_nfe }}</chNFe>
</consChNFe>
</distDFeInt>
</nfeDadosMsg>
</nfeDistDFeInteresse>
</Body>
</Envelope>
<distDFeInt xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.01">
<tpAmb>{{ ambiente }}</tpAmb>
<cUFAutor>{{ estado }}</cUFAutor>
<CNPJ>{{ cnpj_cpf }}</CNPJ>
<distNSU>
<ultNSU>{{ ultimo_nsu }}</ultNSU>
</distNSU>
<consChNFe>
<chNFe>{{ chave_nfe }}</chNFe>
</consChNFe>
</distDFeInt>

2
pytrustnfe/nfe/templates/RecepcaoEvento.xml

@ -15,7 +15,7 @@
<descEvento>{{ evento.descEvento }}</descEvento> <descEvento>{{ evento.descEvento }}</descEvento>
<nProt>{{ evento.nProt }}</nProt> <nProt>{{ evento.nProt }}</nProt>
<xJust>{{ evento.xJust|normalize|escape }}</xJust> <xJust>{{ evento.xJust|normalize|escape }}</xJust>
<xCondUso>{{ evento.xCondUso }}.</xCondUso>
<xCondUso>{{ evento.xCondUso }}</xCondUso>
</detEvento> </detEvento>
</infEvento> </infEvento>
</evento> </evento>

2
pytrustnfe/nfe/templates/RecepcaoEventoManifesto.xml

@ -1,7 +1,7 @@
<envEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"> <envEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00">
<idLote>{{ lote }}</idLote> <idLote>{{ lote }}</idLote>
<evento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"> <evento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00">
<infEvento Id="{{ manifesto.identificador }}">
<infEvento Id="{{ evento.Id }}">
<cOrgao>91</cOrgao> <cOrgao>91</cOrgao>
<tpAmb>{{ ambiente }}</tpAmb> <tpAmb>{{ ambiente }}</tpAmb>
<CNPJ>{{ manifesto.cnpj_empresa }}</CNPJ> <CNPJ>{{ manifesto.cnpj_empresa }}</CNPJ>

Loading…
Cancel
Save