From e8557dcd734fe8a0c0b6e1fe9846c300a19d454e Mon Sep 17 00:00:00 2001 From: Junior Tada Date: Mon, 21 Dec 2015 11:41:19 -0200 Subject: [PATCH] Teste consulta NFS-e ginfes --- pynfe/processamento/assinatura.py | 38 +++++++++++++++++++++++++++++++++ pynfe/processamento/autorizador_nfse.py | 6 +++--- pynfe/processamento/comunicacao.py | 8 ++++--- pynfe/utils/https_nfse.py | 9 +++++--- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/pynfe/processamento/assinatura.py b/pynfe/processamento/assinatura.py index 3045c84..bde6ccb 100644 --- a/pynfe/processamento/assinatura.py +++ b/pynfe/processamento/assinatura.py @@ -182,3 +182,41 @@ class AssinaturaA1(Assinatura): return xml except Exception as e: raise e + + def assinarConsulta(self, xml, retorna_string=False): + try: + xml = etree.fromstring(xml) + # No raiz do XML de saida + tag = 'ConsultarNfseEnvio' # tag que serĂ¡ assinada + raiz = etree.Element('Signature', xmlns='http://www.w3.org/2000/09/xmldsig#') + siginfo = etree.SubElement(raiz, 'SignedInfo') + etree.SubElement(siginfo, 'CanonicalizationMethod', Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315') + etree.SubElement(siginfo, 'SignatureMethod', Algorithm='http://www.w3.org/2000/09/xmldsig#rsa-sha1') + # Consulta nao tem id + ref = etree.SubElement(siginfo, 'Reference', URI='') + + trans = etree.SubElement(ref, 'Transforms') + etree.SubElement(trans, 'Transform', Algorithm='http://www.w3.org/2000/09/xmldsig#enveloped-signature') + etree.SubElement(trans, 'Transform', Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315') + etree.SubElement(ref, 'DigestMethod', Algorithm='http://www.w3.org/2000/09/xmldsig#sha1') + etree.SubElement(ref, 'DigestValue') + etree.SubElement(raiz, 'SignatureValue') + keyinfo = etree.SubElement(raiz, 'KeyInfo') + etree.SubElement(keyinfo, 'X509Data') + + consulta = xml.xpath('/ConsultarNfseEnvio', namespaces={'ns1': 'http://www.ginfes.com.br/servico_consultar_nfse_envio_v03.xsd', 'ns2':'http://www.ginfes.com.br/tipos_v03.xsd'})[0] + consulta.append(raiz) + + # Escreve no arquivo depois de remover caracteres especiais e parse string + with open('nfse.xml', 'w') as arquivo: + arquivo.write(remover_acentos(etree.tostring(xml, encoding="unicode", pretty_print=False))) + + subprocess.call(['xmlsec1', '--sign', '--pkcs12', self.certificado, '--pwd', self.senha, '--crypto', 'openssl', '--output', 'funfa.xml', '--id-attr:Id', tag, 'nfse.xml']) + xml = etree.parse('funfa.xml').getroot() + + if retorna_string: + return etree.tostring(xml, encoding="unicode", pretty_print=False) + else: + return xml + except Exception as e: + raise e diff --git a/pynfe/processamento/autorizador_nfse.py b/pynfe/processamento/autorizador_nfse.py index ab9f43b..7c12ac5 100644 --- a/pynfe/processamento/autorizador_nfse.py +++ b/pynfe/processamento/autorizador_nfse.py @@ -4,7 +4,7 @@ from importlib import import_module class InterfaceAutorizador(): #TODO Colocar raise Exception Not Implemented nos metodos - def consultar(self): + def consultar_rps(self): pass def cancelar(self): @@ -89,7 +89,7 @@ class SerializacaoBetha(InterfaceAutorizador): return gnfse.toxml(element_name='GerarNfseEnvio') - def consultar(self, nfse): + def consultar_rps(self, nfse): """Retorna string de um XML gerado a partir do XML Schema (XSD). Binding gerado pelo modulo PyXB.""" @@ -247,7 +247,7 @@ class SerializacaoGinfes(InterfaceAutorizador): _tipos = import_module('pynfe.utils.nfse.ginfes._tipos') servico_consultar_nfse_envio_v03 = import_module('pynfe.utils.nfse.ginfes.servico_consultar_nfse_envio_v03') - def consultar(self, nfse): + def consultar_rps(self, nfse): """Retorna string de um XML de consulta por Rps gerado a partir do XML Schema (XSD). Binding gerado pelo modulo PyXB.""" diff --git a/pynfe/processamento/comunicacao.py b/pynfe/processamento/comunicacao.py index 1985e3c..e71a4e3 100644 --- a/pynfe/processamento/comunicacao.py +++ b/pynfe/processamento/comunicacao.py @@ -474,7 +474,7 @@ class ComunicacaoNfse(Comunicacao): try: from suds.client import Client from pynfe.utils.https_nfse import HttpAuthenticated - + certificadoA1 = CertificadoA1(self.certificado) chave, cert = certificadoA1.separar_arquivo(self.certificado_senha, caminho=True) @@ -484,9 +484,11 @@ class ComunicacaoNfse(Comunicacao): if metodo == 'gerar': return cliente.service.GerarNfse(cabecalho, xml) elif metodo == 'consulta': - return cliente.service.ConsultarNfsePorRps(cabecalho, xml) + import ipdb + ipdb.set_trace() + return cliente.service.ConsultarNfseV3(cabecalho, xml) elif metodo == 'consultaRps': - return cliente.service.ConsultarNfsePorRps(cabecalho, xml) + return cliente.service.ConsultarNfsePorRpsV3(cabecalho, xml) elif metodo == 'consultaFaixa': return cliente.service.ConsultarNfseFaixa(cabecalho, xml) elif metodo == 'cancelar': diff --git a/pynfe/utils/https_nfse.py b/pynfe/utils/https_nfse.py index f5ea46d..baffcd5 100644 --- a/pynfe/utils/https_nfse.py +++ b/pynfe/utils/https_nfse.py @@ -33,6 +33,9 @@ class HttpAuthenticated(HttpTransport): self.cert = cert self.endereco = endereco - def open(self, request): - opener = urllib.request.build_opener(HTTPSClientAuthHandler(self.key, self.cert)) - return opener.open(self.endereco) \ No newline at end of file + # def open(self, request): + # opener = urllib.request.build_opener(HTTPSClientAuthHandler(self.key, self.cert)) + # return opener.open(self.endereco) + + def u2handlers(self): + return [HTTPSClientAuthHandler(self.key, self.cert)] \ No newline at end of file