Update routes/subscription.py
Browse files- routes/subscription.py +79 -1
routes/subscription.py
CHANGED
@@ -32,6 +32,9 @@ SUPABASE_HEADERS = {
|
|
32 |
"Content-Type": "application/json"
|
33 |
}
|
34 |
|
|
|
|
|
|
|
35 |
class UserIDRequest(BaseModel):
|
36 |
user_id: str
|
37 |
|
@@ -466,7 +469,82 @@ async def create_price(
|
|
466 |
except Exception as e:
|
467 |
logger.error(f"❌ Error creating price: {e}")
|
468 |
raise HTTPException(status_code=500, detail=f"Error creating price: {str(e)}")
|
469 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
470 |
@router.post("/create_checkout_session")
|
471 |
def create_checkout_session(
|
472 |
data: SubscriptionRequest,
|
|
|
32 |
"Content-Type": "application/json"
|
33 |
}
|
34 |
|
35 |
+
class EmergencyPaymentRequest(BaseModel):
|
36 |
+
id: str # ID do estilista
|
37 |
+
|
38 |
class UserIDRequest(BaseModel):
|
39 |
user_id: str
|
40 |
|
|
|
469 |
except Exception as e:
|
470 |
logger.error(f"❌ Error creating price: {e}")
|
471 |
raise HTTPException(status_code=500, detail=f"Error creating price: {str(e)}")
|
472 |
+
|
473 |
+
@router.post("/emergency_checkout_session")
|
474 |
+
def emergency_checkout_session(
|
475 |
+
data: EmergencyPaymentRequest,
|
476 |
+
user_token: str = Header(None, alias="User-key")
|
477 |
+
):
|
478 |
+
try:
|
479 |
+
if not user_token:
|
480 |
+
raise HTTPException(status_code=401, detail="Missing User-key header")
|
481 |
+
|
482 |
+
# 🔹 1. Validate client token and get user_id
|
483 |
+
user_id = verify_token(user_token)
|
484 |
+
|
485 |
+
# 🔹 2. Check if user has an active subscription
|
486 |
+
response_subscription = requests.get(
|
487 |
+
f"{SUPABASE_URL}/rest/v1/Subscriptions?customer_id=eq.{user_id}&stylist_id=eq.{data.id}&active=eq.true",
|
488 |
+
headers=SUPABASE_HEADERS
|
489 |
+
)
|
490 |
+
subscription_data = response_subscription.json()
|
491 |
+
if not subscription_data:
|
492 |
+
raise HTTPException(status_code=403, detail="User is not eligible for emergency checkout")
|
493 |
+
|
494 |
+
# 🔹 3. Get stylist data from Supabase
|
495 |
+
response_stylist = requests.get(
|
496 |
+
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
497 |
+
headers=SUPABASE_HEADERS
|
498 |
+
)
|
499 |
+
stylist_data = response_stylist.json()
|
500 |
+
if not stylist_data:
|
501 |
+
raise HTTPException(status_code=404, detail="Stylist not found")
|
502 |
+
|
503 |
+
stylist = stylist_data[0]
|
504 |
+
stylist_id = stylist.get("id")
|
505 |
+
stylist_stripe_id = stylist.get("stripe_id")
|
506 |
+
emergency_price = stylist.get("emergency_price")
|
507 |
+
|
508 |
+
if not stylist_stripe_id or emergency_price is None:
|
509 |
+
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
510 |
+
|
511 |
+
# 🔹 4. Get client data from Supabase
|
512 |
+
response_user = requests.get(
|
513 |
+
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
514 |
+
headers=SUPABASE_HEADERS
|
515 |
+
)
|
516 |
+
user_data = response_user.json()
|
517 |
+
if not user_data:
|
518 |
+
raise HTTPException(status_code=404, detail="Client not found")
|
519 |
+
|
520 |
+
user = user_data[0]
|
521 |
+
user_stripe_id = user.get("stripe_id")
|
522 |
+
|
523 |
+
if not user_stripe_id:
|
524 |
+
raise HTTPException(status_code=400, detail="Client does not have a Stripe Customer ID")
|
525 |
+
|
526 |
+
# 🔹 5. Create a one-time payment intent
|
527 |
+
payment_intent = stripe.PaymentIntent.create(
|
528 |
+
amount=emergency_price,
|
529 |
+
currency="usd",
|
530 |
+
customer=user_stripe_id,
|
531 |
+
application_fee_amount=int(emergency_price * 0.20), # 20% para a plataforma
|
532 |
+
transfer_data={
|
533 |
+
"destination": stylist_stripe_id, # 80% para o estilista
|
534 |
+
},
|
535 |
+
)
|
536 |
+
|
537 |
+
return {
|
538 |
+
"message": "Emergency checkout session created successfully!",
|
539 |
+
"clientSecret": payment_intent.client_secret,
|
540 |
+
"paymentIntentId": payment_intent.id,
|
541 |
+
"price": f"{emergency_price / 100:.2f} USD"
|
542 |
+
}
|
543 |
+
|
544 |
+
except Exception as e:
|
545 |
+
logger.error(f"Error creating emergency checkout session: {e}")
|
546 |
+
raise HTTPException(status_code=500, detail=str(e))
|
547 |
+
|
548 |
@router.post("/create_checkout_session")
|
549 |
def create_checkout_session(
|
550 |
data: SubscriptionRequest,
|