habulaj commited on
Commit
ef8b28f
·
verified ·
1 Parent(s): 988e9a4

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +70 -68
routes/subscription.py CHANGED
@@ -242,75 +242,77 @@ async def stripe_webhook(request: Request):
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
262
- payment_intent = stripe.PaymentIntent.retrieve(payment_intent_id)
263
- except Exception as e:
264
- logger.error(f"⚠️ Error retrieving PaymentIntent: {e}")
265
- raise HTTPException(status_code=400, detail="Error retrieving PaymentIntent")
266
-
267
- # Extrai os dados do metadata
268
- user_id = session.metadata.get("user_id") # ID do cliente
269
- stylist_id = session.metadata.get("stylist_id") # ID do estilista no Stripe
270
- consultations_per_month = session.metadata.get("consultations_per_month") # Consultas por mês
271
-
272
- # Verifica o valor pago
273
- amount = payment_intent["amount_received"]
274
-
275
- # Define a divisão do pagamento entre a plataforma e o estilista
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
284
- destination=stylist_id, # Conta do estilista
285
- description="Payment for styling service"
286
- )
287
- logger.info(f"💸 Transfer to stylist {stylist_id} successful: R${stylist_share / 100:.2f} BRL.")
288
- except Exception as e:
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%)
296
- currency="brl", # Moeda
297
- destination="your_platform_stripe_account_id", # Substitua com sua conta Stripe da plataforma
298
- description="Platform share"
299
- )
300
- logger.info(f"💸 Transfer to platform successful: R${platform_share / 100:.2f} BRL.")
301
- except Exception as e:
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,
312
- "platform_share": platform_share / 100
313
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
 
315
  return {"status": "Event received, no action needed."}
316
 
 
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
+ invoice_id = subscription.latest_invoice # Aqui obtemos o ID da fatura
256
+ invoice = stripe.Invoice.retrieve(invoice_id) # Recupera a fatura
257
+ payment_intent_id = invoice.payment_intent # Agora conseguimos pegar o PaymentIntent
258
+ logger.info(f"🔹 Found PaymentIntent from subscription: {payment_intent_id}")
259
+ else:
260
+ raise HTTPException(status_code=400, detail="PaymentIntent ID missing.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
 
262
+ try:
263
+ # Recupera o PaymentIntent associado à sessão
264
+ payment_intent = stripe.PaymentIntent.retrieve(payment_intent_id)
265
+ except Exception as e:
266
+ logger.error(f"⚠️ Error retrieving PaymentIntent: {e}")
267
+ raise HTTPException(status_code=400, detail="Error retrieving PaymentIntent")
268
+
269
+ # Extrai os dados do metadata
270
+ user_id = session.metadata.get("user_id") # ID do cliente
271
+ stylist_id = session.metadata.get("stylist_id") # ID do estilista no Stripe
272
+ consultations_per_month = session.metadata.get("consultations_per_month") # Consultas por mês
273
+
274
+ # Verifica o valor pago
275
+ amount = payment_intent["amount_received"]
276
+
277
+ # Define a divisão do pagamento entre a plataforma e o estilista
278
+ stylist_share = int(amount * 0.8) # 80% para o estilista
279
+ platform_share = amount - stylist_share # 20% para a plataforma
280
+
281
+ # Realizando a transferência para o estilista
282
+ try:
283
+ transfer = stripe.Transfer.create(
284
+ amount=stylist_share, # Montante para o estilista (80%)
285
+ currency="brl", # Moeda
286
+ destination=stylist_id, # Conta do estilista
287
+ description="Payment for styling service"
288
+ )
289
+ logger.info(f"💸 Transfer to stylist {stylist_id} successful: R${stylist_share / 100:.2f} BRL.")
290
+ except Exception as e:
291
+ logger.error(f"⚠️ Error transferring funds to stylist: {e}")
292
+ raise HTTPException(status_code=500, detail="Error transferring funds to stylist.")
293
+
294
+ # Transferência para a plataforma
295
+ try:
296
+ transfer_platform = stripe.Transfer.create(
297
+ amount=platform_share, # Montante para a plataforma (20%)
298
+ currency="brl", # Moeda
299
+ destination="your_platform_stripe_account_id", # Substitua com sua conta Stripe da plataforma
300
+ description="Platform share"
301
+ )
302
+ logger.info(f"💸 Transfer to platform successful: R${platform_share / 100:.2f} BRL.")
303
+ except Exception as e:
304
+ logger.error(f"⚠️ Error transferring funds to platform: {e}")
305
+ raise HTTPException(status_code=500, detail="Error transferring funds to platform.")
306
+
307
+ return {
308
+ "status": "Payment processed successfully!",
309
+ "user_id": user_id,
310
+ "stylist_id": stylist_id,
311
+ "consultations_per_month": consultations_per_month,
312
+ "total_paid": amount / 100,
313
+ "stylist_share": stylist_share / 100,
314
+ "platform_share": platform_share / 100
315
+ }
316
 
317
  return {"status": "Event received, no action needed."}
318