Php PayPal签出示例代码错误:JSON中位置0处出现意外标记

Php PayPal签出示例代码错误:JSON中位置0处出现意外标记,php,paypal,paypal-sandbox,paypal-rest-sdk,Php,Paypal,Paypal Sandbox,Paypal Rest Sdk,这是我使用的代码,我从 我在控制台中得到这个错误 js?客户id=AALA1YLO5YJAUBYNMeptah3mDAMQ0O9UAOQGDPY2KGS-SYD8KBJICMK8QYPAEE99ZZZGENOFAPDOYGE和币种=1英镑 未捕获的SyntaxError:JSON中位置0处的意外标记ParseResponseObject PayPalHttp\Curl 1/home/MYSERVER/public/vendor/paypal/paypal checkout sdk/lib/Pay

这是我使用的代码,我从

我在控制台中得到这个错误

js?客户id=AALA1YLO5YJAUBYNMeptah3mDAMQ0O9UAOQGDPY2KGS-SYD8KBJICMK8QYPAEE99ZZZGENOFAPDOYGE和币种=1英镑 未捕获的SyntaxError:JSON中位置0处的意外标记<

错误:JSON中位置0处出现意外标记< 在乌尔。 反对。 在JSON.parse 在 在西铁 在Ir.l.on 红外线 在 在

错误:JSON中位置0处出现意外标记< 在乌尔。 在阵列上。 在JSON.parse 在 在西铁 在Ir.l.on 红外线 在 在

这是来自服务器的响应

未捕获PayPalHttp\HttpException的致命错误: {错误:无效的\u客户端,错误\u描述:客户端 中的}身份验证失败 /home/MYSERVER/public/vendor/paypal/paypalhttp/lib/paypalhttp/HttpClient.php:215 堆栈跟踪: 0/home/MYSERVER/public/vendor/paypal/paypalhttp/lib/paypalhttp/HttpClient.php100: PayPalHttp\HttpClient->ParseResponseObject PayPalHttp\Curl 1/home/MYSERVER/public/vendor/paypal/paypal checkout sdk/lib/PayPalCheckoutSdk/Core/AuthorizationInjector.php37: PayPalHttp\HttpClient->ExecuteObject PayPalCheckOutsdk\Core\AccessTokenRequest 2/home/MYSERVER/public/vendor/paypal/paypal checkout sdk/lib/PayPalCheckoutSdk/Core/AuthorizationInjector.php29: PayPalCheckoutSdk\Core\AuthorizationInjector->fetchAccessToken 3/home/MYSERVER/public/vendor/paypal/paypalhttp/lib/paypalhttp/HttpClient.php64: PayPalCheckoutSdk\Core\AuthorizationInjector->injectObjectPayPalCheckoutSdk\Orders\OrdersGetRequest 4/home/MYSERVER/public/public/capture-paypal-transaction.php23:PayPalHttp\HttpClient-in /home/MYSERVER/public/vendor/paypal/paypalhttp/lib/paypalhttp/HttpClient.php 在线215

下面是服务器代码,取自顶部的paypal文档链接

  <?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->execute(new OrdersGetRequest($orderId));
    /**
     *Enable the following line to print complete response as JSON.
     */
    //print json_encode($response->result);
    print "Status Code: {$response->statusCode}\n";
    print "Status: {$response->result->status}\n";
    print "Order ID: {$response->result->id}\n";
    print "Intent: {$response->result->intent}\n";
    print "Links:\n";
    foreach($response->result->links as $link)
    {
      print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
    }
    // 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
    print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";

    // To print the whole response body, uncomment the following line
    // echo json_encode($response->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);
}
?>
这就是解决办法

<?php

namespace Sample;

require_once '/home/server/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;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;


ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

class PayPalClient
{
    /**
     * Returns PayPal HTTP client instance with environment that has access
     * credentials context. Use this instance to invoke PayPal APIs, provided the
     * credentials have access.
     */
    public static function client()
    {
        return new PayPalHttpClient(self::environment());
    }

