title: Integrating Paypal Rest API SDK to laravel 5.2
body: <p>I am integrating paypal for the first time in laravel 5.2. I am using the PayPal SDK as the api but I have reached a point where I am stuck. when I submit a payment form I get the following error.</p>
  
  <p>"PayPalConnectionException in PayPalHttpConnection.php line 176:
  Got Http response code 400 when accessing <a href="https://api.sandbox.paypal.com/v1/payments/payment" rel="nofollow">https://api.sandbox.paypal.com/v1/payments/payment</a>."</p>
  
  <p>I got the tutorial from <a href="http://learninglaravel.net/integrate-paypal-sdk-into-laravel-4-laravel-5" rel="nofollow">this website</a> and here is the code from my controller</p>
  
  <pre><code>&lt;?php
  namespace App\Http\Controllers; 
  use Illuminate\Http\Request;
  use App\Http\Requests;
  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 Session;
  use Redirect;
  use Config;
  use URL; 
  use Redirects;
  
  class IndexController extends Controller
  
  {
  
  private $_api_context;
  
  public function __construct()
  {
  
      // setup PayPal api context
      $paypal_conf = Config::get('paypal');
      $this-&gt;_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
      $this-&gt;_api_context-&gt;setConfig($paypal_conf['settings']);
  }
  
  public function paypalform()
  {
     return view('sponsors.paypalform'); 
  }
  
  
  
  public function postPayment()
  {
      $input = \Request::all();
      $product = $input['product'];
      $price = $input['price'];
      $shipping = 2;
  
      $total = $price + $shipping;
  
  
  
      $payer = new Payer();
      $payer-&gt;setPaymentMethod('paypal');
  
      $item_1 = new Item();
      $item_1-&gt;setName($product) // item name
          -&gt;setCurrency('USD')
          -&gt;setQuantity(2)
          -&gt;setPrice($price); // unit price
  
  
  
  
      $item_list = new ItemList();
      $item_list-&gt;setItems([$item_1]);
  
      $details = new Details();
      $details-&gt;setShipping($shipping)
              -&gt;setSubtotal($price);
  
      $amount = new Amount();
      $amount-&gt;setCurrency('USD')
             -&gt;setTotal($total)
             -&gt;setDetails($details);
  
  
       $transaction = new Transaction();
       $transaction-&gt;setAmount($amount)
           -&gt;setItemList($item_list)
           -&gt;setDescription('Your transaction description')
             -&gt;setInvoiceNumber(uniqid()); // added
  
  
  
  
       $redirect_urls = new RedirectUrls();
       $redirect_urls-&gt;setReturnUrl(URL::route('payment.status'))
           -&gt;setCancelUrl(URL::route('payment.status'));
  
       $payment = new Payment();
       $payment-&gt;setIntent('Sale')
           -&gt;setPayer($payer)
           -&gt;setRedirectUrls($redirect_urls)
           -&gt;setTransactions(array($transaction));
  
  
       try {
      $payment-&gt;create($this-&gt;_api_context);
  } catch (\PayPal\Exception\PPConnectionException $ex) {
      if (\Config::get('app.debug')) {
          echo "Exception: " . $ex-&gt;getMessage() . PHP_EOL;
          $err_data = json_decode($ex-&gt;getData(), true);
          exit;
      } else {
          die('Some error occur, sorry for inconvenient');
      }
  }
  
  
  
       foreach($payment-&gt;getLinks() as $link) {
           if($link-&gt;getRel() == 'approval_url') {
               $redirect_url = $link-&gt;getHref();
               break;
           }
       }
  
       // add payment ID to session
       Session::put('paypal_payment_id', $payment-&gt;getId());
  
       if(isset($redirect_url)) {
           // redirect to paypal
           return Redirect::away($redirect_url);
      }
  
      return Redirect::route('original.route')
          -&gt;with('error', 'Unknown error occurred');
  }
  
  }
  </code></pre>
  
  <p>I think the problems comes when redireciting to paypal website but I can't figure out what exactly is going wrong.</p>