diff --git a/.gitignore b/.gitignore index b9a723e..c80a57f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ __pycache__/ used.json payments.json + +promo.json diff --git a/account.py b/account.py new file mode 100644 index 0000000..f9eb7db --- /dev/null +++ b/account.py @@ -0,0 +1,22 @@ +import os +import dotenv +import random + +dotenv.load_dotenv() + +EMAIL = os.getenv('LOGIN_EMAIL') +PASSWORD = os.getenv('LOGIN_PASSWORD') + +tokens = [] + +def login(email, password): + if email == EMAIL and password == PASSWORD: + token = str(random.randint(100000, 999999)) + tokens.append(token) + return token + return False + +def token(token): + if token in tokens: + return True + return False \ No newline at end of file diff --git a/payments.py b/payments.py index 20dbd76..dd3094a 100644 --- a/payments.py +++ b/payments.py @@ -29,6 +29,10 @@ else: with open(path+'used.json', 'w') as f: json.dump(used, f, indent=4) +if not os.path.exists(path+'promo.json'): + with open(path+'promo.json', 'w') as f: + json.dump([], f, indent=4) + HNSaddress = os.getenv('ADDRESS') names = { @@ -37,7 +41,7 @@ names = { } -def generate_payment(name,email,mobile,address,country,cart,hns): +def generate_payment(name,email,mobile,address,country,cart,hns,promo=False): # Generate a payment object payment_id = generate_payment_id() if payment_id == "ERROR": @@ -50,6 +54,11 @@ def generate_payment(name,email,mobile,address,country,cart,hns): if item['name'] in names: item['name'] = names[item['name']] + if promo: + promo = promo["id"] + usePromo(promo) + + payment = { "name": name, "email": email, @@ -59,6 +68,7 @@ def generate_payment(name,email,mobile,address,country,cart,hns): "hns": hns, "ID": finalPrice, "cart": cart, + "promo": promo, "status": "Pending" } finalPriceDolarydoo = float(finalPrice) * 1000000 @@ -134,6 +144,68 @@ def generate_payment_id(): if id > 99: return "ERROR" + + +def getPromo(code): + with open(path+'promo.json', 'r') as f: + promos = json.load(f) + for promo in promos: + if promo['id'] == code: + return promo + return False + + +def getPromos(): + with open(path+'promo.json', 'r') as f: + promos = json.load(f) + return promos + +def addPromo(id, constant, percent, uses): + with open(path+'promo.json', 'r') as f: + promos = json.load(f) + + for promo in promos: + if promo['id'] == id: + return False + + + promos.append({ + "id": id, + "constant": constant, + "percent": percent, + "uses": uses + }) + with open(path+'promo.json', 'w') as f: + json.dump(promos, f, indent=4) + return True + +def deletePromo(id): + with open(path+'promo.json', 'r') as f: + promos = json.load(f) + for promo in promos: + if promo['id'] == id: + promos.remove(promo) + with open(path+'promo.json', 'w') as f: + json.dump(promos, f, indent=4) + return True + return False + +def usePromo(id): + with open(path+'promo.json', 'r') as f: + promos = json.load(f) + for promo in promos: + if promo['id'] == id: + if promo['uses'] == "1": + promos.remove(promo) + elif promo['uses'] == "-1": + return True + + uses = int(promo['uses']) + uses -= 1 + promo['uses'] = str(uses) + with open(path+'promo.json', 'w') as f: + json.dump(promos, f, indent=4) + return True if __name__ == '__main__': for i in range(10): diff --git a/render.py b/render.py index cd43830..80517a4 100644 --- a/render.py +++ b/render.py @@ -68,7 +68,7 @@ def cart_total(cert): price = prices[path] else: name = path - image = "tech/image2.jpg" + image = "pins/pack1.jpg" price = 45 hns = usdToHNS(price, True) @@ -122,7 +122,7 @@ def usdToHNS(usd, returnInt=False): return hns -def total_usd(cert): +def total_usd(cert,promo): total = 0 for item in cert: path = item['name'] @@ -130,18 +130,33 @@ def total_usd(cert): total += prices[path] * int(item['quantity']) else: total += 45 * int(item['quantity']) + + if promo: + if promo["constant"]: + total -= int(promo["constant"]) + else: + total = total - (total * int(promo["percent"]) / 100) + return total -def total_hns(cert, returnInt=False): - total = 0 - for item in cert: - path = item['name'] - if path in prices: - total += usdToHNS(prices[path],True) * int(item['quantity']) - else: - total += usdToHNS(45,True) * int(item['quantity']) +def total_hns(cert, promo, returnInt=False): + total = total_usd(cert, promo) + total = usdToHNS(total,True) if returnInt: return int(total) total ="{:,}".format(total) - return total \ No newline at end of file + return total + + +def promoList(promo): + html = '' + return html \ No newline at end of file diff --git a/server.py b/server.py index fc0d017..c85fa73 100644 --- a/server.py +++ b/server.py @@ -7,6 +7,7 @@ import json import render import payments import threading +import account app = Flask(__name__) dotenv.load_dotenv() @@ -170,13 +171,41 @@ def cart(): else: cart = '[]' + promo_message = '' + if 'promo' in cookies: + promo = cookies['promo'] + else: + promo = '' + cart = json.loads(cart) cartHtml = render.cart(cart) - total_usd = render.total_usd(cart) - total_hns = render.total_hns(cart) + if promo != '': + promo = payments.getPromo(promo) + if not promo: + promo_message = 'Invalid promo code' + promo = '' + else: + promo_message = f'Promo code {promo["id"]} applied' - return render_template('cart.html', year=year, cart=cartHtml, total_usd=total_usd, total_hns=total_hns) + total_usd = render.total_usd(cart,promo) + total_hns = render.total_hns(cart,promo) + promoCode = '' + if promo: + promoCode = promo["id"] + else: + total_usd = render.total_usd(cart, False) + total_hns = render.total_hns(cart, False) + promoCode = '' + + return render_template('cart.html', year=year, cart=cartHtml, total_usd=total_usd, total_hns=total_hns,promo=promoCode, promo_message=promo_message) + +@app.route('/cart/promo', methods=['POST']) +def cart_promo(): + data = request.form + response = make_response(redirect('/cart')) + response.set_cookie('promo', data['promo']) + return response @app.route('/payment') def payment(): @@ -194,8 +223,17 @@ def payment(): cart = json.loads(cart) cartHtml = render.cart_total(cart) - total_usd = render.total_usd(cart) - total_hns = render.total_hns(cart) + if 'promo' in cookies: + promo = cookies['promo'] + promo = payments.getPromo(promo) + total_usd = render.total_usd(cart,promo) + total_hns = render.total_hns(cart,promo) + else: + promo = '' + total_usd = render.total_usd(cart, False) + total_hns = render.total_hns(cart, False) + + return render_template('payment.html', year=year, cart=cartHtml, total_usd=total_usd, total_hns=total_hns) @@ -212,8 +250,16 @@ def payment_post(): return redirect('/cart') cart = json.loads(cart) - total_usd = render.total_usd(cart) - total_hns = render.total_hns(cart) + if 'promo' in cookies: + promo = cookies['promo'] + promo = payments.getPromo(promo) + total_usd = render.total_usd(cart,promo) + total_hns = render.total_hns(cart,promo) + else: + promo = False + total_usd = render.total_usd(cart, False) + total_hns = render.total_hns(cart, False) + data = request.form if 'email' in data: @@ -242,15 +288,13 @@ def payment_post(): cartHtml = render.cart_total(cart) - if email == '' or address == '' or name == '' or country == '' or mobile == '': return render_template('payment.html', error='Please fill all fields', email=email, address=address, name=name, mobile=mobile, country=country, cart=cartHtml, total_usd=total_usd, total_hns=total_hns) - # All good, check out - payment = payments.generate_payment(name, email, mobile,address,country, cart, render.total_hns(cart, True)) + payment = payments.generate_payment(name, email, mobile,address,country, cart, render.total_hns(cart, promo,True), promo) if payment == False: return render_template('payment.html', error='There was an error processing your payment', email=email, address=address, name=name, @@ -265,9 +309,67 @@ def payment_post(): responce = make_response(render_template('payment_info.html',cart=cartHtml, total_hns=finalPrice,address=HNSaddress, year=datetime.datetime.now().year, qr=qr)) responce.set_cookie('cart', '[]') - return responce + +# Promo routes +@app.route('/promo') +def promo(): + year = datetime.datetime.now().year + # Check user is logged in + token = request.cookies.get('token') + if not token: + return redirect('/login') + + if not account.token(token): + return redirect('/login') + + promos = payments.getPromos() + promos = render.promoList(promos) + + return render_template('promo.html', year=year, promos=promos) + +@app.route('/promo', methods=['POST']) +def promo_post(): + data = request.form + token = request.cookies.get('token') + if not token: + return redirect('/login') + + if not account.token(token): + return redirect('/login') + + id = data['code'] + constant = data['constant'] + percent = data['percent'] + uses = data['uses'] + payments.addPromo(id, constant, percent, uses) + + return redirect('/promo') + +@app.route('/promo/delete/') +def promo_delete(path): + token = request.cookies.get('token') + if not token: + return redirect('/login') + + if not account.token(token): + return redirect('/login') + + payments.deletePromo(path) + return redirect('/promo') + +@app.route('/login', methods=['POST']) +def login(): + data = request.form + token = account.login(data['email'], data['password']) + if token: + response = make_response(redirect('/promo')) + response.set_cookie('token', token) + return response + return redirect('/login') + + # Main routes @app.route('/') def index(): @@ -305,7 +407,4 @@ def check_payments(): payments.check_payments() if __name__ == '__main__': - # Set timer for payments - - repeat_check_payments() app.run(debug=True, port=5000, host='0.0.0.0') \ No newline at end of file diff --git a/templates/assets/css/vanilla-zoom.min.css b/templates/assets/css/vanilla-zoom.min.css index 6607dfe..8f32918 100644 --- a/templates/assets/css/vanilla-zoom.min.css +++ b/templates/assets/css/vanilla-zoom.min.css @@ -39,3 +39,13 @@ text-decoration: none; } +.align-right { + position: absolute; + right: 1px; + top: 1px; +} + +.price { + font-size: medium !important; +} + diff --git a/templates/cart.html b/templates/cart.html index 783c167..41f2764 100644 --- a/templates/cart.html +++ b/templates/cart.html @@ -77,6 +77,7 @@

Summary

+
{{promo_message}}

Total USDUS${{total_usd}}

Total{{total_hns}} HNS

Checkout
diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..07de22a --- /dev/null +++ b/templates/login.html @@ -0,0 +1,109 @@ + + + + + + + Login - HNSAU + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+

Log In

+
+
+
+
+
+
+
+
+ + + + + + + + \ No newline at end of file diff --git a/templates/promo.html b/templates/promo.html new file mode 100644 index 0000000..6631db9 --- /dev/null +++ b/templates/promo.html @@ -0,0 +1,114 @@ + + + + + + + Login - HNSAU + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+

Create Promo Code

+
+
+
+
+
+
Enter -1 for unlimited uses
+
+
+
+

Existing Promo Codes

{{promos | safe}} +
+
+
+ + + + + + + + \ No newline at end of file diff --git a/templates/sitemap.xml b/templates/sitemap.xml index 8c43408..ac7f791 100644 --- a/templates/sitemap.xml +++ b/templates/sitemap.xml @@ -39,6 +39,9 @@ https://hns.au/links + + https://hns.au/login + https://hns.au/payment @@ -48,6 +51,9 @@ https://hns.au/pins + + https://hns.au/promo + https://hns.au/uv