habulaj commited on
Commit
28e7973
·
verified ·
1 Parent(s): 9cf7236

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +15 -38
routes/subscription.py CHANGED
@@ -65,50 +65,27 @@ def verify_token(user_token: str) -> str:
65
  else:
66
  raise HTTPException(status_code=401, detail="Invalid or expired token")
67
 
68
- @router.get("/subscription/status/{subscription_id}")
69
- async def check_subscription_status(subscription_id: str):
70
  try:
71
- # 🔹 Consulta a Stripe diretamente
72
- stripe_url = f"https://api.stripe.com/v1/subscriptions/{subscription_id}"
73
- headers = {
74
- "Authorization": f"Bearer {stripe.api_key}" # ✅ Usa a chave definida no ambiente
75
- }
76
- response = requests.get(stripe_url, headers=headers)
77
-
78
- if response.status_code != 200:
79
- raise HTTPException(status_code=404, detail="Subscription not found in Stripe")
80
-
81
- subscription = response.json()
82
 
83
- # 🔹 Pega os dados necessários
84
- status = subscription.get("status")
85
- cancel_at_period_end = subscription.get("cancel_at_period_end", False)
86
- canceled_at = subscription.get("canceled_at")
87
- current_period_end = subscription.get("current_period_end")
88
 
89
- # 🔹 Converte timestamps para data legível
90
- ny_tz = pytz.timezone("America/New_York")
 
 
91
 
92
- canceled_at_date = (
93
- datetime.utcfromtimestamp(canceled_at).replace(tzinfo=pytz.utc).astimezone(ny_tz).isoformat()
94
- if canceled_at else None
95
- )
96
-
97
- expiration_date = (
98
- datetime.utcfromtimestamp(current_period_end).replace(tzinfo=pytz.utc).astimezone(ny_tz).isoformat()
99
- if current_period_end else None
100
- )
101
-
102
- return {
103
- "subscription_id": subscription_id,
104
- "status": status,
105
- "cancel_at_period_end": cancel_at_period_end,
106
- "canceled_at": canceled_at_date,
107
- "expiration_date": expiration_date
108
- }
109
 
110
  except Exception as e:
111
- raise HTTPException(status_code=500, detail=str(e))
 
112
 
113
  @router.post("/webhook")
114
  async def stripe_webhook(request: Request):
 
65
  else:
66
  raise HTTPException(status_code=401, detail="Invalid or expired token")
67
 
68
+ @router.post("/generate_dashboard_link")
69
+ async def generate_dashboard_link(data: CheckSubscriptionRequest):
70
  try:
71
+ user_id = data.user_id # O ID do estilista ou cliente
 
 
 
 
 
 
 
 
 
 
72
 
73
+ if user_id.startswith("acct_"):
74
+ # O usuário é um estilista (Stripe Connect)
75
+ dashboard_url = f"https://dashboard.stripe.com/connect/accounts/{user_id}/overview"
76
+ return {"status": "success", "dashboard_link": dashboard_url}
 
77
 
78
+ elif user_id.startswith("cus_"):
79
+ # O usuário é um cliente
80
+ dashboard_url = f"https://billing.stripe.com/p/{user_id}"
81
+ return {"status": "success", "dashboard_link": dashboard_url}
82
 
83
+ else:
84
+ raise HTTPException(status_code=400, detail="Invalid user ID format. Must start with 'acct_' for stylist or 'cus_' for customer.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  except Exception as e:
87
+ logger.error(f"Error generating dashboard link: {str(e)}")
88
+ raise HTTPException(status_code=500, detail="Error generating dashboard link.")
89
 
90
  @router.post("/webhook")
91
  async def stripe_webhook(request: Request):