Spaces:
Runtime error
Runtime error
added renewal to webhook as well as unsubscribe
Browse files
main.py
CHANGED
|
@@ -186,9 +186,10 @@ async def stripe_webhook(request: Request):
|
|
| 186 |
except stripe.error.SignatureVerificationError:
|
| 187 |
return JSONResponse(status_code=400, content={"error": "Invalid signature"})
|
| 188 |
|
|
|
|
| 189 |
if event["type"] == "checkout.session.completed":
|
| 190 |
session = event["data"]["object"]
|
| 191 |
-
user_id = session.get("client_reference_id") #
|
| 192 |
subscription_id = session.get("subscription")
|
| 193 |
|
| 194 |
try:
|
|
@@ -198,15 +199,54 @@ async def stripe_webhook(request: Request):
|
|
| 198 |
|
| 199 |
if plan and user_id and supabase:
|
| 200 |
print(f"Updating plan to {plan} for Supabase user {user_id}")
|
| 201 |
-
supabase.rpc("update_user_plan", {
|
| 202 |
"uid": user_id,
|
| 203 |
"new_plan": plan
|
| 204 |
}).execute()
|
| 205 |
-
|
| 206 |
print("β
Supabase update response:", response)
|
| 207 |
|
| 208 |
except Exception as e:
|
| 209 |
-
print("Webhook error while updating Supabase:", str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
return {"status": "success"}
|
| 212 |
|
|
|
|
| 186 |
except stripe.error.SignatureVerificationError:
|
| 187 |
return JSONResponse(status_code=400, content={"error": "Invalid signature"})
|
| 188 |
|
| 189 |
+
# β
First-time checkout completed
|
| 190 |
if event["type"] == "checkout.session.completed":
|
| 191 |
session = event["data"]["object"]
|
| 192 |
+
user_id = session.get("client_reference_id") # Supabase user.id
|
| 193 |
subscription_id = session.get("subscription")
|
| 194 |
|
| 195 |
try:
|
|
|
|
| 199 |
|
| 200 |
if plan and user_id and supabase:
|
| 201 |
print(f"Updating plan to {plan} for Supabase user {user_id}")
|
| 202 |
+
response = supabase.rpc("update_user_plan", {
|
| 203 |
"uid": user_id,
|
| 204 |
"new_plan": plan
|
| 205 |
}).execute()
|
|
|
|
| 206 |
print("β
Supabase update response:", response)
|
| 207 |
|
| 208 |
except Exception as e:
|
| 209 |
+
print("Webhook error while updating Supabase (checkout):", str(e))
|
| 210 |
+
|
| 211 |
+
# β
Subscription renewals (monthly/annual billing)
|
| 212 |
+
elif event["type"] == "invoice.payment_succeeded":
|
| 213 |
+
invoice = event["data"]["object"]
|
| 214 |
+
subscription_id = invoice.get("subscription")
|
| 215 |
+
|
| 216 |
+
try:
|
| 217 |
+
subscription = stripe.Subscription.retrieve(subscription_id)
|
| 218 |
+
user_id = subscription.get("metadata", {}).get("user_id") \
|
| 219 |
+
or subscription.get("client_reference_id")
|
| 220 |
+
price_id = subscription["items"]["data"][0]["price"]["id"]
|
| 221 |
+
plan = next((k for k, v in PRICE_MAP.items() if v == price_id), None)
|
| 222 |
+
|
| 223 |
+
if plan and user_id and supabase:
|
| 224 |
+
print(f"Renewal: Updating plan to {plan} for Supabase user {user_id}")
|
| 225 |
+
response = supabase.rpc("update_user_plan", {
|
| 226 |
+
"uid": user_id,
|
| 227 |
+
"new_plan": plan
|
| 228 |
+
}).execute()
|
| 229 |
+
print("β
Supabase update response (renewal):", response)
|
| 230 |
+
|
| 231 |
+
except Exception as e:
|
| 232 |
+
print("Webhook error while updating Supabase (renewal):", str(e))
|
| 233 |
+
|
| 234 |
+
# β
Subscription cancelled/expired
|
| 235 |
+
elif event["type"] == "customer.subscription.deleted":
|
| 236 |
+
subscription = event["data"]["object"]
|
| 237 |
+
user_id = subscription.get("metadata", {}).get("user_id")
|
| 238 |
+
|
| 239 |
+
try:
|
| 240 |
+
if user_id and supabase:
|
| 241 |
+
print(f"Downgrading Supabase user {user_id} to free plan")
|
| 242 |
+
response = supabase.rpc("update_user_plan", {
|
| 243 |
+
"uid": user_id,
|
| 244 |
+
"new_plan": "free"
|
| 245 |
+
}).execute()
|
| 246 |
+
print("β
Supabase update response (canceled):", response)
|
| 247 |
+
|
| 248 |
+
except Exception as e:
|
| 249 |
+
print("Webhook error while downgrading Supabase:", str(e))
|
| 250 |
|
| 251 |
return {"status": "success"}
|
| 252 |
|