habulaj commited on
Commit
7306d8c
·
verified ·
1 Parent(s): b57237c

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +23 -2
routes/subscription.py CHANGED
@@ -81,12 +81,33 @@ async def create_price(
81
  if price_id:
82
  try:
83
  price = stripe.Price.retrieve(price_id)
 
84
  if price.unit_amount == amount: # Se o preço for igual, podemos reutilizar
85
  current_price_valid = True
86
  logger.info(f"✅ Reusing existing price {price_id} (same amount) for user {user_id}")
87
  else:
88
- stripe.Price.modify(price_id, active=False) # Desativa o antigo
89
- logger.info(f"🔄 Deactivated old price {price_id} (different amount)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  except stripe.error.InvalidRequestError:
91
  logger.warning(f"⚠️ Invalid price_id ({price_id}), creating a new one...")
92
  price_id = None # Se for inválido, vamos criar um novo
 
81
  if price_id:
82
  try:
83
  price = stripe.Price.retrieve(price_id)
84
+
85
  if price.unit_amount == amount: # Se o preço for igual, podemos reutilizar
86
  current_price_valid = True
87
  logger.info(f"✅ Reusing existing price {price_id} (same amount) for user {user_id}")
88
  else:
89
+ # 🔥 1º TENTATIVA: Tentar excluir o antigo
90
+ try:
91
+ stripe.Price.modify(price_id, active=False) # Desativa primeiro
92
+ stripe.Price.delete(price_id) # Exclui o preço
93
+ logger.info(f"🗑️ Successfully deleted price {price_id}")
94
+ except stripe.error.InvalidRequestError:
95
+ logger.warning(f"⚠️ Failed to delete price {price_id}, trying to deactivate...")
96
+
97
+ # 🔥 2º TENTATIVA: Se não pode excluir, desativa
98
+ try:
99
+ stripe.Price.modify(price_id, active=False)
100
+ logger.info(f"🔄 Deactivated old price {price_id}")
101
+ except stripe.error.InvalidRequestError:
102
+ logger.warning(f"⚠️ Failed to deactivate price {price_id}, trying to archive...")
103
+
104
+ # 🔥 3º TENTATIVA: Se não pode desativar, arquiva
105
+ try:
106
+ stripe.Price.modify(price_id, metadata={"archived": "true"})
107
+ logger.info(f"📦 Archived price {price_id}")
108
+ except Exception as e:
109
+ logger.error(f"❌ Could not archive price {price_id}: {e}")
110
+
111
  except stripe.error.InvalidRequestError:
112
  logger.warning(f"⚠️ Invalid price_id ({price_id}), creating a new one...")
113
  price_id = None # Se for inválido, vamos criar um novo