Update routes/subscription.py
Browse files- routes/subscription.py +52 -0
routes/subscription.py
CHANGED
@@ -39,6 +39,10 @@ SUPABASE_ROLE_HEADERS = {
|
|
39 |
"Content-Type": "application/json"
|
40 |
}
|
41 |
|
|
|
|
|
|
|
|
|
42 |
class EmergencyPaymentRequest(BaseModel):
|
43 |
id: str # ID do estilista
|
44 |
|
@@ -89,6 +93,54 @@ def verify_token(user_token: str) -> str:
|
|
89 |
else:
|
90 |
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
@router.post("/sync_emergency_payments")
|
93 |
async def sync_emergency_payments(request: Request, data: EmergencyPaymentRequest):
|
94 |
try:
|
|
|
39 |
"Content-Type": "application/json"
|
40 |
}
|
41 |
|
42 |
+
class UpdateSubscriptionRequest(BaseModel):
|
43 |
+
subscription_id: str # ID da assinatura a ser modificada
|
44 |
+
new_price_id: str # Novo preço para a assinatura
|
45 |
+
|
46 |
class EmergencyPaymentRequest(BaseModel):
|
47 |
id: str # ID do estilista
|
48 |
|
|
|
93 |
else:
|
94 |
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
95 |
|
96 |
+
@router.post("/update_subscription")
|
97 |
+
async def update_subscription(
|
98 |
+
data: UpdateSubscriptionRequest,
|
99 |
+
user_token: str = Header(None, alias="User-key")
|
100 |
+
):
|
101 |
+
try:
|
102 |
+
if not user_token:
|
103 |
+
raise HTTPException(status_code=401, detail="Missing User-key header")
|
104 |
+
|
105 |
+
user_id = verify_token(user_token)
|
106 |
+
logger.info(f"🔹 User verified. user_id: {user_id}")
|
107 |
+
|
108 |
+
subscription_id = data.subscription_id
|
109 |
+
new_price_id = data.new_price_id
|
110 |
+
|
111 |
+
# 🔹 1. Buscar assinatura específica pelo ID
|
112 |
+
subscription = stripe.Subscription.retrieve(subscription_id)
|
113 |
+
|
114 |
+
if not subscription:
|
115 |
+
raise HTTPException(status_code=404, detail="Subscription not found.")
|
116 |
+
|
117 |
+
# 🔹 2. Verificar se a assinatura pertence ao usuário (se houver um campo identificador)
|
118 |
+
# Se o modelo de dados do Stripe salvar o user_id no metadata, podemos validar assim:
|
119 |
+
if subscription.get("metadata", {}).get("user_id") != user_id:
|
120 |
+
raise HTTPException(status_code=403, detail="You are not authorized to modify this subscription.")
|
121 |
+
|
122 |
+
# 🔹 3. Atualizar a assinatura para o novo preço
|
123 |
+
updated_subscription = stripe.Subscription.modify(
|
124 |
+
subscription_id,
|
125 |
+
items=[{"id": subscription["items"]["data"][0]["id"], "price": new_price_id}]
|
126 |
+
)
|
127 |
+
|
128 |
+
logger.info(f"✅ Subscription {subscription_id} updated to new price {new_price_id}")
|
129 |
+
|
130 |
+
return {
|
131 |
+
"message": "Subscription updated successfully!",
|
132 |
+
"subscription_id": subscription_id,
|
133 |
+
"new_price_id": new_price_id
|
134 |
+
}
|
135 |
+
|
136 |
+
except stripe.error.StripeError as e:
|
137 |
+
logger.error(f"❌ Stripe error: {e}")
|
138 |
+
raise HTTPException(status_code=500, detail=f"Stripe error: {str(e)}")
|
139 |
+
|
140 |
+
except Exception as e:
|
141 |
+
logger.error(f"❌ Error updating subscription: {e}")
|
142 |
+
raise HTTPException(status_code=500, detail=f"Error updating subscription: {str(e)}")
|
143 |
+
|
144 |
@router.post("/sync_emergency_payments")
|
145 |
async def sync_emergency_payments(request: Request, data: EmergencyPaymentRequest):
|
146 |
try:
|