Update routes/subscription.py
Browse files- routes/subscription.py +32 -25
routes/subscription.py
CHANGED
@@ -60,6 +60,38 @@ def verify_token(user_token: str) -> str:
|
|
60 |
else:
|
61 |
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
@router.post("/create_price")
|
64 |
async def create_price(
|
65 |
data: CreatePriceRequest,
|
@@ -139,31 +171,6 @@ async def create_price(
|
|
139 |
except Exception as e:
|
140 |
logger.error(f"❌ Error creating/updating price: {e}")
|
141 |
raise HTTPException(status_code=500, detail="Error creating/updating price.")
|
142 |
-
|
143 |
-
@router.post("/webhook")
|
144 |
-
async def stripe_webhook(request: Request):
|
145 |
-
try:
|
146 |
-
payload = await request.json()
|
147 |
-
|
148 |
-
event_type = payload.get("type")
|
149 |
-
if event_type == "invoice.payment_succeeded":
|
150 |
-
invoice = payload.get("data", {}).get("object", {})
|
151 |
-
amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
|
152 |
-
|
153 |
-
# Cálculo de divisão
|
154 |
-
stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
|
155 |
-
platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
|
156 |
-
|
157 |
-
# Logando os valores
|
158 |
-
logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
|
159 |
-
logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
|
160 |
-
logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
|
161 |
-
|
162 |
-
return {"status": "success"}
|
163 |
-
|
164 |
-
except Exception as e:
|
165 |
-
logger.error(f"❌ Erro no webhook: {str(e)}")
|
166 |
-
return {"status": "error", "message": str(e)}
|
167 |
|
168 |
@router.post("/create_checkout_session")
|
169 |
def create_checkout_session(
|
|
|
60 |
else:
|
61 |
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
62 |
|
63 |
+
@router.post("/webhook")
|
64 |
+
async def stripe_webhook(request: Request):
|
65 |
+
try:
|
66 |
+
payload = await request.json()
|
67 |
+
|
68 |
+
event_type = payload.get("type")
|
69 |
+
if event_type == "invoice.payment_succeeded":
|
70 |
+
invoice = payload.get("data", {}).get("object", {})
|
71 |
+
amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
|
72 |
+
|
73 |
+
# Recuperar metadata do checkout
|
74 |
+
metadata = invoice.get("lines", {}).get("data", [])[0].get("metadata", {})
|
75 |
+
stylist_id = metadata.get("stylist_id", "N/A") # ID do estilista
|
76 |
+
user_id = metadata.get("user_id", "N/A") # ID do usuário
|
77 |
+
|
78 |
+
# Cálculo da divisão
|
79 |
+
stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
|
80 |
+
platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
|
81 |
+
|
82 |
+
# Logando os valores
|
83 |
+
logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
|
84 |
+
logger.info(f"👤 Stylist ID: {stylist_id}")
|
85 |
+
logger.info(f"👥 User ID: {user_id}")
|
86 |
+
logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
|
87 |
+
logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
|
88 |
+
|
89 |
+
return {"status": "success"}
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
logger.error(f"❌ Erro no webhook: {str(e)}")
|
93 |
+
return {"status": "error", "message": str(e)}
|
94 |
+
|
95 |
@router.post("/create_price")
|
96 |
async def create_price(
|
97 |
data: CreatePriceRequest,
|
|
|
171 |
except Exception as e:
|
172 |
logger.error(f"❌ Error creating/updating price: {e}")
|
173 |
raise HTTPException(status_code=500, detail="Error creating/updating price.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
@router.post("/create_checkout_session")
|
176 |
def create_checkout_session(
|