Update routes/subscription.py
Browse files- routes/subscription.py +22 -15
routes/subscription.py
CHANGED
@@ -41,47 +41,54 @@ class CreatePriceRequest(BaseModel):
|
|
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 |
try:
|
47 |
-
|
|
|
|
|
|
|
48 |
user_id = data.user_id
|
49 |
|
50 |
if not amount or not user_id:
|
51 |
raise HTTPException(status_code=400, detail="Amount and user_id are required")
|
52 |
|
53 |
-
# 🔹 Criar
|
54 |
price = stripe.Price.create(
|
55 |
-
unit_amount=amount,
|
56 |
currency="brl",
|
57 |
recurring={"interval": "month"},
|
58 |
-
product_data={
|
59 |
-
"name": "Custom Subscription Price"
|
60 |
-
}
|
61 |
)
|
62 |
|
63 |
logger.info(f"✅ Price created successfully: {price.id} for user {user_id}")
|
64 |
|
65 |
-
# 🔹 Atualizar
|
66 |
supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
|
70 |
response = requests.patch(
|
71 |
supabase_url,
|
72 |
-
headers=
|
73 |
json={"price_id": price.id}
|
74 |
)
|
75 |
|
76 |
-
logger.info(f"🔹 Supabase response: {response.status_code}, {response.text}")
|
77 |
|
78 |
if response.status_code not in [200, 204]:
|
79 |
raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {response.text}")
|
80 |
|
81 |
-
return {
|
82 |
-
"message": "Price created successfully!",
|
83 |
-
"price_id": price.id
|
84 |
-
}
|
85 |
|
86 |
except Exception as e:
|
87 |
logger.error(f"❌ Error creating price: {e}")
|
|
|
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 |
+
from fastapi import Header # Import para pegar headers
|
45 |
+
|
46 |
@router.post("/create_price")
|
47 |
+
async def create_price(
|
48 |
+
data: CreatePriceRequest,
|
49 |
+
user_token: str = Header(None, alias="User-key") # Pegando o token do Header
|
50 |
+
):
|
51 |
try:
|
52 |
+
if not user_token:
|
53 |
+
raise HTTPException(status_code=401, detail="Missing User-key header")
|
54 |
+
|
55 |
+
amount = data.amount
|
56 |
user_id = data.user_id
|
57 |
|
58 |
if not amount or not user_id:
|
59 |
raise HTTPException(status_code=400, detail="Amount and user_id are required")
|
60 |
|
61 |
+
# 🔹 Criar preço no Stripe
|
62 |
price = stripe.Price.create(
|
63 |
+
unit_amount=amount,
|
64 |
currency="brl",
|
65 |
recurring={"interval": "month"},
|
66 |
+
product_data={"name": "Custom Subscription Price"}
|
|
|
|
|
67 |
)
|
68 |
|
69 |
logger.info(f"✅ Price created successfully: {price.id} for user {user_id}")
|
70 |
|
71 |
+
# 🔹 Atualizar Supabase com price_id, autenticando com User-key
|
72 |
supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
|
73 |
|
74 |
+
supabase_headers = {
|
75 |
+
"apikey": SUPABASE_KEY,
|
76 |
+
"Authorization": f"Bearer {user_token}", # 🔥 Passando token do usuário
|
77 |
+
"Content-Type": "application/json"
|
78 |
+
}
|
79 |
|
80 |
response = requests.patch(
|
81 |
supabase_url,
|
82 |
+
headers=supabase_headers,
|
83 |
json={"price_id": price.id}
|
84 |
)
|
85 |
|
86 |
+
logger.info(f"🔹 Supabase response: {response.status_code}, {response.text}")
|
87 |
|
88 |
if response.status_code not in [200, 204]:
|
89 |
raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {response.text}")
|
90 |
|
91 |
+
return {"message": "Price created successfully!", "price_id": price.id}
|
|
|
|
|
|
|
92 |
|
93 |
except Exception as e:
|
94 |
logger.error(f"❌ Error creating price: {e}")
|