<?php
    require $_SERVER['DOCUMENT_ROOT'].'/estaciones/Progymcloud_app/php/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'];
    
    $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(
            '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/estaciones/Progymcloud_app/php/paypal/".$url_complete_success)
    ->setCancelUrl("https://directoriofitness.com.mx/estaciones/Progymcloud_app/php/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;
  
?>