-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (45 loc) · 2.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
from flask import Flask, request, render_template
from utils.validation_utils import ValidationUtils
from utils.format_utils import FormatUtils
from utils.invoice_utils import InvoiceUtils
from utils.config_utils import get_base_url
app = Flask(__name__)
base_url = get_base_url()
@app.route('/invoices', methods=['POST'])
def invoices():
errors = ValidationUtils.validate_invoice_request(request)
if errors:
return json.dumps(errors), 400
new_invoice_url = InvoiceUtils.create_invoice(request.json, base_url)
response_data = {'data': {'url': new_invoice_url}}
return json.dumps(response_data), 200
@app.route('/pay_invoice/<invoice_id>', methods=['GET', 'POST'])
def pay_invoice(invoice_id):
if request.method == 'POST':
payment_amount = float(request.form.get("payment_amount"))
invoice = InvoiceUtils.pay_invoice(invoice_id, payment_amount)
remaining_balance = float(invoice['amount'])
if remaining_balance <= 0:
return render_template('payment_complete.html',
invoice=invoice)
else:
return render_template('pay_invoice.html',
invoice_id=invoice_id,
invoice=invoice,
payment_made=FormatUtils.format_currency_value(payment_amount),
base_url=base_url)
elif request.method == 'GET':
invoice = InvoiceUtils.get_invoice(invoice_id)
remaining_balance = float(invoice['amount'])
if remaining_balance <= 0:
return render_template('payment_complete.html',
invoice=invoice)
else:
return render_template('pay_invoice.html',
invoice_id=invoice_id,
invoice=invoice,
payment_made=None,
base_url=base_url)
if __name__ == '__main__':
app.run()