Update routes/hello.py
Browse files- 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
|
72 |
existing_customers = stripe.Customer.list(email=data.email, limit=1)
|
73 |
|
74 |
if existing_customers.data:
|
75 |
-
|
76 |
-
|
|
|
77 |
|
|
|
78 |
existing_customers_by_phone = stripe.Customer.list(phone=data.phone, limit=1)
|
79 |
|
80 |
if existing_customers_by_phone.data:
|
81 |
-
|
82 |
-
|
|
|
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 |
-
|
93 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
126 |
-
|
|
|
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):
|