habulaj commited on
Commit
a5db2a7
·
verified ·
1 Parent(s): 153209f

Update routes/hello.py

Browse files
Files changed (1) hide show
  1. routes/hello.py +14 -9
routes/hello.py CHANGED
@@ -58,7 +58,7 @@ def verify_token(user_token: str) -> str:
58
  @router.post("/create_customer")
59
  def create_customer(
60
  data: CreateCustomerRequest,
61
- user_token: str = Header(None, alias="User-key") # 🔹 Agora pegamos do "User-key"
62
  ):
63
  try:
64
  if not user_token:
@@ -75,24 +75,29 @@ def create_customer(
75
  name=data.name
76
  )
77
  stripe_id = customer.id
 
78
 
79
  # 🔹 Atualizar o usuário no Supabase com o stripe_id
80
  update_data = {"stripe_id": stripe_id}
 
81
  update_headers = {
82
- "Authorization": f"Bearer {user_token}",
83
- **SUPABASE_HEADERS
 
84
  }
85
 
86
- response = requests.patch(
87
- f"{SUPABASE_URL}/rest/v1/users?id=eq.{user_id}",
88
- headers=update_headers,
89
- json=update_data
90
- )
 
 
91
 
92
  if response.status_code not in [200, 204]:
93
  raise HTTPException(status_code=500, detail=f"Erro ao atualizar o Supabase: {response.text}")
94
 
95
- logger.info(f"✅ Successfully created customer {stripe_id} and updated Supabase user {user_id}")
96
  return {"customer_id": stripe_id}
97
 
98
  except Exception as e:
 
58
  @router.post("/create_customer")
59
  def create_customer(
60
  data: CreateCustomerRequest,
61
+ user_token: str = Header(None, alias="User-key") # 🔹 Pegando do "User-key"
62
  ):
63
  try:
64
  if not user_token:
 
75
  name=data.name
76
  )
77
  stripe_id = customer.id
78
+ logger.info(f"✅ New Stripe customer created: {stripe_id}")
79
 
80
  # 🔹 Atualizar o usuário no Supabase com o stripe_id
81
  update_data = {"stripe_id": stripe_id}
82
+
83
  update_headers = {
84
+ "Authorization": f"Bearer {user_token}", # 🔹 Token do usuário
85
+ "apikey": SUPABASE_KEY, # 🔹 API Key do Supabase
86
+ "Content-Type": "application/json"
87
  }
88
 
89
+ # 🔹 Corrigindo o nome da tabela (User com "U" maiúsculo)
90
+ supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
91
+
92
+ response = requests.patch(supabase_url, headers=update_headers, json=update_data)
93
+
94
+ # 🔹 Logando a resposta para debug
95
+ logger.info(f"🔹 Supabase PATCH response: {response.status_code} - {response.text}")
96
 
97
  if response.status_code not in [200, 204]:
98
  raise HTTPException(status_code=500, detail=f"Erro ao atualizar o Supabase: {response.text}")
99
 
100
+ logger.info(f"✅ Successfully updated user {user_id} with stripe_id {stripe_id}")
101
  return {"customer_id": stripe_id}
102
 
103
  except Exception as e: