Update routes/subscription.py
Browse files- routes/subscription.py +20 -17
routes/subscription.py
CHANGED
@@ -325,6 +325,9 @@ async def subscription_details(data: SubscriptionRequest):
|
|
325 |
if not subscription:
|
326 |
raise HTTPException(status_code=404, detail="Subscription not found")
|
327 |
|
|
|
|
|
|
|
328 |
# Determinar status da assinatura
|
329 |
status = subscription["status"]
|
330 |
if status == "active":
|
@@ -339,25 +342,17 @@ async def subscription_details(data: SubscriptionRequest):
|
|
339 |
# Pegando os detalhes do preço atual
|
340 |
current_price = subscription["items"]["data"][0]["price"]["unit_amount"] / 100
|
341 |
currency = subscription["items"]["data"][0]["price"]["currency"].upper()
|
342 |
-
|
343 |
-
# Data da próxima cobrança
|
344 |
try:
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
except stripe.error.InvalidRequestError as e:
|
350 |
-
# Assinatura pode estar cancelada ou não ter próxima fatura
|
351 |
-
logger.info(f"No upcoming invoice for subscription {subscription_id}: {str(e)}")
|
352 |
-
next_invoice_amount = None
|
353 |
except stripe.error.StripeError as e:
|
354 |
-
logger.
|
355 |
next_invoice_amount = None
|
356 |
-
|
357 |
-
logger.error(f"Unexpected error retrieving upcoming invoice: {str(e)}")
|
358 |
-
next_invoice_amount = None
|
359 |
-
|
360 |
-
# Formatar o valor para exibição
|
361 |
if next_invoice_amount is None:
|
362 |
next_invoice_amount = "N/A"
|
363 |
|
@@ -371,6 +366,7 @@ async def subscription_details(data: SubscriptionRequest):
|
|
371 |
payment_type = payment_method_details["type"]
|
372 |
last4 = payment_method_details[payment_type].get("last4", "N/A")
|
373 |
payment_info = f"{payment_type.upper()} ending in {last4}"
|
|
|
374 |
else:
|
375 |
payment_info = "No default payment method found"
|
376 |
|
@@ -390,6 +386,7 @@ async def subscription_details(data: SubscriptionRequest):
|
|
390 |
if data:
|
391 |
stylist_avatar = data[0].get("avatar")
|
392 |
stylist_name = data[0].get("name")
|
|
|
393 |
|
394 |
# Buscar emergency_subscriptions no Supabase
|
395 |
emergency_subscriptions = []
|
@@ -399,6 +396,7 @@ async def subscription_details(data: SubscriptionRequest):
|
|
399 |
)
|
400 |
if response.status_code == 200:
|
401 |
emergency_subscriptions = response.json()
|
|
|
402 |
|
403 |
# Obter o email do assinante
|
404 |
customer_email = "N/A"
|
@@ -414,8 +412,10 @@ async def subscription_details(data: SubscriptionRequest):
|
|
414 |
id=customer_id
|
415 |
)
|
416 |
customer_email = customer_details.get("email", "N/A")
|
|
|
417 |
|
418 |
-
|
|
|
419 |
"status": subscription_status,
|
420 |
"subscription_id": subscription_id,
|
421 |
"current_price": f"{current_price} {currency}",
|
@@ -428,6 +428,9 @@ async def subscription_details(data: SubscriptionRequest):
|
|
428 |
"emergency_subscriptions": emergency_subscriptions,
|
429 |
"customer_email": customer_email
|
430 |
}
|
|
|
|
|
|
|
431 |
|
432 |
except stripe.error.StripeError as e:
|
433 |
logger.error(f"Stripe error: {str(e)}")
|
|
|
325 |
if not subscription:
|
326 |
raise HTTPException(status_code=404, detail="Subscription not found")
|
327 |
|
328 |
+
# Log do resultado da subscription para debug
|
329 |
+
logger.info(f"Subscription retrieved: {subscription}")
|
330 |
+
|
331 |
# Determinar status da assinatura
|
332 |
status = subscription["status"]
|
333 |
if status == "active":
|
|
|
342 |
# Pegando os detalhes do preço atual
|
343 |
current_price = subscription["items"]["data"][0]["price"]["unit_amount"] / 100
|
344 |
currency = subscription["items"]["data"][0]["price"]["currency"].upper()
|
345 |
+
|
346 |
+
# Data da próxima cobrança - CORREÇÃO AQUI
|
347 |
try:
|
348 |
+
# Usando chamada síncrona diretamente, não através do async_stripe_request
|
349 |
+
upcoming_invoice = stripe.Invoice.upcoming(subscription=subscription_id)
|
350 |
+
next_invoice_amount = upcoming_invoice["amount_due"] / 100 if upcoming_invoice else None
|
351 |
+
logger.info(f"Upcoming invoice retrieved: {upcoming_invoice}")
|
|
|
|
|
|
|
|
|
352 |
except stripe.error.StripeError as e:
|
353 |
+
logger.error(f"Error getting upcoming invoice: {str(e)}")
|
354 |
next_invoice_amount = None
|
355 |
+
|
|
|
|
|
|
|
|
|
356 |
if next_invoice_amount is None:
|
357 |
next_invoice_amount = "N/A"
|
358 |
|
|
|
366 |
payment_type = payment_method_details["type"]
|
367 |
last4 = payment_method_details[payment_type].get("last4", "N/A")
|
368 |
payment_info = f"{payment_type.upper()} ending in {last4}"
|
369 |
+
logger.info(f"Payment method details: {payment_method_details}")
|
370 |
else:
|
371 |
payment_info = "No default payment method found"
|
372 |
|
|
|
386 |
if data:
|
387 |
stylist_avatar = data[0].get("avatar")
|
388 |
stylist_name = data[0].get("name")
|
389 |
+
logger.info(f"Stylist data retrieved: {data}")
|
390 |
|
391 |
# Buscar emergency_subscriptions no Supabase
|
392 |
emergency_subscriptions = []
|
|
|
396 |
)
|
397 |
if response.status_code == 200:
|
398 |
emergency_subscriptions = response.json()
|
399 |
+
logger.info(f"Emergency subscriptions retrieved: {emergency_subscriptions}")
|
400 |
|
401 |
# Obter o email do assinante
|
402 |
customer_email = "N/A"
|
|
|
412 |
id=customer_id
|
413 |
)
|
414 |
customer_email = customer_details.get("email", "N/A")
|
415 |
+
logger.info(f"Customer details retrieved: {customer_details}")
|
416 |
|
417 |
+
# Log do resultado final antes de retornar
|
418 |
+
result = {
|
419 |
"status": subscription_status,
|
420 |
"subscription_id": subscription_id,
|
421 |
"current_price": f"{current_price} {currency}",
|
|
|
428 |
"emergency_subscriptions": emergency_subscriptions,
|
429 |
"customer_email": customer_email
|
430 |
}
|
431 |
+
|
432 |
+
logger.info(f"Final result being returned: {result}")
|
433 |
+
return result
|
434 |
|
435 |
except stripe.error.StripeError as e:
|
436 |
logger.error(f"Stripe error: {str(e)}")
|