Skip to content

Ability to complete payment #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 36 additions & 10 deletions app/app.py
Expand Up @@ -17,6 +17,17 @@

import logging

def getBasketAndTotal(sessionBasket):
theBasket = []
totalPrice = 0
for key in sessionBasket:
theItem = Item.query.filter_by(id=key).first()
quantity = int(sessionBasket[key])
thePrice = theItem.price * quantity
totalPrice += thePrice
theBasket.append([theItem, quantity, thePrice])

return (theBasket, totalPrice)

def populateTables():
"""
Expand Down Expand Up @@ -267,22 +278,14 @@ def basket():
return flask.redirect(flask.url_for("index"))


theBasket = []
#Otherwise we need to work out the Basket
#Get it from the session
sessionBasket = flask.session.get("basket", None)
if not sessionBasket:
flask.flash("No items in basket")
return flask.redirect(flask.url_for("index"))

totalPrice = 0
for key in sessionBasket:
theItem = Item.query.filter_by(id=key).first()
quantity = int(sessionBasket[key])
thePrice = theItem.price * quantity
totalPrice += thePrice
theBasket.append([theItem, quantity, thePrice])

theBasket, totalPrice = getBasketAndTotal(sessionBasket)

return flask.render_template("basket.html",
basket = theBasket,
Expand All @@ -299,11 +302,34 @@ def pay():
cost = flask.request.form.get("total")

theUser = User.query.filter_by(id = flask.session["user"]).first()

return flask.render_template("pay.html",
cost=cost,
user = theUser)

@app.route("/basket/payment/process", methods=["POST"])
def processPayment():
if not flask.session["user"]:
flask.flash("You need to be logged in")
return flask.redirect(flask.url_for("index"))

theUser = User.query.filter_by(id = flask.session["user"]).first()
sessionBasket = flask.session.get("basket", None)
theBasket, _ = getBasketAndTotal(sessionBasket)

lastItemId = None
for itemInfo in theBasket:
thePurchace = Purchace(userId = theUser.id, itemId = itemInfo[0].id)
db.session.add(thePurchace)
lastItemId = itemInfo[0].id

db.session.commit()
flask.session.pop("basket")
flask.flash("Payment success. Total card charge: " + flask.request.form.get("total"))
logging.info(flask.request.form.get("total"))

# redirect user to review page for the last item
return flask.redirect(flask.url_for("reviewItem", userId=theUser.id, itemId=lastItemId))

@app.errorhandler(404)
def page_not_found(e):
Expand Down
4 changes: 2 additions & 2 deletions app/templates/pay.html
Expand Up @@ -11,7 +11,7 @@ <h1>Payment</h1>


<!-- This does nothing -->
<form>
<form method="POST" action="/basket/payment/process" >
<div class="row">
<div class="input-field col s12">
<input placeholder="Placeholder" id="first_name" type="text" class="validate">
Expand All @@ -22,7 +22,7 @@ <h1>Payment</h1>
<input placeholder="Placeholder" id="first_name" type="text" class="validate">
<label for="first_name">Card Number</label>
</div>

<input type="hidden" value="{{ cost }}" name="total" >


<button class="waves-effect waves-light btn">Pay</button>
Expand Down