简体   繁体   中英

paypal rest api payment call returning 401 using php

I am trying to add paypal to my site. I have been following the instructions at https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/ , but it is not working. I have sent the request for an access token successfully and gotten a response. The information in the response is stored in an object called $accessToken. The problem lies when I try to make the API call in step 3 from the site listed above. I get a 401 error sent back from the request. I'm pretty sure the $url that the request is sent to as a function parameter is correct. It is https://api.sandbox.paypal.com/v1/payments/payment . I have been going all over the internet for help for the past week and a half, and I haven't made any progress whatsoever. Any help would be greatly appreciated. Thanks!

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 function

@Jason247

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

I think that CURLOPT_HEADER requires an int or bool, either 1 or TRUE to send headers.

I do believe CURLOP_HTTPHEADER is what you want, you can pass an array directly to it without encoding to a query string.

eg

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

A 401 error generally indicates that the access token is either invalid or expired: https://developer.paypal.com/webapps/developer/docs/integration/direct/rest-payments-error-handling/

Are you including "Bearer" in the Authorization header? Example:

Authorization:Bearer EMxItHE7Zl4cMdkvMg-f7c63GQgYZU8FjyPWKQlpsqQP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM