-
Notifications
You must be signed in to change notification settings - Fork 138
/
__init__.py
80 lines (55 loc) · 2.62 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# © 2020 Danimar Ribeiro, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import os
from requests import Session
from zeep import Client
from zeep.transports import Transport
from requests.packages.urllib3 import disable_warnings
from pytrustnfe.xml import render_xml, sanitize_response
from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key
from pytrustnfe.nfe.assinatura import Assinatura
def _render(certificado, method, **kwargs):
path = os.path.join(os.path.dirname(__file__), "templates")
xml_send = render_xml(path, "%s.xml" % method, True, **kwargs)
reference = ""
if method == "RecepcionarLoteRps":
reference = "rps%s" % kwargs["nfse"]["lista_rps"][0]["numero"]
signer = Assinatura(certificado.pfx, certificado.password)
xml_send = signer.assina_xml(xml_send, reference)
return xml_send
def _send(certificado, method, **kwargs):
base_url = ""
if kwargs["ambiente"] == "producao":
base_url = "https://www.issnetonline.com.br/webserviceabrasf/duquedecaxias/servicos.asmx?wsdl"
else:
raise Exception('Não existe ambiente de homologação para este município!')
cert, key = extract_cert_and_key_from_pfx(certificado.pfx, certificado.password)
cert, key = save_cert_key(cert, key)
disable_warnings()
session = Session()
session.cert = (cert, key)
session.verify = False
transport = Transport(session=session)
client = Client(base_url, transport=transport)
xml_send = kwargs["xml"]
response = client.service[method](xml_send)
response, obj = sanitize_response(response)
return {"sent_xml": xml_send, "received_xml": response, "object": obj}
def xml_recepcionar_lote_rps(certificado, **kwargs):
return _render(certificado, "RecepcionarLoteRps", **kwargs)
def recepcionar_lote_rps(certificado, **kwargs):
if "xml" not in kwargs:
kwargs["xml"] = xml_recepcionar_lote_rps(certificado, **kwargs)
return _send(certificado, "RecepcionarLoteRps", **kwargs)
def xml_consultar_lote_rps(certificado, **kwargs):
return _render(certificado, "ConsultarLoteRps", **kwargs)
def consultar_lote_rps(certificado, **kwargs):
if "xml" not in kwargs:
kwargs["xml"] = xml_consultar_lote_rps(certificado, **kwargs)
return _send(certificado, "ConsultarLoteRps", **kwargs)
def xml_cancelar_nfse(certificado, **kwargs):
return _render(certificado, "CancelarNfse", **kwargs)
def cancelar_nfse(certificado, **kwargs):
if "xml" not in kwargs:
kwargs["xml"] = xml_cancelar_nfse(certificado, **kwargs)
return _send(certificado, "CancelarNfse", **kwargs)