Update routes/subscription.py
Browse files- routes/subscription.py +40 -11
routes/subscription.py
CHANGED
@@ -327,34 +327,34 @@ async def stripe_webhook(request: Request):
|
|
327 |
|
328 |
if event_type == "invoice.payment_succeeded":
|
329 |
invoice = payload.get("data", {}).get("object", {})
|
330 |
-
|
331 |
# 🔹 Capturar valores da fatura
|
332 |
amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
|
333 |
currency = invoice.get("currency", "usd") # Moeda do pagamento
|
334 |
-
|
335 |
# 🔹 Buscando metadados
|
336 |
metadata = invoice.get("metadata", {})
|
337 |
subscription_metadata = invoice.get("subscription_details", {}).get("metadata", {})
|
338 |
line_items = invoice.get("lines", {}).get("data", [])
|
339 |
line_item_metadata = line_items[0].get("metadata", {}) if line_items else {}
|
340 |
-
|
341 |
# 🔹 Pegando os valores corretos
|
342 |
stylist_id = metadata.get("stylist_id") or subscription_metadata.get("stylist_id") or line_item_metadata.get("stylist_id")
|
343 |
user_id = metadata.get("user_id") or subscription_metadata.get("user_id") or line_item_metadata.get("user_id")
|
344 |
price_id = metadata.get("price_id") or subscription_metadata.get("price_id") or line_item_metadata.get("price_id")
|
345 |
subscription_id = invoice.get("subscription") # Capturando o ID da assinatura
|
346 |
-
|
347 |
# 🔹 Calculando a divisão do pagamento
|
348 |
stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
|
349 |
platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
|
350 |
-
|
351 |
# 🔹 Logando as informações detalhadas
|
352 |
logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
|
353 |
logger.info(f"👤 Stylist ID: {stylist_id}")
|
354 |
logger.info(f"👥 User ID: {user_id}")
|
355 |
logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
|
356 |
logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
|
357 |
-
|
358 |
# 🔹 Inserir dados na tabela Subscriptions no Supabase
|
359 |
subscription_data = {
|
360 |
"stylist_id": stylist_id,
|
@@ -363,25 +363,54 @@ async def stripe_webhook(request: Request):
|
|
363 |
"sub_id": subscription_id, # ID da assinatura
|
364 |
"price_id": price_id # ID do preço
|
365 |
}
|
366 |
-
|
367 |
supabase_headers = {
|
368 |
"Authorization": f"Bearer {SUPABASE_KEY}",
|
369 |
"apikey": SUPABASE_KEY,
|
370 |
"Content-Type": "application/json"
|
371 |
}
|
372 |
-
|
373 |
subscription_url = f"{SUPABASE_URL}/rest/v1/Subscriptions"
|
374 |
response_subscription = requests.post(
|
375 |
subscription_url,
|
376 |
headers=supabase_headers,
|
377 |
json=subscription_data
|
378 |
)
|
379 |
-
|
380 |
if response_subscription.status_code == 201:
|
381 |
logger.info(f"✅ Subscription added successfully for user {user_id}")
|
382 |
else:
|
383 |
logger.error(f"❌ Failed to add subscription: {response_subscription.status_code} - {response_subscription.text}")
|
384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
return {
|
386 |
"status": "success",
|
387 |
"total_paid": amount_paid / 100,
|
@@ -390,7 +419,7 @@ async def stripe_webhook(request: Request):
|
|
390 |
"stylist_amount": stylist_amount / 100,
|
391 |
"platform_amount": platform_amount / 100
|
392 |
}
|
393 |
-
|
394 |
elif event_type == "customer.subscription.deleted":
|
395 |
subscription = payload.get("data", {}).get("object", {})
|
396 |
subscription_id = subscription.get("id")
|
|
|
327 |
|
328 |
if event_type == "invoice.payment_succeeded":
|
329 |
invoice = payload.get("data", {}).get("object", {})
|
330 |
+
|
331 |
# 🔹 Capturar valores da fatura
|
332 |
amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
|
333 |
currency = invoice.get("currency", "usd") # Moeda do pagamento
|
334 |
+
|
335 |
# 🔹 Buscando metadados
|
336 |
metadata = invoice.get("metadata", {})
|
337 |
subscription_metadata = invoice.get("subscription_details", {}).get("metadata", {})
|
338 |
line_items = invoice.get("lines", {}).get("data", [])
|
339 |
line_item_metadata = line_items[0].get("metadata", {}) if line_items else {}
|
340 |
+
|
341 |
# 🔹 Pegando os valores corretos
|
342 |
stylist_id = metadata.get("stylist_id") or subscription_metadata.get("stylist_id") or line_item_metadata.get("stylist_id")
|
343 |
user_id = metadata.get("user_id") or subscription_metadata.get("user_id") or line_item_metadata.get("user_id")
|
344 |
price_id = metadata.get("price_id") or subscription_metadata.get("price_id") or line_item_metadata.get("price_id")
|
345 |
subscription_id = invoice.get("subscription") # Capturando o ID da assinatura
|
346 |
+
|
347 |
# 🔹 Calculando a divisão do pagamento
|
348 |
stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
|
349 |
platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
|
350 |
+
|
351 |
# 🔹 Logando as informações detalhadas
|
352 |
logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
|
353 |
logger.info(f"👤 Stylist ID: {stylist_id}")
|
354 |
logger.info(f"👥 User ID: {user_id}")
|
355 |
logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
|
356 |
logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
|
357 |
+
|
358 |
# 🔹 Inserir dados na tabela Subscriptions no Supabase
|
359 |
subscription_data = {
|
360 |
"stylist_id": stylist_id,
|
|
|
363 |
"sub_id": subscription_id, # ID da assinatura
|
364 |
"price_id": price_id # ID do preço
|
365 |
}
|
366 |
+
|
367 |
supabase_headers = {
|
368 |
"Authorization": f"Bearer {SUPABASE_KEY}",
|
369 |
"apikey": SUPABASE_KEY,
|
370 |
"Content-Type": "application/json"
|
371 |
}
|
372 |
+
|
373 |
subscription_url = f"{SUPABASE_URL}/rest/v1/Subscriptions"
|
374 |
response_subscription = requests.post(
|
375 |
subscription_url,
|
376 |
headers=supabase_headers,
|
377 |
json=subscription_data
|
378 |
)
|
379 |
+
|
380 |
if response_subscription.status_code == 201:
|
381 |
logger.info(f"✅ Subscription added successfully for user {user_id}")
|
382 |
else:
|
383 |
logger.error(f"❌ Failed to add subscription: {response_subscription.status_code} - {response_subscription.text}")
|
384 |
+
|
385 |
+
# 🔹 Verificar se já existe um chat entre o cliente e o estilista
|
386 |
+
chat_url = f"{SUPABASE_URL}/rest/v1/chats?client_id=eq.{user_id}&stylist_id=eq.{stylist_id}"
|
387 |
+
response_chat = requests.get(chat_url, headers=supabase_headers)
|
388 |
+
|
389 |
+
if response_chat.status_code == 200:
|
390 |
+
chats = response_chat.json()
|
391 |
+
|
392 |
+
if len(chats) == 0: # Se não houver chat, cria um novo
|
393 |
+
chat_data = {
|
394 |
+
"client_id": user_id,
|
395 |
+
"stylist_id": stylist_id,
|
396 |
+
"created_at": "2025-02-23T00:00:00Z" # Defina a data de criação corretamente (use o horário atual, por exemplo)
|
397 |
+
}
|
398 |
+
chat_url = f"{SUPABASE_URL}/rest/v1/chats"
|
399 |
+
response_create_chat = requests.post(
|
400 |
+
chat_url,
|
401 |
+
headers=supabase_headers,
|
402 |
+
json=chat_data
|
403 |
+
)
|
404 |
+
|
405 |
+
if response_create_chat.status_code == 201:
|
406 |
+
logger.info(f"✅ Chat created successfully between user {user_id} and stylist {stylist_id}")
|
407 |
+
else:
|
408 |
+
logger.error(f"❌ Failed to create chat: {response_create_chat.status_code} - {response_create_chat.text}")
|
409 |
+
else:
|
410 |
+
logger.info(f"✅ Chat already exists between user {user_id} and stylist {stylist_id}")
|
411 |
+
else:
|
412 |
+
logger.error(f"❌ Failed to check chat existence: {response_chat.status_code} - {response_chat.text}")
|
413 |
+
|
414 |
return {
|
415 |
"status": "success",
|
416 |
"total_paid": amount_paid / 100,
|
|
|
419 |
"stylist_amount": stylist_amount / 100,
|
420 |
"platform_amount": platform_amount / 100
|
421 |
}
|
422 |
+
|
423 |
elif event_type == "customer.subscription.deleted":
|
424 |
subscription = payload.get("data", {}).get("object", {})
|
425 |
subscription_id = subscription.get("id")
|