Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -704,10 +704,16 @@ def healthz():
|
|
| 704 |
async def stripe_webhook(request: Request):
|
| 705 |
if not STRIPE_WEBHOOK_SECRET:
|
| 706 |
raise HTTPException(status_code=400, detail="webhook_not_configured")
|
|
|
|
| 707 |
payload = await request.body()
|
| 708 |
sig = request.headers.get("stripe-signature", "")
|
|
|
|
| 709 |
try:
|
| 710 |
-
event = stripe.Webhook.construct_event(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
except Exception as e:
|
| 712 |
raise HTTPException(status_code=400, detail=f"bad_signature:{e}")
|
| 713 |
|
|
@@ -727,46 +733,70 @@ async def stripe_webhook(request: Request):
|
|
| 727 |
conn = db()
|
| 728 |
cur = conn.cursor()
|
| 729 |
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
if customer:
|
| 736 |
-
cur.execute("UPDATE users SET stripe_customer_id=? WHERE user_id=?", (customer, uid))
|
| 737 |
-
|
| 738 |
-
if kind == "pro":
|
| 739 |
-
cur.execute("UPDATE users SET tier='pro', subscription_status='active' WHERE user_id=?", (uid,))
|
| 740 |
-
elif kind == "enterprise":
|
| 741 |
-
cur.execute("UPDATE users SET tier='enterprise', subscription_status='active' WHERE user_id=?", (uid,))
|
| 742 |
-
elif kind in CREDIT_PACKS:
|
| 743 |
-
amt = CREDIT_PACKS[kind]["credits"]
|
| 744 |
-
cur.execute("UPDATE users SET credits=credits+? WHERE user_id=?", (amt, uid))
|
| 745 |
|
| 746 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
|
| 748 |
-
elif etype in ("customer.subscription.created", "subscription.updated"):
|
| 749 |
customer = data.get("customer")
|
| 750 |
status = data.get("status", "unknown")
|
| 751 |
-
if customer:
|
| 752 |
-
# If subscription active/trialing -> keep enterprise if already, else pro. If not active -> free.
|
| 753 |
-
if status in ("active", "trialing"):
|
| 754 |
-
cur.execute("SELECT tier FROM users WHERE stripe_customer_id=?", (customer,))
|
| 755 |
-
row = cur.fetchone()
|
| 756 |
-
current = (row["tier"] if row else "free")
|
| 757 |
-
new_tier = "enterprise" if current == "enterprise" else "pro"
|
| 758 |
-
else:
|
| 759 |
-
new_tier = "free"
|
| 760 |
-
cur.execute("UPDATE users SET tier=?, subscription_status=? WHERE stripe_customer_id=?", (new_tier, status, customer))
|
| 761 |
-
conn.commit()
|
| 762 |
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 768 |
|
| 769 |
-
conn.close()
|
| 770 |
return JSONResponse({"received": True, "type": etype})
|
| 771 |
|
| 772 |
app = mount_gradio_app(app, demo, path="/")
|
|
|
|
| 704 |
async def stripe_webhook(request: Request):
|
| 705 |
if not STRIPE_WEBHOOK_SECRET:
|
| 706 |
raise HTTPException(status_code=400, detail="webhook_not_configured")
|
| 707 |
+
|
| 708 |
payload = await request.body()
|
| 709 |
sig = request.headers.get("stripe-signature", "")
|
| 710 |
+
|
| 711 |
try:
|
| 712 |
+
event = stripe.Webhook.construct_event(
|
| 713 |
+
payload=payload,
|
| 714 |
+
sig_header=sig,
|
| 715 |
+
secret=STRIPE_WEBHOOK_SECRET,
|
| 716 |
+
)
|
| 717 |
except Exception as e:
|
| 718 |
raise HTTPException(status_code=400, detail=f"bad_signature:{e}")
|
| 719 |
|
|
|
|
| 733 |
conn = db()
|
| 734 |
cur = conn.cursor()
|
| 735 |
|
| 736 |
+
try:
|
| 737 |
+
if etype == "checkout.session.completed":
|
| 738 |
+
uid = get_uid(data)
|
| 739 |
+
kind = (data.get("metadata") or {}).get("kind", "")
|
| 740 |
+
customer = data.get("customer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 741 |
|
| 742 |
+
if uid:
|
| 743 |
+
if customer:
|
| 744 |
+
cur.execute(
|
| 745 |
+
"UPDATE users SET stripe_customer_id=? WHERE user_id=?",
|
| 746 |
+
(customer, uid),
|
| 747 |
+
)
|
| 748 |
+
|
| 749 |
+
if kind == "pro":
|
| 750 |
+
cur.execute(
|
| 751 |
+
"UPDATE users SET tier='pro', subscription_status='active' WHERE user_id=?",
|
| 752 |
+
(uid,),
|
| 753 |
+
)
|
| 754 |
+
elif kind == "enterprise":
|
| 755 |
+
cur.execute(
|
| 756 |
+
"UPDATE users SET tier='enterprise', subscription_status='active' WHERE user_id=?",
|
| 757 |
+
(uid,),
|
| 758 |
+
)
|
| 759 |
+
elif kind in CREDIT_PACKS:
|
| 760 |
+
amt = CREDIT_PACKS[kind]["credits"]
|
| 761 |
+
cur.execute(
|
| 762 |
+
"UPDATE users SET credits=credits+? WHERE user_id=?",
|
| 763 |
+
(amt, uid),
|
| 764 |
+
)
|
| 765 |
+
|
| 766 |
+
conn.commit()
|
| 767 |
|
| 768 |
+
elif etype in ("customer.subscription.created", "customer.subscription.updated"):
|
| 769 |
customer = data.get("customer")
|
| 770 |
status = data.get("status", "unknown")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
|
| 772 |
+
if customer:
|
| 773 |
+
if status in ("active", "trialing"):
|
| 774 |
+
# keep enterprise if already, else pro
|
| 775 |
+
cur.execute("SELECT tier FROM users WHERE stripe_customer_id=?", (customer,))
|
| 776 |
+
row = cur.fetchone()
|
| 777 |
+
current = (row["tier"] if row else "free")
|
| 778 |
+
new_tier = "enterprise" if current == "enterprise" else "pro"
|
| 779 |
+
else:
|
| 780 |
+
new_tier = "free"
|
| 781 |
+
|
| 782 |
+
cur.execute(
|
| 783 |
+
"UPDATE users SET tier=?, subscription_status=? WHERE stripe_customer_id=?",
|
| 784 |
+
(new_tier, status, customer),
|
| 785 |
+
)
|
| 786 |
+
conn.commit()
|
| 787 |
+
|
| 788 |
+
elif etype == "customer.subscription.deleted":
|
| 789 |
+
customer = data.get("customer")
|
| 790 |
+
if customer:
|
| 791 |
+
cur.execute(
|
| 792 |
+
"UPDATE users SET tier='free', subscription_status='canceled' WHERE stripe_customer_id=?",
|
| 793 |
+
(customer,),
|
| 794 |
+
)
|
| 795 |
+
conn.commit()
|
| 796 |
+
|
| 797 |
+
finally:
|
| 798 |
+
conn.close()
|
| 799 |
|
|
|
|
| 800 |
return JSONResponse({"received": True, "type": etype})
|
| 801 |
|
| 802 |
app = mount_gradio_app(app, demo, path="/")
|