|
|
|
@ -8,6 +8,10 @@ NAMESPACE_SOAP_NFSE, NAMESPACE_BETHA |
|
|
|
from pynfe.utils.webservices import NFCE, NFE, NFSE |
|
|
|
from .assinatura import AssinaturaA1 |
|
|
|
from pynfe.entidades.certificado import CertificadoA1 |
|
|
|
import requests |
|
|
|
from suds.transport.http import HttpAuthenticated |
|
|
|
from suds.transport import Reply, TransportError |
|
|
|
|
|
|
|
|
|
|
|
class Comunicacao(object): |
|
|
|
u"""Classe abstrata responsavel por definir os metodos e logica das classes |
|
|
|
@ -431,39 +435,38 @@ class ComunicacaoNfse(Comunicacao): |
|
|
|
raise Exception('Autorizador nao encontrado!') |
|
|
|
return self.url |
|
|
|
|
|
|
|
def _post(self, url, xml): |
|
|
|
certificadoA1 = CertificadoA1(self.certificado) |
|
|
|
chave, cert = certificadoA1.separar_arquivo(self.certificado_senha, caminho=True) |
|
|
|
chave_cert = (cert, chave) |
|
|
|
|
|
|
|
# Abre a conexão HTTPS |
|
|
|
def _post(self, url, xml, metodo): |
|
|
|
# cabecalho |
|
|
|
cabecalho = self._cabecalho() |
|
|
|
# comunicacao wsdl |
|
|
|
try: |
|
|
|
xml_declaration='<?xml version="1.0" encoding="utf-8"?>' |
|
|
|
#xml = etree.tostring(xml, encoding='unicode', pretty_print=False).replace('\n','').replace('ns0:','soapenv:').replace(':ns0',':soapenv') |
|
|
|
xml = etree.tostring(xml, encoding='unicode', pretty_print=False).replace('\n','').replace('ns0:','').replace(':ns0','') |
|
|
|
xml = xml_declaration + xml |
|
|
|
|
|
|
|
print (url) |
|
|
|
print (xml) |
|
|
|
import ipdb |
|
|
|
ipdb.set_trace() |
|
|
|
|
|
|
|
# Faz o request com o servidor |
|
|
|
result = requests.post(url, xml, headers=self._post_header(), cert=chave_cert, verify=False) |
|
|
|
result.encoding='utf-8' |
|
|
|
return result |
|
|
|
except requests.exceptions.ConnectionError as e: |
|
|
|
from suds.client import Client |
|
|
|
cliente = Client(url) |
|
|
|
# gerar nfse |
|
|
|
if metodo == 'gerar': |
|
|
|
return cliente.service.GerarNfse(cabecalho, xml) |
|
|
|
elif metodo == 'consultaRps': |
|
|
|
return cliente.service.ConsultarNfsePorRps(cabecalho, xml) |
|
|
|
elif metodo == 'consultaFaixa': |
|
|
|
return cliente.service.ConsultarNfseFaixa(cabecalho, xml) |
|
|
|
elif metodo == 'cancelar': |
|
|
|
return cliente.service.CancelarNfse(cabecalho, xml) |
|
|
|
# TODO outros metodos |
|
|
|
else: |
|
|
|
pass |
|
|
|
except Exception as e: |
|
|
|
raise e |
|
|
|
finally: |
|
|
|
certificadoA1.excluir() |
|
|
|
|
|
|
|
def _post2(self, url, xml, metodo): |
|
|
|
def _post_https(self, url, xml, metodo): |
|
|
|
# cabecalho |
|
|
|
cabecalho = self._cabecalho() |
|
|
|
# comunicacao wsdl |
|
|
|
try: |
|
|
|
from suds.client import Client |
|
|
|
cliente = Client(url) |
|
|
|
headers = {"Content-Type": "text/xml;charset=UTF-8", "SOAPAction": ""} |
|
|
|
t = self.RequestsTransport(cert='/home/leonardo/Documentos/certificado/TADA_SOFTWARE_LTDA_ME.pfx') |
|
|
|
cliente = Client(url, headers=headers, transport=t) |
|
|
|
|
|
|
|
# gerar nfse |
|
|
|
if metodo == 'gerar': |
|
|
|
return cliente.service.GerarNfse(cabecalho, xml) |
|
|
|
@ -478,3 +481,16 @@ class ComunicacaoNfse(Comunicacao): |
|
|
|
pass |
|
|
|
except Exception as e: |
|
|
|
raise e |
|
|
|
|
|
|
|
class RequestsTransport(HttpAuthenticated): |
|
|
|
def __init__(self, **kwargs): |
|
|
|
self.cert = kwargs.pop('cert', None) |
|
|
|
# super won't work because not using new style class |
|
|
|
HttpAuthenticated.__init__(self, **kwargs) |
|
|
|
|
|
|
|
def send(self, request): |
|
|
|
self.addcredentials(request) |
|
|
|
resp = requests.post(request.url, data=request.message, |
|
|
|
headers=request.headers, cert=self.cert) |
|
|
|
result = Reply(resp.status_code, resp.headers, resp.content) |
|
|
|
return result |