stripe tested and working

This commit is contained in:
forest 2020-05-12 13:04:35 -05:00
parent 55d8d8a9a6
commit 7337375ae8
3 changed files with 15 additions and 8 deletions

View File

@ -154,7 +154,7 @@ class DBModel:
def create_stripe_checkout_session(self, id, email, dollars): def create_stripe_checkout_session(self, id, email, dollars):
self.cursor.execute(""" self.cursor.execute("""
INSERT INTO stripe_checkout_sessions (id, email, dollars) INSERT INTO stripe_checkout_sessions (id, email, dollars)
VALUES (%s, %s, %d) VALUES (%s, %s, %s)
""", """,
(id, email, dollars) (id, email, dollars)
) )
@ -174,7 +174,7 @@ class DBModel:
""") """)
# not sure what to do here. For now just log and do nothing. # 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( "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() self.connection.commit()
return rows[0][0] return rows[0][0]
else: else:

View File

@ -25,12 +25,18 @@ bp = Blueprint("stripe", __name__, url_prefix="/stripe")
def index(): def index():
stripe_checkout_session_id=None stripe_checkout_session_id=None
errors = list()
if request.method == "POST": if request.method == "POST":
errors = list()
if "dollars" not in request.form: if "dollars" not in request.form:
errors.append("dollars is required") 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") errors.append("dollars must be >= 1")
if len(errors) == 0: if len(errors) == 0:
@ -48,7 +54,7 @@ def index():
"images": [current_app.config['BASE_URL']+"/static/capsul-product-image.png"], "images": [current_app.config['BASE_URL']+"/static/capsul-product-image.png"],
"quantity": 1, "quantity": 1,
"currency": "usd", "currency": "usd",
"amount": request.form["dollars"] "amount": int(dollars*100)
} }
] ]
) )
@ -76,7 +82,8 @@ def success():
else: else:
checkout_session = stripe.checkout.Session.retrieve(stripe_checkout_session_id) checkout_session = stripe.checkout.Session.retrieve(stripe_checkout_session_id)
if checkout_session and 'display_items' in checkout_session: 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 #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 # its ok to call consume_stripe_checkout_session more than once because it only takes an action if the session exists

View File

@ -11,8 +11,8 @@
<div class="row half-margin"> <div class="row half-margin">
<form method="post"> <form method="post">
<div class="row justify-start"> <div class="row justify-start">
<label for="content">$</label> <label for="dollars">$</label>
<input type="number" id="name" name="name"></input> <input type="number" id="dollars" name="dollars"></input>
</div> </div>
<div class="row justify-end"> <div class="row justify-end">
<input type="submit" value="Pay With Stripe"> <input type="submit" value="Pay With Stripe">