update
This commit is contained in:
parent
dfb7f1288e
commit
9a2529bc4d
3 changed files with 114 additions and 7 deletions
|
@ -8,8 +8,9 @@ class UserWallet(db.Model):
|
|||
user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
balance = db.Column(db.Integer, default=0)
|
||||
|
||||
# def __init__(self):
|
||||
# pass
|
||||
def __init__(self, user, balance=0):
|
||||
self.user = user
|
||||
self.balance = balance
|
||||
|
||||
def __repr__(self):
|
||||
return '<UserWallet id={} user={} balance={}>'.format(
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
{% block content %}
|
||||
<h2>Кошелёк</h2>
|
||||
<p>Баланс: $100500</p>
|
||||
<p>Баланс: {{ balance }} рублей</p>
|
||||
<h2>Пополнить баланс</h2>
|
||||
<form action="" method="post" class="mui-form">
|
||||
<form action="{{ url_for('wallet.robokassa', action='proccess') }}" method="post" class="mui-form">
|
||||
<div class="mui-textfield">
|
||||
<input type='text' name="" value="" placeholder="100" />
|
||||
<input type='text' name="amount" value="" placeholder="100" />
|
||||
</div>
|
||||
<input type='submit' value="pay" class="mui-btn mui-btn--raised" />
|
||||
</form>
|
||||
|
|
|
@ -7,7 +7,7 @@ from jinja2 import TemplateNotFound
|
|||
from wotstats.openid import oid
|
||||
|
||||
from wotstats.database import db
|
||||
from wotstats.models import User
|
||||
from wotstats.models import User, UserWallet
|
||||
from wotstats.lib import parse_wargaming_openid_url
|
||||
|
||||
pages_wallet = Blueprint(
|
||||
|
@ -19,6 +19,112 @@ def index():
|
|||
if not g.user:
|
||||
return redirect(url_for('pages_home.index'))
|
||||
|
||||
if UserWallet.query.filter_by(user=session['user']).count() == 0:
|
||||
n = UserWallet(session['user'], 0)
|
||||
db.session.add(n)
|
||||
db.session.commit()
|
||||
|
||||
balance = UserWallet.query.with_entities(
|
||||
UserWallet.balance
|
||||
).filter_by(
|
||||
user=session['user']
|
||||
).scalar()
|
||||
|
||||
return render_template(
|
||||
'pages/wallet/index.html'
|
||||
'pages/wallet/index.html',
|
||||
balance=balance
|
||||
)
|
||||
|
||||
@pages_wallet.route('/robokassa/<action>', methods=['GET', 'POST'])
|
||||
def robokassa(action):
|
||||
if action == 'process':
|
||||
if not g.user:
|
||||
return redirect(url_for('pages_home.index'))
|
||||
|
||||
user_id = session['user']
|
||||
amount = request.form.get('amount', 0)
|
||||
|
||||
# create transaction data to database
|
||||
transaction_id = controller_robokassa.transaction_create(user_id, amount, 'robokassa', 'process')
|
||||
|
||||
payment_details = {
|
||||
"payment_id": transaction_id,
|
||||
"amount": amount,
|
||||
"login": controller_robokassa.args['PAY_ROBOKASSA_LOGIN'],
|
||||
"password": controller_robokassa.args['PAY_ROBOKASSA_PASSWORD1'],
|
||||
"signature": ''
|
||||
}
|
||||
payment_details["signature"] = md5(
|
||||
"%(login)s:%(amount)s:%(payment_id)s:%(password)s" % payment_details
|
||||
).hexdigest()
|
||||
# print payment_details
|
||||
return render_template('pages/payment/robokassa/process.html', payment=payment_details)
|
||||
|
||||
if action == 'result':
|
||||
if request.method == 'POST':
|
||||
transaction_id = request.form['InvId']
|
||||
signature = request.form['SignatureValue']
|
||||
amount = request.form['OutSum']
|
||||
|
||||
transaction_hash = md5("%s:%s:%s" % (amount, transaction_id, controller_robokassa.args['PAY_ROBOKASSA_PASSWORD2'])).hexdigest()
|
||||
# print transaction_hash
|
||||
# print signature.lower()
|
||||
if signature.lower() == transaction_hash.lower():
|
||||
# update transaction signature
|
||||
controller_robokassa.transaction_set_notified(transaction_id, 1)
|
||||
# update user balance
|
||||
controller_robokassa.balance_update(transaction_id, amount)
|
||||
|
||||
# update transaction signature
|
||||
transaction = models.UsersBalanceTransactions.get(models.UsersBalanceTransactions.id == transaction_id)
|
||||
transaction.status = 'success'
|
||||
transaction.save()
|
||||
else:
|
||||
return jsonify(error="invalid signature")
|
||||
return render_template('pages/payment/robokassa/result.html')
|
||||
return redirect(url_for('account.billing'))
|
||||
|
||||
|
||||
|
||||
if action == 'success':
|
||||
# check session
|
||||
if not ControllerUsers().check_session():
|
||||
return redirect(url_for("account.logout"))
|
||||
# auth user
|
||||
if not ControllerUsers().auth(session['email'], session['password']):
|
||||
return redirect(url_for("account.logout"))
|
||||
|
||||
if request.method == "POST":
|
||||
# print request.form
|
||||
# culture = request.form['Culture']
|
||||
# transaction_id = request.form.get('InvId')
|
||||
|
||||
# TODO: если эта часть делается на шаге `results`, то можно убрать его
|
||||
# update transaction signature
|
||||
# transaction = models.UsersBalanceTransactions.get(models.UsersBalanceTransactions.id == transaction_id)
|
||||
# transaction.status = 'success'
|
||||
# transaction.save()
|
||||
|
||||
return redirect(url_for('payments.success'))
|
||||
return redirect(url_for('payments.success'))
|
||||
|
||||
if action == 'fail':
|
||||
# check session
|
||||
if not ControllerUsers().check_session():
|
||||
return redirect(url_for("account.logout"))
|
||||
|
||||
# auth user
|
||||
if not ControllerUsers().auth(session['email'], session['password']):
|
||||
return redirect(url_for("account.logout"))
|
||||
|
||||
if request.method == "POST":
|
||||
# print request.form
|
||||
|
||||
transaction_id = request.form['InvId']
|
||||
|
||||
# update transaction signature
|
||||
transaction = models.UsersBalanceTransactions.get(models.UsersBalanceTransactions.id == transaction_id)
|
||||
transaction.status = 'fail'
|
||||
transaction.save()
|
||||
|
||||
return redirect(url_for('payments.fail'))
|
||||
|
|
Reference in a new issue