Browse Source

Merge branch 'master3' of https://github.com/ImplantiSolucoes/PyTrustNFe into master3

pull/233/head
Michael Douglas Pereira de Morais 6 years ago
parent
commit
5880217735
  1. 92
      pytrustnfe/nfe/__init__.py
  2. 4
      pytrustnfe/nfe/assinatura.py
  3. 4
      pytrustnfe/nfe/templates/NfeAutorizacao.xml
  4. 1
      setup.py

92
pytrustnfe/nfe/__init__.py

@ -1,8 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# © 2016 Danimar Ribeiro, Trustcode # © 2016 Danimar Ribeiro, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import hashlib
import os import os
import requests import requests
from lxml import etree from lxml import etree
@ -11,10 +10,9 @@ from .assinatura import Assinatura
from pytrustnfe.xml import render_xml, sanitize_response from pytrustnfe.xml import render_xml, sanitize_response
from pytrustnfe.utils import gerar_chave, ChaveNFe from pytrustnfe.utils import gerar_chave, ChaveNFe
from pytrustnfe.Servidores import localizar_url from pytrustnfe.Servidores import localizar_url
from pytrustnfe.urls import url_qrcode, url_qrcode_exibicao
from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key
from requests.packages.urllib3.exceptions import InsecureRequestWarning from requests.packages.urllib3.exceptions import InsecureRequestWarning
# Zeep # Zeep
from requests import Session from requests import Session
from zeep import Client from zeep import Client
@ -42,6 +40,12 @@ def _generate_nfe_id(**kwargs):
def _render(certificado, method, sign, **kwargs): def _render(certificado, method, sign, **kwargs):
path = os.path.join(os.path.dirname(__file__), 'templates') path = os.path.join(os.path.dirname(__file__), 'templates')
if sign and kwargs['NFes'][0]['ide']['mod'] == '65':
kwargs['NFes'][0]['qrCode'] = 1 if 'qrCode' not in kwargs else \
kwargs['NFes'][0]['qrCode']
kwargs['NFes'][0]['urlChave'] = 1 \
if 'urlChave' not in kwargs['NFes'][0] else \
kwargs['NFes'][0]['urlChave']
xmlElem_send = render_xml(path, '%s.xml' % method, True, **kwargs) xmlElem_send = render_xml(path, '%s.xml' % method, True, **kwargs)
modelo = xmlElem_send.find(".//{http://www.portalfiscal.inf.br/nfe}mod") modelo = xmlElem_send.find(".//{http://www.portalfiscal.inf.br/nfe}mod")
@ -53,7 +57,85 @@ def _render(certificado, method, sign, **kwargs):
xml_send = signer.assina_xml(xmlElem_send, kwargs['obj']['id']) xml_send = signer.assina_xml(xmlElem_send, kwargs['obj']['id'])
if method == 'NfeAutorizacao': if method == 'NfeAutorizacao':
xml_send = signer.assina_xml( xml_send = signer.assina_xml(
xmlElem_send, kwargs['NFes'][0]['infNFe']['Id'])
xmlElem_send, kwargs['NFes'][0]['infNFe']['Id'], True)
if modelo == '65' and 'urlChave' not in kwargs['NFes'][0] and \
'qrCode' not in kwargs['NFes'][0]:
if kwargs['NFes'][0]['ide']['tpEmis'] != 1:
digest_value = xmlElem_send.find(
".//{http://www.w3.org/2000/09/xmldsig#}DigestValue")
c_hash_qr_code = \
"{ch_acesso}|{versao}|{tp_amb}|{dh_emi}|" \
"{v_nf}|{dig_val}|{id_csc}|{csc}".format(
ch_acesso=kwargs['NFes'][0]['NFe']['infNFe']['Id'].
replace('NFe', ''),
versao=2,
tp_amb=kwargs['NFes'][0]['ide']['tpAmb'],
dh_emi=kwargs['NFes'][0]['ide']['dhEmi'].split("-")[2].
split("T")[0],
v_nf=kwargs['NFes'][0]['total']['vNF'],
dig_val=digest_value.text,
id_csc=int(kwargs['NFes'][0]['id_csc']),
csc=kwargs['NFes'][0]['csc']
)
c_hash_qr_code = hashlib.sha1(c_hash_qr_code.encode()). \
hexdigest()
qr_code_url = 'p={ch_acesso}|{versao}|{tp_amb}|{dh_emi}|" \
"{v_nf}|{dig_val}|{id_csc}|{hash}'.format(
ch_acesso=kwargs['NFes'][0]['NFe']['infNFe']['Id'].
replace('NFe', ''),
versao=2,
tp_amb=kwargs['NFes'][0]['ide']['tpAmb'],
dh_emi=kwargs['NFes'][0]['ide']['dhEmi'].split("-")[2].
split("T")[0],
v_nf=kwargs['NFes'][0]['total']['vNF'],
dig_val=digest_value.text,
id_csc=int(kwargs['NFes'][0]['id_csc']),
hash=c_hash_qr_code
)
qrcode = url_qrcode(
kwargs['NFes'][0]['emit']['enderEmit']['UF'],
str(kwargs['NFes'][0]['ide']['tpAmb'])
) + qr_code_url
url_consulta = url_qrcode_exibicao(
kwargs['NFes'][0]['emit']['enderEmit']['UF'],
str(kwargs['NFes'][0]['ide']['tpAmb'])
)
else:
c_hash_qr_code = \
"{ch_acesso}|{versao}|{tp_amb}|{id_csc}|{csc}".format(
ch_acesso=kwargs['NFe']['infNFe']['Id'].
replace('NFe', ''),
versao=2,
tp_amb=kwargs['ide']['tpAmb'],
id_csc=int(kwargs['id_csc']),
csc=kwargs['csc']
)
c_hash_qr_code = hashlib.sha1(
c_hash_qr_code.encode()).hexdigest()
qr_code_url = "p={ch_acesso}|{versao}|{tp_amb}|{id_csc}|" \
"{hash}".format(
ch_acesso=kwargs['NFe']['infNFe']['Id']
.replace('NFe', ''),
versao=2,
tp_amb=kwargs['ide']['tpAmb'],
id_csc=int(kwargs['id_csc']),
hash=c_hash_qr_code
)
qrcode = url_qrcode(
kwargs['emit']['enderEmit']['UF'],
str(kwargs['ide']['tpAmb'])
) + qr_code_url
url_consulta = url_qrcode_exibicao(
kwargs['emit']['enderEmit']['UF'],
str(kwargs['ide']['tpAmb'])
)
qrCode = xml_send.find(
'.//{http://www.portalfiscal.inf.br/nfe}qrCode').text = \
qrcode
urlChave = xml_send.find(
'.//{http://www.portalfiscal.inf.br/nfe}urlChave').text = \
url_consulta
xml_send = etree.tostring(xmlElem_send, encoding=str)
elif method == 'RecepcaoEvento': elif method == 'RecepcaoEvento':
xml_send = signer.assina_xml( xml_send = signer.assina_xml(
xmlElem_send, kwargs['eventos'][0]['Id']) xmlElem_send, kwargs['eventos'][0]['Id'])

