paypal php信用卡付款返回401

时间:2015-07-30 10:32:33

标签: php paypal http-status-code-401

我尝试通过paypal REST API服务进行信用卡付款,我可以获取访问令牌,但每次尝试进行信用卡付款时,我都会获得401。

我的示例在.NET上运行得很好,但在PHP上却不行。请建议

$json = '{"intent":"sale","redirect_urls":{"return_url":"","cancel_url":""},"payer":{"payment_method":"credit_card","funding_instruments":[{"credit_card":{"number":"4417119669820331","type":"visa","expire_month":11,"expire_year":2018,"cvv2":"874","first_name":"Betsy","last_name":"Buyer","billing_address":{"line1":"111 First Street","city":"Saratoga","state":"CA","postal_code":"95070","country_code":"US"}}}]},"transactions":[{"amount":{"total":"200","currency":"USD"},"description":"Credit Card Payment - $200"}]}';

$Url = "https://api.sandbox.paypal.com/v1/payments/payment";
$AccessToken = GetAccessToken();
echo "Bearer ".$AccessToken;
echo "<br><br>";
$headers = array("Authorization" => "Bearer ".$AccessToken, "Content-Type" => "text/json", "Content-length" =>strlen($json));

$handle = curl_init($Url);


curl_setopt($handle,CURLOPT_HTTPHEADER,$headers);   
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$response   = curl_exec($handle);
$info = curl_getinfo($handle);
$error= curl_error ( $handle );
curl_close($handle);

var_dump($info);
echo "<br><br>";
var_dump($error);
echo "<br><br>";
var_dump($response);

此处的完整示例https://dl.dropboxusercontent.com/u/3517813/index.php

1 个答案:

答案 0 :(得分:2)

我可以通过将$ headers行更改为:

来处理它

$ headers = array(&#34; Content-Type:application / json&#34;,&#34;授权:Bearer&#34;。$ AccessToken);