Spaces:
Running
Running
Manejo correcto de ambientes
Browse files- app.py +2 -1
- autenticacion.py +5 -2
- funciones.py +1 -0
- ga4Analiticas.py +72 -0
- globales.py +1 -1
- paga_oxxo.py +2 -2
app.py
CHANGED
|
@@ -12,7 +12,8 @@ origins = [
|
|
| 12 |
"http://localhost",
|
| 13 |
"http://localhost:8000", # Si usas algún puerto específico para tu frontend
|
| 14 |
"http://127.0.0.1:5500", # Puerto común de Live Server
|
| 15 |
-
"https://
|
|
|
|
| 16 |
#"*" # ¡CUIDADO! '*' permite CUALQUIER origen. Úsalo solo para desarrollo o si sabes lo que haces.
|
| 17 |
# Es más seguro especificar orígenes concretos en producción.
|
| 18 |
]
|
|
|
|
| 12 |
"http://localhost",
|
| 13 |
"http://localhost:8000", # Si usas algún puerto específico para tu frontend
|
| 14 |
"http://127.0.0.1:5500", # Puerto común de Live Server
|
| 15 |
+
"https://targetvox.com"
|
| 16 |
+
"https://splashmix.ink", # Si despliegas tu frontend, añade su dominio aquí
|
| 17 |
#"*" # ¡CUIDADO! '*' permite CUALQUIER origen. Úsalo solo para desarrollo o si sabes lo que haces.
|
| 18 |
# Es más seguro especificar orígenes concretos en producción.
|
| 19 |
]
|
autenticacion.py
CHANGED
|
@@ -7,17 +7,20 @@ def defineAmbiente(env):
|
|
| 7 |
import bridges
|
| 8 |
if env == 'prod':
|
| 9 |
llave = bridges.llave_prod
|
| 10 |
-
else:
|
| 11 |
llave = bridges.llave_sandbox
|
|
|
|
| 12 |
|
| 13 |
else:
|
| 14 |
print("Entorno remoto listo...")
|
| 15 |
if os.getenv("ambiente_stripe") == 'prod':
|
| 16 |
llave = os.getenv("llave_prod")
|
|
|
|
| 17 |
else:
|
| 18 |
llave = os.getenv("llave_sandbox")
|
|
|
|
| 19 |
|
| 20 |
-
return llave
|
| 21 |
|
| 22 |
def local_check():
|
| 23 |
hostname = socket.gethostname()
|
|
|
|
| 7 |
import bridges
|
| 8 |
if env == 'prod':
|
| 9 |
llave = bridges.llave_prod
|
| 10 |
+
else: #if dev
|
| 11 |
llave = bridges.llave_sandbox
|
| 12 |
+
ga4Key = bridges.GA4_API_SECRET_TARGET
|
| 13 |
|
| 14 |
else:
|
| 15 |
print("Entorno remoto listo...")
|
| 16 |
if os.getenv("ambiente_stripe") == 'prod':
|
| 17 |
llave = os.getenv("llave_prod")
|
| 18 |
+
ga4Key = os.getenv("GA4_SECRET_PROD")
|
| 19 |
else:
|
| 20 |
llave = os.getenv("llave_sandbox")
|
| 21 |
+
ga4Key = os.getenv("GA4_SECRET_DEV")
|
| 22 |
|
| 23 |
+
return llave, ga4Key
|
| 24 |
|
| 25 |
def local_check():
|
| 26 |
hostname = socket.gethostname()
|
funciones.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import stripe
|
| 2 |
import globales
|
|
|
|
| 3 |
|
| 4 |
stripe.api_key = globales.llave
|
| 5 |
|
|
|
|
| 1 |
import stripe
|
| 2 |
import globales
|
| 3 |
+
import ga4Analiticas
|
| 4 |
|
| 5 |
stripe.api_key = globales.llave
|
| 6 |
|
ga4Analiticas.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import globales
|
| 5 |
+
|
| 6 |
+
GA4_MEASUREMENT_ID = os.getenv("GA4_MEASUREMENT_ID")
|
| 7 |
+
|
| 8 |
+
def send_ga4_purchase_event(session):
|
| 9 |
+
"""
|
| 10 |
+
Función para enviar un evento de compra a GA4 usando Measurement Protocol.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
event_data: Un diccionario con los datos del evento de compra de Stripe.
|
| 14 |
+
"""
|
| 15 |
+
url = f"https://www.google-analytics.com/mp/collect?measurement_id={GA4_MEASUREMENT_ID}&api_secret={globales.ga4Key}"
|
| 16 |
+
|
| 17 |
+
moneda = session['currency'].upper()
|
| 18 |
+
imagenes = session['metadata']['imagenes']
|
| 19 |
+
print(f"Imágenes es: {imagenes} y es del tipo {type(imagenes)}...")
|
| 20 |
+
valor = session['amount_total'] / 100.0
|
| 21 |
+
print(f"Valor es {valor} y es del tipo {type(valor)}...")
|
| 22 |
+
cxm = int(valor) / int(imagenes) #costo por imagen
|
| 23 |
+
print(f"cxm es {cxm} y su tipo es {type(cxm)}...")
|
| 24 |
+
|
| 25 |
+
id_imagenes = str(imagenes) + 'i'
|
| 26 |
+
id_valor = str(int(valor)) + moneda
|
| 27 |
+
id_cxm = str(cxm) + moneda
|
| 28 |
+
#print(f"id_cxm es : {id_cxm} y su tipo es {type(id_cxm)}...")
|
| 29 |
+
|
| 30 |
+
item_id = id_imagenes + id_valor
|
| 31 |
+
item_name = id_cxm
|
| 32 |
+
|
| 33 |
+
# Prepara los items (productos) para GA4
|
| 34 |
+
items = []
|
| 35 |
+
# Nota: Tendrías que obtener los detalles de los productos
|
| 36 |
+
# de Stripe de manera más robusta, por ejemplo, de tu base de datos
|
| 37 |
+
# o de los metadatos de la sesión si los guardaste allí.
|
| 38 |
+
# Por simplicidad, aquí se usa un ejemplo estático.
|
| 39 |
+
|
| 40 |
+
# Simulación de obtención de items
|
| 41 |
+
# En un caso real, esto sería más complejo.
|
| 42 |
+
items.append({
|
| 43 |
+
"item_id": item_id, #1000i-1900mxn
|
| 44 |
+
"item_name": item_name, #1.8mxn
|
| 45 |
+
"price": int(valor),
|
| 46 |
+
"quantity": 1
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
payload = {
|
| 50 |
+
"client_id": session['metadata']['gaCliente'], # Aquí deberías usar el Client ID o User ID del usuario
|
| 51 |
+
"events": [
|
| 52 |
+
{
|
| 53 |
+
"name": "purchase",
|
| 54 |
+
"params": {
|
| 55 |
+
"debug_mode": True,
|
| 56 |
+
"transaction_id": session['id'],
|
| 57 |
+
"value": valor, # Convierte de céntimos a la unidad de moneda
|
| 58 |
+
"currency": moneda,
|
| 59 |
+
"items": items
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
]
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
response = requests.post(url, data=json.dumps(payload), headers={"Content-Type": "application/json"})
|
| 67 |
+
response.raise_for_status() # Lanza una excepción si la respuesta no es 2xx
|
| 68 |
+
print("Evento de compra enviado a GA4 con éxito.")
|
| 69 |
+
return True
|
| 70 |
+
except requests.exceptions.RequestException as e:
|
| 71 |
+
print(f"Error al enviar el evento a GA4: {e}")
|
| 72 |
+
return False
|
globales.py
CHANGED
|
@@ -2,4 +2,4 @@ import autenticacion
|
|
| 2 |
|
| 3 |
env = "dev" #o dev (Define si es sandbox o producción en Stripe.)
|
| 4 |
#Importante: Solo sirve en local, si estás en el server de HF, ahí tiene su propia variable.
|
| 5 |
-
llave = autenticacion.defineAmbiente(env)
|
|
|
|
| 2 |
|
| 3 |
env = "dev" #o dev (Define si es sandbox o producción en Stripe.)
|
| 4 |
#Importante: Solo sirve en local, si estás en el server de HF, ahí tiene su propia variable.
|
| 5 |
+
llave, ga4Key = autenticacion.defineAmbiente(env)
|
paga_oxxo.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import stripe
|
| 2 |
import globales
|
| 3 |
|
| 4 |
-
print("Haciendo pago en Oxxo...")
|
| 5 |
|
| 6 |
stripe.api_key = globales.llave
|
| 7 |
|
|
@@ -10,7 +10,7 @@ stripe.api_key = globales.llave
|
|
| 10 |
|
| 11 |
# Si necesitas recuperar el PaymentIntent asociado a la sesión
|
| 12 |
payment_intent = stripe.PaymentIntent.retrieve('pi_3RupzSROVpWRmEfB1wEZghFL')
|
| 13 |
-
print("payment_intent obtenido...")
|
| 14 |
|
| 15 |
# Comprobar si hay alguna acción necesaria
|
| 16 |
if hasattr(payment_intent, 'next_action') and payment_intent.next_action:
|
|
|
|
| 1 |
import stripe
|
| 2 |
import globales
|
| 3 |
|
| 4 |
+
#print("Haciendo pago en Oxxo...")
|
| 5 |
|
| 6 |
stripe.api_key = globales.llave
|
| 7 |
|
|
|
|
| 10 |
|
| 11 |
# Si necesitas recuperar el PaymentIntent asociado a la sesión
|
| 12 |
payment_intent = stripe.PaymentIntent.retrieve('pi_3RupzSROVpWRmEfB1wEZghFL')
|
| 13 |
+
#print("payment_intent obtenido...")
|
| 14 |
|
| 15 |
# Comprobar si hay alguna acción necesaria
|
| 16 |
if hasattr(payment_intent, 'next_action') and payment_intent.next_action:
|