<?php

  require 'vendor/autoload.php';
  
  $access_token = 'TEST-3311809027950880-061115-e08e384a6c5151c92ddf1f5b4b0e3d66-774035859';    
  
  $bandApi = TRUE;
  $PUBLIC_KEY;
  $ACCESS_TOKEN;
  $bd = $_GET['bd'];
  $sucursal = $_GET['sucursal'];
    $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",'direct18_ProGymCloud');
    $queryApi = "SELECT info_mercadopago.*, info_plataformaspago.activar_mercadopago 
                    FROM info_mercadopago 
                INNER JOIN gimnasios ON info_mercadopago.gimnasio_idGimnasio = gimnasios.idGimnasio 
                INNER JOIN info_plataformaspago ON info_plataformaspago.gimnasio_idGimnasio = gimnasios.idGimnasio 
                WHERE gimnasios.BaseDatos = '$bd' AND info_mercadopago.sucursal_idSucursal = '$sucursal'";
    
    $resultApi = $conn->query($queryApi);
    
    
    if ($resultApi->num_rows > 0){
        
        $row = $resultApi->fetch_assoc();
    
        $PUBLIC_KEY = $row['public_key'];
        $ACCESS_TOKEN = $row['access_token'];
    }else{
        $bandApi = FALSE;
    }
    
    
    $conn->close();

  MercadoPago\SDK::setAccessToken($ACCESS_TOKEN); 

  
  $preference = new MercadoPago\Preference();
 
  //"https://directoriofitness.com.mx/estaciones/Progymcloud_app/php/mercado_pago/index.php?bd=${widget.bd}&
  //flag=4&id_cliente=${widget.idCliente}&concepto=${widget.plan}&costo=${widget.costo}&idLocker=${widget.locker}
  //&lock_asig=${widget.lockerAsig}&suc=${widget.sucursal}&tipo=locker" 
  
    
    
    $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'];
    
    
    
    $qr = $_GET['qr'];
    
  //bd=${widget.bd}&flag=4&id_cliente=${widget.idCliente}&concepto=${widget.plan}&costo=${widget.costo}&
  //idLocker=${widget.locker}&lock_asig=${widget.lockerAsig}&suc=${widget.sucursal}&tipo=locker
  
      $url_redirect = 'directoriofitness.com.mx/progymcloud_app/pagos/mercado_pago/successVisit.php?error=0&bd='.$bd.'&id_cliente='.$id_cliente.'&plan='.$plan.'&importe='
    .$importe.'&nombre='.$nombre.'&apellido='.$apellido.'&fechaN='.$fechaN.'&email='.$email.'&telefono='.$tel.'&domi='.$domi
    .'&colonia='.$col.'&cp='.$cp.'&genero='.$genero.'&fechaR='.$fechaR.'&sucursal='.$sucursal.'&qr='.$qr;
  
  # Building an item
  $preference->back_urls = array(
      "success" => $url_redirect,
      "failure" => "directoriofitness.com.mx/progymcloud_app/pagos/mercado_pago/failure.php?error=1",
      "pending" => "directoriofitness.com.mx/progymcloud_app/pagos/mercado_pago/failure.php?error=2"
  );
  
  $item = new MercadoPago\Item();
  $item->id = "00001";
  $item->title = "pago mercado libre"; 
  $item->quantity = 1;
  $item->unit_price = $importe;
  
  $preference->items = array($item);
  
  $preference->save(); # Save the preference and send the HTTP Request to create
  
  //echo json_encode($preference);
  # Return the HTML code for button
 //{"id":775498255,"nickname":"TESTBOISC5EI","password":"qatest8511",
 //"site_status":"active","email":"test_user_54574451@testuser.com"} //vendedor
 
 //{"id":775498915,"nickname":"TETE2436608","password":"qatest785",
    //"site_status":"active","email":"test_user_82460305@testuser.com"} //comprador
?>
<!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>Document</title>
</head>
<style>
    .padre {

    padding: 0 1rem;
    margin: 1rem;
    }

    .hijo {
        /* IMPORTANTE */
        width: 200px;
        margin-left: auto;
        margin-right: auto;
    }
    .recibo{
        border-style: ridge;
    }
    .boton{
        text-align: center; 
    }
</style>
<body>
    <div classs="padre">
        <div class="hijo">
            <?php if($bandApi == TRUE){ ?>
                <form action="/procesar-pago" method="POST" id="myform" name="myform"> 
                    </p class="boton"><script
                        src="https://www.mercadopago.com.mx/integrations/v1/web-payment-checkout.js"
                        data-preference-id="<?= $preference->id ?>"> 
                        
                    </script>  </p>
                </form>
            <?php }else{ ?>
                <h2>Forma de pago no disponible, favor de revisar en Administracion</h2>
            
            <?php } ?>
            <!-- Inicializa el checkout -->
            
            
        </div>
    </div>
</body>
</html>