Spaces:
Sleeping
Sleeping
File size: 11,849 Bytes
02a5061 f37257e 02a5061 f37257e 9a606ad f37257e 02a5061 f37257e 9a606ad f37257e 02a5061 f37257e 02a5061 f37257e 02a5061 f37257e 9a606ad f37257e 02a5061 f37257e 02a5061 f37257e 6448428 |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
from flask import Flask, render_template, request, redirect, url_for, jsonify
from flask_sqlalchemy import SQLAlchemy
import uuid
from datetime import datetime
import requests
import time
import threading
from cryptography.fernet import Fernet
import os
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///astrapay.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'your-secret-key-here'
db = SQLAlchemy(app)
def get_encryption_key():
key_file = 'encryption.key'
if os.path.exists(key_file):
with open(key_file, 'rb') as f:
return f.read()
else:
key = Fernet.generate_key()
with open(key_file, 'wb') as f:
f.write(key)
return key
ENCRYPTION_KEY = get_encryption_key()
cipher_suite = Fernet(ENCRYPTION_KEY)
def encrypt_value(value):
if isinstance(value, str):
value = value.encode()
return cipher_suite.encrypt(value).decode()
def decrypt_value(encrypted_value):
return cipher_suite.decrypt(encrypted_value.encode()).decode()
ENCRYPTED_CONNECT_SID = encrypt_value('s%3A7Bwbww_JK7pW55D-B57-3b7bi-FzrzEZ.ezXPafCUs8%2B9L632zAZaBD51TQfSnUXUwbga8Z9kyAo')
ENCRYPTED_API_URL = encrypt_value('https://astra-bank-moh1812.replit.app/api/transactions')
ENCRYPTED_REFERER = encrypt_value('https://astra-bank-moh1812.replit.app/')
ENCRYPTED_ETAG = encrypt_value('W/"6e3-L0zHI4rHMa4nHmyewyA/4y+lL6c"')
ENCRYPTED_BANK_BASE_URL = encrypt_value('https://astra-bank-moh1812.replit.app')
class PaymentLink(db.Model):
id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
amount = db.Column(db.Integer, nullable=False)
description = db.Column(db.String(500), nullable=False)
recipient_email = db.Column(db.String(255), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
paid = db.Column(db.Boolean, default=False)
def __repr__(self):
return f'<PaymentLink {self.id}: {self.amount} Astras>'
def get_transactions():
url = decrypt_value(ENCRYPTED_API_URL)
referer = decrypt_value(ENCRYPTED_REFERER)
etag = decrypt_value(ENCRYPTED_ETAG)
connect_sid = decrypt_value(ENCRYPTED_CONNECT_SID)
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'if-none-match': etag,
'priority': 'u=1, i',
'referer': referer,
'sec-ch-ua': '"Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
}
cookies = {
'sidebar_state': 'true',
'connect.sid': connect_sid
}
try:
response = requests.get(url, headers=headers, cookies=cookies, timeout=10)
return response.json()
except Exception as e:
print(f"Error fetching transactions: {e}")
return []
def check_for_payment(amount, existing_transaction_ids):
try:
transactions = get_transactions()
for transaction in transactions:
transaction_id = transaction.get('id')
if (transaction_id not in existing_transaction_ids and
transaction.get('amount') == amount and
transaction.get('transactionType') == 'received'):
return transaction
return None
except Exception as e:
print(f"Error checking for payment: {e}")
return None
def get_user_uuid(email):
try:
base_url = decrypt_value(ENCRYPTED_BANK_BASE_URL)
connect_sid = decrypt_value(ENCRYPTED_CONNECT_SID)
referer = decrypt_value(ENCRYPTED_REFERER)
url = f"{base_url}/api/user"
params = {'email': email}
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'no-cache',
'priority': 'u=1, i',
'referer': referer,
'sec-ch-ua': '"Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99"',
'sec-ch-ua-mobile': '?1',
'sec-ch-ua-platform': '"Android"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Mobile Safari/537.36'
}
cookies = {
'connect.sid': connect_sid
}
response = requests.get(url, params=params, headers=headers, cookies=cookies, timeout=10)
if response.status_code == 200:
return response.text.strip()
else:
print(f"Error getting user UUID: {response.status_code} - {response.text}")
return None
except Exception as e:
print(f"Error getting user UUID: {e}")
return None
def send_money(to_user_id, amount, description):
try:
base_url = decrypt_value(ENCRYPTED_BANK_BASE_URL)
connect_sid = decrypt_value(ENCRYPTED_CONNECT_SID)
referer = decrypt_value(ENCRYPTED_REFERER)
url = f"{base_url}/api/send-money"
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'no-cache',
'content-type': 'application/json',
'origin': base_url,
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': referer,
'sec-ch-ua': '"Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99"',
'sec-ch-ua-mobile': '?1',
'sec-ch-ua-platform': '"Android"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Mobile Safari/537.36'
}
cookies = {
'connect.sid': connect_sid
}
data = {
'toUserId': to_user_id,
'amount': amount,
'description': description
}
response = requests.post(url, json=data, headers=headers, cookies=cookies, timeout=10)
if response.status_code == 200:
return True, response.json()
else:
print(f"Error sending money: {response.status_code} - {response.text}")
return False, response.text
except Exception as e:
print(f"Error sending money: {e}")
return False, str(e)
with app.app_context():
db.create_all()
@app.route('/')
def home():
return render_template('home.html')
@app.route('/create-payment-link', methods=['POST'])
def create_payment_link():
try:
data = request.get_json()
if not data or 'amount' not in data or 'description' not in data:
return jsonify({'error': 'Missing required fields'}), 400
amount = data['amount']
description = data['description']
recipient_email = data.get('recipient_email', '').strip() if data.get('recipient_email') else None
if not isinstance(amount, int) or amount <= 0:
return jsonify({'error': 'Invalid amount'}), 400
if not description or len(description.strip()) == 0:
return jsonify({'error': 'Description cannot be empty'}), 400
payment_link = PaymentLink(
amount=amount,
description=description.strip(),
recipient_email=recipient_email if recipient_email else None
)
db.session.add(payment_link)
db.session.commit()
payment_url = url_for('view_payment', payment_id=payment_link.id, _external=True)
return jsonify({
'success': True,
'payment_id': payment_link.id,
'payment_url': payment_url
})
except Exception as e:
db.session.rollback()
return jsonify({'error': str(e)}), 500
@app.route('/init-payment-check/<payment_id>')
def init_payment_check(payment_id):
try:
payment_link = PaymentLink.query.get_or_404(payment_id)
transactions = get_transactions()
existing_ids = [tx.get('id') for tx in transactions]
return jsonify({
'existing_transaction_ids': existing_ids,
'amount': payment_link.amount
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/check-payment/<payment_id>')
def check_payment_status(payment_id):
try:
payment_link = PaymentLink.query.get_or_404(payment_id)
existing_ids_str = request.args.get('existing_ids', '')
if existing_ids_str:
existing_ids = existing_ids_str.split(',')
else:
transactions = get_transactions()
existing_ids = [tx.get('id') for tx in transactions]
payment_transaction = check_for_payment(payment_link.amount, existing_ids)
if payment_transaction:
return jsonify({
'payment_received': True,
'transaction': {
'id': payment_transaction.get('id'),
'amount': payment_transaction.get('amount'),
'from_email': payment_transaction.get('counterpartEmail'),
'description': payment_transaction.get('description')
}
})
else:
return jsonify({'payment_received': False})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/pay/<payment_id>', methods=['POST'])
def process_payment(payment_id):
try:
payment_link = PaymentLink.query.get_or_404(payment_id)
if payment_link.paid:
return jsonify({'error': 'Payment already completed'}), 400
payment_link.paid = True
db.session.commit()
if payment_link.recipient_email:
try:
user_uuid = get_user_uuid(payment_link.recipient_email)
if user_uuid:
success, result = send_money(
to_user_id=user_uuid,
amount=payment_link.amount,
description=payment_link.description
)
if success:
print(f"Successfully sent {payment_link.amount} Astras to {payment_link.recipient_email}")
else:
print(f"Failed to send money: {result}")
else:
print(f"Could not find user UUID for email: {payment_link.recipient_email}")
except Exception as e:
print(f"Error processing recipient payment: {e}")
return jsonify({'success': True, 'message': 'Payment confirmed successfully'})
except Exception as e:
db.session.rollback()
return jsonify({'error': str(e)}), 500
@app.route('/pay/<payment_id>')
def view_payment(payment_id):
payment_link = PaymentLink.query.get_or_404(payment_id)
return render_template('payment.html',
amount=payment_link.amount,
description=payment_link.description,
paid=payment_link.paid,
payment_id=payment_link.id,
recipient_email=payment_link.recipient_email)
@app.route('/bank')
def bank():
return render_template('bank.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=7860)
|