Update routes/subscription.py
Browse files- routes/subscription.py +37 -33
routes/subscription.py
CHANGED
@@ -19,7 +19,7 @@ class PriceRequest(BaseModel):
|
|
19 |
|
20 |
class SubscriptionRequest(BaseModel):
|
21 |
user_id: str
|
22 |
-
stylist_id: str
|
23 |
price_id: str # Now using a pre-created price
|
24 |
|
25 |
class CancelSubscriptionRequest(BaseModel):
|
@@ -88,7 +88,7 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
88 |
logger.error(f"Error creating checkout session: {e}")
|
89 |
raise HTTPException(status_code=500, detail="Error creating checkout session.")
|
90 |
|
91 |
-
### **3️⃣ WEBHOOK TO PROCESS PAYMENTS** ###
|
92 |
@router.post("/webhook")
|
93 |
async def stripe_webhook(request: Request):
|
94 |
payload = await request.body()
|
@@ -104,39 +104,43 @@ async def stripe_webhook(request: Request):
|
|
104 |
logger.info(f"🔹 Event received: {event['type']}")
|
105 |
|
106 |
if event["type"] == "invoice.payment_succeeded":
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
118 |
|
119 |
-
|
120 |
-
try:
|
121 |
-
transfer = stripe.Transfer.create(
|
122 |
-
amount=stylist_share, # Valor em CENTAVOS
|
123 |
-
currency="usd",
|
124 |
-
destination=stylist_id, # Conta Stripe do estilista (Connected Account ID)
|
125 |
-
description=f"Payment for stylist {stylist_id} - Subscription",
|
126 |
-
)
|
127 |
-
logger.info(f"💸 Transferência realizada: {stylist_share / 100:.2f} USD para {stylist_id}")
|
128 |
-
except Exception as e:
|
129 |
-
logger.error(f"🚨 Erro ao transferir para o estilista {stylist_id}: {e}")
|
130 |
-
|
131 |
-
return {
|
132 |
-
"status": "Payment processed and transferred successfully!",
|
133 |
-
"user_id": user_id,
|
134 |
-
"stylist_id": stylist_id,
|
135 |
-
"total_paid": amount / 100,
|
136 |
-
"stylist_share": stylist_share / 100,
|
137 |
-
"platform_share": platform_share / 100,
|
138 |
-
"transfer_status": "Completed" if "transfer" in locals() else "Failed"
|
139 |
-
}
|
140 |
|
141 |
### **4️⃣ CANCEL SUBSCRIPTION** ###
|
142 |
@router.post("/cancel_subscription")
|
|
|
19 |
|
20 |
class SubscriptionRequest(BaseModel):
|
21 |
user_id: str
|
22 |
+
stylist_id: str # This must be the Stripe connected account ID (acct_xxxxx)
|
23 |
price_id: str # Now using a pre-created price
|
24 |
|
25 |
class CancelSubscriptionRequest(BaseModel):
|
|
|
88 |
logger.error(f"Error creating checkout session: {e}")
|
89 |
raise HTTPException(status_code=500, detail="Error creating checkout session.")
|
90 |
|
91 |
+
### **3️⃣ WEBHOOK TO PROCESS PAYMENTS & TRANSFER FUNDS** ###
|
92 |
@router.post("/webhook")
|
93 |
async def stripe_webhook(request: Request):
|
94 |
payload = await request.body()
|
|
|
104 |
logger.info(f"🔹 Event received: {event['type']}")
|
105 |
|
106 |
if event["type"] == "invoice.payment_succeeded":
|
107 |
+
invoice = event["data"]["object"]
|
108 |
+
subscription_id = invoice.get("subscription") # Get subscription ID
|
109 |
+
|
110 |
+
# Retrieve subscription to get metadata
|
111 |
+
subscription = stripe.Subscription.retrieve(subscription_id)
|
112 |
+
user_id = subscription.metadata.get("user_id")
|
113 |
+
stylist_id = subscription.metadata.get("stylist_id") # Must be a Stripe connected account ID
|
114 |
+
|
115 |
+
amount = invoice["amount_paid"]
|
116 |
+
stylist_share = int(amount * 0.8) # 80% for stylist
|
117 |
+
platform_share = amount - stylist_share # 20% for platform
|
118 |
+
|
119 |
+
# 🔹 Transfer the funds to the stylist
|
120 |
+
try:
|
121 |
+
transfer = stripe.Transfer.create(
|
122 |
+
amount=stylist_share, # Value in CENTS
|
123 |
+
currency="usd",
|
124 |
+
destination=stylist_id, # The Stripe Connected Account ID (acct_xxxx)
|
125 |
+
description=f"Payment for stylist {stylist_id} - Subscription",
|
126 |
+
)
|
127 |
+
logger.info(f"💸 Transfer successful: ${stylist_share / 100:.2f} USD to {stylist_id}")
|
128 |
+
transfer_status = "Completed"
|
129 |
+
except Exception as e:
|
130 |
+
logger.error(f"🚨 Transfer error for stylist {stylist_id}: {e}")
|
131 |
+
transfer_status = "Failed"
|
132 |
|
133 |
+
return {
|
134 |
+
"status": "Payment processed successfully!",
|
135 |
+
"user_id": user_id,
|
136 |
+
"stylist_id": stylist_id,
|
137 |
+
"total_paid": amount / 100, # Convert cents to dollars
|
138 |
+
"stylist_share": stylist_share / 100,
|
139 |
+
"platform_share": platform_share / 100,
|
140 |
+
"transfer_status": transfer_status
|
141 |
+
}
|
142 |
|
143 |
+
return {"status": "Event received, no action needed."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
### **4️⃣ CANCEL SUBSCRIPTION** ###
|
146 |
@router.post("/cancel_subscription")
|