Update routes/subscription.py
Browse files- routes/subscription.py +19 -23
routes/subscription.py
CHANGED
|
@@ -379,10 +379,10 @@ def create_checkout_session(
|
|
| 379 |
if not user_token:
|
| 380 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
| 381 |
|
| 382 |
-
# 🔹 1.
|
| 383 |
user_id = verify_token(user_token)
|
| 384 |
|
| 385 |
-
# 🔹 2.
|
| 386 |
response_stylist = requests.get(
|
| 387 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
| 388 |
headers=SUPABASE_HEADERS
|
|
@@ -392,14 +392,14 @@ def create_checkout_session(
|
|
| 392 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
| 393 |
|
| 394 |
stylist = stylist_data[0]
|
| 395 |
-
stylist_id = stylist.get("id")
|
| 396 |
-
stylist_stripe_id = stylist.get("stripe_id")
|
| 397 |
-
price_id = stylist.get("price_id")
|
| 398 |
|
| 399 |
if not stylist_stripe_id or not price_id:
|
| 400 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
| 401 |
|
| 402 |
-
# 🔹 3.
|
| 403 |
response_user = requests.get(
|
| 404 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
| 405 |
headers=SUPABASE_HEADERS
|
|
@@ -409,22 +409,18 @@ def create_checkout_session(
|
|
| 409 |
raise HTTPException(status_code=404, detail="Client not found")
|
| 410 |
|
| 411 |
user = user_data[0]
|
| 412 |
-
user_stripe_id = user.get("stripe_id")
|
| 413 |
|
| 414 |
if not user_stripe_id:
|
| 415 |
raise HTTPException(status_code=400, detail="Client does not have a Stripe Customer ID")
|
| 416 |
|
| 417 |
-
# 🔹
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
currency='usd', # Defina a moeda
|
| 425 |
-
customer=user_stripe_id, # ID do cliente no Stripe
|
| 426 |
-
description="Pagamento para o estilista", # Descrição do pagamento
|
| 427 |
-
metadata={ # Metadados para personalizar a transação
|
| 428 |
"stylist_id": stylist_id,
|
| 429 |
"stylist_stripe_id": stylist_stripe_id,
|
| 430 |
"user_id": user_id,
|
|
@@ -433,15 +429,15 @@ def create_checkout_session(
|
|
| 433 |
}
|
| 434 |
)
|
| 435 |
|
| 436 |
-
# 🔹 Retornar o clientSecret do PaymentIntent para o frontend
|
| 437 |
return {
|
| 438 |
-
"message": "
|
| 439 |
-
"clientSecret": payment_intent.client_secret
|
|
|
|
| 440 |
}
|
| 441 |
|
| 442 |
except Exception as e:
|
| 443 |
-
logger.error(f"
|
| 444 |
-
raise HTTPException(status_code=500, detail=
|
| 445 |
|
| 446 |
### **CANCELAMENTO DE ASSINATURA**
|
| 447 |
class CancelSubscriptionRequest(BaseModel):
|
|
|
|
| 379 |
if not user_token:
|
| 380 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
| 381 |
|
| 382 |
+
# 🔹 1. Validate client token and get user_id
|
| 383 |
user_id = verify_token(user_token)
|
| 384 |
|
| 385 |
+
# 🔹 2. Get stylist data from Supabase
|
| 386 |
response_stylist = requests.get(
|
| 387 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
| 388 |
headers=SUPABASE_HEADERS
|
|
|
|
| 392 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
| 393 |
|
| 394 |
stylist = stylist_data[0]
|
| 395 |
+
stylist_id = stylist.get("id")
|
| 396 |
+
stylist_stripe_id = stylist.get("stripe_id")
|
| 397 |
+
price_id = stylist.get("price_id")
|
| 398 |
|
| 399 |
if not stylist_stripe_id or not price_id:
|
| 400 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
| 401 |
|
| 402 |
+
# 🔹 3. Get client data from Supabase
|
| 403 |
response_user = requests.get(
|
| 404 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
| 405 |
headers=SUPABASE_HEADERS
|
|
|
|
| 409 |
raise HTTPException(status_code=404, detail="Client not found")
|
| 410 |
|
| 411 |
user = user_data[0]
|
| 412 |
+
user_stripe_id = user.get("stripe_id")
|
| 413 |
|
| 414 |
if not user_stripe_id:
|
| 415 |
raise HTTPException(status_code=400, detail="Client does not have a Stripe Customer ID")
|
| 416 |
|
| 417 |
+
# 🔹 Create a subscription
|
| 418 |
+
subscription = stripe.Subscription.create(
|
| 419 |
+
customer=user_stripe_id,
|
| 420 |
+
items=[{"price": price_id}],
|
| 421 |
+
payment_behavior='default_incomplete',
|
| 422 |
+
expand=['latest_invoice.payment_intent'],
|
| 423 |
+
metadata={
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
"stylist_id": stylist_id,
|
| 425 |
"stylist_stripe_id": stylist_stripe_id,
|
| 426 |
"user_id": user_id,
|
|
|
|
| 429 |
}
|
| 430 |
)
|
| 431 |
|
|
|
|
| 432 |
return {
|
| 433 |
+
"message": "Subscription session created successfully!",
|
| 434 |
+
"clientSecret": subscription.latest_invoice.payment_intent.client_secret,
|
| 435 |
+
"subscriptionId": subscription.id
|
| 436 |
}
|
| 437 |
|
| 438 |
except Exception as e:
|
| 439 |
+
logger.error(f"Error creating subscription session: {e}")
|
| 440 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 441 |
|
| 442 |
### **CANCELAMENTO DE ASSINATURA**
|
| 443 |
class CancelSubscriptionRequest(BaseModel):
|