<?php
    
    $seller_id = '251206854467';
    $publishable_key = '6965F831-0BFC-4545-A657-1EFA995FAE42';
    $private_key = '3BEFBF06-BF47-4939-9300-82CB343C8E97';
    $currency_code = 'mxn'; 
    $prueba; 
    
    //4111111111111111
    $localhost = "directoriofitness.com.mx";
    $user = "direct18";
    $passwd = "L4LL4v3M@3str@";
    
    $bd = $_GET['bd'];
    $id_cliente = $_GET['id_cliente'];
    $id_servicio = $_GET['plan'];
    $plan = $_GET['plan'];
    $importe = $_GET['importe'];
    $nombre = $_GET['nombre'];
    $apellido = $_GET['apellido'];
    $fechaN = $_GET['fechaN'];
    $email = $_GET['email'];
    $tel = $_GET['telefono'];
    $domi = $_GET['domi'];
    $col = $_GET['colonia'];
    $cp = $_GET['cp'];
    $genero = $_GET['genero'];
    $fechaR = $_GET['fechaR'];
    $sucursal = $_GET['sucursal'];
    $qr = $_GET['qr'];
    
    $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",'direct18_ProGymCloud');
    $conn -> set_charset("utf8");
    $queryApi = "SELECT info_checkout.*, info_plataformaspago.activar_checkout
                    FROM info_checkout 
                INNER JOIN gimnasios ON info_checkout.gimnasio_idGimnasio = gimnasios.idGimnasio 
                INNER JOIN info_plataformaspago ON info_plataformaspago.gimnasio_idGimnasio = gimnasios.idGimnasio 
                WHERE gimnasios.BaseDatos = '$bd' AND info_checkout.sucursal_idSucursal = '$sucursal'";
    
    $resultApi = $conn->query($queryApi);
    
    
    if ($resultApi->num_rows > 0){
        
        $row = $resultApi->fetch_assoc();
    
        $seller_id = $row['num_cuenta'];
        $publishable_key = $row['public_key'];
        $private_key = $row['private_key'];
        $prueba = $row['prueba'];
        
    }else{
        $bandApi = FALSE;
    }
    
    
    $conn->close();
    
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <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>Forma de Pago</title>
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    
    <script type="text/javascript" src="https://www.2checkout.com/checkout/api/2co.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    
    <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>
    
    <!-- LAS ALERTAS -->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<!-- Popper.JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<!-- Bootstrap JS -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>

    
</head>
<style>
body{
    background-color: #079AF3;
}
div.form
{
    display: block;
    text-align: center;
}
form
{
    border-radius: 15px;
    margin-top: 35px;
    background-color: #FFFFFF;
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    padding: 15px;
    text-align: left;
}
</style>
<body>
    <div class="form">
        
        <form id="myCCForm" action="payment.php" method="post">
            <input id="token" name="token" type="hidden" value="">
            <input value="<?= $prueba ?>" name="prueba" type="hidden" id="">
            <input value="<?= $sucursal ?>" name="sucursal" type="hidden" id="">
            <input value="<?= $id_servicio ?>" name="plan" type="hidden" id="">
            <input value="<?= $importe ?>" name="importe" type="hidden" id="">
            <input value="<?= $nombre ?>" name="nombre" type="hidden" id="">
            <input value="<?= $apellido ?>" name="apellido" type="hidden" id="">
            <input value="<?= $fechaN ?>" name="fechaN" type="hidden" id="">
            <input value="<?= $email ?>" name="email" type="hidden" id="">
            <input value="<?= $tel ?>" name="tel" type="hidden" id="">
            <input value="<?= $domi ?>" name="domi" type="hidden" id="">
            <input value="<?= $colonia ?>" name="colonia" type="hidden" id="">
            <input value="<?= $cp ?>" name="cp" type="hidden" id="">
            <input value="<?= $genero ?>" name="genero" type="hidden" id="">
            <input value="<?= $bd ?>" name="bd" type="hidden" id="">
            <input value="<?= $fechaR ?>" name="fechaR" type="hidden" id="">
            <input value="<?= $qr ?>" name="qr" type="hidden" id="">

            <div class="mb-2">
                <label for="lbInputTarget" class="form-label">Numero de Tarjeta</label>
                <input id="ccNo" type="number" size="20" value="" autocomplete="off" class="form-control" id="lbInputTarget" aria-placeholder="4242 4242 4343 4343" required />
            </div>
            <div class="row g-3">
                <label for="expMonth" class="form-label">Fecha de Vencimiento (MM/YY)</label>
              <div class="col">
                 <input type="number" id="expMonth" class="form-control" aria-label="11" placeholder="11"  required /> 
                <!--<input type="text" class="form-control" placeholder="First name" aria-label="First name">-->
              </div>
              <div class="col">/</div>
              <div class="col" style="margin-left:-150px;">
                <input type="number" id="expYear" class="form-control" aria-label="25" placeholder="25"  required /> 
              </div>
            </div>
            
            <div class="row mt-2">
              
                <div class="col-sm-6" >
                    <div class="row">
                        <label for="lbInputfv" class="form-label">CVC</label>
                        <div class="col-sm-4">
                             
                            <input id="cvv" size="4" type="number" value="" autocomplete="off" class="form-control" id="lbInputcvv" aria-describedby="123" required />
                         </div>
                    </div>
                </div>
            
            </div>
            <div class="mt-2">
                <input type="submit" class="btn btn-primary" value="Pagar">
            </div>
            
        </form>
    </div>
    <script>
        // Called when token created successfully.
        var successCallback = function(data) {
            console.log(data);
            var myForm = document.getElementById('myCCForm');
    
            // Set the token as the value for the token input
            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) {
                console.log('error ' + data);
                tokenRequest();} else {alert(data.errorMsg);}
        };
    
        var tokenRequest = function() {
            // Setup token request arguments
            var args = {
                sellerId: "251206854467",
                publishableKey: "6965F831-0BFC-4545-A657-1EFA995FAE42",
                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('production');//production
    
            $("#myCCForm").submit(function(e) {
                // Call our token request function
                swal("Estamos Realizando la Transaccion! \nEspere por favor, Gracias...", { button: false, icon: "warning",});
                tokenRequest();
    
                // Prevent form from submitting
                return false;
            });
        });
    </script>
</body>
</html>