-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (52 loc) · 1.66 KB
/
Copy pathmain.py
File metadata and controls
64 lines (52 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import stripe
from dotenv import load_dotenv
import os
# Încarcă .env care cont cheia stripe
load_dotenv()
stripe.api_key = os.getenv("STRIPE_SECRET_KEY")
app = FastAPI()
# Montăm folderul frontend ca static files
app.mount("/static", StaticFiles(directory="frontend"), name="static")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Permite cereri din orice frontend
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def home():
return FileResponse("frontend/index.html")
@app.post("/create-checkout-session")
def create_checkout_session():
try:
session = stripe.checkout.Session.create(
payment_method_types=["card"],
mode="payment",
success_url="http://127.0.0.1:8000/success",
cancel_url="http://127.0.0.1:8000/cancel",
line_items=[
{
"price_data": {
"currency": "ron",
"product_data": {"name": "Produs local"},
"unit_amount": 5000,
},
"quantity": 1,
}
],
)
# url pg stripe unde clientul introduce cardul
return {"checkout_url": session.url}
except Exception as e:
return {"error": str(e)}
@app.get("/success")
def success():
return FileResponse("templates/success.html")
@app.get("/cancel")
def cancel():
return FileResponse("templates/cancel.html")