rairo commited on
Commit
601f04b
·
verified ·
1 Parent(s): 94559c6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -23
main.py CHANGED
@@ -1451,10 +1451,6 @@ def create_checkout_session():
1451
 
1452
  @app.route("/api/billing/webhook", methods=["POST"])
1453
  def stripe_webhook():
1454
- if not STRIPE_WEBHOOK_SECRET:
1455
- logger.error("[STRIPE] STRIPE_WEBHOOK_SECRET not set.")
1456
- return jsonify({"error": "Webhook secret not configured."}), 500
1457
-
1458
  payload = request.data
1459
  sig_header = request.headers.get("Stripe-Signature")
1460
 
@@ -1462,35 +1458,37 @@ def stripe_webhook():
1462
  event = stripe.Webhook.construct_event(
1463
  payload, sig_header, STRIPE_WEBHOOK_SECRET
1464
  )
1465
- except stripe.error.SignatureVerificationError as e:
1466
- logger.error(f"[STRIPE] Webhook signature verification failed: {e}")
1467
- return "Invalid signature", 400
1468
  except Exception as e:
1469
- logger.error(f"[STRIPE] Webhook parsing error: {e}")
1470
- return "Bad request", 400
1471
 
1472
  event_type = event.get("type")
1473
  obj = event.get("data", {}).get("object", {})
 
1474
 
1475
- # We only care about successful subscription payments
1476
  if event_type == "invoice.payment_succeeded":
1477
- try:
1478
- subscription_id = obj.get("subscription")
1479
- if not subscription_id:
1480
- return "", 200
1481
-
1482
  sub = stripe.Subscription.retrieve(subscription_id)
1483
- metadata = sub.get("metadata", {}) or {}
1484
- uid = metadata.get("firebase_uid")
1485
- plan = metadata.get("plan")
1486
-
1487
  if uid and plan:
1488
- logger.info(f"[STRIPE] invoice.payment_succeeded for user {uid}, plan {plan}")
1489
  apply_plan_credits(uid, plan)
1490
- except Exception as e:
1491
- logger.error(f"[STRIPE] Error handling invoice.payment_succeeded: {e}")
1492
 
1493
- # You can log other event types here if needed
 
 
 
 
 
 
 
 
 
1494
  return "", 200
1495
  # -----------------------------------------------------------------------------
1496
  # 7. MAIN EXECUTION
 
1451
 
1452
  @app.route("/api/billing/webhook", methods=["POST"])
1453
  def stripe_webhook():
 
 
 
 
1454
  payload = request.data
1455
  sig_header = request.headers.get("Stripe-Signature")
1456
 
 
1458
  event = stripe.Webhook.construct_event(
1459
  payload, sig_header, STRIPE_WEBHOOK_SECRET
1460
  )
 
 
 
1461
  except Exception as e:
1462
+ logger.error(f"[STRIPE] Webhook error: {e}")
1463
+ return "Bad", 400
1464
 
1465
  event_type = event.get("type")
1466
  obj = event.get("data", {}).get("object", {})
1467
+ logger.info(f"[STRIPE] Webhook event: {event_type}")
1468
 
1469
+ # Handle subscription billing
1470
  if event_type == "invoice.payment_succeeded":
1471
+ invoice = obj
1472
+ subscription_id = invoice.get("subscription")
1473
+ if subscription_id:
 
 
1474
  sub = stripe.Subscription.retrieve(subscription_id)
1475
+ md = sub.get("metadata", {}) or {}
1476
+ uid = md.get("firebase_uid")
1477
+ plan = md.get("plan")
1478
+ logger.info(f"[STRIPE] invoice.payment_succeeded uid={uid}, plan={plan}")
1479
  if uid and plan:
 
1480
  apply_plan_credits(uid, plan)
 
 
1481
 
1482
+ # Also handle checkout.session.completed as a fallback
1483
+ if event_type == "checkout.session.completed":
1484
+ session = obj
1485
+ md = session.get("metadata", {}) or {}
1486
+ uid = md.get("firebase_uid")
1487
+ plan = md.get("plan")
1488
+ logger.info(f"[STRIPE] checkout.session.completed uid={uid}, plan={plan}")
1489
+ if uid and plan:
1490
+ apply_plan_credits(uid, plan)
1491
+
1492
  return "", 200
1493
  # -----------------------------------------------------------------------------
1494
  # 7. MAIN EXECUTION