php curl error 401 when trying to get payment info using payment id

rolam
Contributor
Contributor

I use sandbox mode to test paypal payments processing in my website.

On localhost it works perfectly..obtaining a token(using curl) and then getting the payment info(using curl) using the paymend_id provided by paypal response.

When i uploaded the code to test it on my live website i successfully obtain a bearer token using curl but there is a curl error when i try to obtain payment info using the payment_id.

`curl error =The requested URL returned error: 401 Unauthorized`

I don't get why the curl to get access token works but the get payment info doesn't...

Bear in mind that both are working on localhost perfectly.

 

curl to get payment info

 

$txt = "accessToken =" . $accessToken;
file_put_contents('../myfile/log.txt', $txt . PHP_EOL, FILE_APPEND | LOCK_EX);

$txt = "paymentID =" . $paymentID;
file_put_contents('../myfile/log.txt', $txt . PHP_EOL, FILE_APPEND | LOCK_EX);

// 2) Get payment information
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment/" . $paymentID);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer " . $accessToken;
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);

if (curl_errno($curl)) {
$txt = "curl error =" . curl_error($curl);
file_put_contents('../myfile/log.txt', $txt . PHP_EOL, FILE_APPEND | LOCK_EX);
curl_close($curl);
} else { 
$txt = "Error free curl";
file_put_contents('../myfile/log.txt', $txt . PHP_EOL, FILE_APPEND | LOCK_EX);
}

Using file_put_contents i can see that the accessToken and paymentID are there. I even tried to copy paste them directly into my code just in case but same result.

curl to get access token (working fine with no curl errors)

$ch = curl_init();
            $clientId = PayPal_CLIENT_ID;
            $secret = PayPal_SECRET;
            // PayPal_BASE_URL = "https://api.sandbox.paypal.com/v1/"
            curl_setopt($ch, CURLOPT_URL, PayPal_BASE_URL . 'oauth2/token');
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $secret);
            curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
            if (!($result = curl_exec($ch))) // curl failed
            {
                $txt = "curl error token =" . curl_error($ch);
                file_put_contents('../myfile/log.txt', $txt . PHP_EOL, FILE_APPEND | LOCK_EX);
                curl_close($ch);
            } else // curl success
            {
               $txt = "Error free token curl";
               file_put_contents('../myfile/log.txt', $txt . PHP_EOL, FILE_APPEND | LOCK_EX);
            }

 

Login to Me Too
2 REPLIES 2

rolam
Contributor
Contributor
Problem solved the mistake was not in the provided code. The three dots doesn't open a menu so i can't delete this post.
Login to Me Too

PickleGuy
Contributor
Contributor

Well what was cause of the problem?

🙂

 

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.