Skip to main content

Cliente Python Web Service del SAT para la descarga masiva de CFDIs

Project description

python-cfdiclient

Cliente Python Web Service del SAT para la descarga masiva de xml

Consulta y recuperación de comprobantes (Nuevo)

https://www.sat.gob.mx/consultas/42968/consulta-y-recuperacion-de-comprobantes-(nuevo)

Instalacion

En Windows requiere Microsoft Visual C++ Compiler for Python 2.7

pip install cfdiclient

Ejemplo Completo

import base64
import datetime
import os
import time

from cfdiclient import (Autenticacion, DescargaMasiva, Fiel, SolicitaDescarga,
                        VerificaSolicitudDescarga)

RFC = 'IUAL9406031K4'
FIEL_CER = 'asd.cer'
FIEL_KEY = 'df.key'
FIEL_PAS = ''
FECHA_INICIAL = datetime.date(2020, 1, 1)
FECHA_FINAL = datetime.date(2020, 6, 24)
PATH = 'Inputs/IUAL9406031K4/'

cer_der = open(os.path.join(PATH, FIEL_CER), 'rb').read()
key_der = open(os.path.join(PATH, FIEL_KEY), 'rb').read()

fiel = Fiel(cer_der, key_der, FIEL_PAS)

auth = Autenticacion(fiel)

token = auth.obtener_token()

print('TOKEN: ', token)

descarga = SolicitaDescarga(fiel)

# EMITIDOS
# solicitud = descarga.solicitar_descarga(
#     token, RFC, FECHA_INICIAL, FECHA_FINAL, rfc_emisor=RFC, tipo_solicitud='CFDI'
# )

# RECIBIDOS
solicitud = descarga.solicitar_descarga(
    token, RFC, FECHA_INICIAL, FECHA_FINAL, rfc_receptor=RFC, tipo_solicitud='CFDI'
)

print('SOLICITUD:', solicitud)

while True:

    token = auth.obtener_token()

    print('TOKEN: ', token)

    verificacion = VerificaSolicitudDescarga(fiel)

    verificacion = verificacion.verificar_descarga(
        token, RFC, solicitud['id_solicitud'])

    print('SOLICITUD:', verificacion)

    estado_solicitud = int(verificacion['estado_solicitud'])

    # 0, Token invalido.
    # 1, Aceptada
    # 2, En proceso
    # 3, Terminada
    # 4, Error
    # 5, Rechazada
    # 6, Vencida

    if estado_solicitud <= 2:

        # Si el estado de solicitud esta Aceptado o en proceso el programa espera
        # 60 segundos y vuelve a tratar de verificar
        time.sleep(60)

        continue

    elif estado_solicitud >= 4:

        print('ERROR:', estado_solicitud)

        break

    else:
        # Si el estatus es 3 se trata de descargar los paquetes

        for paquete in verificacion['paquetes']:

            descarga = DescargaMasiva(fiel)

            descarga = descarga.descargar_paquete(token, RFC, paquete)

            print('PAQUETE: ', paquete)

            with open('{}.zip'.format(paquete), 'wb') as fp:

                fp.write(base64.b64decode(descarga['paquete_b64']))

        break

Ejemplo

Autenticacion

from cfdiclient import Autenticacion
from cfdiclient import Fiel

FIEL_KEY = 'Claveprivada_FIEL_XAXX010101000_20180918_134149.key'
FIEL_CER = 'XAXX010101000.cer'
FIEL_PAS = 'contrasena'
cer_der = open(FIEL_CER, 'rb').read()
key_der = open(FIEL_KEY, 'rb').read()
fiel = Fiel(cer_der, key_der, FIEL_PAS)

auth = Autenticacion(fiel)

token = auth.obtener_token()

print(token)

Solicita Descarga

import datetime
from cfdiclient import SolicitaDescarga
from cfdiclient import Fiel

FIEL_KEY = 'Claveprivada_FIEL_XAXX010101000_20180918_134149.key'
FIEL_CER = 'XAXX010101000.cer'
FIEL_PAS = 'contrasena'
cer_der = open(FIEL_CER, 'rb').read()
key_der = open(FIEL_KEY, 'rb').read()

fiel = Fiel(cer_der, key_der, FIEL_PAS)

descarga = SolicitaDescarga(fiel)

