Update routes/subscription.py
Browse files- routes/subscription.py +22 -36
routes/subscription.py
CHANGED
@@ -198,79 +198,65 @@ def create_checkout_session(
|
|
198 |
# 🔹 1. Validar o token do cliente e obter user_id (cliente)
|
199 |
user_id = verify_token(user_token)
|
200 |
|
201 |
-
# 🔹 2. Buscar dados do estilista
|
202 |
response_stylist = requests.get(
|
203 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
204 |
headers=SUPABASE_HEADERS
|
205 |
)
|
206 |
-
|
207 |
stylist_data = response_stylist.json()
|
208 |
if not stylist_data:
|
209 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
210 |
|
211 |
stylist = stylist_data[0]
|
212 |
-
|
213 |
-
|
|
|
214 |
|
215 |
-
# 🔹 3. Verificar dados do estilista
|
216 |
-
logger.info(f"📌 Stylist Stripe ID: {stylist_stripe_id}")
|
217 |
-
logger.info(f"📌 Stylist Price ID: {price_id}")
|
218 |
if not stylist_stripe_id or not price_id:
|
219 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
220 |
|
221 |
-
# 🔹
|
222 |
response_user = requests.get(
|
223 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
224 |
headers=SUPABASE_HEADERS
|
225 |
)
|
226 |
-
|
227 |
user_data = response_user.json()
|
228 |
-
|
229 |
-
# Verificando dados do cliente
|
230 |
-
logger.info(f"📌 Client Data from Supabase: {user_data}")
|
231 |
-
|
232 |
if not user_data:
|
233 |
raise HTTPException(status_code=404, detail="Client not found")
|
234 |
|
235 |
user = user_data[0]
|
236 |
-
|
237 |
-
|
238 |
-
# 🔹 5. Logar os stripe_ids para debugging
|
239 |
-
logger.info(f"📌 Customer Stripe ID: {customer_stripe_id}")
|
240 |
|
241 |
-
if not
|
242 |
raise HTTPException(status_code=400, detail="Client does not have a Stripe Customer ID")
|
243 |
|
244 |
-
# 🔹
|
245 |
session = stripe.checkout.Session.create(
|
246 |
success_url="https://yourdomain.com/success",
|
247 |
cancel_url="https://yourdomain.com/cancel",
|
248 |
payment_method_types=["card"],
|
249 |
mode="subscription",
|
250 |
-
customer=
|
251 |
-
line_items=[
|
252 |
-
{
|
253 |
-
"price": price_id,
|
254 |
-
"quantity": 1
|
255 |
-
}
|
256 |
-
],
|
257 |
metadata={
|
258 |
-
"stylist_id":
|
259 |
-
"
|
|
|
|
|
260 |
},
|
261 |
-
subscription_data={
|
262 |
"metadata": {
|
263 |
-
"stylist_id":
|
264 |
-
"
|
|
|
|
|
265 |
}
|
266 |
}
|
267 |
-
)
|
|
|
268 |
logger.info(f"📌 Checkout session created successfully: {session.url}")
|
269 |
|
270 |
-
return {
|
271 |
-
"message": "Checkout session created successfully!",
|
272 |
-
"checkout_url": session.url
|
273 |
-
}
|
274 |
|
275 |
except Exception as e:
|
276 |
logger.error(f"Error creating checkout session: {e}")
|
|
|
198 |
# 🔹 1. Validar o token do cliente e obter user_id (cliente)
|
199 |
user_id = verify_token(user_token)
|
200 |
|
201 |
+
# 🔹 2. Buscar dados do estilista no Supabase
|
202 |
response_stylist = requests.get(
|
203 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
204 |
headers=SUPABASE_HEADERS
|
205 |
)
|
|
|
206 |
stylist_data = response_stylist.json()
|
207 |
if not stylist_data:
|
208 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
209 |
|
210 |
stylist = stylist_data[0]
|
211 |
+
stylist_id = stylist.get("id") # ID do estilista no Supabase
|
212 |
+
stylist_stripe_id = stylist.get("stripe_id") # ID do estilista no Stripe
|
213 |
+
price_id = stylist.get("price_id") # ID do preço no Stripe
|
214 |
|
|
|
|
|
|
|
215 |
if not stylist_stripe_id or not price_id:
|
216 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
217 |
|
218 |
+
# 🔹 3. Buscar dados do cliente no Supabase
|
219 |
response_user = requests.get(
|
220 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
221 |
headers=SUPABASE_HEADERS
|
222 |
)
|
|
|
223 |
user_data = response_user.json()
|
|
|
|
|
|
|
|
|
224 |
if not user_data:
|
225 |
raise HTTPException(status_code=404, detail="Client not found")
|
226 |
|
227 |
user = user_data[0]
|
228 |
+
user_stripe_id = user.get("stripe_id") # ID do cliente no Stripe
|
|
|
|
|
|
|
229 |
|
230 |
+
if not user_stripe_id:
|
231 |
raise HTTPException(status_code=400, detail="Client does not have a Stripe Customer ID")
|
232 |
|
233 |
+
# 🔹 4. Criar Checkout Session no Stripe com os metadados atualizados
|
234 |
session = stripe.checkout.Session.create(
|
235 |
success_url="https://yourdomain.com/success",
|
236 |
cancel_url="https://yourdomain.com/cancel",
|
237 |
payment_method_types=["card"],
|
238 |
mode="subscription",
|
239 |
+
customer=user_stripe_id,
|
240 |
+
line_items=[{"price": price_id, "quantity": 1}],
|
|
|
|
|
|
|
|
|
|
|
241 |
metadata={
|
242 |
+
"stylist_id": stylist_id,
|
243 |
+
"stylist_stripe_id": stylist_stripe_id,
|
244 |
+
"user_id": user_id,
|
245 |
+
"user_stripe_id": user_stripe_id
|
246 |
},
|
247 |
+
subscription_data={
|
248 |
"metadata": {
|
249 |
+
"stylist_id": stylist_id,
|
250 |
+
"stylist_stripe_id": stylist_stripe_id,
|
251 |
+
"user_id": user_id,
|
252 |
+
"user_stripe_id": user_stripe_id
|
253 |
}
|
254 |
}
|
255 |
+
)
|
256 |
+
|
257 |
logger.info(f"📌 Checkout session created successfully: {session.url}")
|
258 |
|
259 |
+
return {"message": "Checkout session created successfully!", "checkout_url": session.url}
|
|
|
|
|
|
|
260 |
|
261 |
except Exception as e:
|
262 |
logger.error(f"Error creating checkout session: {e}")
|