Browse Source

[FIX] Inutilização NFe CE - Add a patch method

pull/207/head
marinaGD 7 years ago
committed by Danimar Ribeiro
parent
commit
c74893b658
  1. 31
      pytrustnfe/nfe/__init__.py
  2. 30
      pytrustnfe/nfe/patch.py
  3. 2
      setup.py

31
pytrustnfe/nfe/__init__.py

@ -6,6 +6,7 @@
import os import os
import requests import requests
from lxml import etree from lxml import etree
from .patch import nfeInutilizacaoCE, has_patch
from .assinatura import Assinatura from .assinatura import Assinatura
from pytrustnfe.xml import render_xml, sanitize_response from pytrustnfe.xml import render_xml, sanitize_response
from pytrustnfe.utils import gerar_chave, ChaveNFe from pytrustnfe.utils import gerar_chave, ChaveNFe
@ -65,11 +66,7 @@ def _render(certificado, method, sign, **kwargs):
return xml_send return xml_send
def _send(certificado, method, **kwargs):
xml_send = kwargs["xml"]
base_url = localizar_url(
method, kwargs['estado'], kwargs['modelo'], kwargs['ambiente'])
def _get_session(certificado):
cert, key = extract_cert_and_key_from_pfx( cert, key = extract_cert_and_key_from_pfx(
certificado.pfx, certificado.password) certificado.pfx, certificado.password)
cert, key = save_cert_key(cert, key) cert, key = save_cert_key(cert, key)
@ -77,16 +74,32 @@ def _send(certificado, method, **kwargs):
session = Session() session = Session()
session.cert = (cert, key) session.cert = (cert, key)
session.verify = False session.verify = False
transport = Transport(session=session)
return session
parser = etree.XMLParser(strip_cdata=False)
xml = etree.fromstring(xml_send, parser=parser)
def _get_client(base_url, transport):
client = Client(base_url, transport=transport) client = Client(base_url, transport=transport)
port = next(iter(client.wsdl.port_types)) port = next(iter(client.wsdl.port_types))
first_operation = [x for x in iter( first_operation = [x for x in iter(
client.wsdl.port_types[port].operations) if "zip" not in x.lower()][0] client.wsdl.port_types[port].operations) if "zip" not in x.lower()][0]
return first_operation, client
def _send(certificado, method, **kwargs):
xml_send = kwargs["xml"]
base_url = localizar_url(
method, kwargs['estado'], kwargs['modelo'], kwargs['ambiente'])
session = _get_session(certificado)
if has_patch:
return nfeInutilizacaoCE(session, xml_send)
transport = Transport(session=session)
first_op, client = _get_client(base_url, transport)
return _send_zeep(first_op, client, xml_send)
def _send_zeep(first_operation, client, xml_send):
parser = etree.XMLParser(strip_cdata=False)
xml = etree.fromstring(xml_send, parser=parser)
namespaceNFe = xml.find(".//{http://www.portalfiscal.inf.br/nfe}NFe") namespaceNFe = xml.find(".//{http://www.portalfiscal.inf.br/nfe}NFe")
if namespaceNFe is not None: if namespaceNFe is not None:

30
pytrustnfe/nfe/patch.py

@ -0,0 +1,30 @@
from ..Servidores import SIGLA_ESTADO
from pytrustnfe.xml import sanitize_response
methods = [
'nfeInutilizacaoCE']
def has_patch(cod_estado, metodo):
uf = SIGLA_ESTADO[cod_estado]
method = metodo+uf
return method in methods
def nfeInutilizacaoCE(session, xml_send):
soap = '<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope"><Body>\
<nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeInutilizacao4"\
>' + xml_send + '</nfeDadosMsg></Body></Envelope>'
headers = {
'SOAPAction': "",
'Content-Type': 'application/soap+xml; charset="utf-8"'
}
response = session.post(
'https://nfeh.sefaz.ce.gov.br/nfe4/services/NFeInutilizacao4',
data=soap, headers=headers)
response, obj = sanitize_response(response.text)
return {
'sent_xml': xml_send,
'received_xml': response,
'object': obj.Body.getchildren()[0]
}

2
setup.py

@ -2,7 +2,7 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = "1.0.28"
VERSION = "1.0.29"
setup( setup(

Loading…
Cancel
Save