habulaj commited on
Commit
4685d50
·
verified ·
1 Parent(s): 290d164

Update routes/hello.py

Browse files
Files changed (1) hide show
  1. routes/hello.py +31 -7
routes/hello.py CHANGED
@@ -4,33 +4,57 @@ from pydantic import BaseModel
4
 
5
  router = APIRouter()
6
 
7
- stripe.api_key = "sk_test_51N6K5JB9VMe0qzbOjlJvMEsfdQyrFgV49vRaeErtmhrzHV3Cu3f5jMDJmrhKdI5uqvpHubjkmwDQgMOtCEmz19t800AouH7W6g"
 
8
  stripe.api_version = "2023-10-16"
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- ### **1️⃣ CREATE CONNECTED ACCOUNT** ###
11
  class AccountRequest(BaseModel):
12
  email: str
13
  name: str # Name of the individual account owner
14
 
15
  class CreateCustomerRequest(BaseModel):
16
  email: str
17
- phone: str # Tel do usuário
18
- name: str # Nome do usuário
 
19
 
20
  @router.post("/create_customer")
21
  def create_customer(data: CreateCustomerRequest):
22
  try:
23
- # Criar o cliente no Stripe com os dados fornecidos
24
  customer = stripe.Customer.create(
25
  email=data.email,
26
  phone=data.phone,
27
  name=data.name
28
  )
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # Retornar o ID do cliente (customer_id) criado
31
- return {"customer_id": customer.id}
32
 
33
  except Exception as e:
 
34
  raise HTTPException(status_code=500, detail=f"Error creating customer: {str(e)}")
35
 
36
  @router.post("/create_account")
 
4
 
5
  router = APIRouter()
6
 
7
+ # Configuração das chaves do Stripe e Supabase
8
+ stripe.api_key = os.getenv("STRIPE_KEY")
9
  stripe.api_version = "2023-10-16"
10
+ SUPABASE_URL = "https://ussxqnifefkgkaumjann.supabase.co"
11
+ SUPABASE_KEY = os.getenv("SUPA_KEY")
12
+
13
+ if not stripe.api_key or not SUPABASE_KEY:
14
+ raise ValueError("❌ STRIPE_KEY ou SUPA_KEY não foram definidos no ambiente!")
15
+
16
+ SUPABASE_HEADERS = {
17
+ "apikey": SUPABASE_KEY,
18
+ "Authorization": f"Bearer {SUPABASE_KEY}",
19
+ "Content-Type": "application/json"
20
+ }
21
 
 
22
  class AccountRequest(BaseModel):
23
  email: str
24
  name: str # Name of the individual account owner
25
 
26
  class CreateCustomerRequest(BaseModel):
27
  email: str
28
+ phone: str
29
+ name: str
30
+ user_id: str # ID do usuário no Supabase
31
 
32
  @router.post("/create_customer")
33
  def create_customer(data: CreateCustomerRequest):
34
  try:
35
+ # Criar o cliente no Stripe
36
  customer = stripe.Customer.create(
37
  email=data.email,
38
  phone=data.phone,
39
  name=data.name
40
  )
41
+ stripe_id = customer.id
42
+
43
+ # Atualizar o usuário no Supabase com o stripe_id
44
+ update_data = {"stripe_id": stripe_id}
45
+ response = requests.patch(
46
+ f"{SUPABASE_URL}/rest/v1/users?id=eq.{data.user_id}",
47
+ headers=SUPABASE_HEADERS,
48
+ json=update_data
49
+ )
50
+
51
+ if response.status_code not in [200, 204]:
52
+ raise HTTPException(status_code=500, detail=f"Erro ao atualizar o Supabase: {response.text}")
53
 
54
+ return {"customer_id": stripe_id}
 
55
 
56
  except Exception as e:
57
+ logger.error(f"Erro ao criar cliente: {str(e)}")
58
  raise HTTPException(status_code=500, detail=f"Error creating customer: {str(e)}")
59
 
60
  @router.post("/create_account")