Update routes/subscription.py
Browse files- routes/subscription.py +27 -12
routes/subscription.py
CHANGED
@@ -131,9 +131,26 @@ async def create_price(
|
|
131 |
raise HTTPException(status_code=500, detail="Error creating/updating price.")
|
132 |
|
133 |
@router.post("/create_checkout_session")
|
134 |
-
def create_checkout_session(
|
|
|
|
|
|
|
135 |
try:
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
response = requests.get(
|
138 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
139 |
headers=SUPABASE_HEADERS
|
@@ -144,17 +161,15 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
144 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
145 |
|
146 |
stylist = stylist_data[0]
|
147 |
-
|
148 |
-
|
149 |
-
consultations = stylist["consultations"]
|
150 |
-
stylist_stripe_id = stylist["stripe_id"]
|
151 |
|
152 |
if not consultations or not stylist_stripe_id:
|
153 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
154 |
|
155 |
-
# 🔹
|
156 |
response_user = requests.get(
|
157 |
-
f"{SUPABASE_URL}/rest/v1/User?id=eq.{
|
158 |
headers=SUPABASE_HEADERS
|
159 |
)
|
160 |
|
@@ -164,14 +179,14 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
164 |
|
165 |
user = user_data[0]
|
166 |
user_stripe_id = user.get("stripe_id")
|
167 |
-
price_id = user.get("price_id")
|
168 |
|
169 |
if not user_stripe_id:
|
170 |
raise HTTPException(status_code=400, detail="User does not have a Stripe ID")
|
171 |
if not price_id:
|
172 |
raise HTTPException(status_code=400, detail="User does not have a valid price ID")
|
173 |
|
174 |
-
# 🔹
|
175 |
session = stripe.checkout.Session.create(
|
176 |
success_url="https://yourdomain.com/success",
|
177 |
cancel_url="https://yourdomain.com/cancel",
|
@@ -182,12 +197,12 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
182 |
{
|
183 |
"price": price_id,
|
184 |
"quantity": 1,
|
185 |
-
"description": "Assinatura personalizada para usuário"
|
186 |
}
|
187 |
],
|
188 |
metadata={
|
189 |
"stylist_id": stylist_stripe_id,
|
190 |
-
"user_id":
|
191 |
"consultations_per_month": consultations
|
192 |
}
|
193 |
)
|
|
|
131 |
raise HTTPException(status_code=500, detail="Error creating/updating price.")
|
132 |
|
133 |
@router.post("/create_checkout_session")
|
134 |
+
def create_checkout_session(
|
135 |
+
data: SubscriptionRequest,
|
136 |
+
user_token: str = Header(None, alias="User-key")
|
137 |
+
):
|
138 |
try:
|
139 |
+
if not user_token:
|
140 |
+
raise HTTPException(status_code=401, detail="Missing User-key header")
|
141 |
+
|
142 |
+
# 🔹 1. Decodificar o token JWT para obter o ID do usuário
|
143 |
+
try:
|
144 |
+
payload = jwt.decode(user_token, JWT_SECRET, algorithms=["HS256"]) # Substitua JWT_SECRET pela sua chave real
|
145 |
+
user_id = payload.get("id")
|
146 |
+
if not user_id:
|
147 |
+
raise HTTPException(status_code=400, detail="Invalid token: User ID not found")
|
148 |
+
except jwt.ExpiredSignatureError:
|
149 |
+
raise HTTPException(status_code=401, detail="Token expired")
|
150 |
+
except jwt.InvalidTokenError:
|
151 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
152 |
+
|
153 |
+
# 🔹 2. Buscar estilista no Supabase
|
154 |
response = requests.get(
|
155 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
156 |
headers=SUPABASE_HEADERS
|
|
|
161 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
162 |
|
163 |
stylist = stylist_data[0]
|
164 |
+
stylist_stripe_id = stylist.get("stripe_id")
|
165 |
+
consultations = stylist.get("consultations")
|
|
|
|
|
166 |
|
167 |
if not consultations or not stylist_stripe_id:
|
168 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
169 |
|
170 |
+
# 🔹 3. Buscar o stripe_id e price_id do usuário autenticado
|
171 |
response_user = requests.get(
|
172 |
+
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
173 |
headers=SUPABASE_HEADERS
|
174 |
)
|
175 |
|
|
|
179 |
|
180 |
user = user_data[0]
|
181 |
user_stripe_id = user.get("stripe_id")
|
182 |
+
price_id = user.get("price_id")
|
183 |
|
184 |
if not user_stripe_id:
|
185 |
raise HTTPException(status_code=400, detail="User does not have a Stripe ID")
|
186 |
if not price_id:
|
187 |
raise HTTPException(status_code=400, detail="User does not have a valid price ID")
|
188 |
|
189 |
+
# 🔹 4. Criar Checkout Session no Stripe
|
190 |
session = stripe.checkout.Session.create(
|
191 |
success_url="https://yourdomain.com/success",
|
192 |
cancel_url="https://yourdomain.com/cancel",
|
|
|
197 |
{
|
198 |
"price": price_id,
|
199 |
"quantity": 1,
|
200 |
+
"description": "Assinatura personalizada para usuário"
|
201 |
}
|
202 |
],
|
203 |
metadata={
|
204 |
"stylist_id": stylist_stripe_id,
|
205 |
+
"user_id": user_id, # 🔹 Continua no metadata para rastreamento interno
|
206 |
"consultations_per_month": consultations
|
207 |
}
|
208 |
)
|