-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
66 lines (57 loc) · 1.83 KB
/
server.py
File metadata and controls
66 lines (57 loc) · 1.83 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
65
66
import os
from flask import Flask, request, jsonify
import stripe
from dotenv import load_dotenv
from flask_cors import CORS
import secrets
load_dotenv()
app = Flask(__name__)
CORS(app, origins=[
"https://theorangecow.github.io",
"https://theorangecow.github.io/cheese"
])
stripe.api_key = os.getenv("STRIPE_SECRET_KEY")
confirmation_keys = {}
@app.route("/ping", methods=["GET"])
def ping():
return "pong", 200
@app.route("/create-session", methods=["POST"])
def create_session():
data = request.get_json()
if not data or "cart" not in data:
return jsonify({"error": "Cart not provided"}), 400
confirm_key = secrets.token_urlsafe(16)
confirmation_keys[confirm_key] = True
line_items = [
{
"price_data": {
"currency": "gbp",
"product_data": {"name": item["name"]},
"unit_amount": int(item["price"] * 100),
},
"quantity": item.get("quantity", 1),
}
for item in data["cart"]
]
try:
session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=line_items,
mode="payment",
success_url=f"https://theorangecow.github.io/cheese/success.html?key={confirm_key}",
cancel_url="https://theorangecow.github.io/cheese/buy.html",
)
return jsonify({"url": session.url})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/verify-key", methods=["GET"])
def verify_key():
key = request.args.get("key")
if key in confirmation_keys:
del confirmation_keys[key]
return jsonify({"valid": True})
else:
return jsonify({"valid": False})
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port)