<?php
    require $_SERVER['DOCUMENT_ROOT'].'/progymcloud_app/pagos/paypal/vendor/autoload.php';
    
    use PayPal\Api\Amount;
    use PayPal\Api\Details;
    use PayPal\Api\Item;
    use PayPal\Api\ItemList;
    use PayPal\Api\Payer;
    use PayPal\Api\Payment;
    use PayPal\Api\RedirectUrls;
    use PayPal\Api\Transaction;
    
    
    $bd = $_GET['bd'];
    $id_cliente = $_GET['id_cliente'];
    $id_servicio = $_GET['idPlan'];
    $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_paypal.*, info_plataformaspago.activar_paypal 
                    FROM info_paypal
                INNER JOIN gimnasios ON info_paypal.gimnasio_idGimnasio = gimnasios.idGimnasio 
                INNER JOIN info_plataformaspago ON info_plataformaspago.gimnasio_idGimnasio = gimnasios.idGimnasio 
                WHERE gimnasios.BaseDatos = '$bd' AND info_paypal.sucursal_idSucursal = '$sucursal'";
    
    $resultApi = $conn->query($queryApi);
    
    
    if ($resultApi->num_rows > 0){
        
        $row = $resultApi->fetch_assoc();
    
        $CLIENT_ID = $row['client_id'];
        $SECRET_ID = $row['secret_id'];
    }else{
        $bandApi = FALSE;
    }
    
    $conn->close();
    
    $url_complete_success = 'executeVisita.php?success=true&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;
    $url_complete_cancel = 'executeVisita.php?success=false';
        
    $apiContext = new \PayPal\Rest\ApiContext(
        new \PayPal\Auth\OAuthTokenCredential(
            $CLIENT_ID,
            $SECRET_ID
            /*'AZ2e7-OodO8jiPAhnHaWENlkzjfUDVPw20UA6BFS1bu__j95ztWXJ0dawDDle9-SAq2YRX2zTLOGIFvX',     // ClientID
            'EOK2gQSj6960G6Au6LYdCs2UpqVvzRHvwF8teBdbQRAqqrBCGU9HcXCGWNDIqjIoDPW9Ds6GDrvRiQ_V' */     // ClientSecret
        )
    );
    
    $payer = new Payer();
    $payer->setPaymentMethod("paypal");
    

    $amount = new Amount();
    $amount->setCurrency("MXN")
            ->setTotal($importe);
    

    $transaction = new Transaction();
    $transaction->setAmount($amount)
    ->setDescription("Payment description")
    ->setInvoiceNumber(uniqid());
    
    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl("https://directoriofitness.com.mx/progymcloud_app/pagos/paypal/".$url_complete_success)
    ->setCancelUrl("https://directoriofitness.com.mx/progymcloud_app/pagos/paypal/".$url_complete_cancel);
    $payment = new Payment();
    $payment->setIntent("sale")
    ->setPayer($payer)
    ->setRedirectUrls($redirectUrls)
    ->setTransactions(array($transaction));

    $request = clone $payment;
    try {
        $payment->create($apiContext);
        header("Location: ".$payment->getApprovalLink());
        
    } catch (Exception $ex) {
        echo "Error Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex;
       
        exit(1);
    }

return $payment;
  
?>