Update app.py
Browse files
app.py
CHANGED
@@ -22,10 +22,7 @@ import psutil
|
|
22 |
|
23 |
|
24 |
|
25 |
-
|
26 |
-
import hashlib
|
27 |
-
from urllib import parse
|
28 |
-
from urllib.parse import urlparse
|
29 |
|
30 |
|
31 |
|
@@ -62,18 +59,6 @@ ws_url_ver = "/checkWhatsapp/"
|
|
62 |
|
63 |
|
64 |
|
65 |
-
# регистрационная информация (Идентификатор магазина, пароль №1)
|
66 |
-
merchant_login = "demo"
|
67 |
-
password_1 = os.getenv('robo_pass_1')
|
68 |
-
password_2 = os.getenv('robo_pass_2')
|
69 |
-
# номер заказа (изначально пустой)
|
70 |
-
invid = 0
|
71 |
-
# предлагаемая валюта платежа
|
72 |
-
incurrlabel = "BANKOCEAN2R"
|
73 |
-
# язык
|
74 |
-
culture = "ru"
|
75 |
-
# кодировка
|
76 |
-
encoding = "utf-8"
|
77 |
|
78 |
|
79 |
|
@@ -1032,151 +1017,6 @@ def add_user_senler_full():
|
|
1032 |
|
1033 |
|
1034 |
|
1035 |
-
# ROBO
|
1036 |
-
def calculate_signature(*args) -> str:
|
1037 |
-
"""Create signature MD5."""
|
1038 |
-
return hashlib.md5(':'.join(str(arg) for arg in args).encode()).hexdigest()
|
1039 |
-
|
1040 |
-
def parse_response(request: str) -> dict:
|
1041 |
-
"""
|
1042 |
-
:param request: Link.
|
1043 |
-
:return: Dictionary.
|
1044 |
-
"""
|
1045 |
-
params = {}
|
1046 |
-
for item in urlparse(request).query.split('&'):
|
1047 |
-
key, value = item.split('=')
|
1048 |
-
params[key] = value
|
1049 |
-
return params
|
1050 |
-
|
1051 |
-
def check_signature_result(
|
1052 |
-
order_number: int, # invoice number
|
1053 |
-
received_sum: decimal, # cost of goods, RU
|
1054 |
-
received_signature: hex, # SignatureValue
|
1055 |
-
password: str # Merchant password
|
1056 |
-
) -> bool:
|
1057 |
-
signature = calculate_signature(received_sum, order_number, password)
|
1058 |
-
return signature.lower() == received_signature.lower()
|
1059 |
-
|
1060 |
-
def generate_payment_link(
|
1061 |
-
merchant_login: str, # Merchant login
|
1062 |
-
merchant_password_1: str, # Merchant password
|
1063 |
-
cost: decimal, # Cost of goods, RU
|
1064 |
-
number: int, # Invoice number
|
1065 |
-
description: str, # Description of the purchase
|
1066 |
-
user_ip: str, # User IP address
|
1067 |
-
is_test: str, # Test mode flag
|
1068 |
-
expiration_date: str, # Срок действия счёта
|
1069 |
-
robokassa_payment_url = 'https://auth.robokassa.ru/Merchant/Index.aspx',
|
1070 |
-
) -> str:
|
1071 |
-
"""URL for redirection of the customer to the service."""
|
1072 |
-
signature = calculate_signature(
|
1073 |
-
merchant_login,
|
1074 |
-
cost,
|
1075 |
-
number,
|
1076 |
-
user_ip,
|
1077 |
-
merchant_password_1
|
1078 |
-
)
|
1079 |
-
|
1080 |
-
data = {
|
1081 |
-
'MerchantLogin': merchant_login,
|
1082 |
-
'OutSum': cost,
|
1083 |
-
'InvId': number,
|
1084 |
-
'Description': description,
|
1085 |
-
'SignatureValue': signature,
|
1086 |
-
'IsTest': is_test,
|
1087 |
-
'UserIp': user_ip,
|
1088 |
-
'ExpirationDate': expiration_date, # Добавляем срок действия счёта
|
1089 |
-
}
|
1090 |
-
return f'{robokassa_payment_url}?{parse.urlencode(data)}'
|
1091 |
-
|
1092 |
-
def update_rkassa(email, rkassa_data):
|
1093 |
-
for db in DATABASES:
|
1094 |
-
conn = sqlite3.connect(db)
|
1095 |
-
cursor = conn.cursor()
|
1096 |
-
|
1097 |
-
# Получаем текущие данные из столбца rkassa
|
1098 |
-
cursor.execute('SELECT rkassa FROM contacts WHERE email = ?', (email,))
|
1099 |
-
current_rkassa = cursor.fetchone()
|
1100 |
-
|
1101 |
-
if current_rkassa and current_rkassa[0]:
|
1102 |
-
# Если данные уже есть, добавляем новые через запятую
|
1103 |
-
new_rkassa = f"{current_rkassa[0]},{rkassa_data}"
|
1104 |
-
else:
|
1105 |
-
# Если данных нет, просто записываем новые
|
1106 |
-
new_rkassa = rkassa_data
|
1107 |
-
|
1108 |
-
# Обновляем столбец rkassa
|
1109 |
-
cursor.execute('UPDATE contacts SET rkassa = ? WHERE email = ?', (new_rkassa, email))
|
1110 |
-
conn.commit()
|
1111 |
-
conn.close()
|
1112 |
-
|
1113 |
-
@app.route('/set_invid', methods=['GET'])
|
1114 |
-
def set_invid():
|
1115 |
-
global invid
|
1116 |
-
invid = int(request.args.get('invid'))
|
1117 |
-
return jsonify({'status': 'success', 'invid': invid})
|
1118 |
-
|
1119 |
-
@app.route('/process_payment', methods=['POST'])
|
1120 |
-
def process_payment():
|
1121 |
-
global invid
|
1122 |
-
|
1123 |
-
# Получаем данные из формы
|
1124 |
-
out_sum = request.form.get('out_sum', '10.00')
|
1125 |
-
user_name = request.form.get('name', '')
|
1126 |
-
user_email = request.form.get('email', '')
|
1127 |
-
user_phone = request.form.get('phone', '')
|
1128 |
-
user_ip = request.form.get('user_ip', '')
|
1129 |
-
user_is_test = request.form.get('test', '0')
|
1130 |
-
|
1131 |
-
product_name = request.form.get('product_name', 'product')
|
1132 |
-
product_id = request.form.get('product_id', 'default_product_id')
|
1133 |
-
expiration_date = request.form.get('expiration_date', '2100-01-01T00:00:08.0+03:00')
|
1134 |
-
dostup_date = request.form.get('dostup_date', '2100-01-01T00:00:08.0+03:00')
|
1135 |
-
|
1136 |
-
# Проверяем, что invid установлен
|
1137 |
-
if invid is None:
|
1138 |
-
return jsonify({'status': 'error', 'message': 'invid is not set'})
|
1139 |
-
|
1140 |
-
# Увеличиваем invid на один
|
1141 |
-
invid += 1
|
1142 |
-
|
1143 |
-
# Формируем данные для запроса к Robokassa
|
1144 |
-
payment_link = generate_payment_link(
|
1145 |
-
merchant_login,
|
1146 |
-
password_1,
|
1147 |
-
decimal.Decimal(out_sum),
|
1148 |
-
invid,
|
1149 |
-
f"Оплата за {product_name}",
|
1150 |
-
user_ip,
|
1151 |
-
user_is_test,
|
1152 |
-
expiration_date # Добавляем срок действия счёта
|
1153 |
-
)
|
1154 |
-
|
1155 |
-
# Отправляем зап��ос на сервер Robokassa
|
1156 |
-
response = requests.get(payment_link)
|
1157 |
-
|
1158 |
-
# Записываем данные в базу данных
|
1159 |
-
rkassa_data = json.dumps({str(invid): {"data_dostup": dostup_date, "product": product_id}})
|
1160 |
-
update_rkassa(user_email, rkassa_data)
|
1161 |
-
|
1162 |
-
# Записываем текущее время в столбец data_on
|
1163 |
-
utc_now = datetime.utcnow()
|
1164 |
-
msk_tz = pytz.timezone('Europe/Moscow')
|
1165 |
-
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
1166 |
-
data_on = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1167 |
-
|
1168 |
-
for db in DATABASES:
|
1169 |
-
conn = sqlite3.connect(db)
|
1170 |
-
cursor = conn.cursor()
|
1171 |
-
cursor.execute('UPDATE contacts SET data_on = ?, name = ?, phone = ? WHERE email = ?', (data_on, user_name, user_phone, user_email))
|
1172 |
-
conn.commit()
|
1173 |
-
conn.close()
|
1174 |
-
|
1175 |
-
# Возвращаем ссылку на страницу оплаты
|
1176 |
-
return jsonify({'payment_url': payment_link})
|
1177 |
-
|
1178 |
-
|
1179 |
-
|
1180 |
|
1181 |
|
1182 |
|
|
|
22 |
|
23 |
|
24 |
|
25 |
+
|
|
|
|
|
|
|
26 |
|
27 |
|
28 |
|
|
|
59 |
|
60 |
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
|
64 |
|
|
|
1017 |
|
1018 |
|
1019 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1020 |
|
1021 |
|
1022 |
|