Update routes/subscription.py
Browse files- routes/subscription.py +25 -4
routes/subscription.py
CHANGED
@@ -69,9 +69,16 @@ async def stripe_webhook(request: Request):
|
|
69 |
if event_type == "invoice.payment_succeeded":
|
70 |
invoice = payload.get("data", {}).get("object", {})
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
# 🔹 Capturando 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 |
|
76 |
# 🔹 Buscando metadados
|
77 |
metadata = invoice.get("metadata", {})
|
@@ -95,6 +102,7 @@ async def stripe_webhook(request: Request):
|
|
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 |
# 🔹 Transferindo 80% para o estilista via Stripe Connect
|
100 |
try:
|
@@ -102,6 +110,7 @@ async def stripe_webhook(request: Request):
|
|
102 |
amount=stylist_amount, # Valor em centavos
|
103 |
currency=currency,
|
104 |
destination=stylist_stripe_id, # Conta do estilista no Stripe Connect
|
|
|
105 |
description=f"Pagamento para Stylist ID: {stylist_id}",
|
106 |
metadata={
|
107 |
"stylist_id": stylist_id,
|
@@ -263,13 +272,25 @@ def create_checkout_session(
|
|
263 |
cancel_url="https://yourdomain.com/cancel",
|
264 |
payment_method_types=["card"],
|
265 |
mode="subscription",
|
266 |
-
customer=
|
267 |
-
line_items=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
metadata={
|
269 |
"stylist_id": stylist_id,
|
270 |
"stylist_stripe_id": stylist_stripe_id,
|
271 |
"user_id": user_id,
|
272 |
-
"user_stripe_id":
|
273 |
},
|
274 |
subscription_data={
|
275 |
"metadata": {
|
|
|
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 |
|
83 |
# 🔹 Buscando metadados
|
84 |
metadata = invoice.get("metadata", {})
|
|
|
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:
|
|
|
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,
|
|
|
272 |
cancel_url="https://yourdomain.com/cancel",
|
273 |
payment_method_types=["card"],
|
274 |
mode="subscription",
|
275 |
+
customer=customer_stripe_id,
|
276 |
+
line_items=[
|
277 |
+
{
|
278 |
+
"price": price_id,
|
279 |
+
"quantity": 1
|
280 |
+
}
|
281 |
+
],
|
282 |
+
payment_intent_data={ # 🔹 Importante para o Brasil! Indica que parte do valor será transferida
|
283 |
+
"application_fee_amount": int(price_amount * 0.2), # 20% para a plataforma
|
284 |
+
"transfer_data": {
|
285 |
+
"destination": stylist_stripe_id, # Conta do estilista no Stripe
|
286 |
+
"amount": int(price_amount * 0.8) # 80% vai para o estilista
|
287 |
+
}
|
288 |
+
},
|
289 |
metadata={
|
290 |
"stylist_id": stylist_id,
|
291 |
"stylist_stripe_id": stylist_stripe_id,
|
292 |
"user_id": user_id,
|
293 |
+
"user_stripe_id": customer_stripe_id
|
294 |
},
|
295 |
subscription_data={
|
296 |
"metadata": {
|