title: PayPalConnectionException in PayPalHttpConnection.php line 174:
body: <p>Hey I'm working on Laravel 5.3 and I'm trying to implement the PayPal Api, and I found a tutorial online, so I followed every step but then I got an error and I don't know how to solve it.</p>
  
  <p>This is the code where I get the error:</p>
  
  <pre><code>try{
          $payment-&gt;create($this-&gt;_apiContext);
      } catch (\PayPal\Exception\PayPalConnectionException $ex){
          if(\Config::get('app.debug')){
              echo 'Exception: ' . $ex-&gt;getMessage() . PHP_EOL;
              $err_data = json_decode($ex-&gt;getData(), true);
              echo $err_data;/*
              echo $ex-&gt;getCode();
              echo $ex-&gt;getData();*/
              exit;
          } else {
              die('Ups! Algo salio mal');
          }
      }
  </code></pre>
  
  <p>And this is the error: </p>
  
  <p>PayPalConnectionException in PayPalHttpConnection.php line 174:
  Got Http response code 400 when accessing <a href="https://api.sandbox.paypal.com/v1/payments/payment/PAY-3SH87897C2313620PLCHWOOY/execute" rel="nofollow noreferrer">https://api.sandbox.paypal.com/v1/payments/payment/PAY-3SH87897C2313620PLCHWOOY/execute</a>.</p>
  
  <p>Any idea?</p>
  
  <p>Code:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use Illuminate\Foundation\Bus\DispatchesCommands;
  use Illuminate\Foundation\Validation\ValidatesRequests;
  use App\Http\Requests;
  use App\Deal;
  use App\Status;
  use PayPal\Rest\ApiContext;
  use PayPal\Auth\OAuthTokenCredential;
  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\ExecutePayment;
  use PayPal\Api\PaymentExecution;
  use PayPal\Api\Transaction;
  use Paypal;
  use Session;
  
  class PaypalController extends Controller
  {
      private $_apiContext;
  
  public function __construct()
  {
      $paypal_conf = \Config::get('paypal');
      $this-&gt;_apiContext = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
      $this-&gt;_apiContext-&gt;setConfig($paypal_conf['settings']);
      /*$this-&gt;_apiContext = Paypal::ApiContext(
          config('services.paypal.client_id'),
          config('services.paypal.secret'));
  
      $this-&gt;_apiContext-&gt;setConfig(array(
          'mode' =&gt;'sandbox',
          'service.EndPoint' =&gt; 'https://api.sandbox.paypal.com',
          'http.ConnectionTimeOut' =&gt; 30,
          'log.LogEnabled' =&gt; true,
          'log.FileName' =&gt; storage_path('logs/paypal.log'),
          'log.LogLevel' =&gt; 'FINE',
      ));*/
  }
  
  public function payPremium()
  {
      return view('payPremium');
  }
  
  public function getCheckout(Request $request)
  {
      $payer = Paypal::Payer();
      $payer-&gt;setPaymentMethod('paypal');
  
      $amount = Paypal::Amount();
      $amount-&gt;setCurrency('USD');
      $amount-&gt;setTotal($request-&gt;input('pay'));
  
      $transaction = Paypal::Transaction();
      $transaction-&gt;setAmount($amount);
      $transaction-&gt;setDescription('Compra de Plan '.$request-&gt;input('type').' Precio por mensualidad '.$request-&gt;input('pay'). 'Descripción del plan: '.$request-&gt;input('description'));
  
      $redirectUrls = Paypal::RedirectUrls();
      $redirectUrls-&gt;setReturnUrl(route('getDone'));
      $redirectUrls-&gt;setCancelUrl(route('getCancel'));
  
      $payment = Paypal::Payment();
      $payment-&gt;setIntent('sale');
      $payment-&gt;setPayer($payer);
      $payment-&gt;setRedirectUrls($redirectUrls);
      $payment-&gt;setTransactions(array($transaction));
  
      try{
          $payment-&gt;create($this-&gt;_apiContext);
      } catch (\PayPal\Exception\PayPalConnectionException $ex){
          if(\Config::get('app.debug')){
              echo 'Exception: ' . $ex-&gt;getMessage() . PHP_EOL;
              $err_data = json_decode($ex-&gt;getData(), true);
              echo $err_data;
              echo $ex-&gt;getMessage();
              echo $ex-&gt;getCode();
              echo $ex-&gt;getData();
              exit(1);
          } else {
              die('Ups! Algo salio mal');
          }
      }
  
      foreach ($payment-&gt;getLinks() as $link) {
          if($link-&gt;getRel() == 'approval_url'){
              $redirectUrls = $link-&gt;getHref();
              break;
          }
      }
  
      Session::put('paypal_payment_id', $payment-&gt;getId());
  
      if(isset($redirectUrls)){
          return \Redirect::away($redirectUrls);
      }
  
      return \Redirect::route('payPremium')-&gt;with('message', 'Ups! Error desconocido.');
  
      /*$response = $payment-&gt;create($this-&gt;_apiContext);*/
      /*$redirectUrl = $response-&gt;links[1]-&gt;href;*/
  
      /*$paymentExecution = Paypal::PaymentExecution();
      $id = $request-&gt;get('paymentId');
      $token = $request-&gt;get('token');
      $payer_id = $request-&gt;get('PayerID');
  
      $payment = Paypal::getById($id, $this-&gt;_apiContext);
  
      $paymentExecution-&gt;setPayerId($payer_id);
      $executePayment = $payment-&gt;execute($paymentExecution, $this-&gt;_apiContext);
      if ($executePayment-&gt;getState() == 'approved') {
          dd($executePayment);
          dd($amount);
          $transactions = new Transaction;
          $transactions-&gt;payer = $payer_id;
          $transactions-&gt;paymentID = $id;
          $transactions-&gt;amount = array_get($amount, 'setTotal');
          $transactions-&gt;description = array_get($transaction, 'setDescription');
          $transactions-&gt;statuses_id = 10;
          $transactions-&gt;save();
      }
      else
      {
          dd('Error en su pago');
          $transactions = new Transaction;
          $transactions-&gt;payer = $payer_id;
          $transactions-&gt;paymentID = $id;
          $transactions-&gt;amount = $amount;
          $transactions-&gt;description = $description;
          $transactions-&gt;statuses_id = 11;
          $transactions-&gt;save();
      }*/
  
      /*return redirect()-&gt;to( $redirectUrl );*/
  }
  
  public function getDone(Request $request)
  {
      $payment_id = \Session::get('paypal_payment_id');
      \Session::forget('paypal_payment_id');
  
      $payerId = $request-&gt;get('paymentId');
      $token = $request-&gt;get('PayerID');
  
      if(empty($payerId) || empty($token)){
          return \Redirect::route('payPremium')-&gt;with('message', 'Hubo un problema al intentar pagar con Paypal');
      }
  
      $payment = Payment::get($payment_id, $this-&gt;_apiContext);
  
      $execution = new PaymentExecution();
      $execution-&gt;setPayerId($request-&gt;get('paymentId'));
  
      $result = $payment-&gt;execute($execution, $this-&gt;_apiContext);
  
      if ($result-&gt;getState() == 'approved') {
          return \Redirect::route('payPremium')-&gt;with('message', 'Compra realizada de forma correcta');
      } else {
          return \Redirect::route('payPremium')-&gt;with('message', 'La compra fue cancelada');
      }
  
      // $paymentExecution = Paypal::PaymentExecution();
      // $id = $request-&gt;get('paymentId');
      // $token = $request-&gt;get('token');
      // $payer_id = $request-&gt;get('PayerID');
  
      // $payment = Paypal::getById($id, $this-&gt;_apiContext);
  
      // $paymentExecution-&gt;setPayerId($payer_id);
      // $executePayment = $payment-&gt;execute($paymentExecution, $this-&gt;_apiContext);
      // if ($executePayment-&gt;getState() == 'approved') {
      //  /*dd($executePayment);*/
   //        dd($amount);
   //        $transactions = new Transaction;
   //        $transactions-&gt;payer = $payer_id;
   //        $transactions-&gt;paymentID = $id;
   //        $transactions-&gt;amount = $price;
   //        $transactions-&gt;description = $description;
   //        $transactions-&gt;statuses_id = 10;
   //        $transactions-&gt;save();
      // }
      // else
      // {
      //  dd('Error en su pago');
   //        $transactions = new Transaction;
   //        $transactions-&gt;payer = $payer_id;
   //        $transactions-&gt;paymentID = $id;
   //        $transactions-&gt;amount = $amount;
   //        $transactions-&gt;description = $description;
   //        $transactions-&gt;statuses_id = 11;
   //        $transactions-&gt;save();
      // }
  }
  
  public function getCancel()
  {
      return redirect()-&gt;route('payPremium');
  }
  </code></pre>
  
  <p>}</p>