habulaj commited on
Commit
68dc6ca
·
verified ·
1 Parent(s): 1735268

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +14 -15
routes/subscription.py CHANGED
@@ -68,23 +68,22 @@ async def stripe_webhook(request: Request):
68
 
69
  if event_type == "invoice.payment_succeeded":
70
  invoice = payload.get("data", {}).get("object", {})
71
- amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
72
 
73
- # 🔹 Recuperar metadata corretamente do nível do invoice
74
  metadata = invoice.get("metadata", {})
75
- stylist_id = metadata.get("stylist_id", "N/A") # ID do estilista
76
- user_id = metadata.get("user_id", "N/A") # ID do usuário
77
-
78
- # 🔹 Cálculo da divisão
79
- stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
80
- platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
81
-
82
- # 🔹 Logando os valores
83
- logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
84
- logger.info(f"👤 Stylist ID: {stylist_id}")
85
- logger.info(f"👥 User ID: {user_id}")
86
- logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
87
- logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
88
 
89
  return {"status": "success"}
90
 
 
68
 
69
  if event_type == "invoice.payment_succeeded":
70
  invoice = payload.get("data", {}).get("object", {})
 
71
 
72
+ # 🔹 Verifica metadata na invoice principal
73
  metadata = invoice.get("metadata", {})
74
+
75
+ # 🔹 Verifica metadata na assinatura associada
76
+ subscription_metadata = invoice.get("subscription_details", {}).get("metadata", {})
77
+
78
+ # 🔹 Verifica metadata dentro dos itens da fatura (invoice.line_items)
79
+ line_items = invoice.get("lines", {}).get("data", [])
80
+ line_item_metadata = line_items[0].get("metadata", {}) if line_items else {}
81
+
82
+ # 🔹 Agora pegamos os valores
83
+ stylist_id = metadata.get("stylist_id") or subscription_metadata.get("stylist_id") or line_item_metadata.get("stylist_id")
84
+ user_id = metadata.get("user_id") or subscription_metadata.get("user_id") or line_item_metadata.get("user_id")
85
+
86
+ logger.info(f" Invoice processada! Stylist ID: {stylist_id}, User ID: {user_id}")
87
 
88
  return {"status": "success"}
89