Update routes/subscription.py
Browse files- routes/subscription.py +38 -4
routes/subscription.py
CHANGED
|
@@ -585,22 +585,56 @@ async def stripe_webhook(request: Request):
|
|
| 585 |
elif event_type == "payment_intent.succeeded":
|
| 586 |
payment_intent = payload.get("data", {}).get("object", {})
|
| 587 |
|
| 588 |
-
# 🔹 Pegando os valores do metadado
|
| 589 |
stylist_id = payment_intent.get("metadata", {}).get("stylist_id")
|
| 590 |
-
client_id = payment_intent.get("metadata", {}).get("
|
| 591 |
-
|
| 592 |
payment_id = payment_intent.get("id") # Payment ID
|
| 593 |
amount_received = payment_intent.get("amount_received", 0) # Valor recebido (em centavos)
|
|
|
|
| 594 |
|
| 595 |
-
if not all([stylist_id, client_id,
|
| 596 |
logger.error("❌ Faltando dados essenciais no metadado do Payment Intent.")
|
| 597 |
return {"status": "error", "message": "Missing essential metadata."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
|
| 599 |
logger.info(f"🔹 Payment Intent succeeded for subscription {subscription_id}")
|
| 600 |
logger.info(f"👤 Client ID: {client_id}")
|
| 601 |
logger.info(f"👗 Stylist ID: {stylist_id}")
|
| 602 |
logger.info(f"💰 Price: R$ {amount_received / 100:.2f}")
|
| 603 |
logger.info(f"💳 Payment ID: {payment_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
# 🔹 Definindo os cabeçalhos do Supabase
|
| 606 |
supabase_headers = {
|
|
|
|
| 585 |
elif event_type == "payment_intent.succeeded":
|
| 586 |
payment_intent = payload.get("data", {}).get("object", {})
|
| 587 |
|
| 588 |
+
# 🔹 Pegando os valores do metadado - CORRIGIDO
|
| 589 |
stylist_id = payment_intent.get("metadata", {}).get("stylist_id")
|
| 590 |
+
client_id = payment_intent.get("metadata", {}).get("user_id") # Alterado de client_id para user_id
|
| 591 |
+
invoice_id = payment_intent.get("invoice") # Obtendo o ID da fatura
|
| 592 |
payment_id = payment_intent.get("id") # Payment ID
|
| 593 |
amount_received = payment_intent.get("amount_received", 0) # Valor recebido (em centavos)
|
| 594 |
+
stylist_stripe_id = payment_intent.get("metadata", {}).get("stylist_stripe_id")
|
| 595 |
|
| 596 |
+
if not all([stylist_id, client_id, payment_id]):
|
| 597 |
logger.error("❌ Faltando dados essenciais no metadado do Payment Intent.")
|
| 598 |
return {"status": "error", "message": "Missing essential metadata."}
|
| 599 |
+
|
| 600 |
+
# Obter subscription_id da fatura
|
| 601 |
+
subscription_id = None
|
| 602 |
+
if invoice_id:
|
| 603 |
+
try:
|
| 604 |
+
invoice = stripe.Invoice.retrieve(invoice_id)
|
| 605 |
+
subscription_id = invoice.get("subscription")
|
| 606 |
+
except Exception as e:
|
| 607 |
+
logger.error(f"❌ Erro ao obter subscription_id da fatura: {str(e)}")
|
| 608 |
|
| 609 |
logger.info(f"🔹 Payment Intent succeeded for subscription {subscription_id}")
|
| 610 |
logger.info(f"👤 Client ID: {client_id}")
|
| 611 |
logger.info(f"👗 Stylist ID: {stylist_id}")
|
| 612 |
logger.info(f"💰 Price: R$ {amount_received / 100:.2f}")
|
| 613 |
logger.info(f"💳 Payment ID: {payment_id}")
|
| 614 |
+
|
| 615 |
+
# Criar transferência para a conta conectada do estilista
|
| 616 |
+
if stylist_stripe_id and amount_received > 0:
|
| 617 |
+
try:
|
| 618 |
+
# Calcular valor do estilista (80%)
|
| 619 |
+
stylist_amount = int(amount_received * 0.8)
|
| 620 |
+
|
| 621 |
+
# Criar transferência
|
| 622 |
+
transfer = stripe.Transfer.create(
|
| 623 |
+
amount=stylist_amount,
|
| 624 |
+
currency="brl",
|
| 625 |
+
destination=stylist_stripe_id,
|
| 626 |
+
source_transaction=payment_intent.get("latest_charge"),
|
| 627 |
+
description=f"Pagamento de assinatura para {stylist_id}",
|
| 628 |
+
metadata={
|
| 629 |
+
"stylist_id": stylist_id,
|
| 630 |
+
"client_id": client_id,
|
| 631 |
+
"payment_intent_id": payment_id,
|
| 632 |
+
"subscription_id": subscription_id
|
| 633 |
+
}
|
| 634 |
+
)
|
| 635 |
+
logger.info(f"✅ Transferência criada com sucesso: {transfer.id} - Valor: R$ {stylist_amount / 100:.2f}")
|
| 636 |
+
except Exception as e:
|
| 637 |
+
logger.error(f"❌ Erro ao criar transferência: {str(e)}")
|
| 638 |
|
| 639 |
# 🔹 Definindo os cabeçalhos do Supabase
|
| 640 |
supabase_headers = {
|