token = 'eyJh'
rfc_solicitante = 'XAXX010101000'
fecha_inicial = datetime.datetime(2018, 1, 1)
fecha_final = datetime.datetime(2018, 12, 31)
rfc_emisor = 'XAXX010101000'
rfc_receptor = 'XAXX010101000'
# Emitidos
result = descarga.solicitar_descarga(token, rfc_solicitante, fecha_inicial, fecha_final, rfc_emisor=rfc_emisor)
print(result)
# Recibidos
result = descarga.solicitar_descarga(token, rfc_solicitante, fecha_inicial, fecha_final, rfc_receptor=rfc_receptor)
print(result)
# {'mensaje': 'Solicitud Aceptada', 'cod_estatus': '5000', 'id_solicitud': 'be2a3e76-684f-416a-afdf-0f9378c346be'}

Verifica Solicitud Descarga

from cfdiclient import VerificaSolicitudDescarga
from cfdiclient import Fiel

FIEL_KEY = 'Claveprivada_FIEL_XAXX010101000_20180918_134149.key'
FIEL_CER = 'XAXX010101000.cer'
FIEL_PAS = 'contrasena'
cer_der = open(FIEL_CER, 'rb').read()
key_der = open(FIEL_KEY, 'rb').read()

fiel = Fiel(cer_der, key_der, FIEL_PAS)

v_descarga = VerificaSolicitudDescarga(fiel)

token = 'eyJhbGci'
rfc_solicitante = 'XAXX010101000'
id_solicitud = '6331caae-c253-406f-9332-126f89cc474a'
result = v_descarga.verificar_descarga(token, rfc_solicitante, id_solicitud)
print(result)
# {'estado_solicitud': '3', 'numero_cfdis': '8', 'cod_estatus': '5000', 'paquetes': ['a4897f62-a279-4f52-bc35-03bde4081627_01'], 'codigo_estado_solicitud': '5000', 'mensaje': 'Solicitud Aceptada'}

Descargar Paquetes

from cfdiclient import DescargaMasiva
from cfdiclient import Fiel

FIEL_KEY = 'Claveprivada_FIEL_XAXX010101000_20180918_134149.key'
FIEL_CER = 'XAXX010101000.cer'
FIEL_PAS = 'contrasena'
cer_der = open(FIEL_CER, 'rb').read()
key_der = open(FIEL_KEY, 'rb').read()

fiel = Fiel(cer_der, key_der, FIEL_PAS)

descarga = DescargaMasiva(fiel)

token = 'eyJhbG'
rfc_solicitante = 'XAXX010101000'
id_paquete = '2d8bbdf1-c36d-4b51-a57c-c1744acdd89c_01'
result = descarga.descargar_paquete(token, rfc_solicitante, id_paquete)
print(result)
# {'cod_estatus': '', 'mensaje': '', 'paquete_b64': 'eyJhbG=='}

Valida estado de documento

from cfdiclient import Validacion

validacion = Validacion()
rfc_emisor = 'XAXX010101000'
rfc_receptor = 'XAXX010101000'
total = '1000.41'
uuid = '0XXX0X00-000-0XX0-XX0X-000X0X0XXX00'

estado = validacion.obtener_estado(rfc_emisor, rfc_receptor, total, uuid)

print(estado)
# {'codigo_estatus': 'S - Comprobante obtenido satisfactoriamente.', 'es_cancelable': 'Cancelable con aceptación', 'estado': 'Vigente'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cfdiclient-1.5.4.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

cfdiclient-1.5.4-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file cfdiclient-1.5.4.tar.gz.

File metadata

  • Download URL: cfdiclient-1.5.4.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for cfdiclient-1.5.4.tar.gz
Algorithm Hash digest
SHA256 3aa2645efcea9619650da6344f3300b15dfd3d56a23dd5360a83de7a1f2ff88d
MD5 8303f9905cd087281c4d9386dbe9d3cc
BLAKE2b-256 5796e648942e9f15edeaa0edbcc553bc323f6cc37ec127262815b8cedb9f6fdb

See more details on using hashes here.

Provenance

File details

Details for the file cfdiclient-1.5.4-py3-none-any.whl.

File metadata

  • Download URL: cfdiclient-1.5.4-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for cfdiclient-1.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a40a0305e0cecb190994b9367bc42564cb648e4483a0124dd248067196b92cd0
MD5 35ca01a6237e73f1614dc4397102cfdb
BLAKE2b-256 f13f6b9ae5e2192fe6bdc9be605e7689696ca1220ee886bee575d79070f59229

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page