Browse Source

Teste consulta NFS-e ginfes

pull/7/head
Junior Tada 10 years ago
parent
commit
e8557dcd73
  1. 38
      pynfe/processamento/assinatura.py
  2. 6
      pynfe/processamento/autorizador_nfse.py
  3. 6
      pynfe/processamento/comunicacao.py
  4. 9
      pynfe/utils/https_nfse.py

38
pynfe/processamento/assinatura.py

@ -182,3 +182,41 @@ class AssinaturaA1(Assinatura):
return xml return xml
except Exception as e: except Exception as e:
raise 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

6
pynfe/processamento/autorizador_nfse.py

@ -4,7 +4,7 @@ from importlib import import_module
class InterfaceAutorizador(): class InterfaceAutorizador():
#TODO Colocar raise Exception Not Implemented nos metodos #TODO Colocar raise Exception Not Implemented nos metodos
def consultar(self):
def consultar_rps(self):
pass pass
def cancelar(self): def cancelar(self):
@ -89,7 +89,7 @@ class SerializacaoBetha(InterfaceAutorizador):
return gnfse.toxml(element_name='GerarNfseEnvio') return gnfse.toxml(element_name='GerarNfseEnvio')
def consultar(self, nfse):
def consultar_rps(self, nfse):
"""Retorna string de um XML gerado a partir do """Retorna string de um XML gerado a partir do
XML Schema (XSD). Binding gerado pelo modulo PyXB.""" XML Schema (XSD). Binding gerado pelo modulo PyXB."""
@ -247,7 +247,7 @@ class SerializacaoGinfes(InterfaceAutorizador):
_tipos = import_module('pynfe.utils.nfse.ginfes._tipos') _tipos = import_module('pynfe.utils.nfse.ginfes._tipos')
servico_consultar_nfse_envio_v03 = import_module('pynfe.utils.nfse.ginfes.servico_consultar_nfse_envio_v03') 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 """Retorna string de um XML de consulta por Rps gerado a partir do
XML Schema (XSD). Binding gerado pelo modulo PyXB.""" XML Schema (XSD). Binding gerado pelo modulo PyXB."""

6
pynfe/processamento/comunicacao.py

@ -484,9 +484,11 @@ class ComunicacaoNfse(Comunicacao):
if metodo == 'gerar': if metodo == 'gerar':
return cliente.service.GerarNfse(cabecalho, xml) return cliente.service.GerarNfse(cabecalho, xml)
elif metodo == 'consulta': elif metodo == 'consulta':
return cliente.service.ConsultarNfsePorRps(cabecalho, xml)
import ipdb
ipdb.set_trace()
return cliente.service.ConsultarNfseV3(cabecalho, xml)
elif metodo == 'consultaRps': elif metodo == 'consultaRps':
return cliente.service.ConsultarNfsePorRps(cabecalho, xml)
return cliente.service.ConsultarNfsePorRpsV3(cabecalho, xml)
elif metodo == 'consultaFaixa': elif metodo == 'consultaFaixa':
return cliente.service.ConsultarNfseFaixa(cabecalho, xml) return cliente.service.ConsultarNfseFaixa(cabecalho, xml)
elif metodo == 'cancelar': elif metodo == 'cancelar':

9
pynfe/utils/https_nfse.py

@ -33,6 +33,9 @@ class HttpAuthenticated(HttpTransport):
self.cert = cert self.cert = cert
self.endereco = endereco self.endereco = endereco
def open(self, request):
opener = urllib.request.build_opener(HTTPSClientAuthHandler(self.key, self.cert))
return opener.open(self.endereco)
# 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)]
Loading…
Cancel
Save