-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (56 loc) · 2.89 KB
/
app.py
File metadata and controls
79 lines (56 loc) · 2.89 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
import json, os, datetime
from flask_migrate import Migrate
from models import GetwayType, Payments
from serializer import PaymentsSerializer
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.getcwd() + '/payments.db'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
@app.route('/payment', methods=['GET'])
def query_records():
name = Payments.query.all()
post_schema = PaymentsSerializer()
posts_schema = PaymentsSerializer(many=True)
return {"data": posts_schema.dump(name)}
@app.route('/addpayment', methods=['POST'])
def addpost():
try:
credit_card_number = request.form['credit_card_number']
card_holder = request.form['card_holder']
expiration_date = request.form['expiration_date']
security_code = request.form['security_code']
amount = request.form['amount']
if len(credit_card_number) != 16:
return json.dumps({'error': "Not a valid card number"}), 400, {'ContentType':'application/json'}
if datetime.datetime.fromisoformat(expiration_date) < datetime.datetime.now():
return json.dumps({'success': "Not a valid Expiration Date"}), 400, {'ContentType':'application/json'}
if amount.isnumeric():
if int(amount) > 0 and int(amount) <= 20:
getway_tp = GetwayType.query.filter_by(name='CheapPaymentGateway').first()
elif int(amount) >= 21 and int(amount) <= 400:
getway_tp = GetwayType.query.filter_by(name='ExpensivePaymentGateway').first()
elif int(amount) > 400:
getway_tp = GetwayType.query.filter_by(name='PremiumPaymentGateway').first()
else:
return json.dumps({'success': "Not a valid amount"}), 400, {'ContentType':'application/json'}
else:
return json.dumps({'success': "Not a valid amount"}), 400, {'ContentType':'application/json'}
payment = Payments(credit_card_number = credit_card_number,
card_holder = card_holder,
expiration_date = datetime.datetime.fromisoformat(expiration_date),
security_code = security_code,
amount = int(amount),
deleted = False,
create_date = datetime.datetime.now(),
getway_type_id = getway_tp.id)
db.session.add(payment)
db.session.commit()
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
except Exception as e:
return json.dumps({}), 400, {'ContentType':'application/json'}
if __name__ == '__main__':
app.run(debug=True)