Update routes/subscription.py
Browse files- routes/subscription.py +50 -23
routes/subscription.py
CHANGED
@@ -24,8 +24,8 @@ SUPABASE_URL = "https://ussxqnifefkgkaumjann.supabase.co"
|
|
24 |
SUPABASE_KEY = os.getenv("SUPA_KEY") # Lendo do ambiente
|
25 |
SUPABASE_ROLE_KEY = os.getenv("SUPA_ROLE_KEY")
|
26 |
|
27 |
-
if not stripe.api_key or not SUPABASE_KEY:
|
28 |
-
raise ValueError("❌ STRIPE_KEY ou
|
29 |
|
30 |
SUPABASE_HEADERS = {
|
31 |
"apikey": SUPABASE_KEY,
|
@@ -33,6 +33,12 @@ SUPABASE_HEADERS = {
|
|
33 |
"Content-Type": "application/json"
|
34 |
}
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
class EmergencyPaymentRequest(BaseModel):
|
37 |
id: str # ID do estilista
|
38 |
|
@@ -341,8 +347,8 @@ async def stripe_webhook(request: Request):
|
|
341 |
invoice = payload.get("data", {}).get("object", {})
|
342 |
|
343 |
# 🔹 Capturar valores da fatura
|
344 |
-
amount_paid = invoice.get("amount_paid", 0)
|
345 |
-
currency = invoice.get("currency", "usd")
|
346 |
|
347 |
# 🔹 Buscando metadados
|
348 |
metadata = invoice.get("metadata", {})
|
@@ -354,38 +360,29 @@ async def stripe_webhook(request: Request):
|
|
354 |
stylist_id = metadata.get("stylist_id") or subscription_metadata.get("stylist_id") or line_item_metadata.get("stylist_id")
|
355 |
user_id = metadata.get("user_id") or subscription_metadata.get("user_id") or line_item_metadata.get("user_id")
|
356 |
price_id = metadata.get("price_id") or subscription_metadata.get("price_id") or line_item_metadata.get("price_id")
|
357 |
-
subscription_id = invoice.get("subscription")
|
358 |
|
359 |
# 🔹 Calculando a divisão do pagamento
|
360 |
-
stylist_amount = int(amount_paid * 0.8)
|
361 |
-
platform_amount = int(amount_paid * 0.2)
|
362 |
|
363 |
-
# 🔹 Logando as informações
|
364 |
logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
|
365 |
logger.info(f"👤 Stylist ID: {stylist_id}")
|
366 |
logger.info(f"👥 User ID: {user_id}")
|
367 |
-
logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
|
368 |
-
logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
|
369 |
|
370 |
-
# 🔹 Inserir dados na tabela Subscriptions
|
371 |
subscription_data = {
|
372 |
"stylist_id": stylist_id,
|
373 |
"customer_id": user_id,
|
374 |
-
"active": True,
|
375 |
-
"sub_id": subscription_id,
|
376 |
-
"price_id": price_id
|
377 |
-
}
|
378 |
-
|
379 |
-
supabase_headers = {
|
380 |
-
"Authorization": f"Bearer {SUPABASE_KEY}",
|
381 |
-
"apikey": SUPABASE_KEY,
|
382 |
-
"Content-Type": "application/json"
|
383 |
}
|
384 |
|
385 |
-
subscription_url = f"{SUPABASE_URL}/rest/v1/Subscriptions"
|
386 |
response_subscription = requests.post(
|
387 |
-
|
388 |
-
headers=
|
389 |
json=subscription_data
|
390 |
)
|
391 |
|
@@ -394,6 +391,36 @@ async def stripe_webhook(request: Request):
|
|
394 |
else:
|
395 |
logger.error(f"❌ Failed to add subscription: {response_subscription.status_code} - {response_subscription.text}")
|
396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
return {
|
398 |
"status": "success",
|
399 |
"total_paid": amount_paid / 100,
|
|
|
24 |
SUPABASE_KEY = os.getenv("SUPA_KEY") # Lendo do ambiente
|
25 |
SUPABASE_ROLE_KEY = os.getenv("SUPA_ROLE_KEY")
|
26 |
|
27 |
+
if not stripe.api_key or not SUPABASE_KEY or not SUPABASE_ROLE_KEY:
|
28 |
+
raise ValueError("❌ STRIPE_KEY, SUPA_KEY ou SUPA_ROLE_KEY não foram definidos no ambiente!")
|
29 |
|
30 |
SUPABASE_HEADERS = {
|
31 |
"apikey": SUPABASE_KEY,
|
|
|
33 |
"Content-Type": "application/json"
|
34 |
}
|
35 |
|
36 |
+
SUPABASE_ROLE_HEADERS = {
|
37 |
+
"apikey": SUPABASE_ROLE_KEY,
|
38 |
+
"Authorization": f"Bearer {SUPABASE_ROLE_KEY}",
|
39 |
+
"Content-Type": "application/json"
|
40 |
+
}
|
41 |
+
|
42 |
class EmergencyPaymentRequest(BaseModel):
|
43 |
id: str # ID do estilista
|
44 |
|
|
|
347 |
invoice = payload.get("data", {}).get("object", {})
|
348 |
|
349 |
# 🔹 Capturar valores da fatura
|
350 |
+
amount_paid = invoice.get("amount_paid", 0)
|
351 |
+
currency = invoice.get("currency", "usd")
|
352 |
|
353 |
# 🔹 Buscando metadados
|
354 |
metadata = invoice.get("metadata", {})
|
|
|
360 |
stylist_id = metadata.get("stylist_id") or subscription_metadata.get("stylist_id") or line_item_metadata.get("stylist_id")
|
361 |
user_id = metadata.get("user_id") or subscription_metadata.get("user_id") or line_item_metadata.get("user_id")
|
362 |
price_id = metadata.get("price_id") or subscription_metadata.get("price_id") or line_item_metadata.get("price_id")
|
363 |
+
subscription_id = invoice.get("subscription")
|
364 |
|
365 |
# 🔹 Calculando a divisão do pagamento
|
366 |
+
stylist_amount = int(amount_paid * 0.8)
|
367 |
+
platform_amount = int(amount_paid * 0.2)
|
368 |
|
369 |
+
# 🔹 Logando as informações
|
370 |
logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
|
371 |
logger.info(f"👤 Stylist ID: {stylist_id}")
|
372 |
logger.info(f"👥 User ID: {user_id}")
|
|
|
|
|
373 |
|
374 |
+
# 🔹 Inserir dados na tabela Subscriptions
|
375 |
subscription_data = {
|
376 |
"stylist_id": stylist_id,
|
377 |
"customer_id": user_id,
|
378 |
+
"active": True,
|
379 |
+
"sub_id": subscription_id,
|
380 |
+
"price_id": price_id
|
|
|
|
|
|
|
|
|
|
|
|
|
381 |
}
|
382 |
|
|
|
383 |
response_subscription = requests.post(
|
384 |
+
f"{SUPABASE_URL}/rest/v1/Subscriptions",
|
385 |
+
headers=SUPABASE_HEADERS,
|
386 |
json=subscription_data
|
387 |
)
|
388 |
|
|
|
391 |
else:
|
392 |
logger.error(f"❌ Failed to add subscription: {response_subscription.status_code} - {response_subscription.text}")
|
393 |
|
394 |
+
# 🔹 Verificar se já existe chat entre stylist e client
|
395 |
+
check_chat_url = f"{SUPABASE_URL}/rest/v1/chats?stylist_id=eq.{stylist_id}&client_id=eq.{user_id}"
|
396 |
+
response_check_chat = requests.get(
|
397 |
+
check_chat_url,
|
398 |
+
headers=SUPABASE_ROLE_HEADERS
|
399 |
+
)
|
400 |
+
|
401 |
+
if response_check_chat.status_code == 200:
|
402 |
+
existing_chats = response_check_chat.json()
|
403 |
+
|
404 |
+
# 🔹 Se não existir chat, criar novo
|
405 |
+
if not existing_chats:
|
406 |
+
chat_data = {
|
407 |
+
"stylist_id": stylist_id,
|
408 |
+
"client_id": user_id
|
409 |
+
}
|
410 |
+
|
411 |
+
response_chat = requests.post(
|
412 |
+
f"{SUPABASE_URL}/rest/v1/chats",
|
413 |
+
headers=SUPABASE_ROLE_HEADERS,
|
414 |
+
json=chat_data
|
415 |
+
)
|
416 |
+
|
417 |
+
if response_chat.status_code == 201:
|
418 |
+
logger.info(f"✅ Chat created for stylist {stylist_id} and client {user_id}")
|
419 |
+
else:
|
420 |
+
logger.error(f"❌ Failed to create chat: {response_chat.status_code} - {response_chat.text}")
|
421 |
+
else:
|
422 |
+
logger.info(f"ℹ️ Chat already exists for stylist {stylist_id} and client {user_id}")
|
423 |
+
|
424 |
return {
|
425 |
"status": "success",
|
426 |
"total_paid": amount_paid / 100,
|