<?php 
    
    require "payment-api/lib/Twocheckout.php";
    require "../registro_pagos_visita.php";
    
    
    $seller_id = '251206854467';
    $publishable_key = '6965F831-0BFC-4545-A657-1EFA995FAE42';
    $private_key = '3BEFBF06-BF47-4939-9300-82CB343C8E97';
    
    //4242424242424242
    $localhost = "directoriofitness.com.mx";
    $user = "direct18";
    $passwd = "L4LL4v3M@3str@";
    
    /*
    <input id="token" name="token" type="hidden" value="">
            <input value="<?= $bd ?>" name="bd" type="hidden" id="">
            <input value="<?= $id_cliente ?>" name="id_cliente" type="hidden" id="">
            <input value="<?= $id_servicio ?>" name="id_servicio" type="hidden" id="">
            <input value="<?= $plan ?>" name="plan" type="hidden" id="">
            <input value="<?= $tipo ?>" name="tipo" type="hidden" id="">
           
        <?php 
            if($tipo == 'planes'){ 
        ?>
                <input value="<?= $paquete_clase ?>" name="paquete_clase" type="hidden" id="">
                <input value="<?= $fecha_visita ?>" name="fecha_visita" type="hidden" id="">
                <input value="<?= $id_servicio ?>" name="id_servicio" type="hidden" id="">
                <input value="<?= $costo ?>" name="costo" type="hidden" id="">
                <input value="<?= $opcion ?>" name="opcion" type="hidden" id="">

        <?php }else{ ?>
            <input value="<?= $concepto ?>" name="concepto" type="hidden" id="">
            <input value="<?= $idLocker ?>" name="idLocker" type="hidden" id="">
            <input value="<?= $lock_asig ?>" name="lock_asig" type="hidden" id="">
            <input value="<?= $sucursal ?>" name="sucursal" type="hidden" id="">
            <input value="<?= $costo ?>" name="costo" type="hidden" id="">
            <input value="<?= $opcion ?>" name="opcion" type="hidden" id="">
            
        <?php } ?>
    */
    
    $token = $_POST['token'];

    $bd = $_POST['bd'];

    $id_cliente = $_POST['id_cliente'];

    $id_servicio = $_POST['id_servicio'];

    $plan = $_POST['plan'];

    $tipo = $_POST['tipo'];
    $costo = $_POST['costo'];

    $opcion = $_POST['opcion'];
    
    $paquete_clase = '0';
    $fecha_visita = '0';
    $concepto;
    $idLocker;
    $lock_asig;
    $sucursal;
    
    if($tipo == 'planes'){
        if($_POST['fecha_visita'] == 'null' && $paquete_clase = $_POST['paquete_clase'] == 'null'){
           
        }else{
            $fecha_visita = $_POST['fecha_visita'];
            $paquete_clase = $_POST['paquete_clase'];
        }
        
    }else{
        $concepto = $_POST['concepto'];
        $idLocker = $_POST['idLocker'];
        $lock_asig = $_POST['lock_asig'];
        $sucursal = $_POST['sucursal'];
    }
    
    $conn = new mysqli($localhost,$user,$passwd, $bd);
    mysqli_set_charset($conn, 'utf8');
    $query = "SELECT pagos.idPago as 'folio' FROM pagos ORDER BY pagos.idPago DESC LIMIT 1";
    $stmt = $conn->prepare($query);
    //$stmt->bind_param('s', $gym);
    $stmt->execute();
    $rs = $stmt->get_result();
    $row = mysqli_fetch_array($rs, MYSQLI_ASSOC);
    $folio = $row["folio"] + 1;
    $conn->close();
    
    Twocheckout::privateKey($private_key);
    Twocheckout::sellerId($seller_id);

    // To use your sandbox account set sandbox to true
    Twocheckout::sandbox(false);
    try {
        $array_charge = array(
                "sellerId" => $seller_id,
                "merchantOrderId" => $folio,
                "token" => $token,
                "currency" => 'MXN',
                "total" => $costo,
                "billingAddr" => array(
                    "name" => 'Testing Tester',
                    "addrLine1" => 'Aguascalientes',
                    "city" => 'Aguascalientes',
                    "state" => 'Aguascalientes',
                    "zipCode" => '20660',
                    "country" => 'MEX',
                    "email" => 'prueba@gmail.com',
                    "phoneNumber" => '4491219096'
                ),
                "shippingAddr" => array(
                    "name" => 'Testing Tester',
                    "addrLine1" => 'Aguascalientes',
                    "city" => 'Aguascalientes',
                    "state" => 'Aguascalientes',
                    "zipCode" => '20660',
                    "country" => 'MEX',
                    "email" => 'prueba@gmail.com',
                    "phoneNumber" =>'4491219096'
                )
            );
        //4242424242424242
        /*$charge_a = array(
                "merchantOrderId" => $folio,
                "token"      => $token,
                "currency"   => 'MXN',
                "total"      => $costo,
                "billingAddr" => array(
                    "name" => $nombre.' '.$apellido,
                    "addrLine1" => $domi,
                    "city" => 'Aguascalientes',
                    "state" => 'Aguascalientes',
                    "zipCode" => $cp,
                    "country" => 'MEX',
                    "email" => $email,
                    "phoneNumber" => $tel
                )
            );*/
        $charge = Twocheckout_Charge::auth($array_charge);
        echo json_encode($array_charge);
        if ($charge['response']['responseCode'] == 'APPROVED') {
            echo "Thanks for your Order!";
            //$obj = new PagoVisitaReg();
            $t = 1;
            if($tipo == 'planes'){
                echo $t = $obj->registrarPagoPlan($bd,$flag,$id_cliente,$id_servicio,$costo,$plan,$fecha_visita,$paquete_clase,$opcion,"2Chk"); 
            }else{
                echo $t = $obj->registrarPagoLocker($bd,$id_cliente,$id_servicio,$plan,$tipo,$costo,$opcion,$concepto,$idLocker,$lock_asig,$sucursal);
            }
            
            ?>
            
            
            <!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">
              <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
              <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
              <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
            
              <title>Mercado Pago</title>
            </head>
            <style>
                .padre {
            
                padding: 0 1rem;
                margin: 1rem;
                }
            
                .hijo {
                    /* IMPORTANTE */
                    width: 200px;
                    margin-left: auto;
                    margin-right: auto;
                }
                .recibo{
                    border-style: ridge;
                }
            
                </style>
            <body>
              <nav class="navbar navbar-dark bg-primary">
                <a class="navbar-brand" href="#" style="margin-left: 15px;">ProgymCloud</a>
              </nav>
              <div class="container">
                <div class="padre">
                  <p class="hijo" style="text-align: center;">
                    <img src="https://tribulant.com/docs/wp-content/uploads/2checkout-logo-blue-green.png" alt="twoco-Logo" width="130px">
                  </p>
                  
                     <?php
                        if($t == 1){ ?>
                            <p class="hijo" style="text-align: center; color: #00E013; font-size: 70px;">  
                                <i class="fas fa-check-circle"></i>
                                <h3 class="hijo" style="text-align: center;">Transaccion Exitosa</h3>
                            </p>
                    <?php }else{ ?>
                    
                            <p class="hijo" style="text-align: center; color: #D3000A; font-size: 70px;">  
                                <i class="fas fa-times-circle"></i>
                                <h3 class="hijo" style="text-align: center;">Error al registrar tu pago</h3>
                            </p>
                    <?php } ?>
                  
                  
                </div>
              </div>
            
            </body>
            </html>   
            
    <?php   }
    } catch (Twocheckout_Error $e) {
        echo $e->getMessage(), " ",$array_charge;
        
        $obj = new PagoVisitaReg();
            $t = 1;
            if($tipo == 'planes'){
                $t = $obj->registrarPagoPlan($bd,$flag,$id_cliente,$id_servicio,$costo,$plan,$fecha_visita,$paquete_clase,$opcion,"2Chk"); 
            }else{
                $t = $obj->registrarPagoLocker($bd,$id_cliente,$id_servicio,$plan,$tipo,$costo,$opcion,$concepto,$idLocker,$lock_asig,$sucursal);
            }
            
            ?>
            
            
            <!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">
              <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
              <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
              <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
            
              <title>Mercado Pago</title>
            </head>
            <style>
                .padre {
            
                padding: 0 1rem;
                margin: 1rem;
                }
            
                .hijo {
                    /* IMPORTANTE */
                    width: 200px;
                    margin-left: auto;
                    margin-right: auto;
                }
                .recibo{
                    border-style: ridge;
                }
            
                </style>
            <body>
              <nav class="navbar navbar-dark bg-primary">
                <a class="navbar-brand" href="#" style="margin-left: 15px;">ProgymCloud</a>
              </nav>
              <div class="container">
                <div class="padre">
                  <p class="hijo" style="text-align: center;">
                    <img src="https://i.pinimg.com/originals/71/81/e8/7181e84d50cb87fa4ab9a5a8ab613dbe.jpg" alt="PayPal-Logo" width="130px">
                  </p>
                  
                     <?php
                        if($t == 1){ ?>
                            <p class="hijo" style="text-align: center; color: #00E013; font-size: 70px;">  
                                <i class="fas fa-check-circle"></i>
                                <h3 class="hijo" style="text-align: center;">Transaccion Exitosa</h3>
                            </p>
                    <?php }else{ ?>
                    
                            <p class="hijo" style="text-align: center; color: #D3000A; font-size: 70px;">  
                                <i class="fas fa-times-circle"></i>
                                <h3 class="hijo" style="text-align: center;">Error al registrar tu pago</h3>
                            </p>
                    <?php } ?>
                  
                  
                </div>
              </div>
            
            </body>
            </html> 
   <?php }

?>