<?php
session_start();
if($_SESSION['loggedin']){
    
    $idg = $_SESSION['idg'];
    $foto=$_SESSION['icon'];
    $gym=$_SESSION['gym'];
    $cliente = $_SESSION['num_cliente'];
    $nombre_cliente = $_SESSION['nombre'];
    $db = $_SESSION['db'];
    $img_socio = $_SESSION['foto'];
    
    // TRAEMOS LOS DATOS DEL SERVIDOR PARA LA CONEXION A LA BD
    include("../../../mysql_connection/mysql_data.php");
    $server_data = new serverData;
    $servername = $server_data -> getServerName();
    $username = $server_data -> getUserName();
    $password = $server_data -> getPassword();
    // **END

    $db = "direct18_ProGymCloud";
    $conn = new mysqli($servername,$username,$password, $db);
    mysqli_set_charset($conn, 'utf8');
    
    // PARA OBTENER LOS DATOS DE 2CHECKOUT
    $query="SELECT num_cuenta as 'cuenta', private_key as 'privkey', public_key as 'pubkey' FROM info_checkout WHERE info_checkout.sucursal_idSucursal = ? AND info_checkout.activo = 1; ";
    $stmt = $conn->prepare($query);
    $stmt->bind_param('i', $idg);
    $stmt->execute();
    $rs = $stmt->get_result();
    $row = mysqli_fetch_array($rs, MYSQLI_ASSOC);
    $cuenta = $row['cuenta'];
    $cuenta = !empty($cuenta) ? $cuenta : "";
    $publica = $row['pubkey'];
    $publica = !empty($publica) ? $publica : "";
    $privada = $row['privkey'];
    $privada = !empty($privada) ? $privada : "";
    
    //RECIBIMOS LOS DATOS
    $cncp = $_POST["concepto"];
    $total = $_POST["total"];
    $id_paquete = $_POST['idp'];
    $nombre_paquete = $_POST['np'];
    $concepto = $_POST['concepto'];
    $fecha_vencimiento = $_POST['venci'];
    $n_concepto = $_POST['ncon'];
    
    /*echo $id_paquete."<br>";
    echo $cliente."<br>";
    echo $nombre_cliente."<br>";
    echo $gym."<br>";
    echo $nombre_paquete."<br>";
    echo $n_concepto."<br>";
    echo $total."<br>";
    echo $fecha_vencimiento."<br>";
    echo $cncp."<br>";*/
}
else{
  header('Location: ../../index.php?err=3');
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <title>ProGym Cloud - Finalizar Pago</title>
    
    <!-- Bootstrap CSS CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
    <!-- Our Custom CSS -->
    <link rel="stylesheet" href="../../css/style.css">

    <!-- Font Awesome JS -->
    <script defer src="https://use.fontawesome.com/releases/v5.0.13/js/solid.js" integrity="sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ" crossorigin="anonymous"></script>
    <script defer src="https://use.fontawesome.com/releases/v5.0.13/js/fontawesome.js" integrity="sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY" crossorigin="anonymous"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <script type="text/javascript" src="https://www.2checkout.com/checkout/api/2co.min.js"></script>
    
    <style>
        .form-control {
            background-color: #FFFFF5;
            border: 1px solid #0291DD;
            font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
            font-size: 11px;
        }
        h6{
            color: #061282;
            font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
        }
        h7{
            font-size: 15px;
        }
        .containerz{
            border: 1px solid #5DADE2;
            background-color: #EBF5FB;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <?php include("../../includes/side_bar_deep.php"); ?>
    <div class="container-fluid dclientes" >
        <div class="container-fluid" style="border-radius: 15px;  height: auto; padding-top: 20px; padding-bottom: 20px; max-width: 600px; background-color: #FFFFFF; opacity: 1;">
            <div class="containerz" style="">
                <div class="row">
                    <div class="col-auto col-md-12">
                        <h6>Informacion del Pago:</h6>
                    </div>
                    <div class="col-auto col-md-12">
                        <h7>Nombre de la Sucursal: <?php echo "<span style='font-size: 14px; color: #1A5276'>$gym</span>" ?></h7>
                    </div>
                    <div class="col-auto col-md-12">
                        <h7>Nombre del Cliente: <?php echo "<span style='font-size: 14px; color: #1A5276'>$nombre_cliente</span>" ?></h7>
                    </div>
                </div>
                <div class="row">
                    <div class="col-auto col-md-12">
                        <h7>Concepto de Pago: <?php echo "<span style='font-size: 14px; color: #1A5276'>$cncp</span>" ?></h7>
                    </div>
                    <div class="col-auto col-md-12">
                        <h7>Monto a Pagar: <?php echo "<span style='font-size: 14px; color: #1A5276'>$$total</span>" ?></h7>
                    </div>
                </div>
            </div>
            <br>
            <form id="myCCForm" action="./payment.php" method="post">
                <input id="token" name="token" type="hidden" value="">
                <div class="form-row">
                    <div class="col-auto col-md-12">
                        <input name="importe" type="hidden" value="<?= $total ?>">
                        <input name="concepto" type="hidden" value="<?= $cncp ?>">
                        <input name="idp" type="hidden" value="<?= $id_paquete ?>">
                        <input name="np" type="hidden" value="<?= $nombre_paquete ?>">
                        <input name="venci" type="hidden" value="<?= $fecha_vencimiento ?>">
                        <input name="ncon" type="hidden" value="<?= $n_concepto ?>">
                        <label>
                            <span>Numero de Tarjeta</span>
                        </label>
                    </div>
                    <div class="col-auto col-md-12">
                        <input class="form-control" id="ccNo" type="text" maxlength="16" value="" autocomplete="off" required />
                    </div>
                </div>
                <div class="form-row">
                    <div class="col-auto col-md-12">
                        <label>
                            <span>Fecha de Expiracion (MM/YYYY)</span>
                        </label>
                    </div>
                    <div class="col-auto col-md-5">
                        <input class="form-control" type="text" maxlength="2" id="expMonth" required />
                    </div>
                    <div class="col-auto col-md-2">
                        <span>/</span>
                    </div>
                    <div class="col-auto col-md-5">
                        <input class="form-control" type="text" maxlength="4" id="expYear" required />
                    </div>
                </div>
                <br>
                <div class="form-row">
                    <div class="col-auto">
                        <label>
                        <span>CVC</span>
                        </label>
                    </div>
                    <div class="col-auto">
                        <input class="form-control" id="cvv" type="text" maxlength="4" value="" autocomplete="off" required />
                    </div>
                </div>
                <br>
                <div class="form-row">
                    <?php if(!empty($cuenta) or !empty($publica) or !empty($privada)){ ?>
                    <div class="col-auto col-md-12">
                        <input class="form-control" type="submit" value="Pagar" style="background-color: #D5F5E3; color: #000000;">
                    </div>
                    <?php }else{ ?>
                    <div class="col-auto col-md-12">
                        <input class="form-control btn btn-secondary" value="Pagar">
                    </div>
                    <?php } ?>
                    <div class="col-auto col-md-12" style="margin-top: 15px;">
                        <a class="btn btn-danger btn-sm btn-block" type="link" href="../gestionar_paquete.php" >Cancelar</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
<!-- AQUI VAN LOS CDN DE JAVASCRIPT, JQUERY Y POOPER PARA BOOTSTRAP -->
<?php include("../../includes/footer.php"); ?>
    <script>
        // Called when token created successfully.
        var successCallback = function(data) {
            var myForm = document.getElementById('myCCForm');
    
            // Set the token as the value for the token input class="form-control"
            myForm.token.value = data.response.token.token;
    
            // IMPORTANT: Here we call `submit()` on the form element directly instead of using jQuery to prevent and infinite token request loop.
            myForm.submit();
        };
    
        // Called when token creation fails.
        var errorCallback = function(data) {
            if (data.errorCode === 200) {tokenRequest();} else {alert(data.errorMsg);}
        };
    
        var tokenRequest = function() {
            // Setup token request arguments
            var args = {
                sellerId: '<?= $cuenta ?>',
                publishableKey: '<?= $publica ?>',
                ccNo: $("#ccNo").val(),
                cvv: $("#cvv").val(),
                expMonth: $("#expMonth").val(),
                expYear: $("#expYear").val()
            };
    
            // Make the token request
            TCO.requestToken(successCallback, errorCallback, args);
        };
    
        $(function() {
            // Pull in the public encryption key for our environment
            TCO.loadPubKey('sandbox');
    
            $("#myCCForm").submit(function(e) {
                swal("Estamos Realizando la Transaccion! \nEspere por favor, Gracias...", { button: false, icon: "warning",});
                // Call our token request function
                tokenRequest();
    
                // Prevent form from submitting
                return false;
            });
        });
    </script>
</body>
</html>