Update routes/subscription.py
Browse files- routes/subscription.py +28 -16
routes/subscription.py
CHANGED
@@ -3,8 +3,10 @@ import logging
|
|
3 |
import json
|
4 |
import os
|
5 |
import requests
|
|
|
6 |
from fastapi import APIRouter, HTTPException, Request
|
7 |
from pydantic import BaseModel
|
|
|
8 |
|
9 |
router = APIRouter()
|
10 |
|
@@ -32,7 +34,6 @@ class CheckSubscriptionRequest(BaseModel):
|
|
32 |
user_id: str
|
33 |
stylist_id: str
|
34 |
|
35 |
-
# 📌 Agora recebemos `user_id` (ID do cliente que está comprando)
|
36 |
class SubscriptionRequest(BaseModel):
|
37 |
id: str # ID do estilista
|
38 |
|
@@ -40,8 +41,6 @@ class CreatePriceRequest(BaseModel):
|
|
40 |
amount: int # Valor em centavos (ex: 2500 para R$25,00)
|
41 |
user_id: str # ID do usuário que está criando o preço
|
42 |
|
43 |
-
from fastapi import Header # Import para pegar headers
|
44 |
-
|
45 |
@router.post("/create_price")
|
46 |
async def create_price(
|
47 |
data: CreatePriceRequest,
|
@@ -51,14 +50,26 @@ async def create_price(
|
|
51 |
if not user_token:
|
52 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
amount = data.amount
|
55 |
-
|
56 |
|
57 |
-
if not amount or not
|
58 |
raise HTTPException(status_code=400, detail="Amount and user_id are required")
|
59 |
|
60 |
# 🔹 Buscar `price_id` do usuário no Supabase
|
61 |
-
supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{
|
62 |
supabase_headers = {
|
63 |
"apikey": SUPABASE_KEY,
|
64 |
"Authorization": f"Bearer {user_token}",
|
@@ -128,7 +139,7 @@ async def create_price(
|
|
128 |
except Exception as e:
|
129 |
logger.error(f"❌ Error creating/updating price: {e}")
|
130 |
raise HTTPException(status_code=500, detail="Error creating/updating price.")
|
131 |
-
|
132 |
@router.post("/create_checkout_session")
|
133 |
def create_checkout_session(
|
134 |
data: SubscriptionRequest,
|
@@ -138,17 +149,18 @@ def create_checkout_session(
|
|
138 |
if not user_token:
|
139 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
140 |
|
141 |
-
# 🔹 1.
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
raise HTTPException(status_code=400, detail="Invalid token: User ID not found")
|
147 |
-
except jwt.ExpiredSignatureError:
|
148 |
-
raise HTTPException(status_code=401, detail="Token expired")
|
149 |
-
except jwt.InvalidTokenError:
|
150 |
raise HTTPException(status_code=401, detail="Invalid token")
|
151 |
|
|
|
|
|
|
|
|
|
|
|
152 |
# 🔹 2. Buscar estilista no Supabase
|
153 |
response = requests.get(
|
154 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|
|
|
3 |
import json
|
4 |
import os
|
5 |
import requests
|
6 |
+
import jwt
|
7 |
from fastapi import APIRouter, HTTPException, Request
|
8 |
from pydantic import BaseModel
|
9 |
+
from fastapi import Header
|
10 |
|
11 |
router = APIRouter()
|
12 |
|
|
|
34 |
user_id: str
|
35 |
stylist_id: str
|
36 |
|
|
|
37 |
class SubscriptionRequest(BaseModel):
|
38 |
id: str # ID do estilista
|
39 |
|
|
|
41 |
amount: int # Valor em centavos (ex: 2500 para R$25,00)
|
42 |
user_id: str # ID do usuário que está criando o preço
|
43 |
|
|
|
|
|
44 |
@router.post("/create_price")
|
45 |
async def create_price(
|
46 |
data: CreatePriceRequest,
|
|
|
50 |
if not user_token:
|
51 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
52 |
|
53 |
+
# 🔹 Verificação do token JWT no Supabase
|
54 |
+
supabase_url = f"{SUPABASE_URL}/auth/v1/user"
|
55 |
+
response = requests.get(supabase_url, headers={"Authorization": f"Bearer {user_token}"})
|
56 |
+
|
57 |
+
if response.status_code != 200:
|
58 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
59 |
+
|
60 |
+
user_data = response.json()
|
61 |
+
user_id = user_data.get("id")
|
62 |
+
if not user_id:
|
63 |
+
raise HTTPException(status_code=400, detail="Invalid token: User ID not found")
|
64 |
+
|
65 |
amount = data.amount
|
66 |
+
user_id_request = data.user_id
|
67 |
|
68 |
+
if not amount or not user_id_request:
|
69 |
raise HTTPException(status_code=400, detail="Amount and user_id are required")
|
70 |
|
71 |
# 🔹 Buscar `price_id` do usuário no Supabase
|
72 |
+
supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id_request}"
|
73 |
supabase_headers = {
|
74 |
"apikey": SUPABASE_KEY,
|
75 |
"Authorization": f"Bearer {user_token}",
|
|
|
139 |
except Exception as e:
|
140 |
logger.error(f"❌ Error creating/updating price: {e}")
|
141 |
raise HTTPException(status_code=500, detail="Error creating/updating price.")
|
142 |
+
|
143 |
@router.post("/create_checkout_session")
|
144 |
def create_checkout_session(
|
145 |
data: SubscriptionRequest,
|
|
|
149 |
if not user_token:
|
150 |
raise HTTPException(status_code=401, detail="Missing User-key header")
|
151 |
|
152 |
+
# 🔹 1. Verificação do token JWT no Supabase
|
153 |
+
supabase_url = f"{SUPABASE_URL}/auth/v1/user"
|
154 |
+
response = requests.get(supabase_url, headers={"Authorization": f"Bearer {user_token}"})
|
155 |
+
|
156 |
+
if response.status_code != 200:
|
|
|
|
|
|
|
|
|
157 |
raise HTTPException(status_code=401, detail="Invalid token")
|
158 |
|
159 |
+
user_data = response.json()
|
160 |
+
user_id = user_data.get("id")
|
161 |
+
if not user_id:
|
162 |
+
raise HTTPException(status_code=400, detail="Invalid token: User ID not found")
|
163 |
+
|
164 |
# 🔹 2. Buscar estilista no Supabase
|
165 |
response = requests.get(
|
166 |
f"{SUPABASE_URL}/rest/v1/User?id=eq.{data.id}",
|