habulaj commited on
Commit
ab80e0b
·
verified ·
1 Parent(s): c1043bc

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +14 -10
routes/subscription.py CHANGED
@@ -71,10 +71,12 @@ def create_checkout_session(data: SubscriptionRequest):
71
  "quantity": 1
72
  }
73
  ],
74
- metadata={
75
- "user_id": data.user_id,
76
- "stylist_id": data.stylist_id,
77
- },
 
 
78
  )
79
 
80
  return {
@@ -93,9 +95,7 @@ async def stripe_webhook(request: Request):
93
  event = None
94
 
95
  try:
96
- event = stripe.Event.construct_from(
97
- json.loads(payload), stripe.api_key
98
- )
99
  except Exception as e:
100
  logger.error(f"⚠️ Error processing webhook: {e}")
101
  raise HTTPException(status_code=400, detail="Webhook error")
@@ -104,10 +104,14 @@ async def stripe_webhook(request: Request):
104
 
105
  if event["type"] == "invoice.payment_succeeded":
106
  invoice = event["data"]["object"]
107
- user_id = invoice.get("metadata", {}).get("user_id")
108
- stylist_id = invoice.get("metadata", {}).get("stylist_id")
 
 
 
 
 
109
  amount = invoice["amount_paid"]
110
-
111
  stylist_share = int(amount * 0.8) # 80% for the stylist
112
  platform_share = amount - stylist_share # 20% for the platform
113
 
 
71
  "quantity": 1
72
  }
73
  ],
74
+ subscription_data={
75
+ "metadata": {
76
+ "user_id": data.user_id,
77
+ "stylist_id": data.stylist_id,
78
+ }
79
+ }
80
  )
81
 
82
  return {
 
95
  event = None
96
 
97
  try:
98
+ event = stripe.Event.construct_from(json.loads(payload), stripe.api_key)
 
 
99
  except Exception as e:
100
  logger.error(f"⚠️ Error processing webhook: {e}")
101
  raise HTTPException(status_code=400, detail="Webhook error")
 
104
 
105
  if event["type"] == "invoice.payment_succeeded":
106
  invoice = event["data"]["object"]
107
+ subscription_id = invoice.get("subscription") # Get subscription ID
108
+
109
+ # Retrieve subscription to get metadata
110
+ subscription = stripe.Subscription.retrieve(subscription_id)
111
+ user_id = subscription.metadata.get("user_id")
112
+ stylist_id = subscription.metadata.get("stylist_id")
113
+
114
  amount = invoice["amount_paid"]
 
115
  stylist_share = int(amount * 0.8) # 80% for the stylist
116
  platform_share = amount - stylist_share # 20% for the platform
117