habulaj commited on
Commit
9a8e4f6
·
verified ·
1 Parent(s): 2ffaca9

Update routes/hello.py

Browse files
Files changed (1) hide show
  1. routes/hello.py +20 -11
routes/hello.py CHANGED
@@ -62,24 +62,27 @@ def create_customer(
62
  ):
63
  try:
64
  if not user_token:
65
- raise HTTPException(status_code=401, detail="Missing User-key header")
66
 
67
  # 🔹 Validar o token e obter user_id
68
  user_id = verify_token(user_token)
69
  logger.info(f"🔹 User verified. user_id: {user_id}")
70
 
71
- # 🔹 Verificar se já existe um cliente com o mesmo e-mail ou telefone no Stripe
72
  existing_customers = stripe.Customer.list(email=data.email, limit=1)
73
 
74
  if existing_customers.data:
75
- logger.warning(f"⚠️ Customer with email {data.email} already exists.")
76
- raise HTTPException(status_code=400, detail="A customer with this email already exists in Stripe.")
 
77
 
 
78
  existing_customers_by_phone = stripe.Customer.list(phone=data.phone, limit=1)
79
 
80
  if existing_customers_by_phone.data:
81
- logger.warning(f"⚠️ Customer with phone {data.phone} already exists.")
82
- raise HTTPException(status_code=400, detail="A customer with this phone number already exists in Stripe.")
 
83
 
84
  # 🔹 Criar o cliente no Stripe
85
  try:
@@ -89,8 +92,9 @@ def create_customer(
89
  name=data.name
90
  )
91
  except stripe.error.StripeError as e:
92
- logger.error(f" Stripe error: {e}")
93
- raise HTTPException(status_code=500, detail="Error creating customer in Stripe")
 
94
 
95
  stripe_id = customer.id
96
  logger.info(f"✅ New Stripe customer created: {stripe_id}")
@@ -116,14 +120,19 @@ def create_customer(
116
  # 🚨 Se a atualização falhar, deletamos o cliente do Stripe!
117
  logger.warning(f"⚠️ Rolling back: Deleting Stripe customer {stripe_id} due to Supabase failure.")
118
  stripe.Customer.delete(stripe_id)
119
- raise HTTPException(status_code=500, detail=f"Erro ao atualizar o Supabase: {response.text}")
 
120
 
121
  logger.info(f"✅ Successfully updated user {user_id} with stripe_id {stripe_id}")
122
  return {"customer_id": stripe_id}
123
 
 
 
 
124
  except Exception as e:
125
- logger.error(f"❌ Error creating customer: {str(e)}")
126
- raise HTTPException(status_code=500, detail=f"Error creating customer: {str(e)}")
 
127
 
128
  @router.post("/create_account")
129
  def create_account(account_data: AccountRequest):
 
62
  ):
63
  try:
64
  if not user_token:
65
+ raise HTTPException(status_code=401, detail={"detail": "Missing User-key header"})
66
 
67
  # 🔹 Validar o token e obter user_id
68
  user_id = verify_token(user_token)
69
  logger.info(f"🔹 User verified. user_id: {user_id}")
70
 
71
+ # 🔹 Verificar se já existe um cliente com o mesmo e-mail no Stripe
72
  existing_customers = stripe.Customer.list(email=data.email, limit=1)
73
 
74
  if existing_customers.data:
75
+ error_message = f"Customer with email {data.email} already exists."
76
+ logger.warning(f"⚠️ {error_message}")
77
+ raise HTTPException(status_code=400, detail={"detail": error_message})
78
 
79
+ # 🔹 Verificar se já existe um cliente com o mesmo telefone
80
  existing_customers_by_phone = stripe.Customer.list(phone=data.phone, limit=1)
81
 
82
  if existing_customers_by_phone.data:
83
+ error_message = f"Customer with phone {data.phone} already exists."
84
+ logger.warning(f"⚠️ {error_message}")
85
+ raise HTTPException(status_code=400, detail={"detail": error_message})
86
 
87
  # 🔹 Criar o cliente no Stripe
88
  try:
 
92
  name=data.name
93
  )
94
  except stripe.error.StripeError as e:
95
+ error_message = str(e) if str(e) else "An unknown error occurred. Please try again."
96
+ logger.error(f" Stripe error: {error_message}")
97
+ raise HTTPException(status_code=500, detail={"detail": error_message})
98
 
99
  stripe_id = customer.id
100
  logger.info(f"✅ New Stripe customer created: {stripe_id}")
 
120
  # 🚨 Se a atualização falhar, deletamos o cliente do Stripe!
121
  logger.warning(f"⚠️ Rolling back: Deleting Stripe customer {stripe_id} due to Supabase failure.")
122
  stripe.Customer.delete(stripe_id)
123
+ error_message = f"Error updating Supabase: {response.text}"
124
+ raise HTTPException(status_code=500, detail={"detail": error_message})
125
 
126
  logger.info(f"✅ Successfully updated user {user_id} with stripe_id {stripe_id}")
127
  return {"customer_id": stripe_id}
128
 
129
+ except HTTPException as http_err:
130
+ raise http_err # Já estruturamos o erro corretamente acima, então só propagamos.
131
+
132
  except Exception as e:
133
+ error_message = str(e) if str(e) else "An unknown error occurred. Please try again."
134
+ logger.error(f"Error creating customer: {error_message}")
135
+ raise HTTPException(status_code=500, detail={"detail": error_message})
136
 
137
  @router.post("/create_account")
138
  def create_account(account_data: AccountRequest):