<?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'];
    $flag = $_GET['flag'];
    $id_cliente = $_GET['id_cliente'];
    $id_servicio = $_GET['id_servicio'];
    $costo;
    $plan = $_GET['plan'];
    $concepto;
    $idLocker;
    $lock_asig;
    $sucursal;
    $tipo = $_GET['tipo'];
    $paquete_clase;
    $fecha_visita;
    $opcion;
    $url_complete_success;
    $url_complete_cancel;
    //$url = '&bd='.$bd.'&flag='.$flag.'&id_cliente='.$id_cliente.'&id_servicio='.$id_servicio.'&costo='.$costo.'&plan='.$plan;
     /*$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($tipo == 'planes'){
        
        $paquete_clase = $_GET['paquete_clase'];
        $fecha_visita = $_GET['fecha_visita'];
        $id_servicio = $_GET['id_servicio'];
        //$costo = $_GET['importe'];
        $plan = $_GET['plan'];
        $costo = $_GET['importe'];
        $opcion =$_GET['opcion'];
        $url_complete_success = 'excecutePayment.php?success=true&bd='.$bd.'&flag='.$flag.'&id_cliente='.$id_cliente.'&id_servicio='.$id_servicio.'&costo='.$costo.'&plan='.$plan.'&paquete_clase='.$paquete_clase.'&fecha_visita='.$fecha_visita.'&opcion='.$opcion;
        $url_complete_cancel = 'excecutePayment.php?success=false';
    }
    if($tipo == 'locker'){
        $concepto = $_GET['concepto'];
        $idLocker = $_GET['idLocker'];
        $lock_asig = $_GET['lock_asig'];
        $sucursal = $_GET['suc'];
        $costo = $_GET['costo'];
        $opcion = $_GET['opcion'];
        $url_complete_success = 'excecutePaymentLockers.php?success=true&bd='.$bd.'&flag='.$flag.'&id_cliente='.$id_cliente.'&concepto='.$concepto.'&costo='.$costo.'&idLocker='.$idLocker.'&lock_asig='.$lock_asig.'&suc='.$sucursal.'&opcion='.$opcion;
        $url_complete_cancel = 'excecutePaymentLockers.php?success=false';

    }
    
    //echo 'bd: '.$bd.' flag: '.$flag.' id_servicio: '.$id_servicio.' plan: '.$plan.' tipo: '.$tipo.' paquete_clase: '.$paquete_clase.' fecha_visita: '.$fecha_visita.' plan: '.$plan.' costo: '.$costo.' opcion: '.$opcion;

   
    
    $payer = new Payer();
    $payer->setPaymentMethod("paypal");
    

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

    $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;
        //echo 'bd: '.$bd.' flag: '.$flag.' id_servicio: '.$id_servicio.' plan: '.$plan.' tipo: '.$tipo.' paquete_clase: '.$paquete_clase.' fecha_visita: '.$fecha_visita.' plan: '.$plan.' costo: '.$costo.' opcion: '.$opcion."\n";

        //echo $payment->getApprovalLink();
        //header("Location: http://localhost/paypal_new/excecutePayment.php?success=true");
        exit(1);
    }

    //echo $approvalUrl = $payment->getApprovalLink();
    //header("Location: ".$approvalUrl);
    //echo "Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "<a href='$approvalUrl' >$approvalUrl</a>", $request, $payment;

return $payment;
?>