Update routes/subscription.py
Browse files- routes/subscription.py +12 -15
routes/subscription.py
CHANGED
@@ -241,15 +241,21 @@ async def stripe_webhook(request: Request):
|
|
241 |
|
242 |
logger.info(f"🔹 Event received: {event['type']}")
|
243 |
|
244 |
-
# Verifica se o evento é de uma sessão de checkout completada
|
245 |
if event["type"] == "checkout.session.completed":
|
246 |
session = event["data"]["object"]
|
247 |
payment_intent_id = session.get("payment_intent")
|
248 |
|
249 |
-
# Verifica se o
|
250 |
if not payment_intent_id:
|
251 |
logger.error("⚠️ No PaymentIntent ID found in session.")
|
252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
|
254 |
try:
|
255 |
# Recupera o PaymentIntent associado à sessão
|
@@ -270,13 +276,8 @@ async def stripe_webhook(request: Request):
|
|
270 |
stylist_share = int(amount * 0.8) # 80% para o estilista
|
271 |
platform_share = amount - stylist_share # 20% para a plataforma
|
272 |
|
273 |
-
# Logando os valores
|
274 |
-
logger.info(f"💸 Payment of R${amount / 100:.2f} BRL received for stylist {stylist_id}.")
|
275 |
-
logger.info(f"💸 Stylist share: R${stylist_share / 100:.2f} BRL, Platform share: R${platform_share / 100:.2f} BRL.")
|
276 |
-
|
277 |
# Realizando a transferência para o estilista
|
278 |
try:
|
279 |
-
# Transfere os fundos para o estilista (Stripe Connect)
|
280 |
transfer = stripe.Transfer.create(
|
281 |
amount=stylist_share, # Montante para o estilista (80%)
|
282 |
currency="brl", # Moeda
|
@@ -288,7 +289,7 @@ async def stripe_webhook(request: Request):
|
|
288 |
logger.error(f"⚠️ Error transferring funds to stylist: {e}")
|
289 |
raise HTTPException(status_code=500, detail="Error transferring funds to stylist.")
|
290 |
|
291 |
-
#
|
292 |
try:
|
293 |
transfer_platform = stripe.Transfer.create(
|
294 |
amount=platform_share, # Montante para a plataforma (20%)
|
@@ -301,14 +302,10 @@ async def stripe_webhook(request: Request):
|
|
301 |
logger.error(f"⚠️ Error transferring funds to platform: {e}")
|
302 |
raise HTTPException(status_code=500, detail="Error transferring funds to platform.")
|
303 |
|
304 |
-
# Aqui você pode atualizar seu banco de dados com as informações do pagamento
|
305 |
-
# Exemplo de atualização do banco com o valor e o status do pagamento
|
306 |
-
# update_payment_status(user_id, stylist_id, amount, stylist_share, platform_share)
|
307 |
-
|
308 |
return {
|
309 |
"status": "Payment processed successfully!",
|
310 |
-
"user_id": user_id,
|
311 |
-
"stylist_id": stylist_id,
|
312 |
"consultations_per_month": consultations_per_month,
|
313 |
"total_paid": amount / 100,
|
314 |
"stylist_share": stylist_share / 100,
|
|
|
241 |
|
242 |
logger.info(f"🔹 Event received: {event['type']}")
|
243 |
|
|
|
244 |
if event["type"] == "checkout.session.completed":
|
245 |
session = event["data"]["object"]
|
246 |
payment_intent_id = session.get("payment_intent")
|
247 |
|
248 |
+
# Verifica se o PaymentIntent ID está presente
|
249 |
if not payment_intent_id:
|
250 |
logger.error("⚠️ No PaymentIntent ID found in session.")
|
251 |
+
# Verifica se a sessão de checkout tem um PaymentIntent associado
|
252 |
+
if "subscription" in session:
|
253 |
+
subscription_id = session["subscription"]
|
254 |
+
subscription = stripe.Subscription.retrieve(subscription_id)
|
255 |
+
payment_intent_id = subscription.latest_invoice.payment_intent
|
256 |
+
logger.info(f"🔹 Found PaymentIntent from subscription: {payment_intent_id}")
|
257 |
+
else:
|
258 |
+
raise HTTPException(status_code=400, detail="PaymentIntent ID missing.")
|
259 |
|
260 |
try:
|
261 |
# Recupera o PaymentIntent associado à sessão
|
|
|
276 |
stylist_share = int(amount * 0.8) # 80% para o estilista
|
277 |
platform_share = amount - stylist_share # 20% para a plataforma
|
278 |
|
|
|
|
|
|
|
|
|
279 |
# Realizando a transferência para o estilista
|
280 |
try:
|
|
|
281 |
transfer = stripe.Transfer.create(
|
282 |
amount=stylist_share, # Montante para o estilista (80%)
|
283 |
currency="brl", # Moeda
|
|
|
289 |
logger.error(f"⚠️ Error transferring funds to stylist: {e}")
|
290 |
raise HTTPException(status_code=500, detail="Error transferring funds to stylist.")
|
291 |
|
292 |
+
# Transferência para a plataforma
|
293 |
try:
|
294 |
transfer_platform = stripe.Transfer.create(
|
295 |
amount=platform_share, # Montante para a plataforma (20%)
|
|
|
302 |
logger.error(f"⚠️ Error transferring funds to platform: {e}")
|
303 |
raise HTTPException(status_code=500, detail="Error transferring funds to platform.")
|
304 |
|
|
|
|
|
|
|
|
|
305 |
return {
|
306 |
"status": "Payment processed successfully!",
|
307 |
+
"user_id": user_id,
|
308 |
+
"stylist_id": stylist_id,
|
309 |
"consultations_per_month": consultations_per_month,
|
310 |
"total_paid": amount / 100,
|
311 |
"stylist_share": stylist_share / 100,
|