PHP PayPal REST API authorization call - php

I'm trying to use PHP and cURL to request an auth token from PayPal to create a payment.
my code is:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$output = curl_exec($ch);
?>
I get the response:
curl_setopt() expects parameter 2 to be long, string given in Users/anth/sites/abyss/auth.php on line 11
{"error":"invalid_client","error_description":"Invalid client credentials"}
line 11 is:
curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');
I have checked my Client ID and Secret and they are fine.
Does anyone know if I am using the wrong syntax or something?
Thanks

You have used the curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx') instead of curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX'); . Note the missed 'E' and apart from that you need to add one more line :
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
So the corrected version is below :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
print_r($output);

Related

paypal api curl refund getting error AUTHENTICATION_FAILURE

I am trying to implement paypal refund using php curl.
This is my code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.paypal.com/v1/oauth2/token");
//curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
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");
$result = curl_exec($ch);
$json = json_decode($result);
$access_token=$json->access_token;
curl_close($ch);
$ch=curl_init();
$headers=array('Content-Type: application/json','Authorization: Bearer '.$access_token);
curl_setopt($ch,CRULOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_URL, "https://api.paypal.com/v1/payments/sale/".$paypal_transaction_id."/refund");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
$result = curl_exec($ch);
$result_array=json_decode($result,true);
I got the access token successfully.
But, the following error occurs in refund.
"Authentication failed due to invalid authentication credentials or a
missing Authorization header."
What's wrong with my code?
Or
My PayPal account is a business account and there is no problem with receiving money (using php paypal api).
Look at https://developer.paypal.com/docs/platforms/get-started/ it says you need to fill out the PayPal Marketplaces and Platforms form to be approved.
Is this the problem?
Haha, I found the error.
It's just spell error.(curl_setopt($ch,CRULOPT_HTTPHEADER,$headers);)
not CRULOPT_HTTPHEADER , is CURLOPT_HTTPHEADER
and I have change code more simply.(using API v2)
$ch=curl_init();
$headers=array('Content-Type: application/json','Authorization: Basic '.base64_encode($clientId.':'.$secret));
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$refund_url="https://api.paypal.com/v2/payments/captures/".$order['paypal_txn_id']."/refund";
curl_setopt($ch, CURLOPT_URL, $refund_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
$result = curl_exec($ch);
$result_array=json_decode($result,true);
curl_close($ch);

Curl to test Sabre REST API Authentication Failing

I'm creating the authentication and getting an access token fine but the problem arises when I try to send a request. I'm creating the headers using PHP
<?php
define("POST", "POST");
**$environment=' https://api-crt.cert.havail.sabre.com';**
$userId='XXXXXXX';
$domain='AA';
$Group='XXXXXXXXXX';
$formatVersion='V1';
$clientSecret=base64_encode('XXXXXXXXXXX');
$client_id=base64_encode($formatVersion.":".$userId.":".$Group.":".$domain);
$bulid_credential=base64_encode($client_id.":".$clientSecret);
######################## Step 1: Get Token #############################
**$ch =curl_init("https://api-crt.cert.havail.sabre.com/v2/auth/token");**
$vars ="grant_type=client_credentials";
$header =array(
'Authorization: Basic '.$bulid_credential,
'Accept: */*',
'Content-Type: application/x-www-form-urlencoded'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res= curl_exec($ch);
$result=json_decode($res);
$token=$result->access_token;
########################## Step 2: Call the REST API###################
**$url="https://api.test.sabre.com/v3.3.0/shop/flights?mode=live";**
$header = array(
'Authorization: Bearer'. $token,
'Content-Type: application/json'
);
$calltype='POST';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $calltype);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonrequest);//Request
array_push($header, 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result=curl_exec($ch);
$result1=json_decode($result);
curl_close($ch);
?>
The Sabre Response :
{"status":"Incomplete","type":"Application","errorCode":"ERR.2SG.PROVIDER_ERROR","timeStamp":"2017-10-11T06:02:08.658-05:00","message":"Error occurred while invoking service rest:readMetadata:1.13.7.SNAPSHOT"}
1) Did you ensure that $token is a string and not an object?
I use this:
$result=json_decode($res, TRUE);
$token=$result['access_token'];
Instead of this:
$result=json_decode($res);
$token=$result->access_token;
2) Did you ensure that the content of $jsonrequest is valid?
Maybe it's not the PHP code that is incorrect, but your JSON query.

how to convert curl command into php script

I have a situation where i want to use curl functionality to get data by api call.
the curl command i'm interested to convert look like this:
"curl -X POST \\ \n'http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806' \\ \n -H 'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU= \\ \n -H 'cache-control: no-cache' \\ \""
I have tried this much
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$array('merchantKey'=>40747,'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
please help me to correct above code!
thank in advance
You are missing a semicolon in your code and you don't need to declare the array as a variable.
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806));
PHP Code
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
// Add authorization header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU=',
));
// Remove $ from array()
curl_setopt($ch, CURLOPT_POSTFIELDS, array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// For No-Cache
curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);
$result = curl_exec($ch);
?>
You should do it like this, here i am assuming all your parameters are correct. You were missing some required parameter which were sending in curl command but not in PHP curl that is CURLOPT_HTTPHEADER and CURLOPT_HEADER and you should use http_build_query while sending array in CURLOPT_POSTFIELDS and You should use CURLOPT_FOLLOWLOCATION because your current is URL is redirecting from HTTP to HTTPS
<?php
ini_set('display_errors', 1);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('merchantKey'=>"40747",'merchantTransactionIds'=>"396132-58876806")));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('cache-control: no-cache',
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU='));
curl_exec($ch);

when CURLOPT_HTTPHEADER need 'Content-Length'

I have this code in the client side of my application:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length:0 ' )
);
$body = curl_exec($ch);
while in the server files:
$ch = curl_init($this->modelAddress);
$data = array("params"=>array("resource" => "artist","operation" => $operation,"params"=>$params));
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
They are made from 2 different people and I want to ask what is the most correct.
When the use of 'Content-Length:0' instead of 'Content-Length:'.strlen($data_string) is better.
It's only question of POST/GET method?
I've searched explanations of this in the web but I've found nothing
If you use http PUT with curl then you have to specify the Content-Length header. Otherwise it is automatically handled by the curl for both GET and POST.
For example if you are posting the following data with curl with CURLOPT_POSTFIELDS. Then curl will automatically add the length of the data with the header.
$data = "name=alex&email=none!";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
For better understand, run your curl code using verbose mode and you'll see how its handling the requests and responses.
curl_setopt($ch, CURLOPT_VERBOSE, true);

how to pass authorization token in header in php

i am trying to pass the authorization token in my php page.
The code i am using is as below:
function getValues(){
$path='webservice path';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic token_no'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Access-Control-Allow-Origin: *'));
$retValue = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $retValue;
}
but i am not able to include the header.
Please help me out with this problem.
You should only set CURLOPT_HTTPHEADER once, with an array containing all headers.
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Authorization: Basic token_no',
'Content-type: text/xml',
'Access-Control-Allow-Origin: *'
));

Categories