1

我正在尝试找出 paypal API,并且我有以下代码,它应该进行调用,获取访问令牌,然后进行 API 调用。第一部分有效(直到该$accesstoken行),并正确返回访问令牌,但第二部分不返回任何内容。可以在此处找到应该模仿的代码:拨打您的第一个电话

$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$headers = array(
    'Accept' => 'application/json',
    'Accept-Language' => 'en_US',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $clientID . ':' . $clientSecret);
$curl = curl_exec($ch);

$x = json_decode($curl, TRUE);
print_r($x);
$accesstoken = $x['access_token'];


$headers2 = array(
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer' . $accesstoken
);

$data = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "http://example.com/your_redirect_url/",
        "cancel_url" => "http://example.com/your_cancel_url/"
    ),
    "payer" => array(
        "payment_method" => "paypal"
    ),
    "transactions" => array(
        "transactions" => array(
            "total" => ".99",
            "currency" => "USD"
        )
    )
);

$saleurl = "https://api.sandbox.paypal.com/v1/payments/payment";

$sale = curl_init();
curl_setopt($sale, CURLOPT_URL, $saleurl);
curl_setopt($sale, CURLOPT_VERBOSE, TRUE);
curl_setopt($sale, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($sale, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($sale, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($sale, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($sale, CURLOPT_HTTPHEADER, $headers2);
$finalsale = curl_exec($sale);

$verb = json_decode($finalsale, TRUE);
print_r($verb);

卷曲对我来说没有完全意义,任何帮助将不胜感激。

更新:我将标题的格式更改为:

$headers2 = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $accesstoken
);

根据答案之一。现在它显示:

[name] => MALFORMED_REQUEST
[message] => Incoming JSON request does not map to API request
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST
[debug_id] => f53a882702a04
4

3 回答 3

2

您没有正确设置标题...

$headers = array(
    'Accept: application/json',
    'Accept-Language: en_US'
);

$headers2 = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $accesstoken
);

是正确的格式。

还要注意 $accesstoken 之间的 Bearer 之后的(空格)

编辑:更新您的 JSON(我认为这是正确的,但将其回显并对照参考进行检查,我可能有一对多数组()

$data = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "http://example.com/your_redirect_url/",
        "cancel_url" => "http://example.com/your_cancel_url/"
    ),
    "payer" => array(
        "payment_method" => "paypal"
    ),
    "transactions" => array(array(
            "amount" => array(
                "total" => ".99",
                "currency" => "USD"
            )
        )
    )
);
于 2013-10-07T18:29:01.537 回答
1

你需要一个空间

$headers2 = array(
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ' . $accesstoken // Added a space after Bearer
);

看看现在能不能用

于 2013-10-07T18:30:53.373 回答
0

更改 curl_setopt($sale, CURLOPT_POSTFIELDS, json_encode($data));

到 curl_setopt($sale, CURLOPT_POSTFIELDS, $data);

json 编码函数破坏了它

于 2014-01-16T03:52:54.013 回答