Update routes/subscription.py
Browse files- routes/subscription.py +34 -5
routes/subscription.py
CHANGED
@@ -61,13 +61,42 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
61 |
async def stripe_webhook(request: Request):
|
62 |
payload = await request.body()
|
63 |
headers = dict(request.headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
logger.info("🔹
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
|
|
|
|
71 |
### 4. CANCELAR ASSINATURA ###
|
72 |
@router.post("/cancel_subscription")
|
73 |
def cancel_subscription(data: CancelSubscriptionRequest):
|
|
|
61 |
async def stripe_webhook(request: Request):
|
62 |
payload = await request.body()
|
63 |
headers = dict(request.headers)
|
64 |
+
event = None
|
65 |
+
try:
|
66 |
+
event = stripe.Event.construct_from(
|
67 |
+
json.loads(payload), stripe.api_key
|
68 |
+
)
|
69 |
+
except Exception as e:
|
70 |
+
logger.error(f"⚠️ Erro ao processar webhook: {e}")
|
71 |
+
raise HTTPException(status_code=400, detail="Webhook error")
|
72 |
|
73 |
+
logger.info(f"🔹 Evento recebido: {event['type']}")
|
74 |
+
|
75 |
+
if event["type"] == "invoice.payment_succeeded":
|
76 |
+
invoice = event["data"]["object"]
|
77 |
+
estilista_id = invoice.get("metadata", {}).get("estilista_id")
|
78 |
+
amount = invoice["amount_paid"]
|
79 |
+
estilista_share = int(amount * 0.8) # 80% para o estilista
|
80 |
+
platform_share = amount - estilista_share # 20% para a plataforma
|
81 |
+
|
82 |
+
logger.info(f"Pagamento confirmado: {amount / 100:.2f} BRL")
|
83 |
+
logger.info(f"Estilista {estilista_id} receberá: {estilista_share / 100:.2f} BRL")
|
84 |
+
logger.info(f"Plataforma receberá: {platform_share / 100:.2f} BRL")
|
85 |
+
|
86 |
+
# Transferir os fundos automaticamente
|
87 |
+
try:
|
88 |
+
transfer = stripe.Transfer.create(
|
89 |
+
amount=estilista_share,
|
90 |
+
currency="brl",
|
91 |
+
destination=estilista_id, # Conta conectada do estilista
|
92 |
+
description="Pagamento de assinatura"
|
93 |
+
)
|
94 |
+
logger.info(f"Transferência bem-sucedida: {transfer.id}")
|
95 |
+
except Exception as e:
|
96 |
+
logger.error(f"Erro na transferência: {e}")
|
97 |
|
98 |
+
return {"status": "Webhook processado com sucesso!"}
|
99 |
+
|
100 |
### 4. CANCELAR ASSINATURA ###
|
101 |
@router.post("/cancel_subscription")
|
102 |
def cancel_subscription(data: CancelSubscriptionRequest):
|