2015-12-01 02:43:10 +03:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
from hashlib import md5
|
2016-04-02 00:42:22 +03:00
|
|
|
|
|
|
|
|
|
from SWSCloudCore.controllers.payments import ControllerPaymentsRobokassa
|
|
|
|
|
from flask import Blueprint
|
2015-12-01 02:43:10 +03:00
|
|
|
|
from flask import g
|
2016-04-02 00:42:22 +03:00
|
|
|
|
from flask import redirect
|
2015-12-01 02:43:10 +03:00
|
|
|
|
from flask import render_template
|
2016-04-02 00:42:22 +03:00
|
|
|
|
from flask import request
|
2015-12-01 02:43:10 +03:00
|
|
|
|
from flask import session
|
2016-05-28 02:24:21 +03:00
|
|
|
|
from flask import url_for, jsonify
|
2016-04-02 00:42:22 +03:00
|
|
|
|
|
|
|
|
|
from SWSCloudCore import models
|
|
|
|
|
from SWSCloudCore.controllers.users import ControllerUsers
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
viewPayments = Blueprint('payments', __name__, url_prefix='/payments')
|
|
|
|
|
|
|
|
|
|
|
2016-05-28 02:24:21 +03:00
|
|
|
|
@viewPayments.route('/fail.html')
|
|
|
|
|
def fail():
|
|
|
|
|
return render_template('default/payment/fail.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@viewPayments.route('/success.html')
|
|
|
|
|
def success():
|
|
|
|
|
return render_template('default/payment/success.html')
|
|
|
|
|
|
|
|
|
|
|
2015-12-01 02:43:10 +03:00
|
|
|
|
@viewPayments.route('/robokassa/<action>', methods=['GET', 'POST'])
|
|
|
|
|
def robokassa(action):
|
|
|
|
|
controller_robokassa = ControllerPaymentsRobokassa(
|
|
|
|
|
PAY_ROBOKASSA_MODE=g.settings['PAY_ROBOKASSA_MODE'],
|
|
|
|
|
PAY_ROBOKASSA_LOGIN=g.settings['PAY_ROBOKASSA_LOGIN'],
|
|
|
|
|
PAY_ROBOKASSA_PASSWORD1=g.settings['PAY_ROBOKASSA_PASSWORD1'],
|
|
|
|
|
PAY_ROBOKASSA_PASSWORD2=g.settings['PAY_ROBOKASSA_PASSWORD2'],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if action == 'process':
|
|
|
|
|
# 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"))
|
|
|
|
|
|
|
|
|
|
user_id = session['user_id']
|
|
|
|
|
amount = request.form['amount']
|
|
|
|
|
|
|
|
|
|
# create transaction data to database
|
|
|
|
|
transaction_id = controller_robokassa.transaction_create(user_id, amount, 'process')
|
|
|
|
|
|
2015-12-06 17:18:48 +03:00
|
|
|
|
payment_details = {
|
|
|
|
|
"payment_id": transaction_id,
|
|
|
|
|
"amount": amount,
|
|
|
|
|
"login": controller_robokassa.args['PAY_ROBOKASSA_LOGIN'],
|
|
|
|
|
"password": controller_robokassa.args['PAY_ROBOKASSA_PASSWORD1'],
|
|
|
|
|
"signature": ''
|
|
|
|
|
}
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2015-12-06 17:18:48 +03:00
|
|
|
|
payment_details["signature"] = md5(
|
|
|
|
|
"%(login)s:%(amount)s:%(payment_id)s:%(password)s" % payment_details
|
|
|
|
|
).hexdigest()
|
2016-05-28 02:24:21 +03:00
|
|
|
|
# print payment_details
|
2015-12-06 17:18:48 +03:00
|
|
|
|
return render_template('default/payment/robokassa/process.html', payment=payment_details)
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
if action == 'result':
|
2016-05-28 02:24:21 +03:00
|
|
|
|
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('default/payment/robokassa/result.html')
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
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":
|
2016-05-28 02:24:21 +03:00
|
|
|
|
# print request.form
|
2015-12-01 02:43:10 +03:00
|
|
|
|
# culture = request.form['Culture']
|
2016-05-28 02:24:21 +03:00
|
|
|
|
# transaction_id = request.form.get('InvId')
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2016-05-28 02:24:21 +03:00
|
|
|
|
# TODO: если эта часть делается на шаге `results`, то можно убрать его
|
2015-12-01 02:43:10 +03:00
|
|
|
|
# update transaction signature
|
2016-05-28 02:24:21 +03:00
|
|
|
|
# transaction = models.UsersBalanceTransactions.get(models.UsersBalanceTransactions.id == transaction_id)
|
|
|
|
|
# transaction.status = 'success'
|
|
|
|
|
# transaction.save()
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2016-05-28 02:24:21 +03:00
|
|
|
|
return redirect(url_for('payments.success'))
|
|
|
|
|
return redirect(url_for('payments.success'))
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
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":
|
2016-05-28 02:24:21 +03:00
|
|
|
|
# print request.form
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
transaction_id = request.form['InvId']
|
|
|
|
|
|
|
|
|
|
# update transaction signature
|
|
|
|
|
transaction = models.UsersBalanceTransactions.get(models.UsersBalanceTransactions.id == transaction_id)
|
|
|
|
|
transaction.status = 'fail'
|
|
|
|
|
transaction.save()
|
|
|
|
|
|
2016-05-28 02:24:21 +03:00
|
|
|
|
return redirect(url_for('payments.fail'))
|