Update routes/subscription.py
Browse files- routes/subscription.py +21 -11
routes/subscription.py
CHANGED
@@ -38,41 +38,51 @@ class SubscriptionRequest(BaseModel):
|
|
38 |
user_id: str # ID do usuário que está comprando a assinatura
|
39 |
|
40 |
@router.post("/create_price")
|
41 |
-
async def create_price(
|
42 |
try:
|
43 |
-
|
44 |
-
|
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=
|
53 |
currency="brl",
|
54 |
recurring={"interval": "month"},
|
55 |
product_data={
|
56 |
-
"name": "Custom Subscription Price"
|
57 |
}
|
58 |
)
|
59 |
|
|
|
|
|
60 |
# 🔹 Atualizar o usuário no Supabase com o price_id
|
|
|
|
|
|
|
|
|
61 |
response = requests.patch(
|
62 |
-
|
63 |
headers=SUPABASE_HEADERS,
|
64 |
json={"price_id": price.id}
|
65 |
)
|
66 |
|
|
|
|
|
67 |
if response.status_code not in [200, 204]:
|
68 |
-
raise HTTPException(status_code=500, detail="Failed to update Supabase
|
69 |
|
70 |
-
return {
|
|
|
|
|
|
|
71 |
|
72 |
except Exception as e:
|
73 |
-
logger.error(f"Error creating price: {e}")
|
74 |
raise HTTPException(status_code=500, detail="Error creating price.")
|
75 |
-
|
76 |
@router.post("/create_checkout_session")
|
77 |
def create_checkout_session(data: SubscriptionRequest):
|
78 |
try:
|
|
|
38 |
user_id: str # ID do usuário que está comprando a assinatura
|
39 |
|
40 |
@router.post("/create_price")
|
41 |
+
async def create_price(data: CreatePriceRequest):
|
42 |
try:
|
43 |
+
amount = data.amount # ✅ Já está em centavos, não multiplica por 100
|
44 |
+
user_id = data.user_id
|
|
|
45 |
|
46 |
if not amount or not user_id:
|
47 |
raise HTTPException(status_code=400, detail="Amount and user_id are required")
|
48 |
|
49 |
# 🔹 Criar o preço no Stripe
|
50 |
price = stripe.Price.create(
|
51 |
+
unit_amount=amount, # ✅ Sem multiplicação
|
52 |
currency="brl",
|
53 |
recurring={"interval": "month"},
|
54 |
product_data={
|
55 |
+
"name": "Custom Subscription Price"
|
56 |
}
|
57 |
)
|
58 |
|
59 |
+
logger.info(f"✅ Price created successfully: {price.id} for user {user_id}")
|
60 |
+
|
61 |
# 🔹 Atualizar o usuário no Supabase com o price_id
|
62 |
+
supabase_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
|
63 |
+
|
64 |
+
logger.info(f"🔄 Updating Supabase: {supabase_url}, price_id={price.id}") # Debug log
|
65 |
+
|
66 |
response = requests.patch(
|
67 |
+
supabase_url,
|
68 |
headers=SUPABASE_HEADERS,
|
69 |
json={"price_id": price.id}
|
70 |
)
|
71 |
|
72 |
+
logger.info(f"🔹 Supabase response: {response.status_code}, {response.text}") # Debug log
|
73 |
+
|
74 |
if response.status_code not in [200, 204]:
|
75 |
+
raise HTTPException(status_code=500, detail=f"Failed to update Supabase: {response.text}")
|
76 |
|
77 |
+
return {
|
78 |
+
"message": "Price created successfully!",
|
79 |
+
"price_id": price.id
|
80 |
+
}
|
81 |
|
82 |
except Exception as e:
|
83 |
+
logger.error(f"❌ Error creating price: {e}")
|
84 |
raise HTTPException(status_code=500, detail="Error creating price.")
|
85 |
+
|
86 |
@router.post("/create_checkout_session")
|
87 |
def create_checkout_session(data: SubscriptionRequest):
|
88 |
try:
|