habulaj commited on
Commit
290d164
·
verified ·
1 Parent(s): 4150aeb

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +16 -5
routes/subscription.py CHANGED
@@ -87,13 +87,24 @@ async def generate_dashboard_link(data: UserIDRequest):
87
  logger.error(f"Error creating login link for stylist account: {str(e)}")
88
  raise HTTPException(status_code=500, detail="Error creating login link for stylist account.")
89
 
90
- # Caso seja um cliente, não podemos criar um login link
91
  elif user_id.startswith("cus_"):
92
- return {
93
- "status": "success",
94
- "dashboard_link": f"https://billing.stripe.com/p/{user_id}"
95
- }
 
 
 
 
 
 
 
96
 
 
 
 
 
97
  else:
98
  raise HTTPException(status_code=400, detail="Invalid user ID format. Must start with 'acct_' for stylist or 'cus_' for customer.")
99
 
 
87
  logger.error(f"Error creating login link for stylist account: {str(e)}")
88
  raise HTTPException(status_code=500, detail="Error creating login link for stylist account.")
89
 
90
+ # Caso seja um cliente, criamos uma sessão do portal de cobrança
91
  elif user_id.startswith("cus_"):
92
+ try:
93
+ # Cria uma sessão do portal de cobrança para o cliente
94
+ session = stripe.billing_portal.Session.create(
95
+ customer=user_id,
96
+ return_url="https://your-website.com/account" # URL para onde o cliente será redirecionado após sair do portal
97
+ )
98
+
99
+ return {
100
+ "status": "success",
101
+ "dashboard_link": session.url # Retorna o link para o painel do cliente
102
+ }
103
 
104
+ except stripe.error.StripeError as e:
105
+ logger.error(f"Error creating billing portal session for customer: {str(e)}")
106
+ raise HTTPException(status_code=500, detail="Error creating billing portal session for customer.")
107
+
108
  else:
109
  raise HTTPException(status_code=400, detail="Invalid user ID format. Must start with 'acct_' for stylist or 'cus_' for customer.")
110