<!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.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
        <title>Paypal</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">
<?php
    require $_SERVER['DOCUMENT_ROOT'].'/estaciones/Progymcloud_app/php/paypal/vendor/autoload.php';
    use PayPal\Api\Amount;
    use PayPal\Api\Details;
    use PayPal\Api\Payment;
    use PayPal\Api\PaymentExecution;
    use PayPal\Api\Transaction;
    $msg1 = '';
    $msg2 = '';
    $msg3 = '';
    $msg4 = '';
    $msg5 = '';
    $msgQuery = '';
    
    /*$apiContext = new \PayPal\Rest\ApiContext(
            new \PayPal\Auth\OAuthTokenCredential(
                'AbMNQuC6FuWRkohnxxl4AhiZetgL8_99SDtSz_4fVDkKHZD_hEHmY71Sx-2j9UOycGTni5swWzyAD_7l',     // ClientID
                'EFViUhNVgqghUeIZFy0R8sdIdP2XSeheTGQVnyY1VIKs3ioeG0lwz4n96t42hohh_5fs3K2AYVn5JCb6'      // ClientSecret
            )
        );
        
        $apiContext->setConfig(
          array(
            'mode' => 'live',
          )
        );*/
        $apiContext = new \PayPal\Rest\ApiContext(
            new \PayPal\Auth\OAuthTokenCredential(
                'AZ2e7-OodO8jiPAhnHaWENlkzjfUDVPw20UA6BFS1bu__j95ztWXJ0dawDDle9-SAq2YRX2zTLOGIFvX',     // ClientID
                'EOK2gQSj6960G6Au6LYdCs2UpqVvzRHvwF8teBdbQRAqqrBCGU9HcXCGWNDIqjIoDPW9Ds6GDrvRiQ_V'      // ClientSecret
            )
        );
    if (isset($_GET['success']) && $_GET['success'] == 'true') {
        $bd = $_GET['bd'];
        //echo "<br>";
        $flag = $_GET['flag'];
        //echo "<br>";
        $id_cliente = $_GET['id_cliente'];
        //echo "<br>";
        $concepto = $_GET['concepto'];
        //echo "<br>";
        $costo = $_GET['costo'];
        //echo "<br>";
        $idLocker = $_GET['idLocker'];
        //echo "<br>";
        $lock_asig = $_GET['lock_asig']; 
        //echo "<br>";
        $suc = $_GET['suc'];
        $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$bd);
        $desc_plan;
        
        $paymentId = $_GET['paymentId'];
        $payment = Payment::get($paymentId, $apiContext);
        $execution = new PaymentExecution();
        $execution->setPayerId($_GET['PayerID']);
        $transaction = new Transaction();
        $amount = new Amount();
        $details = new Details();
        /*$details->setShipping(2.2)
        ->setTax(1.3)
        ->setSubtotal(17.50);*/
    
        $amount->setCurrency('MXN');
        $amount->setTotal($costo);
        //$amount->setDetails($details);
        $transaction->setAmount($amount);
        $execution->addTransaction($transaction);
        try {
            $result = $payment->execute($execution, $apiContext);
            //echo "Executed Payment", "Payment", $payment->getId(), $execution, $result;
            try {
                $payment = Payment::get($paymentId, $apiContext);
                //echo 'Payment success';
            } catch (Exception $ex) {
                echo "Error Get Payment", "Payment", null, null, $ex;
                exit(1);
            }
        } catch (Exception $ex) {
            echo "Error Executed Payment", "Payment", null, null, $ex;
            exit(1);
            
        }
        //codigo para insertar
            $fechaA;
            $addFecha;
            $queryLocker ="SELECT ls.*, l.*, locks.*
            FROM socios s JOIN lockers_socios ls ON s.idSocio = ls.Socios_idSocio JOIN locker l ON ls.id_lockers_socios = l.id_locker JOIN lockers locks ON locks.idLocker = l.Lockers_idLockers
            WHERE ls.id_lockers_socios = (SELECT MAX(id_lockers_socios) FROM lockers_socios WHERE Socios_idSocio = '$id_cliente')";
            
            //$resultadoL = $conn->insert_query($queryLocker,"directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
            //$row_fecha = mysqli_fetch_array($resultadoL);
            $resultadoL =$conn->query($queryLocker);
            $row_fecha = $resultadoL->fetch_assoc();
            $fechaVen = $row_fecha['FechaVencimiento'];
            $fecha_act = $row_fecha['FechaAsignacion'];
            switch($concepto){
                case '1':
                    $desc_plan = 'Pago Locker 1 d&iacute;a';
                    $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 1 DAY)";
                    $fechaA = date("d/m/Y",strtotime($fechaVen."+ 1 days"));
                    break;
                case '2':
                    $desc_plan = 'Pago Locker semanal';
                    $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 7 DAY)";
                    $fechaA = date("d/m/Y",strtotime($fechaVen."+ 7 days"));
                    break;
                case '3':
                    $desc_plan = 'Pago Locker quincenal';
                    $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 15 DAY)";
                    $fechaA = date("d/m/Y",strtotime($fechaVen."+ 15 days"));
                    break;
                case '4':
                    $desc_plan = 'Pago Locker mensual';
                    $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 1 MONTH)";
                    $fechaA = date("d/m/Y",strtotime($fechaVen."+ 1 month"));
                    break;
                case '5':
                    $desc_plan = 'Pago Locker semestral';
                    $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 6 MONTH)";
                    $fechaA = date("d/m/Y",strtotime($fechaVen."+ 6 month"));
                    break;
                case '6':
                    $desc_plan = 'Pago Locker anual';
                    $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 12 MONTH)";
                    $fechaA = date("d/m/Y",strtotime($fechaVen."+ 12 month"));
                    break;
            }
            
            $query = "INSERT lockers_socios(Lockers_idLocker,Socios_idSocio,locker_asignado,FechaAsignacion,FechaVencimiento,estado,concepto_pago,tipo_pago,
            cantidad_tarjeta,importe,user_idUsuario,sucursal_idSucursal,cancelado)
            VALUES('$idLocker','$id_cliente','$lock_asig','$fechaVen',$addFecha,1,'$concepto',2,'$costo','$costo',21,'$suc',0)";
            
            //$resultado = $conn->insert_query($query,"directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
            if($conn->query($query) == TRUE){
                $msgQuery = '&iexcl;Transacci&oacute;n Exitosa!';
            }else{
                $msgQuery = 'Error al registrar su pago, cheque en Administraci&oacute;n';
            }
            
        
         ?>
        <div class="padre">
            <p class="hijo" style="text-align: center;">
                <img src="https://logos-marcas.com/wp-content/uploads/2020/04/PayPal-Logo.png" alt="PayPal-Logo" width="180px">
            </p>
            <p class="hijo" style="text-align: center; color: #73E45E; font-size: 70px;">
                <i class="fas fa-check-circle"></i>
            </p>
            <h3 class="hijo" style="text-align: center;">Gracias por su pago</h3>
            <h3 class="hijo" style="text-align: center;"><?php echo $msgQuery; ?></h3>
        </div>
        <div class="padre recibo">
            <img src="https://logos-marcas.com/wp-content/uploads/2020/04/PayPal-Logo.png" alt="PayPal-Logo" width="100px">
            <p> Gracias por su pago!</p>
            <p><?php echo $hoy; ?></p>
            <hr>
            <p><b>ID de pago:</b> <?php echo $paymentId;?></p>
            <p><?php echo '<b>Nombre:</b> '.$payment->payer->payer_info->first_name.' '.$payment->payer->payer_info->last_name;?></p>
            <p><?php echo '<b>Correo electronico:</b> '.$payment->payer->payer_info->email?></p>
            <hr>
            <p><b>FECHAS</b></p>
            <div class="table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th scope="col"><b>Fecha de vencimiento anterior:</b></th>
                      <th scope="col"><b>Fecha de vencimiento actual:</b></th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td><?php echo date("d/m/Y", strtotime($fechaVen)); ?></td>
                      <td><?php echo $fechaA; ?></td>
                    </tr>
                  </tbody>
                </table>
            </div>
            <p><b>PAGO</b></p>
            <div class="table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th scope="col">Descripcion</th>
                      <th scope="col">Costo</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td><?php echo $desc_plan; ?></td>
                      <td><?php echo $costo.' MXN';?></td>
                    </tr>
                  </tbody>
                </table>
            </div>
            
            <div class="table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th scope="col"></th>
                      <th scope="col"></th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td><b>TOTAL</b></td>
                      <td><?php echo $costo?></td>
                    </tr>
                  </tbody>
                </table>
            </div>
            <hr>
            <p><b>Pago via PayPal</b></p>
        </div>
      <? //php $conn->close();
    //echo "Get Payment", "Payment", $payment->getId(), null, $payment;
     $conn->close();
    return $payment;
    }else{ //$msg2 = "User Cancelled the Approval", null; ?>
        
        <div class="padre">
            <p class="hijo" style="text-align: center;">
                <img src="https://logos-marcas.com/wp-content/uploads/2020/04/PayPal-Logo.png" alt="PayPal-Logo" width="180px">
            </p>
            <p class="hijo" style="text-align: center; color: #d11515; font-size: 70px;">
                <i class="fas fa-times-circle"></i>
            </p>
            <h3 class="hijo" style="text-align: center;">Cancelo el Pago</h3>
            <h3 class="hijo" style="text-align: center;"><?php echo $msgQuery; ?></h3>
        </div> 
    <?php
        exit;
    }
    ?>
 </div>
</body>    
</html>