habulaj commited on
Commit
b014290
·
verified ·
1 Parent(s): 2a1ba20

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +18 -10
routes/subscription.py CHANGED
@@ -183,7 +183,7 @@ async def create_price(
183
 
184
  logger.info(f"🔹 Validated amounts: amount = {amount}, emergency_price = {emergency_price}, consultations = {consultations}")
185
 
186
- # 🔹 4. Buscar price_id do usuário no Supabase
187
  supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
188
  headers = {
189
  "Authorization": f"Bearer {user_token}", # 🔹 Incluindo token correto no header
@@ -201,15 +201,23 @@ async def create_price(
201
 
202
  user = user_data[0]
203
  existing_price_id = user.get("price_id")
204
- logger.info(f"🔹 Existing price_id: {existing_price_id}")
205
-
206
- # 🔹 5. Criar novo preço no Stripe
207
- price = stripe.Price.create(
208
- unit_amount=amount,
209
- currency="brl",
210
- recurring={"interval": "month"},
211
- product_data={"name": "Custom Subscription Price"}
212
- )
 
 
 
 
 
 
 
 
213
  new_price_id = price.id
214
  logger.info(f"✅ New price created: {new_price_id}")
215
 
 
183
 
184
  logger.info(f"🔹 Validated amounts: amount = {amount}, emergency_price = {emergency_price}, consultations = {consultations}")
185
 
186
+ # 🔹 4. Buscar price_id, name e avatar do usuário no Supabase
187
  supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
188
  headers = {
189
  "Authorization": f"Bearer {user_token}", # 🔹 Incluindo token correto no header
 
201
 
202
  user = user_data[0]
203
  existing_price_id = user.get("price_id")
204
+ user_name = user.get("name", "Unknown User") # 🔹 Recuperar o nome do usuário
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 novo preço no Stripe com nome e imagem baseados no usuário
209
+ price_name = f"{user_name} - {amount} BRL" # 🔹 Nome formatado para o preço
210
+ price_data = {
211
+ "unit_amount": amount,
212
+ "currency": "brl",
213
+ "recurring": {"interval": "month"},
214
+ "product_data": {"name": price_name}
215
+ }
216
+
217
+ if user_avatar: # 🔹 Verificar se o avatar existe e adicionar a imagem
218
+ price_data["product_data"]["images"] = [user_avatar]
219
+
220
+ price = stripe.Price.create(**price_data)
221
  new_price_id = price.id
222
  logger.info(f"✅ New price created: {new_price_id}")
223