Update routes/subscription.py
Browse files- routes/subscription.py +25 -18
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,25 +351,32 @@ 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 |
-
|
355 |
payment_intent = payload.get("data", {}).get("object", {})
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
subscription_id = metadata.get("subscription_id")
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
emergency_sub_data = {
|
366 |
-
"sub_id": subscription_id,
|
367 |
-
"stylist_id": stylist_id,
|
368 |
-
"client_id": client_id,
|
369 |
-
"price":
|
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,
|
@@ -378,13 +385,13 @@ async def stripe_webhook(request: Request):
|
|
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:
|
|
|
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 |
+
if 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 |
+
|
357 |
+
# 🔹 Pegando os valores do metadado
|
358 |
+
stylist_id = payment_intent.get("metadata", {}).get("stylist_id")
|
359 |
+
client_id = payment_intent.get("metadata", {}).get("client_id")
|
360 |
+
subscription_id = payment_intent.get("metadata", {}).get("subscription_id")
|
361 |
+
amount_received = payment_intent.get("amount_received", 0) # Valor recebido (em centavos)
|
362 |
+
|
363 |
+
if not all([stylist_id, client_id, subscription_id]):
|
364 |
+
logger.error("❌ Faltando dados essenciais no metadado do Payment Intent.")
|
365 |
+
return {"status": "error", "message": "Missing essential metadata."}
|
366 |
+
|
367 |
+
logger.info(f"🔹 Payment Intent succeeded for subscription {subscription_id}")
|
368 |
+
logger.info(f"👤 Client ID: {client_id}")
|
369 |
+
logger.info(f"👗 Stylist ID: {stylist_id}")
|
370 |
+
logger.info(f"💰 Price: R$ {amount_received / 100:.2f}")
|
371 |
+
|
372 |
+
# 🔹 Inserir dados na tabela Emergency_sub no Supabase
|
373 |
emergency_sub_data = {
|
374 |
+
"sub_id": subscription_id, # Subscription ID
|
375 |
+
"stylist_id": stylist_id, # Stylist ID
|
376 |
+
"client_id": client_id, # Client ID
|
377 |
+
"price": amount_received # Price in cents
|
378 |
}
|
379 |
|
|
|
380 |
emergency_sub_url = f"{SUPABASE_URL}/rest/v1/Emergency_sub"
|
381 |
response_emergency_sub = requests.post(
|
382 |
emergency_sub_url,
|
|
|
385 |
)
|
386 |
|
387 |
if response_emergency_sub.status_code == 201:
|
388 |
+
logger.info(f"✅ Emergency subscription data added successfully for client {client_id}")
|
389 |
else:
|
390 |
logger.error(f"❌ Failed to add emergency subscription: {response_emergency_sub.status_code} - {response_emergency_sub.text}")
|
391 |
|
392 |
return {
|
393 |
"status": "success",
|
394 |
+
"message": "Emergency subscription added successfully"
|
395 |
}
|
396 |
|
397 |
except Exception as e:
|