Update routes/subscription.py
Browse files- routes/subscription.py +18 -16
routes/subscription.py
CHANGED
@@ -149,16 +149,16 @@ def create_checkout_session(
|
|
149 |
if not user_token:
|
150 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
151 |
|
152 |
-
# 🔹 1. Validar o token e obter user_id
|
153 |
user_id = verify_token(user_token)
|
154 |
|
155 |
-
# 🔹 2. Buscar
|
156 |
-
|
157 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
158 |
headers=SUPABASE_HEADERS
|
159 |
)
|
160 |
|
161 |
-
stylist_data =
|
162 |
if not stylist_data:
|
163 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
164 |
|
@@ -167,10 +167,12 @@ def create_checkout_session(
|
|
167 |
consultations = stylist.get("consultations")
|
168 |
price_id = stylist.get("price_id") # Buscar o price_id do estilista
|
169 |
|
|
|
|
|
170 |
if not consultations or not stylist_stripe_id or not price_id:
|
171 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete or missing price_id")
|
172 |
|
173 |
-
# 🔹
|
174 |
response_user = requests.get(
|
175 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
176 |
headers=SUPABASE_HEADERS
|
@@ -178,36 +180,36 @@ def create_checkout_session(
|
|
178 |
|
179 |
user_data = response_user.json()
|
180 |
|
181 |
-
# Depuração: Verificando o que foi retornado
|
182 |
-
logger.info(f"📌
|
183 |
|
184 |
if not user_data:
|
185 |
-
raise HTTPException(status_code=404, detail="
|
186 |
|
187 |
user = user_data[0]
|
188 |
user_stripe_id = user.get("stripe_id")
|
189 |
stripe_customer_id = user.get("stripe_customer_id") # Garantir que o campo customer_id esteja presente
|
190 |
|
191 |
-
#
|
192 |
-
logger.info(f"📌 Retrieved
|
193 |
-
logger.info(f"📌 Retrieved
|
194 |
|
195 |
if not user_stripe_id:
|
196 |
-
raise HTTPException(status_code=400, detail="
|
197 |
|
198 |
if not stripe_customer_id:
|
199 |
-
raise HTTPException(status_code=400, detail="
|
200 |
|
201 |
-
# 🔹
|
202 |
session = stripe.checkout.Session.create(
|
203 |
success_url="https://yourdomain.com/success",
|
204 |
cancel_url="https://yourdomain.com/cancel",
|
205 |
payment_method_types=["card"],
|
206 |
mode="subscription",
|
207 |
-
customer=stripe_customer_id, #
|
208 |
line_items=[
|
209 |
{
|
210 |
-
"price": price_id, #
|
211 |
"quantity": 1
|
212 |
}
|
213 |
],
|
|
|
149 |
if not user_token:
|
150 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
151 |
|
152 |
+
# 🔹 1. Validar o token do cliente e obter user_id (cliente)
|
153 |
user_id = verify_token(user_token)
|
154 |
|
155 |
+
# 🔹 2. Buscar dados do estilista com o id enviado na solicitação
|
156 |
+
response_stylist = requests.get(
|
157 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
158 |
headers=SUPABASE_HEADERS
|
159 |
)
|
160 |
|
161 |
+
stylist_data = response_stylist.json()
|
162 |
if not stylist_data:
|
163 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
164 |
|
|
|
167 |
consultations = stylist.get("consultations")
|
168 |
price_id = stylist.get("price_id") # Buscar o price_id do estilista
|
169 |
|
170 |
+
# 🔹 3. Validar dados do estilista
|
171 |
+
logger.info(f"📌 Stylist Data: {stylist}")
|
172 |
if not consultations or not stylist_stripe_id or not price_id:
|
173 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete or missing price_id")
|
174 |
|
175 |
+
# 🔹 4. Buscar dados do cliente (cliente autenticado) usando o user_id
|
176 |
response_user = requests.get(
|
177 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
178 |
headers=SUPABASE_HEADERS
|
|
|
180 |
|
181 |
user_data = response_user.json()
|
182 |
|
183 |
+
# Depuração: Verificando o que foi retornado para o cliente
|
184 |
+
logger.info(f"📌 Client Data from Supabase: {user_data}")
|
185 |
|
186 |
if not user_data:
|
187 |
+
raise HTTPException(status_code=404, detail="Client not found")
|
188 |
|
189 |
user = user_data[0]
|
190 |
user_stripe_id = user.get("stripe_id")
|
191 |
stripe_customer_id = user.get("stripe_customer_id") # Garantir que o campo customer_id esteja presente
|
192 |
|
193 |
+
# Depuração: Verificando se o stripe_customer_id existe
|
194 |
+
logger.info(f"📌 Retrieved Client stripe_id: {user_stripe_id}")
|
195 |
+
logger.info(f"📌 Retrieved Client stripe_customer_id: {stripe_customer_id}")
|
196 |
|
197 |
if not user_stripe_id:
|
198 |
+
raise HTTPException(status_code=400, detail="Client does not have a Stripe ID")
|
199 |
|
200 |
if not stripe_customer_id:
|
201 |
+
raise HTTPException(status_code=400, detail="Client does not have a Stripe Customer ID")
|
202 |
|
203 |
+
# 🔹 5. Criar Checkout Session no Stripe
|
204 |
session = stripe.checkout.Session.create(
|
205 |
success_url="https://yourdomain.com/success",
|
206 |
cancel_url="https://yourdomain.com/cancel",
|
207 |
payment_method_types=["card"],
|
208 |
mode="subscription",
|
209 |
+
customer=stripe_customer_id, # Usando o stripe_customer_id do cliente
|
210 |
line_items=[
|
211 |
{
|
212 |
+
"price": price_id, # Usando o price_id do estilista
|
213 |
"quantity": 1
|
214 |
}
|
215 |
],
|