habulaj commited on
Commit
702a9cd
·
verified ·
1 Parent(s): 7778e50

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +14 -2
routes/subscription.py CHANGED
@@ -71,6 +71,7 @@ def verify_token(user_token: str) -> str:
71
  else:
72
  raise HTTPException(status_code=401, detail="Invalid or expired token")
73
 
 
74
  @router.post("/subscription_details")
75
  async def subscription_details(data: SubscriptionRequest):
76
  try:
@@ -360,9 +361,10 @@ async def stripe_webhook(request: Request):
360
  stylist_id = payment_intent.get("metadata", {}).get("stylist_id")
361
  client_id = payment_intent.get("metadata", {}).get("client_id")
362
  subscription_id = payment_intent.get("metadata", {}).get("subscription_id")
 
363
  amount_received = payment_intent.get("amount_received", 0) # Valor recebido (em centavos)
364
 
365
- if not all([stylist_id, client_id, subscription_id]):
366
  logger.error("❌ Faltando dados essenciais no metadado do Payment Intent.")
367
  return {"status": "error", "message": "Missing essential metadata."}
368
 
@@ -370,6 +372,7 @@ async def stripe_webhook(request: Request):
370
  logger.info(f"👤 Client ID: {client_id}")
371
  logger.info(f"👗 Stylist ID: {stylist_id}")
372
  logger.info(f"💰 Price: R$ {amount_received / 100:.2f}")
 
373
 
374
  # 🔹 Definindo os cabeçalhos do Supabase
375
  supabase_headers = {
@@ -378,12 +381,21 @@ async def stripe_webhook(request: Request):
378
  "Content-Type": "application/json"
379
  }
380
 
 
 
 
 
 
 
 
 
381
  # 🔹 Inserir dados na tabela Emergency_sub no Supabase
382
  emergency_sub_data = {
383
  "sub_id": subscription_id, # Subscription ID
384
  "stylist_id": stylist_id, # Stylist ID
385
  "client_id": client_id, # Client ID
386
- "price": amount_received # Price in cents
 
387
  }
388
 
389
  emergency_sub_url = f"{SUPABASE_URL}/rest/v1/Emergency_sub"
 
71
  else:
72
  raise HTTPException(status_code=401, detail="Invalid or expired token")
73
 
74
+
75
  @router.post("/subscription_details")
76
  async def subscription_details(data: SubscriptionRequest):
77
  try:
 
361
  stylist_id = payment_intent.get("metadata", {}).get("stylist_id")
362
  client_id = payment_intent.get("metadata", {}).get("client_id")
363
  subscription_id = payment_intent.get("metadata", {}).get("subscription_id")
364
+ payment_id = payment_intent.get("id") # Payment ID
365
  amount_received = payment_intent.get("amount_received", 0) # Valor recebido (em centavos)
366
 
367
+ if not all([stylist_id, client_id, subscription_id, payment_id]):
368
  logger.error("❌ Faltando dados essenciais no metadado do Payment Intent.")
369
  return {"status": "error", "message": "Missing essential metadata."}
370
 
 
372
  logger.info(f"👤 Client ID: {client_id}")
373
  logger.info(f"👗 Stylist ID: {stylist_id}")
374
  logger.info(f"💰 Price: R$ {amount_received / 100:.2f}")
375
+ logger.info(f"💳 Payment ID: {payment_id}")
376
 
377
  # 🔹 Definindo os cabeçalhos do Supabase
378
  supabase_headers = {
 
381
  "Content-Type": "application/json"
382
  }
383
 
384
+ # 🔹 Verificar se o payment_id já existe na tabela Emergency_sub
385
+ check_url = f"{SUPABASE_URL}/rest/v1/Emergency_sub?payment_id=eq.{payment_id}"
386
+ response_check = requests.get(check_url, headers=supabase_headers)
387
+
388
+ if response_check.status_code == 200 and len(response_check.json()) > 0:
389
+ logger.info(f"❌ Payment ID {payment_id} já existe. Nenhuma nova linha será inserida.")
390
+ return {"status": "error", "message": "Payment ID already exists."}
391
+
392
  # 🔹 Inserir dados na tabela Emergency_sub no Supabase
393
  emergency_sub_data = {
394
  "sub_id": subscription_id, # Subscription ID
395
  "stylist_id": stylist_id, # Stylist ID
396
  "client_id": client_id, # Client ID
397
+ "price": amount_received, # Price in cents
398
+ "payment_id": payment_id # Payment ID
399
  }
400
 
401
  emergency_sub_url = f"{SUPABASE_URL}/rest/v1/Emergency_sub"