    /**
     * Set up and return PayPal PHP SDK environment with PayPal access credentials.
     * This sample uses SandboxEnvironment. In production, use LiveEnvironment.
     */
    public static function environment()
    {
        $clientId = getenv("CLIENT_ID") ?: "CLIENT_ID-here";
        $clientSecret = getenv("CLIENT_SECRET") ?: "CLIENT_SECRET-here";
        return new SandboxEnvironment($clientId, $clientSecret);
    }
}


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)
  {

    echo "testtttt ".$orderId;
    // 3. Call PayPal to get the transaction details
    $client = PayPalClient::client();
    $response = $client->execute(new OrdersGetRequest($orderId));
    /**
     *Enable the following line to print complete response as JSON.
     */
    print json_encode($response->result);
    print "Status Code: {$response->statusCode}\n";
    print "Status: {$response->result->status}\n";
    print "Order ID: {$response->result->id}\n";
    print "Intent: {$response->result->intent}\n";
    print "Links:\n";
    foreach($response->result->links as $link)
    {
      print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
    }
    // 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
    print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";

    // To print the whole response body, uncomment the following line
    // echo json_encode($response->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.
 */
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
print_r($data);
echo 'order id = '.$data->orderID;
if (!count(debug_backtrace()))
{
  GetOrder::getOrder($data->orderID, true);
}
?>

这是您的主要错误:{error:invalid_client,error\u description:client Authentication failed}您需要进行一些调试以缩小原因。在发布后感谢@PatrickQ我确实找到了答案,但我现在有点不确定如何调试它。。。我还在想。。。我真的希望这里有人能知道答案。在我看来,服务器端脚本是不正确的。我需要进一步查看文档,但这里肯定有人可以为我指出正确的方向???肯定在服务器脚本中的某个地方我应该把客户机密码放在哪里?我不相信无效的_客户机错误是主要错误。@FeelsUnique我们可能需要查看您的PayPalClient类。另外,您能否在网络检查器中显示对请求的响应https://www.paypal.com/sdk/js?client-id=AaLa1ylO5YjAUByNm─EPTAH3MQ0O9UAOQGDPY2KGS-SYD8KBJICMK8QYPAEE99ZZZENOFAPDOYGE和货币=GBP
<?php

namespace Sample;

require_once '/home/server/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;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;


ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

class PayPalClient
{
    /**
     * Returns PayPal HTTP client instance with environment that has access
     * credentials context. Use this instance to invoke PayPal APIs, provided the
     * credentials have access.
     */
    public static function client()
    {
        return new PayPalHttpClient(self::environment());
    }

    /**
     * Set up and return PayPal PHP SDK environment with PayPal access credentials.
     * This sample uses SandboxEnvironment. In production, use LiveEnvironment.
     */
    public static function environment()
    {
        $clientId = getenv("CLIENT_ID") ?: "CLIENT_ID-here";
        $clientSecret = getenv("CLIENT_SECRET") ?: "CLIENT_SECRET-here";
        return new SandboxEnvironment($clientId, $clientSecret);
    }
}


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)
  {

    echo "testtttt ".$orderId;
    // 3. Call PayPal to get the transaction details
    $client = PayPalClient::client();
    $response = $client->execute(new OrdersGetRequest($orderId));
    /**
     *Enable the following line to print complete response as JSON.
     */
    print json_encode($response->result);
    print "Status Code: {$response->statusCode}\n";
    print "Status: {$response->result->status}\n";
    print "Order ID: {$response->result->id}\n";
    print "Intent: {$response->result->intent}\n";
    print "Links:\n";
    foreach($response->result->links as $link)
    {
      print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
    }
    // 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
    print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";

    // To print the whole response body, uncomment the following line
    // echo json_encode($response->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.
 */
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
print_r($data);
echo 'order id = '.$data->orderID;
if (!count(debug_backtrace()))
{
  GetOrder::getOrder($data->orderID, true);
}
?>