Update routes/subscription.py
Browse files- routes/subscription.py +19 -38
routes/subscription.py
CHANGED
@@ -124,7 +124,6 @@ async def create_price(
|
|
124 |
user_id = verify_token(user_token)
|
125 |
|
126 |
amount = data.amount
|
127 |
-
|
128 |
if not amount:
|
129 |
raise HTTPException(status_code=400, detail="Amount is required")
|
130 |
|
@@ -139,57 +138,39 @@ async def create_price(
|
|
139 |
user = user_data[0]
|
140 |
existing_price_id = user.get("price_id")
|
141 |
|
142 |
-
# 🔹 3.
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
|
|
145 |
if existing_price_id:
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
)
|
152 |
-
logger.info(f"✅ Price {existing_price_id} updated successfully.")
|
153 |
-
updated_price_id = existing_price_id
|
154 |
-
except stripe.error.InvalidRequestError:
|
155 |
-
logger.warning(f"⚠️ Failed to update price {existing_price_id}, creating a new one instead.")
|
156 |
-
|
157 |
-
if not updated_price_id:
|
158 |
-
price = stripe.Price.create(
|
159 |
-
unit_amount=amount,
|
160 |
-
currency="brl",
|
161 |
-
recurring={"interval": "month"},
|
162 |
-
product_data={"name": "Custom Subscription Price"}
|
163 |
-
)
|
164 |
-
updated_price_id = price.id
|
165 |
-
logger.info(f"✅ New price created: {updated_price_id}")
|
166 |
-
|
167 |
-
# 🔹 4. Pausar assinaturas antigas e desativar o preço antigo
|
168 |
-
if existing_price_id:
|
169 |
-
subscriptions = stripe.Subscription.list(status="active")
|
170 |
-
for sub in subscriptions.auto_paging_iter():
|
171 |
-
if sub["items"]["data"][0]["price"]["id"] == existing_price_id:
|
172 |
-
stripe.Subscription.modify(sub.id, pause_collection={"behavior": "void"})
|
173 |
-
logger.info(f"🔹 Subscription {sub.id} paused.")
|
174 |
-
|
175 |
-
stripe.Price.modify(existing_price_id, active=False)
|
176 |
-
logger.info(f"🚫 Price {existing_price_id} deactivated.")
|
177 |
|
178 |
# 🔹 5. Atualizar Supabase com o novo price_id
|
179 |
update_response = requests.patch(
|
180 |
supabase_url,
|
181 |
headers=SUPABASE_HEADERS,
|
182 |
-
json={"price_id":
|
183 |
)
|
184 |
|
185 |
if update_response.status_code not in [200, 204]:
|
186 |
raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {update_response.text}")
|
187 |
|
188 |
-
return {"message": "Price created
|
189 |
|
190 |
except Exception as e:
|
191 |
-
logger.error(f"❌ Error creating
|
192 |
-
raise HTTPException(status_code=500, detail="Error creating
|
193 |
|
194 |
@router.post("/create_checkout_session")
|
195 |
def create_checkout_session(
|
|
|
124 |
user_id = verify_token(user_token)
|
125 |
|
126 |
amount = data.amount
|
|
|
127 |
if not amount:
|
128 |
raise HTTPException(status_code=400, detail="Amount is required")
|
129 |
|
|
|
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(
|
143 |
+
unit_amount=amount,
|
144 |
+
currency="brl",
|
145 |
+
recurring={"interval": "month"},
|
146 |
+
product_data={"name": "Custom Subscription Price"}
|
147 |
+
)
|
148 |
+
new_price_id = price.id
|
149 |
+
logger.info(f"✅ New price created: {new_price_id}")
|
150 |
|
151 |
+
# 🔹 4. Se já houver um price_id, cancelar assinaturas ativas ao final do período
|
152 |
if existing_price_id:
|
153 |
+
subscriptions = stripe.Subscription.list(status="active")
|
154 |
+
for sub in subscriptions.auto_paging_iter():
|
155 |
+
if sub["items"]["data"][0]["price"]["id"] == existing_price_id:
|
156 |
+
stripe.Subscription.modify(sub.id, cancel_at_period_end=True)
|
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(
|