4
pytrustnfe/nfe/assinatura.py

@ -14,7 +14,7 @@ class Assinatura(object):
self.arquivo = arquivo self.arquivo = arquivo
self.senha = senha self.senha = senha
def assina_xml(self, xml_element, reference):
def assina_xml(self, xml_element, reference, return_element_etree=False):
cert, key = extract_cert_and_key_from_pfx(self.arquivo, self.senha) cert, key = extract_cert_and_key_from_pfx(self.arquivo, self.senha)
for element in xml_element.iter("*"): for element in xml_element.iter("*"):
@ -42,4 +42,6 @@ class Assinatura(object):
if element_signed is not None and signature is not None: if element_signed is not None and signature is not None:
parent = element_signed.getparent() parent = element_signed.getparent()
parent.append(signature) parent.append(signature)
if return_element_etree is True:
return signed_root
return etree.tostring(signed_root, encoding=str) return etree.tostring(signed_root, encoding=str)

4
pytrustnfe/nfe/templates/NfeAutorizacao.xml

@ -28,6 +28,10 @@
<indPres>{{ ide.indPres }}</indPres> <indPres>{{ ide.indPres }}</indPres>
<procEmi>{{ ide.procEmi }}</procEmi> <procEmi>{{ ide.procEmi }}</procEmi>
<verProc>{{ ide.verProc }}</verProc> <verProc>{{ ide.verProc }}</verProc>
{% if ide.tpEmis != 1 %}
<dhCont>{{ ide.dhCont }}</dhCont>
<xJust>{{ ide.xJust }}</xJust>
{% endif %}
{% if ide.NFref is defined -%} {% if ide.NFref is defined -%}
{% for ref in ide.NFref %} {% for ref in ide.NFref %}
<NFref> <NFref>

1
setup.py

@ -1,7 +1,6 @@
# coding=utf-8 # coding=utf-8
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = "1.0.44" VERSION = "1.0.44"

Loading…
Cancel
Save