Update routes/subscription.py
Browse files- routes/subscription.py +9 -21
routes/subscription.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
import stripe
|
2 |
from fastapi import APIRouter, HTTPException, Request
|
3 |
from pydantic import BaseModel
|
|
|
4 |
|
5 |
router = APIRouter()
|
6 |
|
|
|
|
|
7 |
stripe.api_key = "sk_test_51N6K5JB9VMe0qzbOjlJvMEsfdQyrFgV49vRaeErtmhrzHV3Cu3f5jMDJmrhKdI5uqvpHubjkmwDQgMOtCEmz19t800AouH7W6g"
|
8 |
stripe.api_version = "2023-10-16"
|
9 |
|
@@ -57,29 +60,14 @@ def create_checkout_session(data: SubscriptionRequest):
|
|
57 |
@router.post("/webhook")
|
58 |
async def stripe_webhook(request: Request):
|
59 |
payload = await request.body()
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
raise HTTPException(status_code=400, detail="Invalid payload")
|
66 |
-
except stripe.error.SignatureVerificationError:
|
67 |
-
raise HTTPException(status_code=400, detail="Invalid signature")
|
68 |
-
|
69 |
-
if event["type"] == "invoice.payment_succeeded":
|
70 |
-
invoice = event["data"]["object"]
|
71 |
-
estilista_id = invoice["metadata"]["estilista_id"]
|
72 |
-
user_id = invoice["metadata"]["user_id"]
|
73 |
-
amount_paid = invoice["amount_paid"]
|
74 |
-
|
75 |
-
# 80% do valor fica pendente para o estilista
|
76 |
-
amount_for_stylist = int(amount_paid * 0.8)
|
77 |
-
|
78 |
-
# Aqui você salvaria o saldo pendente do estilista no banco de dados
|
79 |
-
save_pending_balance(estilista_id, amount_for_stylist)
|
80 |
-
|
81 |
-
return {"success": True}
|
82 |
|
|
|
|
|
83 |
### 4. CANCELAR ASSINATURA ###
|
84 |
@router.post("/cancel_subscription")
|
85 |
def cancel_subscription(data: CancelSubscriptionRequest):
|
|
|
1 |
import stripe
|
2 |
from fastapi import APIRouter, HTTPException, Request
|
3 |
from pydantic import BaseModel
|
4 |
+
import logging
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
8 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
stripe.api_key = "sk_test_51N6K5JB9VMe0qzbOjlJvMEsfdQyrFgV49vRaeErtmhrzHV3Cu3f5jMDJmrhKdI5uqvpHubjkmwDQgMOtCEmz19t800AouH7W6g"
|
11 |
stripe.api_version = "2023-10-16"
|
12 |
|
|
|
60 |
@router.post("/webhook")
|
61 |
async def stripe_webhook(request: Request):
|
62 |
payload = await request.body()
|
63 |
+
headers = dict(request.headers)
|
64 |
|
65 |
+
logger.info("🔹 Webhook recebido!")
|
66 |
+
logger.info(f"🔹 Headers: {headers}")
|
67 |
+
logger.info(f"🔹 Corpo: {payload.decode('utf-8')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
return {"status": "Webhook recebido com sucesso!"}
|
70 |
+
|
71 |
### 4. CANCELAR ASSINATURA ###
|
72 |
@router.post("/cancel_subscription")
|
73 |
def cancel_subscription(data: CancelSubscriptionRequest):
|