habulaj commited on
Commit
01ccbf3
·
verified ·
1 Parent(s): 117451f

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +42 -22
routes/subscription.py CHANGED
@@ -1,8 +1,10 @@
1
  import stripe
2
- from fastapi import APIRouter, HTTPException, Request
3
- from pydantic import BaseModel
4
- import json
5
  import logging
 
 
 
 
 
6
 
7
  router = APIRouter()
8
 
@@ -12,27 +14,44 @@ logger = logging.getLogger(__name__)
12
  stripe.api_key = "sk_test_51N6K5JB9VMe0qzbOjlJvMEsfdQyrFgV49vRaeErtmhrzHV3Cu3f5jMDJmrhKdI5uqvpHubjkmwDQgMOtCEmz19t800AouH7W6g"
13
  stripe.api_version = "2023-10-16"
14
 
15
- ### **DATA MODELS** ###
16
- class SubscriptionRequest(BaseModel):
17
- user_id: str
18
- stylist_id: str # Stripe Connected Account ID (acct_xxxxx)
19
- amount: int # Value in cents (e.g., R$25.00 -> 2500)
20
- stylist_name: str # Name of the stylist
21
- stylist_avatar: str # URL da foto do estilista
22
- consultations_per_month: int # Número de consultas inclusas no plano
23
 
24
- class CancelSubscriptionRequest(BaseModel):
25
- subscription_id: str
 
26
 
27
- ### **CREATE CHECKOUT SESSION** ###
28
  @router.post("/create_checkout_session")
29
  def create_checkout_session(data: SubscriptionRequest):
30
  try:
31
- product_name = f"{data.stylist_name}'s Subscription"
32
- product_description = (
33
- f"{data.consultations_per_month} video call consultations per month"
 
34
  )
 
 
 
 
 
 
 
 
 
 
 
 
35
 
 
 
 
 
36
  session = stripe.checkout.Session.create(
37
  success_url="https://yourdomain.com/success",
38
  cancel_url="https://yourdomain.com/cancel",
@@ -43,11 +62,11 @@ def create_checkout_session(data: SubscriptionRequest):
43
  "price_data": {
44
  "currency": "brl",
45
  "product_data": {
46
- "name": product_name,
47
- "description": product_description,
48
- "images": [data.stylist_avatar], # Adiciona a foto do estilista
49
  },
50
- "unit_amount": data.amount,
51
  "recurring": {"interval": "month"}
52
  },
53
  "quantity": 1
@@ -57,7 +76,7 @@ def create_checkout_session(data: SubscriptionRequest):
57
  "metadata": {
58
  "user_id": data.user_id,
59
  "stylist_id": data.stylist_id,
60
- "consultations_per_month": data.consultations_per_month
61
  }
62
  }
63
  )
@@ -66,6 +85,7 @@ def create_checkout_session(data: SubscriptionRequest):
66
  "message": "Checkout session created successfully!",
67
  "checkout_url": session.url
68
  }
 
69
  except Exception as e:
70
  logger.error(f"Error creating checkout session: {e}")
71
  raise HTTPException(status_code=500, detail="Error creating checkout session.")
 
1
  import stripe
 
 
 
2
  import logging
3
+ import json
4
+ import os
5
+ import requests
6
+ from fastapi import APIRouter, HTTPException
7
+ from pydantic import BaseModel
8
 
9
  router = APIRouter()
10
 
 
14
  stripe.api_key = "sk_test_51N6K5JB9VMe0qzbOjlJvMEsfdQyrFgV49vRaeErtmhrzHV3Cu3f5jMDJmrhKdI5uqvpHubjkmwDQgMOtCEmz19t800AouH7W6g"
15
  stripe.api_version = "2023-10-16"
16
 
17
+ # 🔥 Supabase Configuração
18
+ SUPABASE_URL = "https://ussxqnifefkgkaumjann.supabase.co"
19
+ SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InVzc3hxbmlmZWZrZ2thdW1qYW5uIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Mzc2NTE2MjMsImV4cCI6MjA1MzIyNzYyM30.SWb6lh3foBp8MrZaHpjt5kKkzXNyBYh3vgfayIM7bzs"
20
+ SUPABASE_HEADERS = {
21
+ "apikey": SUPABASE_KEY,
22
+ "Authorization": f"Bearer {SUPABASE_KEY}",
23
+ "Content-Type": "application/json"
24
+ }
25
 
26
+ class SubscriptionRequest(BaseModel):
27
+ user_id: str # ID do usuário que está comprando
28
+ stylist_id: str # ID do estilista (será usado para buscar os dados)
29
 
 
30
  @router.post("/create_checkout_session")
31
  def create_checkout_session(data: SubscriptionRequest):
32
  try:
33
+ # 🔹 1. Buscar estilista no Supabase
34
+ response = requests.get(
35
+ f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.stylist_id}",
36
+ headers=SUPABASE_HEADERS
37
  )
38
+
39
+ stylist_data = response.json()
40
+ if not stylist_data:
41
+ raise HTTPException(status_code=404, detail="Stylist not found")
42
+
43
+ stylist = stylist_data[0] # Pega o primeiro resultado
44
+
45
+ # 🔹 2. Extrair informações do estilista
46
+ stylist_name = stylist["name"]
47
+ stylist_price = stylist["price"]
48
+ stylist_avatar = stylist["avatar"]
49
+ consultations = stylist["consultations"]
50
 
51
+ if not stylist_price or not consultations:
52
+ raise HTTPException(status_code=400, detail="Stylist has not set up their pricing or consultations")
53
+
54
+ # 🔹 3. Criar Checkout Session no Stripe
55
  session = stripe.checkout.Session.create(
56
  success_url="https://yourdomain.com/success",
57
  cancel_url="https://yourdomain.com/cancel",
 
62
  "price_data": {
63
  "currency": "brl",
64
  "product_data": {
65
+ "name": f"{stylist_name}'s Subscription",
66
+ "description": f"✔ {consultations} video call consultations per month",
67
+ "images": [stylist_avatar],
68
  },
69
+ "unit_amount": stylist_price,
70
  "recurring": {"interval": "month"}
71
  },
72
  "quantity": 1
 
76
  "metadata": {
77
  "user_id": data.user_id,
78
  "stylist_id": data.stylist_id,
79
+ "consultations_per_month": consultations
80
  }
81
  }
82
  )
 
85
  "message": "Checkout session created successfully!",
86
  "checkout_url": session.url
87
  }
88
+
89
  except Exception as e:
90
  logger.error(f"Error creating checkout session: {e}")
91
  raise HTTPException(status_code=500, detail="Error creating checkout session.")