Update routes/subscription.py
Browse files- routes/subscription.py +25 -23
routes/subscription.py
CHANGED
@@ -85,46 +85,48 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
85 |
logger.error(f"Error creating checkout session: {e}")
|
86 |
raise HTTPException(status_code=500, detail="Error creating checkout session.")
|
87 |
|
88 |
-
### 3. WEBHOOK
|
89 |
@router.post("/webhook")
|
90 |
async def stripe_webhook(request: Request):
|
91 |
payload = await request.body()
|
92 |
headers = dict(request.headers)
|
93 |
event = None
|
|
|
94 |
try:
|
95 |
event = stripe.Event.construct_from(
|
96 |
json.loads(payload), stripe.api_key
|
97 |
)
|
98 |
except Exception as e:
|
99 |
-
logger.error(f"⚠️
|
100 |
raise HTTPException(status_code=400, detail="Webhook error")
|
101 |
|
102 |
-
logger.info(f"🔹
|
103 |
-
|
104 |
if event["type"] == "invoice.payment_succeeded":
|
105 |
invoice = event["data"]["object"]
|
106 |
-
|
|
|
107 |
amount = invoice["amount_paid"]
|
108 |
-
estilista_share = int(amount * 0.8) # 80% para o estilista
|
109 |
-
platform_share = amount - estilista_share # 20% para a plataforma
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
logger.info(f"Plataforma receberá: {platform_share / 100:.2f} BRL")
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
128 |
|
129 |
### 4. CANCELAR ASSINATURA ###
|
130 |
@router.post("/cancel_subscription")
|
|
|
85 |
logger.error(f"Error creating checkout session: {e}")
|
86 |
raise HTTPException(status_code=500, detail="Error creating checkout session.")
|
87 |
|
88 |
+
### 3. WEBHOOK TO PROCESS PAYMENTS ###
|
89 |
@router.post("/webhook")
|
90 |
async def stripe_webhook(request: Request):
|
91 |
payload = await request.body()
|
92 |
headers = dict(request.headers)
|
93 |
event = None
|
94 |
+
|
95 |
try:
|
96 |
event = stripe.Event.construct_from(
|
97 |
json.loads(payload), stripe.api_key
|
98 |
)
|
99 |
except Exception as e:
|
100 |
+
logger.error(f"⚠️ Error processing webhook: {e}")
|
101 |
raise HTTPException(status_code=400, detail="Webhook error")
|
102 |
|
103 |
+
logger.info(f"🔹 Event received: {event['type']}")
|
104 |
+
|
105 |
if event["type"] == "invoice.payment_succeeded":
|
106 |
invoice = event["data"]["object"]
|
107 |
+
user_id = invoice.get("metadata", {}).get("user_id")
|
108 |
+
stylist_id = invoice.get("metadata", {}).get("stylist_id")
|
109 |
amount = invoice["amount_paid"]
|
|
|
|
|
110 |
|
111 |
+
stylist_share = int(amount * 0.8) # 80% for the stylist
|
112 |
+
platform_share = amount - stylist_share # 20% for the platform
|
|
|
113 |
|
114 |
+
logger.info(f"✅ Payment confirmed: {amount / 100:.2f} BRL")
|
115 |
+
logger.info(f"👤 User ID: {user_id}")
|
116 |
+
logger.info(f"✂️ Stylist ID: {stylist_id}")
|
117 |
+
logger.info(f"💰 Stylist will receive: {stylist_share / 100:.2f} BRL")
|
118 |
+
logger.info(f"🏢 Platform will receive: {platform_share / 100:.2f} BRL")
|
119 |
+
|
120 |
+
return {
|
121 |
+
"status": "Payment processed successfully!",
|
122 |
+
"user_id": user_id,
|
123 |
+
"stylist_id": stylist_id,
|
124 |
+
"total_paid": amount / 100, # Convert cents to real currency
|
125 |
+
"stylist_share": stylist_share / 100,
|
126 |
+
"platform_share": platform_share / 100,
|
127 |
+
}
|
128 |
+
|
129 |
+
return {"status": "Event received, no action needed."}
|
130 |
|
131 |
### 4. CANCELAR ASSINATURA ###
|
132 |
@router.post("/cancel_subscription")
|