Spaces:
Sleeping
Sleeping
File size: 3,202 Bytes
9365a43 2d73b8f 918b080 f0995ed 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 767123a c5d5fac 918b080 abcca44 95c027a 2817194 a7699b6 c02ddff 2817194 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 |
import stripe
import globales
import herramientas
import ga4Analiticas
from fastapi import FastAPI, Request, Header
app = FastAPI()
string_key = globales.llave
@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']))
gaCliente = event_data['metadata']['gaCliente']
print("gacliente:", gaCliente)
print("Sending to GA4...")
ga4Analiticas.send_ga4_purchase_event(event_data)
#Si hay un elemento que es card abona inmediatamente.
#Si no, por ahora ignora.
if len(event_data['payment_method_options']) > 0: #Si es mayor que cero checa si existe card.
if len(event_data['payment_method_options']["card"]) > 1: #Si el contenido de card es mayor de uno, si existe y puedes finalmente procesar el pago.
cus_id = event_data['customer']
print("Customer directo:", cus_id)
imagenes = event_data['metadata']['imagenes']
print("Imágenes:")
print(imagenes)
firebase_user = event_data['metadata']['firebase_user']
herramientas.registrar_evento(cus_id, firebase_user, int(imagenes))
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))
else:
print(f'unhandled event: {event_type}')
return {"status": "success"} |