from flask import Flask, request, jsonify, render_template import logging import os from pathlib import Path from .handlers.webhook_handler import handle_payment_webhook from .handlers.payment_handler import BTCPayHandler from .utils.db.operations import DatabaseManager logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) app = Flask(__name__) app.config['SECRET_KEY'] = os.getenv('FLASK_SECRET_KEY', 'dev-secret-key') btcpay_handler = BTCPayHandler() # Register blueprints from .routes.user import user_bp app.register_blueprint(user_bp, url_prefix='/user') # Existing webhook route @app.route('/webhook/vpn', methods=['POST']) def handle_payment(): return handle_payment_webhook(request) # Frontend routes @app.route('/') def index(): return render_template('index.html') @app.route('/api/calculate-price', methods=['POST']) def calculate_price(): hours = request.json.get('hours', 0) # Basic price calculation - adjust formula as needed base_price = hours * 100 # 100 sats per hour # Apply volume discounts if hours >= 720: # 1 month base_price = base_price * 0.85 # 15% discount elif hours >= 168: # 1 week base_price = base_price * 0.90 # 10% discount elif hours >= 24: # 1 day base_price = base_price * 0.95 # 5% discount return jsonify({ 'price': int(base_price), 'duration': hours }) @app.route('/create-invoice', methods=['POST']) def create_invoice(): try: logger.info("=== Create Invoice Request Started ===") logger.info(f"Received invoice creation request with data: {request.json}") data = request.json logger.debug(f"Request data: {data}") # Validate input data duration_hours = data.get('duration') user_id = data.get('user_id') public_key = data.get('public_key') logger.info(f"Validating request parameters: duration={duration_hours}, user_id={user_id}, has_public_key={bool(public_key)}") # Validate required fields if not duration_hours: logger.error("Duration missing from request") return jsonify({'error': 'Duration is required'}), 400 if not user_id: logger.error("User ID missing from request") return jsonify({'error': 'User ID is required'}), 400 if not public_key: logger.error("Public key missing from request") return jsonify({'error': 'Public key is required'}), 400 try: duration_hours = int(duration_hours) logger.info(f"Converted duration to integer: {duration_hours}") except ValueError: logger.error(f"Invalid duration value: {duration_hours}") return jsonify({'error': 'Invalid duration value'}), 400 # Calculate price base_price = duration_hours * 100 # 100 sats per hour if duration_hours >= 720: # 1 month base_price = base_price * 0.85 # 15% discount elif duration_hours >= 168: # 1 week base_price = base_price * 0.90 # 10% discount elif duration_hours >= 24: # 1 day base_price = base_price * 0.95 # 5% discount amount_sats = int(base_price) logger.info(f"Calculated price: {amount_sats} sats for {duration_hours} hours") # Create BTCPay invoice logger.info("Creating BTCPay invoice") invoice_data = btcpay_handler.create_invoice( amount_sats=amount_sats, duration_hours=duration_hours, user_id=user_id, public_key=public_key ) if not invoice_data: logger.error("Failed to create invoice - no data returned from BTCPayHandler") return jsonify({'error': 'Failed to create invoice'}), 500 logger.info(f"Successfully created invoice with ID: {invoice_data.get('invoice_id')}") logger.info("=== Create Invoice Request Completed ===") return jsonify(invoice_data) except Exception as e: logger.error(f"Error in create_invoice endpoint: {str(e)}") logger.error(f"Traceback: ", exc_info=True) return jsonify({'error': str(e)}), 500 @app.route('/api/vpn-config/') def get_vpn_config(user_id): try: logger.info(f"Fetching VPN config for user: {user_id}") subscription = DatabaseManager.get_active_subscription_for_user(user_id) if not subscription: logger.error(f"No active subscription found for user {user_id}") return jsonify({"error": "No active subscription found"}), 404 # Get the config based on test or production path base_path = Path('/etc/wireguard') if subscription.invoice_id.startswith('__test__'): config_path = base_path / 'test_clients' / subscription.invoice_id / 'wg0.conf' else: config_path = base_path / 'clients' / subscription.invoice_id / 'wg0.conf' logger.info(f"Looking for config at: {config_path}") if not config_path.exists(): logger.error(f"Configuration file not found at {config_path}") return jsonify({"error": "Configuration file not found"}), 404 with open(config_path) as f: config_text = f.read() logger.info(f"Successfully retrieved config for user {user_id}") return jsonify({ "configText": config_text, "status": "active", "expiryTime": subscription.expiry_time.isoformat() if subscription.expiry_time else None }) except Exception as e: logger.error(f"Error retrieving VPN config: {str(e)}") logger.error("Traceback:", exc_info=True) return jsonify({"error": "Failed to retrieve configuration"}), 500 @app.route('/payment/success') def payment_success(): return render_template('payment_success.html') if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)