title: PayPal express checkout server side, authentication failure
body: <p>I searched a lot everywhere but I didn't find an answer for my problem.</p>
  
  <p>I can't make it work. My PayPal button is using express checkout Server side Rest, but with client-side I tested, it's easy, and it works.</p>
  
  <p>I am using CodeIgniter so here is my code.</p>
  
  <p>Here is the javascript code in my views folder. <code>CREATE_URL</code> and <code>EXECUTE_URL</code> goes to a controller.</p>
  
  <pre><code>paypal.Button.render({
  
          env: 'sandbox', // sandbox | production
  
          // Show the buyer a 'Pay Now' button in the checkout flow
          commit: true,
  
          // payment() is called when the button is clicked
          payment: function() {
  
              // Set up a url on your server to create the payment
              var CREATE_URL = '/projects/createurl';
  
              // Make a call to your server to set up the payment
              return paypal.request.post(CREATE_URL)
                  .then(function(res) {
                      return res.paymentID;
                  });
          },
  
          // onAuthorize() is called when the buyer approves the payment
          onAuthorize: function(data, actions) {
  
              // Set up a url on your server to execute the payment
              var EXECUTE_URL = '/projects/executeurl';
  
              // Set up the data you need to pass to your server
              var data = {
                  paymentID: data.paymentID,
                  payerID: data.payerID
              };
  
              // Make a call to your server to execute the payment
              return paypal.request.post(EXECUTE_URL, data)
                  .then(function (res) {
                      window.alert('Payment Complete!');
                  });
          }
  
      }, '#paypal-button-container');
  </code></pre>
  
  <p>Here is <code>CREATE_URL</code> controller</p>
  
  <pre><code>public function createurl() {
  
              $ch = curl_init();
      $clientId = "something";
      $secret = "something";
  
      curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSLVERSION , 6); 
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
      curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
  
      $result = curl_exec($ch);
  
      if(empty($result))die("Error: No response.");
      else
      {
          $json = json_decode($result, TRUE);
      }
  
  
  
      curl_close($ch); 
  
  
      $ch2 = curl_init();
      $token = $json['access_token'];
      $data = '{
              "transactions": [{
              "amount": {
                  "currency":"EUR",
                  "total":"12"
              },
              "description":"creating a payment"
              }],
              "payer": {
                  "payment_method":"paypal"
              },
              "intent":"sale",
              "redirect_urls": {
                  "cancel_url":"https://mysite.gr/projects/project/10",
                  "return_url":"https://mysite.gr/projects/project/10"
              }
         }'; 
  
  
      curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
      curl_setopt($ch2, CURLOPT_VERBOSE, 1);
      curl_setopt($ch2, CURLOPT_HEADER, 0);
      curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch2, CURLOPT_HTTPHEADER , "Content-Type: application/json");
      curl_setopt($ch2, CURLOPT_HTTPHEADER , "Authorization: Bearer ".$token);
      curl_setopt($ch2, CURLOPT_POST, 1);
      curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
  
      $result = curl_exec($ch2);
      $json = json_decode($result, TRUE); 
      print_r($json);
      curl_close($ch2);
  } 
  </code></pre>
  
  <p>So when I click the PayPal button - it fails. Logs from the console :</p>
  
  <blockquote>
    <p>ppxo_no_token_passed_to_payment</p>
    
    <p>ppxo_unhandled_error {stack: "Error: No value passed to payment↵    at
    <code>https://w…://www.paypalobjects.com/api/checkout.js:3076:13</code>)", errtype:
    "[object Error]"</p>
    
    <p>Uncaught Error: No value passed to payment</p>
  </blockquote>
  
  <p>And also when I enter the controller into the url to see the array it says this: </p>
  
  <blockquote>
    <p>Array ( [name] => AUTHENTICATION_FAILURE [message] => Authentication
    failed due to invalid authentication credentials or a missing
    Authorization header. [links] => Array ( [0] => Array ( [href] =>
    <code>https://developer.paypal.com/docs/api/overview/#error</code> [rel] =>
    information_link ) ) )</p>
  </blockquote>