Update routes/subscription.py
Browse files- routes/subscription.py +11 -40
routes/subscription.py
CHANGED
@@ -339,34 +339,34 @@ async def stripe_webhook(request: Request):
|
|
339 |
|
340 |
if event_type == "invoice.payment_succeeded":
|
341 |
invoice = payload.get("data", {}).get("object", {})
|
342 |
-
|
343 |
# 🔹 Capturar valores da fatura
|
344 |
amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
|
345 |
currency = invoice.get("currency", "usd") # Moeda do pagamento
|
346 |
-
|
347 |
# 🔹 Buscando metadados
|
348 |
metadata = invoice.get("metadata", {})
|
349 |
subscription_metadata = invoice.get("subscription_details", {}).get("metadata", {})
|
350 |
line_items = invoice.get("lines", {}).get("data", [])
|
351 |
line_item_metadata = line_items[0].get("metadata", {}) if line_items else {}
|
352 |
-
|
353 |
# 🔹 Pegando os valores corretos
|
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") # Capturando o ID da assinatura
|
358 |
-
|
359 |
# 🔹 Calculando a divisão do pagamento
|
360 |
stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
|
361 |
platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
|
362 |
-
|
363 |
# 🔹 Logando as informações detalhadas
|
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 no Supabase
|
371 |
subscription_data = {
|
372 |
"stylist_id": stylist_id,
|
@@ -375,54 +375,25 @@ async def stripe_webhook(request: Request):
|
|
375 |
"sub_id": subscription_id, # ID da assinatura
|
376 |
"price_id": price_id # ID do preço
|
377 |
}
|
378 |
-
|
379 |
supabase_headers = {
|
380 |
-
"Authorization": f"Bearer {
|
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 |
subscription_url,
|
388 |
headers=supabase_headers,
|
389 |
json=subscription_data
|
390 |
)
|
391 |
-
|
392 |
if response_subscription.status_code == 201:
|
393 |
logger.info(f"✅ Subscription added successfully for user {user_id}")
|
394 |
else:
|
395 |
logger.error(f"❌ Failed to add subscription: {response_subscription.status_code} - {response_subscription.text}")
|
396 |
-
|
397 |
-
# 🔹 Verificar se já existe um chat entre o cliente e o estilista
|
398 |
-
chat_url = f"{SUPABASE_URL}/rest/v1/chats?client_id=eq.{user_id}&stylist_id=eq.{stylist_id}"
|
399 |
-
response_chat = requests.get(chat_url, headers=supabase_headers)
|
400 |
-
|
401 |
-
if response_chat.status_code == 200:
|
402 |
-
chats = response_chat.json()
|
403 |
-
|
404 |
-
if len(chats) == 0: # Se não houver chat, cria um novo
|
405 |
-
chat_data = {
|
406 |
-
"client_id": user_id,
|
407 |
-
"stylist_id": stylist_id,
|
408 |
-
"created_at": "2025-02-23T00:00:00Z" # Defina a data de criação corretamente (use o horário atual, por exemplo)
|
409 |
-
}
|
410 |
-
chat_url = f"{SUPABASE_URL}/rest/v1/chats"
|
411 |
-
response_create_chat = requests.post(
|
412 |
-
chat_url,
|
413 |
-
headers=supabase_headers,
|
414 |
-
json=chat_data
|
415 |
-
)
|
416 |
-
|
417 |
-
if response_create_chat.status_code == 201:
|
418 |
-
logger.info(f"✅ Chat created successfully between user {user_id} and stylist {stylist_id}")
|
419 |
-
else:
|
420 |
-
logger.error(f"❌ Failed to create chat: {response_create_chat.status_code} - {response_create_chat.text}")
|
421 |
-
else:
|
422 |
-
logger.info(f"✅ Chat already exists between user {user_id} and stylist {stylist_id}")
|
423 |
-
else:
|
424 |
-
logger.error(f"❌ Failed to check chat existence: {response_chat.status_code} - {response_chat.text}")
|
425 |
-
|
426 |
return {
|
427 |
"status": "success",
|
428 |
"total_paid": amount_paid / 100,
|
|
|
339 |
|
340 |
if event_type == "invoice.payment_succeeded":
|
341 |
invoice = payload.get("data", {}).get("object", {})
|
342 |
+
|
343 |
# 🔹 Capturar valores da fatura
|
344 |
amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
|
345 |
currency = invoice.get("currency", "usd") # Moeda do pagamento
|
346 |
+
|
347 |
# 🔹 Buscando metadados
|
348 |
metadata = invoice.get("metadata", {})
|
349 |
subscription_metadata = invoice.get("subscription_details", {}).get("metadata", {})
|
350 |
line_items = invoice.get("lines", {}).get("data", [])
|
351 |
line_item_metadata = line_items[0].get("metadata", {}) if line_items else {}
|
352 |
+
|
353 |
# 🔹 Pegando os valores corretos
|
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") # Capturando o ID da assinatura
|
358 |
+
|
359 |
# 🔹 Calculando a divisão do pagamento
|
360 |
stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
|
361 |
platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
|
362 |
+
|
363 |
# 🔹 Logando as informações detalhadas
|
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 no Supabase
|
371 |
subscription_data = {
|
372 |
"stylist_id": stylist_id,
|
|
|
375 |
"sub_id": subscription_id, # ID da assinatura
|
376 |
"price_id": price_id # ID do preço
|
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 |
subscription_url,
|
388 |
headers=supabase_headers,
|
389 |
json=subscription_data
|
390 |
)
|
391 |
+
|
392 |
if response_subscription.status_code == 201:
|
393 |
logger.info(f"✅ Subscription added successfully for user {user_id}")
|
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,
|