Spaces:
Sleeping
Sleeping
Nuevos Parámetros incluídos en createSession
Browse files- app.py +23 -8
- funciones.py +3 -3
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import funciones
|
| 2 |
-
from
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
-
from typing import Optional
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 6 |
|
| 7 |
#FUTURE: Quizá en el futuro cambiarla de Form a Json con Pydantic.
|
| 8 |
-
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
# Configuración de CORS
|
|
@@ -101,13 +100,29 @@ async def creaCliente(
|
|
| 101 |
@app.post("/creaLinkSesion/")
|
| 102 |
async def creaLinkSesion(
|
| 103 |
price_id: str = Form(...),
|
|
|
|
|
|
|
| 104 |
customer_email: Optional[str] = Form(None),
|
| 105 |
customer_id: Optional[str] = Form(None),
|
| 106 |
-
firebase_user: Optional[str] = Form(None)
|
| 107 |
):
|
| 108 |
"""
|
| 109 |
-
Crea una Checkout Session
|
| 110 |
-
|
| 111 |
"""
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import funciones
|
| 2 |
+
from typing import Optional, Literal
|
| 3 |
from fastapi.responses import JSONResponse
|
|
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi import FastAPI, Form, HTTPException, status
|
| 6 |
|
| 7 |
#FUTURE: Quizá en el futuro cambiarla de Form a Json con Pydantic.
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
# Configuración de CORS
|
|
|
|
| 100 |
@app.post("/creaLinkSesion/")
|
| 101 |
async def creaLinkSesion(
|
| 102 |
price_id: str = Form(...),
|
| 103 |
+
unidades: int = Form(...), # <--- ¡Nuevo parámetro 'unidades' como int!
|
| 104 |
+
mode: Literal["payment", "subscription"] = Form(...), # <--- ¡Nuevo parámetro 'mode' con validación!
|
| 105 |
customer_email: Optional[str] = Form(None),
|
| 106 |
customer_id: Optional[str] = Form(None),
|
| 107 |
+
firebase_user: Optional[str] = Form(None)
|
| 108 |
):
|
| 109 |
"""
|
| 110 |
+
Crea una Checkout Session en Stripe.
|
| 111 |
+
Acepta 'unidades' (cantidad) y 'mode' ('payment' o 'subscription').
|
| 112 |
"""
|
| 113 |
+
try:
|
| 114 |
+
# Llama a la función de Stripe con todos los parámetros, incluyendo los nuevos
|
| 115 |
+
return funciones.create_checkout_session(
|
| 116 |
+
price_id,
|
| 117 |
+
customer_email,
|
| 118 |
+
customer_id,
|
| 119 |
+
firebase_user,
|
| 120 |
+
unidades, # Pasar el nuevo parámetro
|
| 121 |
+
mode # Pasar el nuevo parámetro
|
| 122 |
+
)
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"Error al crear la sesión de checkout: {e}")
|
| 125 |
+
raise HTTPException(
|
| 126 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 127 |
+
detail=f"Error interno al intentar crear la sesión de checkout: {e}"
|
| 128 |
+
)
|
funciones.py
CHANGED
|
@@ -3,7 +3,7 @@ import globales
|
|
| 3 |
|
| 4 |
stripe.api_key = globales.llave
|
| 5 |
|
| 6 |
-
def create_checkout_session(price_id, customer_email=None, customer_id=None, firebase_user=None):
|
| 7 |
"""
|
| 8 |
Crea una nueva Checkout Session para un pago único.
|
| 9 |
Busca o crea un cliente en Stripe si no se provee customer_id ni customer_email,
|
|
@@ -88,13 +88,13 @@ def create_checkout_session(price_id, customer_email=None, customer_id=None, fir
|
|
| 88 |
'price': price_id,
|
| 89 |
'quantity': 1, #maneja cantidades cuando es subscription
|
| 90 |
}],
|
| 91 |
-
'mode':
|
| 92 |
'success_url': 'https://app.splashmix.ink/',
|
| 93 |
'cancel_url': 'https://app.splashmix.ink/',
|
| 94 |
'locale': 'auto',
|
| 95 |
'client_reference_id': 'HERC',
|
| 96 |
'metadata': {
|
| 97 |
-
'imagenes':
|
| 98 |
} # Inicializamos metadata para añadir el customer_id y firebase_user si existen
|
| 99 |
}
|
| 100 |
|
|
|
|
| 3 |
|
| 4 |
stripe.api_key = globales.llave
|
| 5 |
|
| 6 |
+
def create_checkout_session(price_id, customer_email=None, customer_id=None, firebase_user=None, unidades=None, mode=None):
|
| 7 |
"""
|
| 8 |
Crea una nueva Checkout Session para un pago único.
|
| 9 |
Busca o crea un cliente en Stripe si no se provee customer_id ni customer_email,
|
|
|
|
| 88 |
'price': price_id,
|
| 89 |
'quantity': 1, #maneja cantidades cuando es subscription
|
| 90 |
}],
|
| 91 |
+
'mode': mode, #payment o subscription
|
| 92 |
'success_url': 'https://app.splashmix.ink/',
|
| 93 |
'cancel_url': 'https://app.splashmix.ink/',
|
| 94 |
'locale': 'auto',
|
| 95 |
'client_reference_id': 'HERC',
|
| 96 |
'metadata': {
|
| 97 |
+
'imagenes': unidades
|
| 98 |
} # Inicializamos metadata para añadir el customer_id y firebase_user si existen
|
| 99 |
}
|
| 100 |
|