diff --git a/source/app/controllers/utils.py b/source/app/controllers/utils.py index ff5cf06..d1c0ee2 100644 --- a/source/app/controllers/utils.py +++ b/source/app/controllers/utils.py @@ -32,6 +32,7 @@ import subprocess import threading import zipfile from pathlib import Path +from urllib import request from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase @@ -700,7 +701,61 @@ def cancel_xml_sign(invoice, auth, certificado): return data +def _get_data_sat(xml): + BF = 'string(//*[local-name()="{}"]/@{})' + NS_CFDI = {'cfdi': 'http://www.sat.gob.mx/cfd/3'} + + try: + tree = etree.fromstring(xml.encode()) + emisor = escape( + tree.xpath('string(//cfdi:Emisor/@rfc)', namespaces=NS_CFDI) or + tree.xpath('string(//cfdi:Emisor/@Rfc)', namespaces=NS_CFDI) + ) + receptor = escape( + tree.xpath('string(//cfdi:Receptor/@rfc)', namespaces=NS_CFDI) or + tree.xpath('string(//cfdi:Receptor/@Rfc)', namespaces=NS_CFDI) + ) + total = tree.get('total') or tree.get('Total') + uuid = tree.xpath(BF.format('TimbreFiscalDigital', 'UUID')) + except Exception as e: + return '' + + data = f'?re={emisor}&rr={receptor}&tt={total}&id={uuid}' + return data + + def get_status_sat(xml): + data = _get_data_sat(xml) + if not data: + return 'XML inválido' + + data = """ + + + + + + {} + + + + """.format(data) + headers = { + 'SOAPAction': '"http://tempuri.org/IConsultaCFDIService/Consulta"', + 'Content-type': 'text/xml; charset="UTF-8"' + } + URL = 'https://consultaqr.facturaelectronica.sat.gob.mx/consultacfdiservice.svc' + + try: + result = requests.post(URL, data=data, headers=headers) + tree = etree.fromstring(result.text) + node = tree.xpath("//*[local-name() = 'Estado']")[0] + except Exception as e: + return 'Error: {}'.format(str(e)) + + return node.text - return