Update routes/subscription.py
Browse files- routes/subscription.py +44 -0
routes/subscription.py
CHANGED
@@ -68,6 +68,50 @@ def verify_token(user_token: str) -> str:
|
|
68 |
else:
|
69 |
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
@router.post("/generate_dashboard_link")
|
72 |
async def generate_dashboard_link(data: UserIDRequest):
|
73 |
try:
|
|
|
68 |
else:
|
69 |
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
70 |
|
71 |
+
@router.post("/subscription_details")
|
72 |
+
async def subscription_details(data: SubscriptionRequest):
|
73 |
+
try:
|
74 |
+
subscription_id = data.id
|
75 |
+
|
76 |
+
# Buscar detalhes da assinatura no Stripe
|
77 |
+
subscription = await async_stripe_request(
|
78 |
+
stripe.Subscription.retrieve,
|
79 |
+
id=subscription_id,
|
80 |
+
expand=["items.data.price"]
|
81 |
+
)
|
82 |
+
|
83 |
+
if not subscription:
|
84 |
+
raise HTTPException(status_code=404, detail="Subscription not found")
|
85 |
+
|
86 |
+
# Pegando os detalhes do preço atual
|
87 |
+
current_price = subscription["items"]["data"][0]["price"]["unit_amount"] / 100
|
88 |
+
currency = subscription["items"]["data"][0]["price"]["currency"].upper()
|
89 |
+
|
90 |
+
# Data da próxima cobrança
|
91 |
+
next_billing_date = datetime.utcfromtimestamp(subscription["current_period_end"]).isoformat()
|
92 |
+
|
93 |
+
# Preço da próxima cobrança
|
94 |
+
upcoming_invoice = await async_stripe_request(
|
95 |
+
stripe.Invoice.upcoming,
|
96 |
+
subscription=subscription_id
|
97 |
+
)
|
98 |
+
next_invoice_amount = upcoming_invoice["amount_due"] / 100 if upcoming_invoice else None
|
99 |
+
|
100 |
+
return {
|
101 |
+
"status": "success",
|
102 |
+
"subscription_id": subscription_id,
|
103 |
+
"current_price": f"{current_price} {currency}",
|
104 |
+
"next_invoice_amount": f"{next_invoice_amount} {currency}" if next_invoice_amount else "N/A",
|
105 |
+
"next_billing_date": next_billing_date
|
106 |
+
}
|
107 |
+
|
108 |
+
except stripe.error.StripeError as e:
|
109 |
+
logger.error(f"Stripe error: {str(e)}")
|
110 |
+
raise HTTPException(status_code=500, detail=f"Stripe error: {str(e)}")
|
111 |
+
except Exception as e:
|
112 |
+
logger.error(f"Error retrieving subscription details: {str(e)}")
|
113 |
+
raise HTTPException(status_code=500, detail="Error retrieving subscription details.")
|
114 |
+
|
115 |
@router.post("/generate_dashboard_link")
|
116 |
async def generate_dashboard_link(data: UserIDRequest):
|
117 |
try:
|