php - paypal php信用卡付款返回401

标签 php paypal http-status-code-401

我正在尝试通过 paypal REST API 服务进行信用卡支付,我可以获取访问 token ,但每次尝试进行信用卡支付时我都会收到 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

最佳答案

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

$headers = array("Content-Type: application/json", "Authorization: Bearer ". $AccessToken);

关于php - paypal php信用卡付款返回401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721399/

相关文章:

php - linux中的mssql连接

php - 需要有关使用 Paypal IPN 创建自定义订单的建议/示例

ios7 - IOSLinkedInAPI : Can't share post with the LinkedIn API

visual-studio - 为 IIS 使用 Web 部署工具 - 使用管理员帐户,但不适用于 IIS 或 Windows 用户帐户

button - 带自定义字段的 Paypal 按钮

google-app-engine - 如何通过 Apps 脚本对服务帐户进行身份验证以调用 GCP 云函数

php - 为什么我得到 Undefined property: stdClass::with php 8 nullsafe operator

php - 这些在php中是什么?例如:$ _ COOKIE [],$ _ POST []等

php - 使数据不可编辑,但在 php mysql 中单击按钮时保持可读

php - 将用户电子邮件与来自 IPN 的 PayPal 电子邮件匹配