forked from luisiturrios1/python-cfdiclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejemplo_completo.py
More file actions
101 lines (67 loc) · 2.22 KB
/
ejemplo_completo.py
File metadata and controls
101 lines (67 loc) · 2.22 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
import base64
import datetime
import os
import time
from cfdiclient import Autenticacion
from cfdiclient import DescargaMasiva
from cfdiclient import Fiel
from cfdiclient import SolicitaDescarga
from cfdiclient import VerificaSolicitudDescarga
##
## Constantes de Loggin
##
RFC = 'ESI920427886'
FIEL_CER = 'ejemploCer.cer'
FIEL_KEY = 'ejemploKey.key'
FIEL_PAS = '12345678a'
PATH = 'certificados/'
cer_der = open(os.path.join(PATH, FIEL_CER), 'rb').read()
key_der = open(os.path.join(PATH, FIEL_KEY), 'rb').read()
FECHA_INICIAL = datetime.date(2020, 1, 1)
FECHA_FINAL = datetime.date(2020, 6, 24)
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'])
# 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