Update routes/subscription.py
Browse files- routes/subscription.py +13 -10
routes/subscription.py
CHANGED
@@ -46,7 +46,7 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
46 |
stylist_price = stylist["price"]
|
47 |
stylist_avatar = stylist["avatar"]
|
48 |
consultations = stylist["consultations"]
|
49 |
-
stylist_stripe_id = stylist["stripe_id"]
|
50 |
|
51 |
if not stylist_price or not consultations or not stylist_stripe_id:
|
52 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
@@ -74,7 +74,8 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
74 |
],
|
75 |
subscription_data={
|
76 |
"metadata": {
|
77 |
-
"stylist_id": stylist_stripe_id, # Agora
|
|
|
78 |
"consultations_per_month": consultations
|
79 |
}
|
80 |
}
|
@@ -89,7 +90,7 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
89 |
logger.error(f"Error creating checkout session: {e}")
|
90 |
raise HTTPException(status_code=500, detail="Error creating checkout session.")
|
91 |
|
92 |
-
### **WEBHOOK
|
93 |
@router.post("/webhook")
|
94 |
async def stripe_webhook(request: Request):
|
95 |
payload = await request.body()
|
@@ -109,14 +110,15 @@ async def stripe_webhook(request: Request):
|
|
109 |
subscription_id = invoice.get("subscription")
|
110 |
|
111 |
subscription = stripe.Subscription.retrieve(subscription_id)
|
112 |
-
user_id = subscription.metadata.get("user_id")
|
113 |
-
stylist_id = subscription.metadata.get("stylist_id")
|
|
|
114 |
|
115 |
amount = invoice["amount_paid"]
|
116 |
-
stylist_share = int(amount * 0.8) # 80%
|
117 |
-
platform_share = amount - stylist_share # 20%
|
118 |
|
119 |
-
# 🔹
|
120 |
try:
|
121 |
transfer = stripe.Transfer.create(
|
122 |
amount=stylist_share,
|
@@ -132,8 +134,9 @@ async def stripe_webhook(request: Request):
|
|
132 |
|
133 |
return {
|
134 |
"status": "Payment processed successfully!",
|
135 |
-
"user_id": user_id,
|
136 |
"stylist_id": stylist_id,
|
|
|
137 |
"total_paid": amount / 100,
|
138 |
"stylist_share": stylist_share / 100,
|
139 |
"platform_share": platform_share / 100,
|
@@ -142,7 +145,7 @@ async def stripe_webhook(request: Request):
|
|
142 |
|
143 |
return {"status": "Event received, no action needed."}
|
144 |
|
145 |
-
### **
|
146 |
class CancelSubscriptionRequest(BaseModel):
|
147 |
subscription_id: str
|
148 |
|
|
|
46 |
stylist_price = stylist["price"]
|
47 |
stylist_avatar = stylist["avatar"]
|
48 |
consultations = stylist["consultations"]
|
49 |
+
stylist_stripe_id = stylist["stripe_id"]
|
50 |
|
51 |
if not stylist_price or not consultations or not stylist_stripe_id:
|
52 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
|
|
74 |
],
|
75 |
subscription_data={
|
76 |
"metadata": {
|
77 |
+
"stylist_id": stylist_stripe_id, # Agora enviando o ID do Stripe correto
|
78 |
+
"user_id": data.user_id, # ID do cliente que está assinando
|
79 |
"consultations_per_month": consultations
|
80 |
}
|
81 |
}
|
|
|
90 |
logger.error(f"Error creating checkout session: {e}")
|
91 |
raise HTTPException(status_code=500, detail="Error creating checkout session.")
|
92 |
|
93 |
+
### **WEBHOOK PARA PROCESSAR PAGAMENTOS**
|
94 |
@router.post("/webhook")
|
95 |
async def stripe_webhook(request: Request):
|
96 |
payload = await request.body()
|
|
|
110 |
subscription_id = invoice.get("subscription")
|
111 |
|
112 |
subscription = stripe.Subscription.retrieve(subscription_id)
|
113 |
+
user_id = subscription.metadata.get("user_id") # ID do cliente
|
114 |
+
stylist_id = subscription.metadata.get("stylist_id") # ID do estilista no Stripe
|
115 |
+
consultations_per_month = subscription.metadata.get("consultations_per_month")
|
116 |
|
117 |
amount = invoice["amount_paid"]
|
118 |
+
stylist_share = int(amount * 0.8) # 80% para o estilista
|
119 |
+
platform_share = amount - stylist_share # 20% para a plataforma
|
120 |
|
121 |
+
# 🔹 Transferência do pagamento para o estilista
|
122 |
try:
|
123 |
transfer = stripe.Transfer.create(
|
124 |
amount=stylist_share,
|
|
|
134 |
|
135 |
return {
|
136 |
"status": "Payment processed successfully!",
|
137 |
+
"user_id": user_id, # Retornando o ID do usuário que assinou
|
138 |
"stylist_id": stylist_id,
|
139 |
+
"consultations_per_month": consultations_per_month,
|
140 |
"total_paid": amount / 100,
|
141 |
"stylist_share": stylist_share / 100,
|
142 |
"platform_share": platform_share / 100,
|
|
|
145 |
|
146 |
return {"status": "Event received, no action needed."}
|
147 |
|
148 |
+
### **CANCELAMENTO DE ASSINATURA**
|
149 |
class CancelSubscriptionRequest(BaseModel):
|
150 |
subscription_id: str
|
151 |
|