habulaj commited on
Commit
deff01c
·
verified ·
1 Parent(s): b3116aa

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +3 -34
routes/subscription.py CHANGED
@@ -69,14 +69,7 @@ async def stripe_webhook(request: Request):
69
  if event_type == "invoice.payment_succeeded":
70
  invoice = payload.get("data", {}).get("object", {})
71
 
72
- # 🔹 Capturar a cobrança associada
73
- charge_id = invoice.get("charge") # Transação de origem (Obrigatório no Brasil)
74
-
75
- if not charge_id:
76
- logger.error("❌ Nenhuma transação de origem encontrada. Não é possível transferir.")
77
- return {"status": "error", "message": "Nenhuma transação de origem encontrada"}
78
-
79
- # 🔹 Capturando valores da fatura
80
  amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
81
  currency = invoice.get("currency", "usd") # Moeda do pagamento
82
 
@@ -96,35 +89,12 @@ async def stripe_webhook(request: Request):
96
  stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
97
  platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
98
 
99
- # 🔹 Logando as informações
100
  logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
101
  logger.info(f"👤 Stylist ID: {stylist_id} | Stripe ID: {stylist_stripe_id}")
102
  logger.info(f"👥 User ID: {user_id} | Stripe ID: {user_stripe_id}")
103
  logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
104
  logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
105
- logger.info(f"💳 Charge ID: {charge_id}") # 🔹 Log da transação de origem
106
-
107
- # 🔹 Transferindo 80% para o estilista via Stripe Connect
108
- try:
109
- transfer = stripe.Transfer.create(
110
- amount=stylist_amount, # Valor em centavos
111
- currency=currency,
112
- destination=stylist_stripe_id, # Conta do estilista no Stripe Connect
113
- source_transaction=charge_id, # 🔹 Transação de origem (obrigatória no Brasil)
114
- description=f"Pagamento para Stylist ID: {stylist_id}",
115
- metadata={
116
- "stylist_id": stylist_id,
117
- "user_id": user_id,
118
- "total_paid": amount_paid / 100,
119
- "stylist_amount": stylist_amount / 100,
120
- "platform_amount": platform_amount / 100
121
- }
122
- )
123
-
124
- logger.info(f"✅ Transferência realizada! ID da Transferência: {transfer.id}")
125
- except Exception as e:
126
- logger.error(f"❌ Erro na transferência: {str(e)}")
127
- return {"status": "error", "message": "Erro ao transferir pagamento ao estilista"}
128
 
129
  return {
130
  "status": "success",
@@ -134,8 +104,7 @@ async def stripe_webhook(request: Request):
134
  "user_id": user_id,
135
  "user_stripe_id": user_stripe_id,
136
  "stylist_amount": stylist_amount / 100,
137
- "platform_amount": platform_amount / 100,
138
- "transfer_id": transfer.id # Retorna o ID da transferência
139
  }
140
 
141
  except Exception as e:
 
69
  if event_type == "invoice.payment_succeeded":
70
  invoice = payload.get("data", {}).get("object", {})
71
 
72
+ # 🔹 Capturar valores da fatura
 
 
 
 
 
 
 
73
  amount_paid = invoice.get("amount_paid", 0) # Valor total pago (em centavos)
74
  currency = invoice.get("currency", "usd") # Moeda do pagamento
75
 
 
89
  stylist_amount = int(amount_paid * 0.8) # 80% para o estilista
90
  platform_amount = int(amount_paid * 0.2) # 20% para a plataforma
91
 
92
+ # 🔹 Logando as informações detalhadas
93
  logger.info(f"✅ Pagamento bem-sucedido! Valor total: R$ {amount_paid / 100:.2f}")
94
  logger.info(f"👤 Stylist ID: {stylist_id} | Stripe ID: {stylist_stripe_id}")
95
  logger.info(f"👥 User ID: {user_id} | Stripe ID: {user_stripe_id}")
96
  logger.info(f"💰 Estilista recebe: R$ {stylist_amount / 100:.2f}")
97
  logger.info(f"🏛️ Plataforma fica com: R$ {platform_amount / 100:.2f}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  return {
100
  "status": "success",
 
104
  "user_id": user_id,
105
  "user_stripe_id": user_stripe_id,
106
  "stylist_amount": stylist_amount / 100,
107
+ "platform_amount": platform_amount / 100
 
108
  }
109
 
110
  except Exception as e: