Update routes/subscription.py
Browse files- routes/subscription.py +37 -1
routes/subscription.py
CHANGED
@@ -313,7 +313,7 @@ async def stripe_webhook(request: Request):
|
|
313 |
logger.error(f"❌ Failed to update subscription: {response_update.status_code} - {response_update.text}")
|
314 |
return {"status": "error", "message": "Failed to update subscription."}
|
315 |
|
316 |
-
|
317 |
subscription = payload.get("data", {}).get("object", {})
|
318 |
subscription_id = subscription.get("id")
|
319 |
canceled_at = subscription.get("canceled_at") # Timestamp do cancelamento
|
@@ -351,6 +351,42 @@ async def stripe_webhook(request: Request):
|
|
351 |
logger.info(f"🔹 Subscription {subscription_id} updated, but not canceled.")
|
352 |
return {"status": "success", "message": "Subscription updated but not canceled."}
|
353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
354 |
except Exception as e:
|
355 |
logger.error(f"❌ Erro no webhook: {str(e)}")
|
356 |
return {"status": "error", "message": str(e)}
|
|
|
313 |
logger.error(f"❌ Failed to update subscription: {response_update.status_code} - {response_update.text}")
|
314 |
return {"status": "error", "message": "Failed to update subscription."}
|
315 |
|
316 |
+
elif event_type == "customer.subscription.updated":
|
317 |
subscription = payload.get("data", {}).get("object", {})
|
318 |
subscription_id = subscription.get("id")
|
319 |
canceled_at = subscription.get("canceled_at") # Timestamp do cancelamento
|
|
|
351 |
logger.info(f"🔹 Subscription {subscription_id} updated, but not canceled.")
|
352 |
return {"status": "success", "message": "Subscription updated but not canceled."}
|
353 |
|
354 |
+
elif event_type == "payment_intent.succeeded":
|
355 |
+
payment_intent = payload.get("data", {}).get("object", {})
|
356 |
+
metadata = payment_intent.get("metadata", {})
|
357 |
+
|
358 |
+
# 🔹 Capturando os dados do metadado
|
359 |
+
stylist_id = metadata.get("stylist_id")
|
360 |
+
subscription_id = metadata.get("subscription_id")
|
361 |
+
client_id = metadata.get("client_id")
|
362 |
+
amount_paid = payment_intent.get("amount_received", 0) # Valor pago
|
363 |
+
|
364 |
+
# 🔹 Criando a nova linha na tabela Emergency_sub
|
365 |
+
emergency_sub_data = {
|
366 |
+
"sub_id": subscription_id,
|
367 |
+
"stylist_id": stylist_id,
|
368 |
+
"client_id": client_id,
|
369 |
+
"price": amount_paid # Preço em centavos
|
370 |
+
}
|
371 |
+
|
372 |
+
# Inserir dados na tabela Emergency_sub no Supabase
|
373 |
+
emergency_sub_url = f"{SUPABASE_URL}/rest/v1/Emergency_sub"
|
374 |
+
response_emergency_sub = requests.post(
|
375 |
+
emergency_sub_url,
|
376 |
+
headers=supabase_headers,
|
377 |
+
json=emergency_sub_data
|
378 |
+
)
|
379 |
+
|
380 |
+
if response_emergency_sub.status_code == 201:
|
381 |
+
logger.info(f"✅ Emergency subscription added successfully for client {client_id}")
|
382 |
+
else:
|
383 |
+
logger.error(f"❌ Failed to add emergency subscription: {response_emergency_sub.status_code} - {response_emergency_sub.text}")
|
384 |
+
|
385 |
+
return {
|
386 |
+
"status": "success",
|
387 |
+
"message": "Emergency subscription added successfully."
|
388 |
+
}
|
389 |
+
|
390 |
except Exception as e:
|
391 |
logger.error(f"❌ Erro no webhook: {str(e)}")
|
392 |
return {"status": "error", "message": str(e)}
|