From 7337375ae8676339c043e8fb198ed91c17dd78a7 Mon Sep 17 00:00:00 2001 From: forest Date: Tue, 12 May 2020 13:04:35 -0500 Subject: [PATCH] stripe tested and working --- capsulflask/db_model.py | 4 ++-- capsulflask/payment_stripe.py | 15 +++++++++++---- capsulflask/templates/stripe.html | 4 ++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py index 86c187f..9390c34 100644 --- a/capsulflask/db_model.py +++ b/capsulflask/db_model.py @@ -154,7 +154,7 @@ class DBModel: def create_stripe_checkout_session(self, id, email, dollars): self.cursor.execute(""" INSERT INTO stripe_checkout_sessions (id, email, dollars) - VALUES (%s, %s, %d) + VALUES (%s, %s, %s) """, (id, email, dollars) ) @@ -174,7 +174,7 @@ class DBModel: """) # not sure what to do here. For now just log and do nothing. self.cursor.execute( "DELETE FROM stripe_checkout_sessions WHERE id = %s", (id,) ) - self.cursor.execute( "INSERT INTO payments (email, dollars) VALUES (%s, %d)", (rows[0][0], rows[0][1]) ) + self.cursor.execute( "INSERT INTO payments (email, dollars) VALUES (%s, %s)", (rows[0][0], rows[0][1]) ) self.connection.commit() return rows[0][0] else: diff --git a/capsulflask/payment_stripe.py b/capsulflask/payment_stripe.py index 84e4c70..c9e6b87 100644 --- a/capsulflask/payment_stripe.py +++ b/capsulflask/payment_stripe.py @@ -25,12 +25,18 @@ bp = Blueprint("stripe", __name__, url_prefix="/stripe") def index(): stripe_checkout_session_id=None + errors = list() if request.method == "POST": - errors = list() if "dollars" not in request.form: errors.append("dollars is required") - elif decimal.Decimal(request.form["dollars"]) < decimal.Decimal(1): + dollars = None + try: + dollars = decimal.Decimal(request.form["dollars"]) + except: + errors.append("dollars must be a number") + + if dollars and dollars < decimal.Decimal(1): errors.append("dollars must be >= 1") if len(errors) == 0: @@ -48,7 +54,7 @@ def index(): "images": [current_app.config['BASE_URL']+"/static/capsul-product-image.png"], "quantity": 1, "currency": "usd", - "amount": request.form["dollars"] + "amount": int(dollars*100) } ] ) @@ -76,7 +82,8 @@ def success(): else: checkout_session = stripe.checkout.Session.retrieve(stripe_checkout_session_id) if checkout_session and 'display_items' in checkout_session: - dollars = checkout_session['display_items'][0]['amount'] + cents = checkout_session['display_items'][0]['amount'] + dollars = decimal.Decimal(cents)/100 #consume_stripe_checkout_session deletes the checkout session row and inserts a payment row # its ok to call consume_stripe_checkout_session more than once because it only takes an action if the session exists diff --git a/capsulflask/templates/stripe.html b/capsulflask/templates/stripe.html index df51f53..7fc11b6 100644 --- a/capsulflask/templates/stripe.html +++ b/capsulflask/templates/stripe.html @@ -11,8 +11,8 @@
- - + +