Update routes/subscription.py
Browse files- routes/subscription.py +15 -11
routes/subscription.py
CHANGED
@@ -38,6 +38,8 @@ class SubscriptionRequest(BaseModel):
|
|
38 |
|
39 |
class CreatePriceRequest(BaseModel):
|
40 |
amount: int # Valor em centavos (ex: 2500 para R$25,00)
|
|
|
|
|
41 |
|
42 |
def verify_token(user_token: str) -> str:
|
43 |
"""
|
@@ -125,8 +127,11 @@ async def create_price(
|
|
125 |
logger.info(f"🔹 User verified. user_id: {user_id}")
|
126 |
|
127 |
amount = data.amount
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
130 |
|
131 |
# 🔹 2. Buscar price_id do usuário no Supabase
|
132 |
supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
|
@@ -166,14 +171,13 @@ async def create_price(
|
|
166 |
stripe.Subscription.modify(sub.id, cancel_at_period_end=True)
|
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 = {
|
171 |
-
|
172 |
-
"
|
173 |
-
"
|
174 |
-
"
|
175 |
}
|
176 |
-
|
177 |
update_response = requests.patch(supabase_url, headers=headers, json=update_data)
|
178 |
|
179 |
# Log detalhado para verificar a resposta
|
@@ -182,8 +186,8 @@ async def create_price(
|
|
182 |
if update_response.status_code not in [200, 204]:
|
183 |
raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {update_response.text}")
|
184 |
|
185 |
-
logger.info(f"✅ Successfully updated user {user_id} with new price_id
|
186 |
-
return {"message": "Price created successfully!", "price_id": new_price_id}
|
187 |
|
188 |
except Exception as e:
|
189 |
logger.error(f"❌ Error creating price: {e}")
|
|
|
38 |
|
39 |
class CreatePriceRequest(BaseModel):
|
40 |
amount: int # Valor em centavos (ex: 2500 para R$25,00)
|
41 |
+
emergency_price: int # Valor de emergência (ex: 500 para R$5,00)
|
42 |
+
consultations: int # Número de consultas (ex: 3)
|
43 |
|
44 |
def verify_token(user_token: str) -> str:
|
45 |
"""
|
|
|
127 |
logger.info(f"🔹 User verified. user_id: {user_id}")
|
128 |
|
129 |
amount = data.amount
|
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}"
|
|
|
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'
|
178 |
+
"emergency_price": emergency_price, # Atualizando o valor de 'emergency_price'
|
179 |
+
"consultations": consultations # Atualizando o valor de 'consultations'
|
180 |
}
|
|
|
181 |
update_response = requests.patch(supabase_url, headers=headers, json=update_data)
|
182 |
|
183 |
# Log detalhado para verificar a resposta
|
|
|
186 |
if update_response.status_code not in [200, 204]:
|
187 |
raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {update_response.text}")
|
188 |
|
189 |
+
logger.info(f"✅ Successfully updated user {user_id} with new price_id, price, emergency_price, and consultations")
|
190 |
+
return {"message": "Price created and user updated successfully!", "price_id": new_price_id}
|
191 |
|
192 |
except Exception as e:
|
193 |
logger.error(f"❌ Error creating price: {e}")
|