habulaj commited on
Commit
940799f
·
verified ·
1 Parent(s): 2375d08

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +34 -0
routes/subscription.py CHANGED
@@ -64,6 +64,40 @@ def verify_token(user_token: str) -> str:
64
  else:
65
  raise HTTPException(status_code=401, detail="Invalid or expired token")
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  @router.post("/webhook")
68
  async def stripe_webhook(request: Request):
69
  try:
 
64
  else:
65
  raise HTTPException(status_code=401, detail="Invalid or expired token")
66
 
67
+ @router.get("/subscription/status/{subscription_id}")
68
+ async def check_subscription_status(subscription_id: str):
69
+ try:
70
+ # 🔹 Consulta a Stripe pelo subscription_id
71
+ stripe_url = f"https://api.stripe.com/v1/subscriptions/{subscription_id}"
72
+ headers = {
73
+ "Authorization": f"Bearer {STRIPE_SECRET_KEY}"
74
+ }
75
+ response = requests.get(stripe_url, headers=headers)
76
+
77
+ if response.status_code != 200:
78
+ raise HTTPException(status_code=404, detail="Subscription not found in Stripe")
79
+
80
+ subscription = response.json()
81
+
82
+ # 🔹 Pega o status diretamente da Stripe
83
+ status = subscription.get("status")
84
+
85
+ # 🔹 Se precisar verificar expiração (apenas para "active" status)
86
+ if status == "active":
87
+ current_period_end = subscription.get("current_period_end")
88
+ if current_period_end:
89
+ ny_tz = pytz.timezone("America/New_York")
90
+ expiration_date = datetime.utcfromtimestamp(current_period_end).replace(tzinfo=pytz.utc).astimezone(ny_tz)
91
+ now = datetime.now(ny_tz)
92
+
93
+ if now > expiration_date:
94
+ return {"subscription_id": subscription_id, "status": "expired"}
95
+
96
+ return {"subscription_id": subscription_id, "status": status}
97
+
98
+ except Exception as e:
99
+ raise HTTPException(status_code=500, detail=str(e))
100
+
101
  @router.post("/webhook")
102
  async def stripe_webhook(request: Request):
103
  try: