Update routes/subscription.py
Browse files- routes/subscription.py +19 -13
routes/subscription.py
CHANGED
@@ -205,23 +205,29 @@ async def create_price(
|
|
205 |
user_avatar = user.get("avatar", None) # 🔹 Recuperar a URL do avatar do usuário, se existir
|
206 |
logger.info(f"🔹 Existing price_id: {existing_price_id}, user_name: {user_name}, user_avatar: {user_avatar}")
|
207 |
|
208 |
-
# 🔹 5. Criar
|
209 |
-
|
210 |
-
|
211 |
-
"unit_amount": amount,
|
212 |
-
"currency": "brl",
|
213 |
-
"recurring": {"interval": "month"},
|
214 |
-
"product_data": {"name": price_name}
|
215 |
}
|
216 |
|
217 |
-
if user_avatar: #
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
new_price_id = price.id
|
222 |
logger.info(f"✅ New price created: {new_price_id}")
|
223 |
|
224 |
-
# 🔹
|
225 |
if existing_price_id:
|
226 |
subscriptions = stripe.Subscription.list(status="active")
|
227 |
for sub in subscriptions.auto_paging_iter():
|
@@ -229,7 +235,7 @@ async def create_price(
|
|
229 |
stripe.Subscription.modify(sub.id, cancel_at_period_end=True)
|
230 |
logger.info(f"🔹 Subscription {sub.id} set to cancel at period end.")
|
231 |
|
232 |
-
# 🔹
|
233 |
update_data = {
|
234 |
"price_id": new_price_id, # Novo price_id
|
235 |
"price": amount, # Atualizando o valor de 'price'
|
|
|
205 |
user_avatar = user.get("avatar", None) # 🔹 Recuperar a URL do avatar do usuário, se existir
|
206 |
logger.info(f"🔹 Existing price_id: {existing_price_id}, user_name: {user_name}, user_avatar: {user_avatar}")
|
207 |
|
208 |
+
# 🔹 5. Criar produto no Stripe com nome e imagem baseados no usuário
|
209 |
+
product_data = {
|
210 |
+
"name": f"{user_name} - {amount} BRL", # Nome do produto
|
|
|
|
|
|
|
|
|
211 |
}
|
212 |
|
213 |
+
if user_avatar: # Verificar se o avatar existe e adicionar a imagem
|
214 |
+
product_data["images"] = [user_avatar]
|
215 |
+
|
216 |
+
product = stripe.Product.create(**product_data)
|
217 |
+
product_id = product.id
|
218 |
+
logger.info(f"✅ New product created: {product_id}")
|
219 |
+
|
220 |
+
# 🔹 6. Criar novo preço no Stripe associado ao produto
|
221 |
+
price = stripe.Price.create(
|
222 |
+
unit_amount=amount,
|
223 |
+
currency="brl",
|
224 |
+
recurring={"interval": "month"},
|
225 |
+
product=product_id # Associando o preço ao produto
|
226 |
+
)
|
227 |
new_price_id = price.id
|
228 |
logger.info(f"✅ New price created: {new_price_id}")
|
229 |
|
230 |
+
# 🔹 7. Se já houver um price_id, cancelar assinaturas ativas ao final do período
|
231 |
if existing_price_id:
|
232 |
subscriptions = stripe.Subscription.list(status="active")
|
233 |
for sub in subscriptions.auto_paging_iter():
|
|
|
235 |
stripe.Subscription.modify(sub.id, cancel_at_period_end=True)
|
236 |
logger.info(f"🔹 Subscription {sub.id} set to cancel at period end.")
|
237 |
|
238 |
+
# 🔹 8. Atualizar Supabase com o novo price_id e valores adicionais
|
239 |
update_data = {
|
240 |
"price_id": new_price_id, # Novo price_id
|
241 |
"price": amount, # Atualizando o valor de 'price'
|