Update routes/hello.py
Browse files- routes/hello.py +49 -13
routes/hello.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
-
import os
|
2 |
import stripe
|
3 |
-
from fastapi import APIRouter, HTTPException
|
4 |
-
from pydantic import BaseModel
|
5 |
import requests
|
|
|
|
|
|
|
6 |
|
7 |
router = APIRouter()
|
8 |
|
@@ -21,20 +22,49 @@ SUPABASE_HEADERS = {
|
|
21 |
"Content-Type": "application/json"
|
22 |
}
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
name: str # Name of the individual account owner
|
27 |
|
28 |
class CreateCustomerRequest(BaseModel):
|
29 |
email: str
|
30 |
phone: str
|
31 |
name: str
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
@router.post("/create_customer")
|
35 |
-
def create_customer(
|
|
|
|
|
|
|
36 |
try:
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
customer = stripe.Customer.create(
|
39 |
email=data.email,
|
40 |
phone=data.phone,
|
@@ -42,21 +72,27 @@ def create_customer(data: CreateCustomerRequest):
|
|
42 |
)
|
43 |
stripe_id = customer.id
|
44 |
|
45 |
-
# Atualizar o usuário no Supabase com o stripe_id
|
46 |
update_data = {"stripe_id": stripe_id}
|
|
|
|
|
|
|
|
|
|
|
47 |
response = requests.patch(
|
48 |
-
f"{SUPABASE_URL}/rest/v1/users?id=eq.{
|
49 |
-
headers=
|
50 |
json=update_data
|
51 |
)
|
52 |
|
53 |
if response.status_code not in [200, 204]:
|
54 |
raise HTTPException(status_code=500, detail=f"Erro ao atualizar o Supabase: {response.text}")
|
55 |
|
|
|
56 |
return {"customer_id": stripe_id}
|
57 |
|
58 |
except Exception as e:
|
59 |
-
logger.error(f"
|
60 |
raise HTTPException(status_code=500, detail=f"Error creating customer: {str(e)}")
|
61 |
|
62 |
@router.post("/create_account")
|
|
|
1 |
+
import os
|
2 |
import stripe
|
|
|
|
|
3 |
import requests
|
4 |
+
import logging
|
5 |
+
from fastapi import APIRouter, HTTPException, Header
|
6 |
+
from pydantic import BaseModel
|
7 |
|
8 |
router = APIRouter()
|
9 |
|
|
|
22 |
"Content-Type": "application/json"
|
23 |
}
|
24 |
|
25 |
+
logging.basicConfig(level=logging.INFO)
|
26 |
+
logger = logging.getLogger(__name__)
|
|
|
27 |
|
28 |
class CreateCustomerRequest(BaseModel):
|
29 |
email: str
|
30 |
phone: str
|
31 |
name: str
|
32 |
+
|
33 |
+
def verify_token(user_token: str) -> str:
|
34 |
+
"""
|
35 |
+
Valida o token JWT no Supabase e retorna o user_id se for válido.
|
36 |
+
"""
|
37 |
+
headers = {
|
38 |
+
"Authorization": f"Bearer {user_token}",
|
39 |
+
"apikey": SUPABASE_KEY,
|
40 |
+
"Content-Type": "application/json"
|
41 |
+
}
|
42 |
+
|
43 |
+
response = requests.get(f"{SUPABASE_URL}/auth/v1/user", headers=headers)
|
44 |
+
|
45 |
+
if response.status_code == 200:
|
46 |
+
user_data = response.json()
|
47 |
+
user_id = user_data.get("id")
|
48 |
+
if not user_id:
|
49 |
+
raise HTTPException(status_code=400, detail="Invalid token: User ID not found")
|
50 |
+
return user_id
|
51 |
+
else:
|
52 |
+
raise HTTPException(status_code=401, detail="Invalid or expired token")
|
53 |
|
54 |
@router.post("/create_customer")
|
55 |
+
def create_customer(
|
56 |
+
data: CreateCustomerRequest,
|
57 |
+
user_token: str = Header(None, alias="User-token") # Token JWT do usuário no cabeçalho
|
58 |
+
):
|
59 |
try:
|
60 |
+
if not user_token:
|
61 |
+
raise HTTPException(status_code=401, detail="Missing User-token header")
|
62 |
+
|
63 |
+
# 🔹 Validar o token e obter user_id
|
64 |
+
user_id = verify_token(user_token)
|
65 |
+
logger.info(f"🔹 User verified. user_id: {user_id}")
|
66 |
+
|
67 |
+
# 🔹 Criar o cliente no Stripe
|
68 |
customer = stripe.Customer.create(
|
69 |
email=data.email,
|
70 |
phone=data.phone,
|
|
|
72 |
)
|
73 |
stripe_id = customer.id
|
74 |
|
75 |
+
# 🔹 Atualizar o usuário no Supabase com o stripe_id
|
76 |
update_data = {"stripe_id": stripe_id}
|
77 |
+
update_headers = {
|
78 |
+
"Authorization": f"Bearer {user_token}",
|
79 |
+
**SUPABASE_HEADERS
|
80 |
+
}
|
81 |
+
|
82 |
response = requests.patch(
|
83 |
+
f"{SUPABASE_URL}/rest/v1/users?id=eq.{user_id}",
|
84 |
+
headers=update_headers,
|
85 |
json=update_data
|
86 |
)
|
87 |
|
88 |
if response.status_code not in [200, 204]:
|
89 |
raise HTTPException(status_code=500, detail=f"Erro ao atualizar o Supabase: {response.text}")
|
90 |
|
91 |
+
logger.info(f"✅ Successfully created customer {stripe_id} and updated Supabase user {user_id}")
|
92 |
return {"customer_id": stripe_id}
|
93 |
|
94 |
except Exception as e:
|
95 |
+
logger.error(f"❌ Error creating customer: {str(e)}")
|
96 |
raise HTTPException(status_code=500, detail=f"Error creating customer: {str(e)}")
|
97 |
|
98 |
@router.post("/create_account")
|