File size: 3,126 Bytes
9365a43
 
 
5358f12
2d73b8f
 
9365a43
 
 
2d73b8f
9365a43
 
 
 
 
2902d8c
9365a43
 
2d73b8f
18b664c
2d73b8f
 
9365a43
1b1c3e0
2d73b8f
368095c
2d73b8f
 
 
 
 
 
027196c
 
cf3230e
2d73b8f
 
58e9d96
2d73b8f
047ff2c
 
 
92dc722
047ff2c
9365a43
 
 
4dc3e52
92dc722
3f05953
 
a9bba9f
3f05953
 
 
 
 
abcca44
 
 
 
 
 
 
c02ddff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f05953
 
 
 
 
c02ddff
3f05953
 
 
 
 
 
c02ddff
 
2d73b8f
9365a43
2d73b8f
7b23511
ce612b0
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
import time
import stripe
from fastapi import FastAPI, Request, Header
import sulkuPypi
import globales
import herramientas

app = FastAPI()

string_key = globales.llave
# This is a terrible idea, only used for demo purposes!
app.state.stripe_customer_id = None

@app.get("/")
def start(): 
    return {f"Status":"Deployed"}

@app.post("/webhook")
async def webhook_received(request: Request, stripe_signature: str = Header(None)):    
    
    webhook_secret = globales.webhook 

    data = await request.body()
    
    print("Construyendo el evento:")
    
    try:         
        event = stripe.Webhook.construct_event(
            payload=data,
            sig_header=stripe_signature,
            secret=webhook_secret
        )
        # print("Evento construido...") 
        # print(event)   
        # print("Evento impreso")         

    except Exception as e:     
        print("Excepción es: ", e)         

    try:
        event_data = event['data']['object']     
    except Exception as e:     
        print("Excepción-183 es: ", e)
    
    event_type = event['type']
    print("Voy a imprimir el event type:")
    print(event_type)
    print("Sucedido a las: ", herramientas.imprimeTime())

    if event_type == 'checkout.session.completed': 
        print("Checkout Session completado.")
        print("Status:")
        print(event_data['status'])   
        print("Payment Method Options:")     
        print(event_data['payment_method_options']) 
        print("Cantidad de elementos:")
        print(len(event_data['payment_method_options']))
        #Si hay un elemento que es card abona inmediatamente. 
        #Si no, por ahora ignora. 
        #if len(event_data['payment_method_options']) > 0: 
        print("Imprimiendo payment method cards con pago en oxxo: ")
        time.sleep(2)
        print(event_data['payment_method_options']["card"])
        time.sleep(3) 

    if event_type == 'checkout.session.async_payment_succeeded': 
        print("Pago asíncrono completado.")
        print("Checkout Session completado.")
        print("Status:")
        print(event_data['status'])  
        print("Payment Method Options:")     
        print(event_data['payment_method_options']) 
        print("Cantidad de elementos:")
        print(len(event_data['payment_method_options']))
        cus_id = event_data['customer']
        print("Customer directo:", cus_id)
        imagenes = event_data['metadata']['imagenes']
        print("Imágenes:")
        print(imagenes)  
        herramientas.registrar_evento(cus_id, int(imagenes)) 

    
    
    # if event_type == 'payment_intent.succeeded':
    #     print("Pago realizado...")
    #     #print(event_data)       
    #     
    #     
    #     # print(event_data['customer_details']['name'])
    #     print("Metadata:")
    #     print(event_data['metadata'])
    #     #cus_id = event_data['metadata']['stripe_customer_id']
    #     # print("Metadata Cusid:")
    #     # print(cus_id)
    #          
    #     
        
    else:
        print(f'unhandled event: {event_type}')      
    
    return {"status": "success"}