Spaces:
Sleeping
Sleeping
Welcome funciones standard
Browse files- funciones.py +83 -0
- globales.py +1 -4
- main.py +4 -81
funciones.py
CHANGED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import stripe
|
| 2 |
+
import globales
|
| 3 |
+
import herramientas
|
| 4 |
+
import ga4Analiticas
|
| 5 |
+
from fastapi import Request, Header
|
| 6 |
+
|
| 7 |
+
async def procesa_evento(request: Request, stripe_signature: str = Header(None)):
|
| 8 |
+
|
| 9 |
+
webhook_secret = globales.webhook
|
| 10 |
+
data = await request.body()
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
event = stripe.Webhook.construct_event(
|
| 14 |
+
payload=data,
|
| 15 |
+
sig_header=stripe_signature,
|
| 16 |
+
secret=webhook_secret
|
| 17 |
+
)
|
| 18 |
+
# print("Evento construido...")
|
| 19 |
+
# print(event)
|
| 20 |
+
# print("Evento impreso")
|
| 21 |
+
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print("Excepción es: ", e)
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
event_data = event['data']['object']
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print("Excepción-183 es: ", e)
|
| 29 |
+
|
| 30 |
+
event_type = event['type']
|
| 31 |
+
print("Voy a imprimir el event type:")
|
| 32 |
+
print(event_type)
|
| 33 |
+
print("Sucedido a las: ", herramientas.imprimeTime())
|
| 34 |
+
gaCliente = event_data['metadata'].get('gaCliente', None)
|
| 35 |
+
|
| 36 |
+
if event_type == 'checkout.session.completed':
|
| 37 |
+
print("Checkout Session completado.")
|
| 38 |
+
print("Status:")
|
| 39 |
+
print(event_data['status'])
|
| 40 |
+
print("Payment Method Options:")
|
| 41 |
+
print(event_data['payment_method_options'])
|
| 42 |
+
print("Cantidad de elementos:")
|
| 43 |
+
print(len(event_data['payment_method_options']))
|
| 44 |
+
|
| 45 |
+
#Importante: Todas las acciones relacionadas a la compra con tarjeta deben ir en el IF.
|
| 46 |
+
#De lo contrario se marcará también en OXXO, antes de hacerce el pago.
|
| 47 |
+
if len(event_data['payment_method_options']) > 0: #Si es mayor que cero checa si existe card.
|
| 48 |
+
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.
|
| 49 |
+
cus_id = event_data['customer']
|
| 50 |
+
print("Customer directo:", cus_id)
|
| 51 |
+
imagenes = event_data['metadata']['imagenes']
|
| 52 |
+
print("Imágenes:")
|
| 53 |
+
print(imagenes)
|
| 54 |
+
firebase_user = event_data['metadata']['firebase_user']
|
| 55 |
+
herramientas.registrar_evento(cus_id, firebase_user, int(imagenes))
|
| 56 |
+
#Analytics se hace al final después de registrar la venta.
|
| 57 |
+
print("gacliente:", gaCliente)
|
| 58 |
+
print("Sending to GA4...")
|
| 59 |
+
ga4Analiticas.send_ga4_purchase_event(event_data)
|
| 60 |
+
|
| 61 |
+
if event_type == 'checkout.session.async_payment_succeeded':
|
| 62 |
+
print("Pago asíncrono completado.")
|
| 63 |
+
print("Checkout Session completado.")
|
| 64 |
+
print("Status:")
|
| 65 |
+
print(event_data['status'])
|
| 66 |
+
print("Payment Method Options:")
|
| 67 |
+
print(event_data['payment_method_options'])
|
| 68 |
+
print("Cantidad de elementos:")
|
| 69 |
+
print(len(event_data['payment_method_options']))
|
| 70 |
+
cus_id = event_data['customer']
|
| 71 |
+
print("Customer directo:", cus_id)
|
| 72 |
+
imagenes = event_data['metadata']['imagenes']
|
| 73 |
+
print("Imágenes:")
|
| 74 |
+
print(imagenes)
|
| 75 |
+
herramientas.registrar_evento(cus_id, int(imagenes))
|
| 76 |
+
print("gacliente:", gaCliente)
|
| 77 |
+
print("Sending to GA4...")
|
| 78 |
+
ga4Analiticas.send_ga4_purchase_event(event_data)
|
| 79 |
+
|
| 80 |
+
else:
|
| 81 |
+
print(f'unhandled event: {event_type}')
|
| 82 |
+
|
| 83 |
+
return {"status": "success"}
|
globales.py
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
import autenticacion
|
| 2 |
|
| 3 |
env = 'dev' #o dev
|
| 4 |
-
llave, webhook, ga4Key = autenticacion.defineAmbiente(env)
|
| 5 |
-
|
| 6 |
-
# print("La llave es: ", llave)
|
| 7 |
-
# print("Y el webhook es: ", webhook)
|
|
|
|
| 1 |
import autenticacion
|
| 2 |
|
| 3 |
env = 'dev' #o dev
|
| 4 |
+
llave, webhook, ga4Key = autenticacion.defineAmbiente(env)
|
|
|
|
|
|
|
|
|
main.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
-
import stripe
|
| 2 |
import globales
|
| 3 |
-
import
|
| 4 |
-
import ga4Analiticas
|
| 5 |
from fastapi import FastAPI, Request, Header
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
@@ -13,83 +11,8 @@ def start():
|
|
| 13 |
return {f"Status":"Deployed"}
|
| 14 |
|
| 15 |
@app.post("/webhook")
|
| 16 |
-
async def webhook_received(request: Request, stripe_signature: str = Header(None)):
|
| 17 |
-
|
| 18 |
-
webhook_secret = globales.webhook
|
| 19 |
-
|
| 20 |
-
data = await request.body()
|
| 21 |
-
|
| 22 |
-
print("Construyendo el evento:")
|
| 23 |
-
|
| 24 |
-
try:
|
| 25 |
-
event = stripe.Webhook.construct_event(
|
| 26 |
-
payload=data,
|
| 27 |
-
sig_header=stripe_signature,
|
| 28 |
-
secret=webhook_secret
|
| 29 |
-
)
|
| 30 |
-
# print("Evento construido...")
|
| 31 |
-
# print(event)
|
| 32 |
-
# print("Evento impreso")
|
| 33 |
-
|
| 34 |
-
except Exception as e:
|
| 35 |
-
print("Excepción es: ", e)
|
| 36 |
-
|
| 37 |
-
try:
|
| 38 |
-
event_data = event['data']['object']
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print("Excepción-183 es: ", e)
|
| 41 |
-
|
| 42 |
-
event_type = event['type']
|
| 43 |
-
print("Voy a imprimir el event type:")
|
| 44 |
-
print(event_type)
|
| 45 |
-
print("Sucedido a las: ", herramientas.imprimeTime())
|
| 46 |
-
gaCliente = event_data['metadata'].get('gaCliente', None)
|
| 47 |
-
|
| 48 |
-
if event_type == 'checkout.session.completed':
|
| 49 |
-
print("Checkout Session completado.")
|
| 50 |
-
print("Status:")
|
| 51 |
-
print(event_data['status'])
|
| 52 |
-
print("Payment Method Options:")
|
| 53 |
-
print(event_data['payment_method_options'])
|
| 54 |
-
print("Cantidad de elementos:")
|
| 55 |
-
print(len(event_data['payment_method_options']))
|
| 56 |
-
|
| 57 |
-
#Importante: Todas las acciones relacionadas a la compra con tarjeta deben ir en el IF.
|
| 58 |
-
#De lo contrario se marcará también en OXXO, antes de hacerce el pago.
|
| 59 |
-
if len(event_data['payment_method_options']) > 0: #Si es mayor que cero checa si existe card.
|
| 60 |
-
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.
|
| 61 |
-
cus_id = event_data['customer']
|
| 62 |
-
print("Customer directo:", cus_id)
|
| 63 |
-
imagenes = event_data['metadata']['imagenes']
|
| 64 |
-
print("Imágenes:")
|
| 65 |
-
print(imagenes)
|
| 66 |
-
firebase_user = event_data['metadata']['firebase_user']
|
| 67 |
-
herramientas.registrar_evento(cus_id, firebase_user, int(imagenes))
|
| 68 |
-
#Analytics se hace al final después de registrar la venta.
|
| 69 |
-
print("gacliente:", gaCliente)
|
| 70 |
-
print("Sending to GA4...")
|
| 71 |
-
ga4Analiticas.send_ga4_purchase_event(event_data)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
print("Pago asíncrono completado.")
|
| 75 |
-
print("Checkout Session completado.")
|
| 76 |
-
print("Status:")
|
| 77 |
-
print(event_data['status'])
|
| 78 |
-
print("Payment Method Options:")
|
| 79 |
-
print(event_data['payment_method_options'])
|
| 80 |
-
print("Cantidad de elementos:")
|
| 81 |
-
print(len(event_data['payment_method_options']))
|
| 82 |
-
cus_id = event_data['customer']
|
| 83 |
-
print("Customer directo:", cus_id)
|
| 84 |
-
imagenes = event_data['metadata']['imagenes']
|
| 85 |
-
print("Imágenes:")
|
| 86 |
-
print(imagenes)
|
| 87 |
-
herramientas.registrar_evento(cus_id, int(imagenes))
|
| 88 |
-
print("gacliente:", gaCliente)
|
| 89 |
-
print("Sending to GA4...")
|
| 90 |
-
ga4Analiticas.send_ga4_purchase_event(event_data)
|
| 91 |
-
|
| 92 |
-
else:
|
| 93 |
-
print(f'unhandled event: {event_type}')
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
| 1 |
import globales
|
| 2 |
+
import funciones
|
|
|
|
| 3 |
from fastapi import FastAPI, Request, Header
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
|
|
| 11 |
return {f"Status":"Deployed"}
|
| 12 |
|
| 13 |
@app.post("/webhook")
|
| 14 |
+
async def webhook_received(request: Request, stripe_signature: str = Header(None)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
return funciones.procesa_evento(request, stripe_signature)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
|