<?php

include "classes/class.loader.php";
$db = new db();
$calls = new functions();
$wallet_amount = $calls->get_wallet_amount($_SESSION['user_id']);
$credit_package = $_POST['credit_package'];
// get credit_package Price
$credits_res = $db->query("SELECT * FROM `park_prices` WHERE `record_id` = '$credit_package'");
$credits = $credits_res->fetch_assoc();
?>
<style>
    body {
        background-color: black;
    }
</style>
<div id="overlay"
    style="display:block;position:fixed;top:0;left:0;width:100%;height:100%;background-color:rgba(0,0,0,0.5);z-index:9999;">
    <div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);text-align:center;">
        <!-- <img src="images/loader.gif" alt="loading..." style="width:50px;height:50px;" /> -->
        <p style="color:white;font-size:1.5em;">Loading...</p>
    </div>
</div>

<?php

if ($wallet_amount - $credits['price'] * 1.15 >= 0) {
    $payment_id = $db->query("INSERT INTO `payments` (`user_id`, `zar`, `booking_id`) VALUES ('" . $_SESSION['user_id'] . "', '-" . ($credits['price'] * 1.15) . "', -1)");
    $db->query("INSERT INTO `visit_trans` (`amount`,`user_id`,`booking_id`,`payment_id`,`type`) VALUES ({$credits['credits']},{$_SESSION['user_id']},-1,$payment_id,'PARK')");
    echo "<script>window.location.href='home.php';</script>";
} else {
    $nett = $credits['price'] * 1.15 - $wallet_amount;
    $payment_id = $db->query("INSERT  INTO `payments` (`user_id`, `zar`, `booking_id`) VALUES ('" . $_SESSION['user_id'] . "', '-" . ($wallet_amount) . "', -1)");

    $price = $nett;
    // testing
    $merchant_id = '10000100';
    $mechhant_key = '46f0cd694581a';
    $passphrase = 'jt7NOE43FZPn';
    $link = "https://sandbox.payfast.co.za/eng/process";

    // Live
    // $merchant_id = 23635991;
    // $mechhant_key = '5n8lir8uw1cun';
    // $passphrase = 'eWG123456789';
    // $link = "https://www.payfast.co.za/eng/process";

    function generateSignature($data, $passPhrase = null)
    {
        // Create parameter string
        $pfOutput = '';
        foreach ($data as $key => $val) {
            if ($val !== '') {
                $pfOutput .= $key . '=' . urlencode(trim($val)) . '&';
            }
        }
        // Remove last ampersand
        $getString = substr($pfOutput, 0, -1);
        if ($passPhrase !== null) {
            $getString .= '&passphrase=' . urlencode(trim($passPhrase));
        }
        return md5($getString);
    }


    $data = array(
        // Merchant details
        'merchant_id' => $merchant_id,
        'merchant_key' => $mechhant_key,
        'return_url' => "https://unleashed.elegantwork.co.za/successfull.php?credits=$credits[record_id]&zar=$price&from_wallet=1",
        'cancel_url' => 'https://unleashed.elegantwork.co.za/home.php',
        'notify_url' => "https://unleashed.elegantwork.co.za/send_notification.php?credits=$credits[record_id]&from_wallet=1",
        // Buyer details
        'name_first' => $_SESSION['username'],
        'email_address' => $_SESSION['email'],
        // Transaction details
        'amount' => $price,
        'item_name' => "buy_credits"
    );
    $signature = generateSignature($data, $passphrase);

    ?>

    <form action="<?php echo $link; ?>" method="post" id="for">
        <input type="hidden" name="signature" value="<?php echo $signature; ?>">
        <input type="hidden" name="merchant_id" value="<?php echo $merchant_id; ?>">
        <input type="hidden" name="merchant_key" value="<?php echo $mechhant_key; ?>">
        <input type="hidden" name="amount" value="<?php echo $price; ?>">
        <input type="hidden" name="item_name" value="<?php echo "buy_credits"; ?>">
        <input type="hidden" name="name_first" value="<?php echo $_SESSION['username']; ?>">
        <input type="hidden" name="email_address" value="<?php echo $_SESSION['email']; ?>">
        <input type="hidden" name="return_url"
            value="https://unleashed.elegantwork.co.za/successfull.php?credits=<?php echo $credits['record_id']; ?>&zar=<?php echo $price; ?>&from_wallet=1">
        <input type="hidden" name="cancel_url" value="https://unleashed.elegantwork.co.za/home.php">
        <input type="hidden" name="notify_url"
            value="https://unleashed.elegantwork.co.za/send_notification.php?credits=<?php echo $credits['record_id']; ?>&from_wallet=1">
        <!-- <input type="submit"  class="button" value="BUY NOW">
                      -->
    </form>
    <script>
        document.getElementById("for").submit();
    </script>
    <?php
}