Update routes/subscription.py
Browse files- routes/subscription.py +48 -27
routes/subscription.py
CHANGED
@@ -37,6 +37,43 @@ class SubscriptionRequest(BaseModel):
|
|
37 |
id: str # ID do estilista
|
38 |
user_id: str # ID do usuário que está comprando a assinatura
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
@router.post("/create_checkout_session")
|
41 |
def create_checkout_session(data: SubscriptionRequest):
|
42 |
try:
|
@@ -50,19 +87,16 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
50 |
if not stylist_data:
|
51 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
52 |
|
53 |
-
stylist = stylist_data[0]
|
54 |
-
|
55 |
-
# 🔹 2. Extrair informações do estilista
|
56 |
stylist_name = stylist["name"]
|
57 |
-
stylist_price = stylist["price"]
|
58 |
stylist_avatar = stylist["avatar"]
|
59 |
consultations = stylist["consultations"]
|
60 |
stylist_stripe_id = stylist["stripe_id"]
|
61 |
|
62 |
-
if not
|
63 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
64 |
|
65 |
-
# 🔹
|
66 |
response_user = requests.get(
|
67 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.user_id}",
|
68 |
headers=SUPABASE_HEADERS
|
@@ -73,40 +107,27 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
73 |
raise HTTPException(status_code=404, detail="User not found")
|
74 |
|
75 |
user = user_data[0]
|
76 |
-
user_stripe_id = user.get("stripe_id")
|
|
|
77 |
|
78 |
if not user_stripe_id:
|
79 |
raise HTTPException(status_code=400, detail="User does not have a Stripe ID")
|
|
|
|
|
80 |
|
81 |
-
# 🔹
|
82 |
-
product = stripe.Product.create(
|
83 |
-
name=f"{stylist_name}'s Service",
|
84 |
-
description=f"✔ {consultations} consultations",
|
85 |
-
images=[stylist_avatar], # Imagem do estilista
|
86 |
-
)
|
87 |
-
|
88 |
-
# 🔹 5. Criar o preço recorrente associado ao produto no Stripe
|
89 |
-
price = stripe.Price.create(
|
90 |
-
unit_amount=int(stylist_price * 100), # Preço em centavos
|
91 |
-
currency="brl",
|
92 |
-
recurring={"interval": "month"}, # Define o intervalo como mensal
|
93 |
-
product=product.id, # Associa o produto criado ao preço
|
94 |
-
)
|
95 |
-
|
96 |
-
# 🔹 6. Criar Checkout Session no Stripe
|
97 |
session = stripe.checkout.Session.create(
|
98 |
success_url="https://yourdomain.com/success",
|
99 |
cancel_url="https://yourdomain.com/cancel",
|
100 |
payment_method_types=["card"],
|
101 |
-
mode="subscription",
|
102 |
-
customer=user_stripe_id,
|
103 |
line_items=[
|
104 |
{
|
105 |
-
"price":
|
106 |
"quantity": 1
|
107 |
}
|
108 |
],
|
109 |
-
# Não passamos mais o "payment_intent_data" no modo de assinatura
|
110 |
metadata={
|
111 |
"stylist_id": stylist_stripe_id,
|
112 |
"user_id": data.user_id,
|
|
|
37 |
id: str # ID do estilista
|
38 |
user_id: str # ID do usuário que está comprando a assinatura
|
39 |
|
40 |
+
@router.post("/create_price")
|
41 |
+
def create_price(request: Request):
|
42 |
+
try:
|
43 |
+
data = await request.json()
|
44 |
+
amount = data.get("amount")
|
45 |
+
user_id = data.get("user_id")
|
46 |
+
|
47 |
+
if not amount or not user_id:
|
48 |
+
raise HTTPException(status_code=400, detail="Amount and user_id are required")
|
49 |
+
|
50 |
+
# 🔹 Criar o preço no Stripe
|
51 |
+
price = stripe.Price.create(
|
52 |
+
unit_amount=int(amount * 100), # Convertendo para centavos
|
53 |
+
currency="brl",
|
54 |
+
recurring={"interval": "month"}, # Preço recorrente mensal
|
55 |
+
product_data={
|
56 |
+
"name": "Custom Subscription Price",
|
57 |
+
"description": "Custom price for user subscription"
|
58 |
+
}
|
59 |
+
)
|
60 |
+
|
61 |
+
# 🔹 Atualizar o usuário no Supabase com o price_id
|
62 |
+
response = requests.patch(
|
63 |
+
f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}",
|
64 |
+
headers=SUPABASE_HEADERS,
|
65 |
+
json={"price_id": price.id}
|
66 |
+
)
|
67 |
+
|
68 |
+
if response.status_code not in [200, 204]:
|
69 |
+
raise HTTPException(status_code=500, detail="Failed to update Supabase with price_id")
|
70 |
+
|
71 |
+
return {"message": "Price created successfully!", "price_id": price.id}
|
72 |
+
|
73 |
+
except Exception as e:
|
74 |
+
logger.error(f"Error creating price: {e}")
|
75 |
+
raise HTTPException(status_code=500, detail="Error creating price.")
|
76 |
+
|
77 |
@router.post("/create_checkout_session")
|
78 |
def create_checkout_session(data: SubscriptionRequest):
|
79 |
try:
|
|
|
87 |
if not stylist_data:
|
88 |
raise HTTPException(status_code=404, detail="Stylist not found")
|
89 |
|
90 |
+
stylist = stylist_data[0]
|
|
|
|
|
91 |
stylist_name = stylist["name"]
|
|
|
92 |
stylist_avatar = stylist["avatar"]
|
93 |
consultations = stylist["consultations"]
|
94 |
stylist_stripe_id = stylist["stripe_id"]
|
95 |
|
96 |
+
if not consultations or not stylist_stripe_id:
|
97 |
raise HTTPException(status_code=400, detail="Stylist profile is incomplete")
|
98 |
|
99 |
+
# 🔹 2. Buscar o stripe_id e price_id do usuário no banco de dados (Supabase)
|
100 |
response_user = requests.get(
|
101 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.user_id}",
|
102 |
headers=SUPABASE_HEADERS
|
|
|
107 |
raise HTTPException(status_code=404, detail="User not found")
|
108 |
|
109 |
user = user_data[0]
|
110 |
+
user_stripe_id = user.get("stripe_id")
|
111 |
+
price_id = user.get("price_id") # Pegamos o price_id salvo no Supabase
|
112 |
|
113 |
if not user_stripe_id:
|
114 |
raise HTTPException(status_code=400, detail="User does not have a Stripe ID")
|
115 |
+
if not price_id:
|
116 |
+
raise HTTPException(status_code=400, detail="User does not have a valid price ID")
|
117 |
|
118 |
+
# 🔹 3. Criar Checkout Session no Stripe com o price_id salvo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
session = stripe.checkout.Session.create(
|
120 |
success_url="https://yourdomain.com/success",
|
121 |
cancel_url="https://yourdomain.com/cancel",
|
122 |
payment_method_types=["card"],
|
123 |
+
mode="subscription",
|
124 |
+
customer=user_stripe_id,
|
125 |
line_items=[
|
126 |
{
|
127 |
+
"price": price_id, # Agora usamos o price_id salvo no banco
|
128 |
"quantity": 1
|
129 |
}
|
130 |
],
|
|
|
131 |
metadata={
|
132 |
"stylist_id": stylist_stripe_id,
|
133 |
"user_id": data.user_id,
|