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

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +25 -23
routes/subscription.py CHANGED
@@ -85,46 +85,48 @@ def create_checkout_session(data: SubscriptionRequest):
85
  logger.error(f"Error creating checkout session: {e}")
86
  raise HTTPException(status_code=500, detail="Error creating checkout session.")
87
 
88
- ### 3. WEBHOOK PARA PROCESSAR PAGAMENTOS ###
89
  @router.post("/webhook")
90
  async def stripe_webhook(request: Request):
91
  payload = await request.body()
92
  headers = dict(request.headers)
93
  event = None
 
94
  try:
95
  event = stripe.Event.construct_from(
96
  json.loads(payload), stripe.api_key
97
  )
98
  except Exception as e:
99
- logger.error(f"⚠️ Erro ao processar webhook: {e}")
100
  raise HTTPException(status_code=400, detail="Webhook error")
101
 
102
- logger.info(f"🔹 Evento recebido: {event['type']}")
103
-
104
  if event["type"] == "invoice.payment_succeeded":
105
  invoice = event["data"]["object"]
106
- estilista_id = invoice.get("metadata", {}).get("estilista_id")
 
107
  amount = invoice["amount_paid"]
108
- estilista_share = int(amount * 0.8) # 80% para o estilista
109
- platform_share = amount - estilista_share # 20% para a plataforma
110
 
111
- logger.info(f"Pagamento confirmado: {amount / 100:.2f} BRL")
112
- logger.info(f"Estilista {estilista_id} receberá: {estilista_share / 100:.2f} BRL")
113
- logger.info(f"Plataforma receberá: {platform_share / 100:.2f} BRL")
114
 
115
- # Transferir os fundos automaticamente
116
- try:
117
- transfer = stripe.Transfer.create(
118
- amount=estilista_share,
119
- currency="brl",
120
- destination=estilista_id, # Conta conectada do estilista
121
- description="Pagamento de assinatura"
122
- )
123
- logger.info(f"Transferência bem-sucedida: {transfer.id}")
124
- except Exception as e:
125
- logger.error(f"Erro na transferência: {e}")
126
-
127
- return {"status": "Webhook processado com sucesso!"}
 
 
 
128
 
129
  ### 4. CANCELAR ASSINATURA ###
130
  @router.post("/cancel_subscription")
 
85
  logger.error(f"Error creating checkout session: {e}")
86
  raise HTTPException(status_code=500, detail="Error creating checkout session.")
87
 
88
+ ### 3. WEBHOOK TO PROCESS PAYMENTS ###
89
  @router.post("/webhook")
90
  async def stripe_webhook(request: Request):
91
  payload = await request.body()
92
  headers = dict(request.headers)
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")
102
 
103
+ logger.info(f"🔹 Event received: {event['type']}")
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
 
114
+ logger.info(f"✅ Payment confirmed: {amount / 100:.2f} BRL")
115
+ logger.info(f"👤 User ID: {user_id}")
116
+ logger.info(f"✂️ Stylist ID: {stylist_id}")
117
+ logger.info(f"💰 Stylist will receive: {stylist_share / 100:.2f} BRL")
118
+ logger.info(f"🏢 Platform will receive: {platform_share / 100:.2f} BRL")
119
+
120
+ return {
121
+ "status": "Payment processed successfully!",
122
+ "user_id": user_id,
123
+ "stylist_id": stylist_id,
124
+ "total_paid": amount / 100, # Convert cents to real currency
125
+ "stylist_share": stylist_share / 100,
126
+ "platform_share": platform_share / 100,
127
+ }
128
+
129
+ return {"status": "Event received, no action needed."}
130
 
131
  ### 4. CANCELAR ASSINATURA ###
132
  @router.post("/cancel_subscription")