贝宝rest api支付调用返回401使用php



我正在尝试将贝宝添加到我的网站。我一直在遵循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函数

@Jason247

http://php.net/manual/en/function.curl-setopt.php

我认为CURLOPT_HEADER需要int或bool,1或TRUE来发送标头。

我相信CURLOP_HTTPHEADER就是您想要的,您可以直接将数组传递给它,而无需对查询字符串进行编码。

例如

curl_setopt($curlHandle, CURLOPT_HEADER, 1);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $curlHeaders);

401错误通常表示访问令牌无效或过期:https://developer.paypal.com/webapps/developer/docs/integration/direct/rest-payments-error-handling/

您是否在授权标头中包含"Bearer"?示例:

Authorization:Bearer EMxItHE7Zl4cMdkvMg-f7c63GQgYZU8FjyPWKQlpsqQP

相关内容

  • 没有找到相关文章

最新更新