<?php
include "inc/app-top-member.php";

// Calculate the grand total
$result_cart_summary = $eagro->getCartSummaryCheckout($_SESSION["member_id"]);
$array_cart_summary = json_decode($result_cart_summary, true);

if ($array_cart_summary['grand_total'] == 0) {
    echo "<script>window.location.href = 'index.php'</script>";
}

// Get user infor
$result_user = $eagro4->getUserInformation($_SESSION["member_id"]);
$array_user_result = json_decode($result_user, true);

// Insert the order into database.
$result_placeorder = $eagro4->placeorderWithIpay88($_SESSION['member_id'], $_GET['addressValue']);
$array_result_placeorder = json_decode($result_placeorder, true);

$merchant_code = "M34252";
$merchant_key = "1QjAl35Rpe";
$order_number = $array_result_placeorder['order_id'];
$formatted_grand_total = number_format((float)$_SESSION["checkout_grand_total"], 2, '.', '');
$encode_grand_total = str_replace(".", "", $formatted_grand_total);
$currency = 'MYR';

$signature = $merchant_key . "" . $merchant_code . "" . $order_number . "" . $encode_grand_total . "" . $currency;
$encode_signature = base64_encode(hex2bin(sha1($signature)));

?>

<!DOCTYPE html>
<html lang="en">


<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>E-Agro</title>

    <style>
        .lds-dual-ring {
            display: block;
            width: 80px;
            margin-left: auto;
            margin-right: auto;
        }

        .lds-dual-ring:after {
            content: " ";
            display: block;
            width: 64px;
            height: 64px;
            margin: 8px;
            border-radius: 50%;
            border: 6px solid #000000;
            border-color: #000000 transparent #000000 transparent;
            animation: lds-dual-ring 1.2s linear infinite;
        }

        @keyframes lds-dual-ring {
            0% {
                transform: rotate(0deg);
            }

            100% {
                transform: rotate(360deg);
            }
        }

        .spinner h1 {
            margin-top: 100px;
            text-align: center;
        }

        .spinner p {
            text-align: center;
        }

        a {
            color: #007bff !important;
            cursor: pointer;
        }

        a:hover {
            color: #007bff !important;
            text-decoration: underline !important;
        }
    </style>

    <!-- Bootstrap -->
    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"-->
    <!--      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">-->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
            integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
            integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"></script>
    <!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"-->
    <!--        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"-->
    <!--        crossorigin="anonymous"></script>-->
</head>

<body>

<div class="d-flex justify-content-center">
    <div class="spinner">
        <h1>Processing...</h1><br>
        <p>You will be redirected to Ipay88. It might take a few seconds.</p>
        <p>Please do not refresh the page or click the "Back" or "Close" button of your browser.</p>
        <div class="lds-dual-ring"></div>

        <br>
        <br>
        <br>
        <p>
            If you are not redirected then please click <a class="redirect">Here</a>
        </p>
    </div>
</div>
<form id="ePayment" name="ePayment" method="post" action="https://payment.ipay88.com.my/ePayment/entry.asp">
    <input type="hidden" name="MerchantCode" value="<?php echo $merchant_code; ?>"><br>
    <input type="hidden" name="PaymentId" value="2"><br>
    <input type="hidden" name="RefNo" value="<?php echo $order_number; ?>"><br>
    <input type="hidden" name="Amount" value="<?php echo $formatted_grand_total; ?>"> <br>
    <input type="hidden" name="Currency" value="<?php echo $currency; ?>"><br>
    <input type="hidden" name="ProdDesc" value="eagro"><br>
    <input type="hidden" name="UserName" value="<?php echo $array_user_result['fullname'] ?>"> <br>
    <input type="hidden" name="UserEmail" value="<?php echo $array_user_result['email'] ?>"><br>
    <input type="hidden" name="UserContact" value="<?php echo $array_user_result['phone_number'] ?>"><br>
    <input type="hidden" name="Remark" value=""><br>
    <input type="hidden" name="Lang" value="UTF-8"><br>
    <input type="hidden" name="Signature" value="<?php echo $encode_signature; ?>"><br>
    <input type="hidden" name="ResponseURL" value="https://eagro.com.my/payment_complete.php"> <br>
    <input type="hidden" name="BackendURL" value="https://eagro.com.my/backend_payment_complete.php"><br>
</form>
</body>

<script>
    $(document).ready(function () {
        setTimeout(() => {
            $('form').submit()
        }, 2000);

        $('.redirect').click(function () {
            $('form').submit()
        })
    })
</script>

</html>
