title: PayPal Checkout example code error: Unexpected token < in JSON at position 0
body: <p>Here is the code I'm using which I obtained from <a href="https://developer.paypal.com/docs/checkout/integrate/" rel="nofollow noreferrer">https://developer.paypal.com/docs/checkout/integrate/</a></p>
  
  <pre><code> &lt;script
                  src="https://www.paypal.com/sdk/js?client-id=&lt;?= $paypalID; ?&gt;&amp;currency=GBP"&gt; 
                &lt;/script&gt;
  
                &lt;div id="paypal-button-container"&gt;&lt;/div&gt;
  
                &lt;script&gt;
  
  
  
                  paypal.Buttons({
                      createOrder: function(data, actions) {
                        // This function sets up the details of the transaction, including the amount and line item details.
                        return actions.order.create({
                          purchase_units: [{
                            amount: {
  
                              "value": "&lt;?= $grandTotalOP; ?&gt;"
                            }
                          }]
                        });
                      },
                      onApprove: function(data) {
                          console.log(data);
  
                          var theBody = {'orderID' : data.orderID};
                          theBody = JSON.stringify(theBody);
  
                        return fetch('/capture-paypal-transaction', {
                          headers: {
                            'content-type': 'application/json'
                          },
                          method: "POST",
                          body: theBody
                        }).then(function(res) {
                           console.log('test');
                           console.log(res);
                          return res.json();
                        }).then(function(details) {
                          alert('Transaction funds captured from ' + details.payer_given_name);
                        })
                      }
                    }).render('#paypal-button-container');
  
                &lt;/script&gt;
  </code></pre>
  
  <p>i am getting this error in the console</p>
  
  <blockquote>
    <p>js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1
    Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0</p>
    
    <p>Error: Unexpected token &lt; in JSON at position 0
        at ur. (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:295788" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:295788</a>)
        at Object. (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302957" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302957</a>)
        at JSON.parse ()
        at o (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302816" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302816</a>)
        at wr (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302969" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302969</a>)
        at Ir.l.on (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:307996" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:307996</a>)
        at Ir (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:308185" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:308185</a>)
        at <a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313190" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313190</a>
        at <a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313236" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313236</a></p>
    
    <p>Error: Unexpected token &lt; in JSON at position 0
        at ur. (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:295788" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:295788</a>)
        at Array. (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302957" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302957</a>)
        at JSON.parse ()
        at o (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302816" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302816</a>)
        at wr (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302969" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:302969</a>)
        at Ir.l.on (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:307996" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:307996</a>)
        at Ir (<a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:308185" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:308185</a>)
        at <a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313190" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313190</a>
        at <a href="https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313236" rel="nofollow noreferrer">https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm_epTAh3MDAmQ0o9UAoqGdPy2kgS-syd8KbJIcmk8QYpAeE99ZzgEnOfaPdoYfGe&amp;currency=GBP:1:313236</a></p>
  </blockquote>
  
  <p>and this is the response from the server</p>
  
  <blockquote>
    <p>Fatal error   Uncaught PayPalHttp\HttpException:
    {&quot;error&quot;:&quot;invalid_client&quot;,&quot;error_description&quot;:&quot;Client
    Authentication failed&quot;} in
    /home/MYSERVER/public/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php:215
    Stack trace:
    #0 /home/MYSERVER/public/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php(100):
    PayPalHttp\HttpClient-&gt;parseResponse(Object(PayPalHttp\Curl))
    #1 /home/MYSERVER/public/vendor/paypal/paypal-checkout-sdk/lib/PayPalCheckoutSdk/Core/AuthorizationInjector.php(37):
    PayPalHttp\HttpClient-&gt;execute(Object(PayPalCheckoutSdk\Core\AccessTokenRequest))
    #2 /home/MYSERVER/public/vendor/paypal/paypal-checkout-sdk/lib/PayPalCheckoutSdk/Core/AuthorizationInjector.php(29):
    PayPalCheckoutSdk\Core\AuthorizationInjector-&gt;fetchAccessToken()
    #3 /home/MYSERVER/public/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php(64):
    PayPalCheckoutSdk\Core\AuthorizationInjector-&gt;inject(Object(PayPalCheckoutSdk\Orders\OrdersGetRequest))
    #4 /home/MYSERVER/public/public/capture-paypal-transaction.php(23): PayPalHttp\HttpClient- in
    <b>/home/MYSERVER/public/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.php</b>
    on line <b>215</b><br /></p>
  </blockquote>
  
  <p>here is the server code which is taken from the paypal docs link at the top</p>
  
  <pre><code>  &lt;?php
  
  namespace Sample;
  
  require_once '/home/MYSERVER/public/vendor/autoload.php'; 
  
  //1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
  use Sample\PayPalClient;
  use PayPalCheckoutSdk\Orders\OrdersGetRequest;
  
  class GetOrder
  {
  
    // 2. Set up your server to receive a call from the client
    /**
     *You can use this function to retrieve an order by passing order ID as an argument.
     */
    public static function getOrder($orderId)
    {
  
      // 3. Call PayPal to get the transaction details
      $client = PayPalClient::client();
      $response = $client-&gt;execute(new OrdersGetRequest($orderId));
      /**
       *Enable the following line to print complete response as JSON.
       */
      //print json_encode($response-&gt;result);
      print "Status Code: {$response-&gt;statusCode}\n";
      print "Status: {$response-&gt;result-&gt;status}\n";
      print "Order ID: {$response-&gt;result-&gt;id}\n";
      print "Intent: {$response-&gt;result-&gt;intent}\n";
      print "Links:\n";
      foreach($response-&gt;result-&gt;links as $link)
      {
        print "\t{$link-&gt;rel}: {$link-&gt;href}\tCall Type: {$link-&gt;method}\n";
      }
      // 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
      print "Gross Amount: {$response-&gt;result-&gt;purchase_units[0]-&gt;amount-&gt;currency_code} {$response-&gt;result-&gt;purchase_units[0]-&gt;amount-&gt;value}\n";
  
      // To print the whole response body, uncomment the following line
      // echo json_encode($response-&gt;result, JSON_PRETTY_PRINT);
    }
  }
  
  /**
   *This driver function invokes the getOrder function to retrieve
   *sample order details.
   *
   *To get the correct order ID, this sample uses createOrder to create a new order
   *and then uses the newly-created order ID with GetOrder.
   */
  
  if (!count(debug_backtrace()))
  {
    GetOrder::getOrder('REPLACE-WITH-ORDER-ID', true);
  }
  ?&gt;
  </code></pre>