diff --git a/pynfe/processamento/comunicacao.py b/pynfe/processamento/comunicacao.py index e43e5f3..e8de7ec 100644 --- a/pynfe/processamento/comunicacao.py +++ b/pynfe/processamento/comunicacao.py @@ -30,18 +30,36 @@ class ComunicacaoSefaz(Comunicacao): _versao = VERSAO_PADRAO _assinatura = AssinaturaA1 - def autorizacao(self, modelo, nota_fiscal, idlote=1): + def autorizacao(self, modelo, nota_fiscal, idlote=1, indSinc=1): # url do serviço url = self._get_url(modelo=modelo, consulta='AUTORIZACAO') # Monta XML do corpo da requisição raiz = etree.Element('enviNFe', xmlns=NAMESPACE_NFE, versao=VERSAO_PADRAO) etree.SubElement(raiz, 'idLote').text = str(idlote) # numero autoincremental gerado pelo sistema - etree.SubElement(raiz, 'indSinc').text = str(1) # 0 para assincrono, 1 para sincrono + etree.SubElement(raiz, 'indSinc').text = str(indSinc) # 0 para assincrono, 1 para sincrono raiz.append(nota_fiscal) # Monta XML para envio da requisição xml = self._construir_xml_status_pr(cabecalho=self._cabecalho_soap(metodo='NfeAutorizacao'), metodo='NfeAutorizacao', dados=raiz) + # Faz request no Servidor da Sefaz + retorno = self._post(url, xml) - return self._post(url, xml) + # Em caso de sucesso, retorna xml com nfe e protocolo de autorização. + # Caso contrário, envia todo o soap de resposta da Sefaz para decisão do usuário. + import ipdb + ipdb.set_trace() + if retorno.status_code == 200: + if indSinc == 1: + # Procuta status no xml + ns = {'ns':'http://www.portalfiscal.inf.br/nfe'} # namespace + prot = etree.fromstring(retorno.text) + prot = prot[1][0][0][6] # root protNFe + status = prot.xpath("ns:infProt/ns:cStat", namespaces=ns)[0].text + if status == '100': + raiz = etree.Element('nfeProc', xmlns=NAMESPACE_NFE, versao=VERSAO_PADRAO) + raiz.append(nota_fiscal) + raiz.append(prot) + return 0, raiz + return 1, retorno def consulta_recibo(self, modelo, numero): """ @@ -253,13 +271,9 @@ class ComunicacaoSefaz(Comunicacao): xml = etree.tostring(xml, encoding='unicode', pretty_print=False).replace('\n','') xml = xml_declaration + xml # Faz o request com o servidor - print (xml) result = requests.post(url, xml, headers=self._post_header(), cert=chave_cert, verify=False) - if result == 200: - result.encoding='utf-8' - return result - else: - return result + result.encoding='utf-8' + return result except requests.exceptions.ConnectionError as e: raise e finally: diff --git a/pynfe/processamento/danfe.py b/pynfe/processamento/danfe.py index a03b337..9173f6f 100644 --- a/pynfe/processamento/danfe.py +++ b/pynfe/processamento/danfe.py @@ -13,25 +13,32 @@ class DanfeNfce(Danfe): """ Classe para geração de Danfe para Nota Fiscal de Consumidor Eletrônica (NFC-e). """ def gerar_qrcode(self, token, csc, xml, uf): - # Procura atributos no xml - nfe = xml[1][0][0][2] - chave = nfe[0].attrib['Id'].replace('NFe','') - data = nfe[0][0][7].text.encode() - tpamb = nfe[0][0][14].text - cpf = nfe[0][2][0].text - total = nfe[0][4][0][14].text - icms = nfe[0][4][0][1].text - digest = nfe[1][0][2][2].text.encode() - - data = base64.b16encode(data).decode() - digest = base64.b16encode(digest).decode() - - url = 'chNFe={}&nVersao={}&tpAmb={}&cDest={}&dhEmi={}&vNF={}&vICMS={}&digVal={}&cIdToken={}'.format( - chave, VERSAO_QRCODE, tpamb, cpf, data.lower(), total, icms, digest.lower(), token) - - url_hash = hashlib.sha1(url.encode()+csc.encode()).digest() - url_hash = base64.b16encode(url_hash).decode() - - url = url + '&cHashQRCode=' + url_hash.upper() - - return NFCE[uf.upper()]['QR'] + url \ No newline at end of file + """ Classe para gerar url do qrcode da NFC-e """ + try: + # Procura atributos no xml + ns = {'ns':'http://www.portalfiscal.inf.br/nfe'} + sig = {'sig':'http://www.w3.org/2000/09/xmldsig#'} + # Tag Raiz NFe Ex: + nfe = xml[0] + chave = nfe[0].attrib['Id'].replace('NFe','') + data = nfe.xpath('ns:infNFe/ns:ide/ns:dhEmi/text()', namespaces=ns)[0].encode() + tpamb = nfe.xpath('ns:infNFe/ns:ide/ns:tpAmb/text()', namespaces=ns)[0] + cpf = nfe.xpath('ns:infNFe/ns:dest/ns:CPF/text()', namespaces=ns)[0] + total = nfe.xpath('ns:infNFe/ns:total/ns:ICMSTot/ns:vNF/text()', namespaces=ns)[0] + icms = nfe.xpath('ns:infNFe/ns:total/ns:ICMSTot/ns:vICMS/text()', namespaces=ns)[0] + digest = nfe.xpath('sig:Signature/sig:SignedInfo/sig:Reference/sig:DigestValue/text()', namespaces=sig)[0].encode() + + data = base64.b16encode(data).decode() + digest = base64.b16encode(digest).decode() + + url = 'chNFe={}&nVersao={}&tpAmb={}&cDest={}&dhEmi={}&vNF={}&vICMS={}&digVal={}&cIdToken={}'.format( + chave, VERSAO_QRCODE, tpamb, cpf, data.lower(), total, icms, digest.lower(), token) + + url_hash = hashlib.sha1(url.encode()+csc.encode()).digest() + url_hash = base64.b16encode(url_hash).decode() + + url = url + '&cHashQRCode=' + url_hash.upper() + + return NFCE[uf.upper()]['QR'] + url + except Exception as e: + raise e \ No newline at end of file