habulaj commited on
Commit
6e544eb
·
verified ·
1 Parent(s): 261640c

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +18 -9
routes/subscription.py CHANGED
@@ -122,6 +122,7 @@ async def create_price(
122
 
123
  # 🔹 1. Validar o token e obter user_id
124
  user_id = verify_token(user_token)
 
125
 
126
  amount = data.amount
127
  if not amount:
@@ -129,14 +130,23 @@ async def create_price(
129
 
130
  # 🔹 2. Buscar price_id do usuário no Supabase
131
  supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
132
- response = requests.get(supabase_url, headers=SUPABASE_HEADERS)
133
- user_data = response.json()
 
 
 
 
 
 
 
134
 
 
135
  if not user_data:
136
- raise HTTPException(status_code=404, detail="User not found")
137
 
138
  user = user_data[0]
139
  existing_price_id = user.get("price_id")
 
140
 
141
  # 🔹 3. Criar novo preço no Stripe
142
  price = stripe.Price.create(
@@ -157,20 +167,19 @@ async def create_price(
157
  logger.info(f"🔹 Subscription {sub.id} set to cancel at period end.")
158
 
159
  # 🔹 5. Atualizar Supabase com o novo price_id
160
- update_response = requests.patch(
161
- supabase_url,
162
- headers=SUPABASE_HEADERS,
163
- json={"price_id": new_price_id}
164
- )
165
 
166
  if update_response.status_code not in [200, 204]:
167
  raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {update_response.text}")
168
 
 
169
  return {"message": "Price created successfully!", "price_id": new_price_id}
170
 
171
  except Exception as e:
172
  logger.error(f"❌ Error creating price: {e}")
173
- raise HTTPException(status_code=500, detail="Error creating price.")
174
 
175
  @router.post("/create_checkout_session")
176
  def create_checkout_session(
 
122
 
123
  # 🔹 1. Validar o token e obter user_id
124
  user_id = verify_token(user_token)
125
+ logger.info(f"🔹 User verified. user_id: {user_id}")
126
 
127
  amount = data.amount
128
  if not amount:
 
130
 
131
  # 🔹 2. Buscar price_id do usuário no Supabase
132
  supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
133
+ headers = {
134
+ "Authorization": f"Bearer {user_token}", # 🔹 Incluindo token correto no header
135
+ **SUPABASE_HEADERS
136
+ }
137
+ response = requests.get(supabase_url, headers=headers)
138
+ logger.info(f"🔹 Supabase GET response: {response.status_code} - {response.text}")
139
+
140
+ if response.status_code != 200:
141
+ raise HTTPException(status_code=500, detail=f"Failed to fetch user from Supabase: {response.text}")
142
 
143
+ user_data = response.json()
144
  if not user_data:
145
+ raise HTTPException(status_code=404, detail="User not found in Supabase")
146
 
147
  user = user_data[0]
148
  existing_price_id = user.get("price_id")
149
+ logger.info(f"🔹 Existing price_id: {existing_price_id}")
150
 
151
  # 🔹 3. Criar novo preço no Stripe
152
  price = stripe.Price.create(
 
167
  logger.info(f"🔹 Subscription {sub.id} set to cancel at period end.")
168
 
169
  # 🔹 5. Atualizar Supabase com o novo price_id
170
+ update_data = {"price_id": new_price_id}
171
+ update_response = requests.patch(supabase_url, headers=headers, json=update_data)
172
+ logger.info(f"🔹 Supabase PATCH response: {update_response.status_code} - {update_response.text}")
 
 
173
 
174
  if update_response.status_code not in [200, 204]:
175
  raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {update_response.text}")
176
 
177
+ logger.info(f"✅ Successfully updated user {user_id} with new price_id {new_price_id}")
178
  return {"message": "Price created successfully!", "price_id": new_price_id}
179
 
180
  except Exception as e:
181
  logger.error(f"❌ Error creating price: {e}")
182
+ raise HTTPException(status_code=500, detail=f"Error creating price: {str(e)}")
183
 
184
  @router.post("/create_checkout_session")
185
  def create_checkout_session(