-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
87 lines (63 loc) · 2.8 KB
/
app.py
File metadata and controls
87 lines (63 loc) · 2.8 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
80
81
82
83
84
85
86
87
from flask import Flask, jsonify, request, send_file, render_template
from repository.database import db
from db_models.payment import Payment
from datetime import datetime, timedelta
from payments.Pix import Pix
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'SECRET_KEY_WEBSOCKET'
db.init_app(app)
socketio = SocketIO(app)
@app.route('/payments/pix', methods=['POST'])
def create_payment_pix():
data = request.get_json()
if 'value' not in data:
return jsonify({"error": "Missing 'value' field"}), 400
expiration_date = datetime.now() + timedelta(minutes=30)
new_payment = Payment(value=data['value'],
expiration_date=expiration_date)
#Criando o QRCODE
pix_obj = Pix()
data_payment_pix = pix_obj.create_payment()
new_payment.bank_payment_id = data_payment_pix['bank_payment_id']
new_payment.qr_code = data_payment_pix['qr_cide_path']
db.session.add(new_payment)
db.session.commit()
return jsonify({"message": "Payment has been created",
"payment": new_payment.to_dict()})
@app.route('/payments/pix/qr_code/<file_name>', methods=['GET'])
def get_image(file_name):
return send_file(f"static/img/{file_name}.png",mimetype='image/png')
@app.route('/payments/pix/confirmation', methods=['POST'])
def pix_confirmation():
data = request.get_json()
if 'bank_payment_id' not in data or 'value' not in data:
return jsonify({"error": "Invalid payment data"}), 400
payment = Payment.query.filter_by(bank_payment_id=data.get('bank_payment_id')).first()
if not payment or payment.paid:
return jsonify({"error": "Payment not found"}), 404
if payment.value!= data.get('value'):
return jsonify({"error": "Invalid payment data"}), 400
payment.paid = True
db.session.commit()
socketio.emit(f'payment-confirmed-{payment.id}')
return jsonify({"message": "Payment has been created successfully"})
@app.route('/payments/pix/<int:payment_id>', methods=['GET'])
def payment_pix_page(payment_id):
payment = Payment.query.get(payment_id)
if payment.paid == True:
return render_template('confirmed_payment.html',
payment_id=payment.id,
value=payment.value
)
return render_template('payment.html',
payment_id=payment.id,
value=payment.value,
host="http://127.0.0.1:5000",
qr_code=payment.qr_code)
@socketio.on('connect')
def handle_connect():
print('Client connected')
if __name__ == '__main__':
socketio.run(app, debug=True, allow_unsafe_werkzeug=True)