Update routes/subscription.py
Browse files- routes/subscription.py +24 -9
routes/subscription.py
CHANGED
@@ -69,23 +69,38 @@ async def stripe_webhook(request: Request):
|
|
69 |
if event_type == "invoice.payment_succeeded":
|
70 |
invoice = payload.get("data", {}).get("object", {})
|
71 |
|
72 |
-
# 🔹
|
73 |
-
|
74 |
|
75 |
-
# 🔹
|
|
|
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 |
-
# 🔹
|
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 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
except Exception as e:
|
91 |
logger.error(f"❌ Erro no webhook: {str(e)}")
|
|
|
69 |
if event_type == "invoice.payment_succeeded":
|
70 |
invoice = payload.get("data", {}).get("object", {})
|
71 |
|
72 |
+
# 🔹 Capturando valores da fatura
|
73 |
+
amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
|
74 |
|
75 |
+
# 🔹 Buscando metadados
|
76 |
+
metadata = invoice.get("metadata", {})
|
77 |
subscription_metadata = invoice.get("subscription_details", {}).get("metadata", {})
|
|
|
|
|
78 |
line_items = invoice.get("lines", {}).get("data", [])
|
79 |
line_item_metadata = line_items[0].get("metadata", {}) if line_items else {}
|
80 |
|
81 |
+
# 🔹 Pegando os valores corretos
|
82 |
stylist_id = metadata.get("stylist_id") or subscription_metadata.get("stylist_id") or line_item_metadata.get("stylist_id")
|
83 |
user_id = metadata.get("user_id") or subscription_metadata.get("user_id") or line_item_metadata.get("user_id")
|
84 |
|
85 |
+
# 🔹 Calculando a divisão do pagamento
|
86 |
+
stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
|
87 |
+
platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
|
88 |
+
|
89 |
+
# 🔹 Logando as informações
|
90 |
+
logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
|
91 |
+
logger.info(f"👤 Stylist ID: {stylist_id}")
|
92 |
+
logger.info(f"👥 User ID: {user_id}")
|
93 |
+
logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
|
94 |
+
logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
|
95 |
+
|
96 |
+
return {
|
97 |
+
"status": "success",
|
98 |
+
"total_paid": amount_paid / 100,
|
99 |
+
"stylist_id": stylist_id,
|
100 |
+
"user_id": user_id,
|
101 |
+
"stylist_amount": stylist_amount / 100,
|
102 |
+
"platform_amount": platform_amount / 100
|
103 |
+
}
|
104 |
|
105 |
except Exception as e:
|
106 |
logger.error(f"❌ Erro no webhook: {str(e)}")
|