pepettes/server.py

96 lines
2.8 KiB
Python
Raw Normal View History

2021-02-15 04:36:24 +01:00
#! /usr/bin/env python3.6
"""
server.py
Stripe Sample.
Python 3.6 or newer required.
"""
import stripe
import json
import os
2021-02-15 05:25:55 +01:00
import random
import string
2021-02-15 04:36:24 +01:00
2024-05-22 23:13:08 +02:00
from flask import Flask, render_template, jsonify, request, session
from flask_babel import Babel
2021-02-15 05:10:36 +01:00
from flask_simple_csrf import CSRF
2021-02-15 04:36:24 +01:00
2021-02-15 05:10:36 +01:00
2024-03-07 14:25:25 +01:00
static_dir = str(os.path.abspath(os.path.join(__file__, "..", "assets")))
app = Flask(
__name__, static_folder=static_dir, static_url_path="", template_folder=static_dir
)
app.config.from_pyfile("settings.py")
stripe.api_key = app.config["STRIPE_SECRET_KEY"]
CSRF = CSRF(config={"SECRET_CSRF_KEY": app.config["SECRET_CSRF_KEY"]})
2021-02-15 05:10:36 +01:00
app = CSRF.init_app(app)
2021-02-19 00:14:04 +01:00
babel = Babel(app)
2021-02-15 04:36:24 +01:00
2024-03-07 14:25:25 +01:00
2021-02-15 05:10:36 +01:00
@app.before_request
def before_request():
2024-03-07 14:25:25 +01:00
if "CSRF_TOKEN" not in session or "USER_CSRF" not in session:
session["USER_CSRF"] = "".join(
random.SystemRandom().choice(string.ascii_uppercase + string.digits)
for _ in range(64)
)
session["CSRF_TOKEN"] = CSRF.create(session["USER_CSRF"])
2021-02-15 04:36:24 +01:00
2021-02-19 00:14:04 +01:00
@babel.localeselector
def get_locale():
2024-05-23 02:53:45 +02:00
return request.accept_languages.best_match(app.config["LANGUAGES"])
2021-02-19 00:14:04 +01:00
2024-03-07 14:25:25 +01:00
@app.route("/", methods=["GET"])
2021-02-15 04:36:24 +01:00
def get_index():
2024-03-07 14:25:25 +01:00
return render_template(
"index.html", **app.config["CUSTOM"], csrf=session["USER_CSRF"]
)
2021-02-19 00:14:04 +01:00
2024-03-07 14:25:25 +01:00
@app.route("/success", methods=["GET"])
2021-02-19 00:14:04 +01:00
def get_success():
2024-03-07 14:25:25 +01:00
return render_template("success.html", **app.config["CUSTOM"])
2021-02-19 00:14:04 +01:00
2021-02-15 04:36:24 +01:00
2024-03-07 14:25:25 +01:00
@app.route("/canceled", methods=["GET"])
2021-02-19 00:14:04 +01:00
def get_canceled():
2024-03-07 14:25:25 +01:00
return render_template("canceled.html", **app.config["CUSTOM"])
2021-02-15 04:36:24 +01:00
2024-03-07 14:25:25 +01:00
@app.route("/create-checkout-session", methods=["POST"])
2021-02-15 04:36:24 +01:00
def create_checkout_session():
data = json.loads(request.data)
2024-03-07 14:25:25 +01:00
domain_url = app.config["DOMAIN"]
2021-02-15 04:36:24 +01:00
try:
2024-03-07 14:25:25 +01:00
donation = app.config["DONATION"]
currencies = [iso for iso, symbol in app.config["CUSTOM"]["currencies"]]
if (
CSRF.verify(data["user_csrf"], session["CSRF_TOKEN"]) is False
or data["frequency"] not in ["recuring", "one_time"]
or data["currency"] not in currencies
or int(data["quantity"]) <= 0
):
2021-02-15 04:53:44 +01:00
return jsonify(error="Bad value"), 400
2021-02-15 04:36:24 +01:00
# Create new Checkout Session for the order
2024-03-07 14:25:25 +01:00
price = donation[data["frequency"]][data["currency"]]
mode = "payment" if data["frequency"] == "one_time" else "subscription"
2021-02-15 04:36:24 +01:00
checkout_session = stripe.checkout.Session.create(
2024-03-07 14:25:25 +01:00
success_url=domain_url + "/success?session_id={CHECKOUT_SESSION_ID}",
2021-02-19 00:14:04 +01:00
cancel_url=domain_url + "/canceled",
2024-03-07 14:25:25 +01:00
payment_method_types=["card"],
2021-02-15 04:36:24 +01:00
mode=mode,
2024-03-07 14:25:25 +01:00
line_items=[{"price": price, "quantity": data["quantity"]}],
2021-02-15 04:36:24 +01:00
)
2024-03-07 14:25:25 +01:00
return jsonify({"sessionId": checkout_session["id"]})
2021-02-15 04:36:24 +01:00
except Exception as e:
return jsonify(error=str(e)), 403
2024-03-07 14:25:25 +01:00
if __name__ == "__main__":
app.run(port=app.config["PORT"], debug=app.debug)