我正在尝试将贝宝添加到我的网站。我一直在遵循https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/上的说明,但它不起作用。我已成功发送访问令牌请求并得到响应。响应中的信息存储在一个名为 $accessToken 的对象中。当我尝试从上面列出的站点在步骤 3 中进行 API 调用时,问题就出现了。我收到从请求发回的 401 错误。我很确定将请求作为函数参数发送到的 $url 是正确的。它是https://api.sandbox.paypal.com/v1/payments/payment. 在过去的一个半星期里,我一直在互联网上寻求帮助,但我没有任何进展。任何帮助将不胜感激。谢谢!
function MakePaymentAPICall($accessToken, $sale, $url, $url_success, $url_cancel){
// Create cURL resource
$ch = curl_init();
// Set url
curl_setopt($ch, CURLOPT_URL, $url);
$tokenType = $accessToken->GetTokenType();
$token = $accessToken->GetAccessToken();
$auth = "Authorization:" . $tokenType . " " . $token;
$saleTotal = $sale->GetTotal();
$header = array(
'Content-Type' => 'application/json',
'Authorization' => $tokenType . ' ' . $token
);
$dataArray = array(
'intent' => 'sale',
'redirect_urls' => array(
'return_url' => $url_success,
'cancel_url' => $url_cancel
),
'payer' => array(
'payment_method' => 'paypal'
),
'transactions' => array(
'amount' => array(
'total' => $saleTotal,
'currency' => 'USD'
),
'description' => 'Test payment.'
)
);
curl_setopt($ch, CURLOPT_HEADER, http_build_query($header));
// set data to post
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($dataArray));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
// Execute curl command
$output = curl_exec($ch);
// Get info about request
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL resource to free up system resources
curl_close($ch);
return $output;
} // MakePaymentAPICall 函数