Skip to content

Commit

Permalink
Merge 69bc7a9 into 59d4cf9
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaGD committed Dec 19, 2018
2 parents 59d4cf9 + 69bc7a9 commit d49e255
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
32 changes: 23 additions & 9 deletions pytrustnfe/nfe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import requests
from lxml import etree
from .patch import nfeInutilizacaoCE, has_patch
from .assinatura import Assinatura
from pytrustnfe.xml import render_xml, sanitize_response
from pytrustnfe.utils import gerar_chave, ChaveNFe
Expand Down Expand Up @@ -65,28 +66,41 @@ def _render(certificado, method, sign, **kwargs):
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(
certificado.pfx, certificado.password)
cert, key = save_cert_key(cert, key)

session = Session()
session.cert = (cert, key)
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)

port = next(iter(client.wsdl.port_types))
first_operation = [x for x in iter(
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)
else:
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")
if namespaceNFe is not None:
Expand Down
30 changes: 30 additions & 0 deletions pytrustnfe/nfe/patch.py
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages


VERSION = "1.0.28"
VERSION = "1.0.29"


setup(
Expand Down

0 comments on commit d49e255

Please sign in to comment.