habulaj commited on
Commit
8093d40
·
verified ·
1 Parent(s): d97c844

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +17 -6
routes/subscription.py CHANGED
@@ -130,10 +130,21 @@ async def create_price(
130
  emergency_price = data.emergency_price
131
  consultations = data.consultations
132
 
133
- if not amount or not emergency_price or not consultations:
134
- raise HTTPException(status_code=400, detail="Amount, emergency_price, and consultations are required")
 
 
 
135
 
136
- # 🔹 2. Buscar price_id do usuário no Supabase
 
 
 
 
 
 
 
 
137
  supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
138
  headers = {
139
  "Authorization": f"Bearer {user_token}", # 🔹 Incluindo token correto no header
@@ -153,7 +164,7 @@ async def create_price(
153
  existing_price_id = user.get("price_id")
154
  logger.info(f"🔹 Existing price_id: {existing_price_id}")
155
 
156
- # 🔹 3. Criar novo preço no Stripe
157
  price = stripe.Price.create(
158
  unit_amount=amount,
159
  currency="brl",
@@ -163,7 +174,7 @@ async def create_price(
163
  new_price_id = price.id
164
  logger.info(f"✅ New price created: {new_price_id}")
165
 
166
- # 🔹 4. Se já houver um price_id, cancelar assinaturas ativas ao final do período
167
  if existing_price_id:
168
  subscriptions = stripe.Subscription.list(status="active")
169
  for sub in subscriptions.auto_paging_iter():
@@ -171,7 +182,7 @@ async def create_price(
171
  stripe.Subscription.modify(sub.id, cancel_at_period_end=True)
172
  logger.info(f"🔹 Subscription {sub.id} set to cancel at period end.")
173
 
174
- # 🔹 5. Atualizar Supabase com o novo price_id e valores adicionais
175
  update_data = {
176
  "price_id": new_price_id, # Novo price_id
177
  "price": amount, # Atualizando o valor de 'price'
 
130
  emergency_price = data.emergency_price
131
  consultations = data.consultations
132
 
133
+ # 🔹 2. Verificar se os valores estão dentro dos limites permitidos
134
+ if not (500 <= amount <= 99900):
135
+ raise HTTPException(status_code=400, detail="Amount must be between $5 and $999")
136
+ if not (500 <= emergency_price <= 99900):
137
+ raise HTTPException(status_code=400, detail="Emergency price must be between $5 and $999")
138
 
139
+ # 🔹 3. Consultations precisa ser 0 obrigatoriamente se não for definido
140
+ if consultations is None:
141
+ consultations = 0
142
+ elif consultations < 0:
143
+ raise HTTPException(status_code=400, detail="Consultations must be greater than or equal to 0")
144
+
145
+ logger.info(f"🔹 Validated amounts: amount = {amount}, emergency_price = {emergency_price}, consultations = {consultations}")
146
+
147
+ # 🔹 4. Buscar price_id do usuário no Supabase
148
  supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
149
  headers = {
150
  "Authorization": f"Bearer {user_token}", # 🔹 Incluindo token correto no header
 
164
  existing_price_id = user.get("price_id")
165
  logger.info(f"🔹 Existing price_id: {existing_price_id}")
166
 
167
+ # 🔹 5. Criar novo preço no Stripe
168
  price = stripe.Price.create(
169
  unit_amount=amount,
170
  currency="brl",
 
174
  new_price_id = price.id
175
  logger.info(f"✅ New price created: {new_price_id}")
176
 
177
+ # 🔹 6. Se já houver um price_id, cancelar assinaturas ativas ao final do período
178
  if existing_price_id:
179
  subscriptions = stripe.Subscription.list(status="active")
180
  for sub in subscriptions.auto_paging_iter():
 
182
  stripe.Subscription.modify(sub.id, cancel_at_period_end=True)
183
  logger.info(f"🔹 Subscription {sub.id} set to cancel at period end.")
184
 
185
+ # 🔹 7. Atualizar Supabase com o novo price_id e valores adicionais
186
  update_data = {
187
  "price_id": new_price_id, # Novo price_id
188
  "price": amount, # Atualizando o valor de 'price'