Hello, I have used REST API for my checkout integration in codeigniter. I am getting error while executing create_payement() function of my papal library like: {"name":"AUTHENTICATION_FAILURE","message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.","links":[{"href":"https://developer.paypal.com/docs/api/overview/#error","rel":"information_link"}]} Here is my code: class Paypal_lib
{
private $PP_SERVER;
private $API_USER;
public function __construct($is_sandbox=true)
{
$this->API_USER['ACCOUNT'] = 'myemail';
$this->API_USER['CLIENT_ID'] = 'myclientkeyforsandbox';
$this->API_USER['SECRET'] = 'mysecretkeyforsandbox';
if(!$is_sandbox) {
$this->PP_SERVER['PP_CHECKOUT_HOST'] = 'https://api.paypal.com';
} else {
$this->PP_SERVER['PP_CHECKOUT_HOST'] = 'https://api.sandbox.paypal.com';
}
}
public function create_payment()
{
$return_url = base_url('paypal/success');
$cancel_url = base_url('paypal/cancel');
$endpoint = '/v1/payments/payment';
$url = $this ->PP_SERVER['PP_CHECKOUT_HOST'] . $endpoint;
$invoice_number = 'test111';
$access_token = $this ->get_access_token();
$params = array(
'intent' => 'sale',
'payer' => array(
'payment_method' => 'paypal'
),
"transactions" => array(
"amount" => array(
"total" => "4.00",
"currency" => "USD"
),
),
'redirect_urls' => array(
'return_url' => $return_url,
'cancel_url' => $cancel_url
),
);
$data = json_encode($params);
$header = array(
'Content-Type' => 'application/json',
'Content-Length' => strlen($data),
'Authorization' => 'Bearer ' . $access_token,
'Accept' => 'application/json',
'Accept-Language' => 'en_US'
);
$addition_data = array(
CURLOPT_VERBOSE => 1,
);
$result = $this->execCurl($url, $data, $header, $addition_data);
var_dump($result);exit;
return $result;
}
public function execute_payment()
{
$return_url = base_url('paypal/success');
$cancel_url = base_url('paypal/cancel');
$endpoint = '/v1/payments/payment';
$url = $this ->PP_SERVER['PP_CHECKOUT_HOST'] . $endpoint;
$invoice_number = 'test111';
$access_token = $this -> get_access_token();
$params = array(
'intent' => 'sale',
'redirect_urls' => array(
'return_url' => $return_url,
'cancel_url' => $cancel_url
),
'payer' => array(
'payment_method' => 'paypal'
),
"transactions" => array(
0 => array(
"amount" => array(
"total" => "4.00",
"currency" => "USD",
"details" => array(
"subtotal" => "2.00",
"shipping" => "1.00",
"tax" => "2.00",
"shipping_discount" => "-1.00"
)
),
"item_list" => array(
"items" => array(
array(
"quantity" => "1",
"name" => "item 1",
"price" => "1",
"currency" => "USD",
"description" => "item 1 description",
"tax" => "1"
),
array(
"quantity" => "1",
"name" => "item 2",
"price" => "1",
"currency" => "USD",
"description" => "item 2 description",
"tax" => "1"
),
)
),
"description" => "The payment transaction description.",
"invoice_number" => "test111"
),
),
);
$data = json_encode($params);
$header = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $access_token
);
$addition_data = array(
CURLOPT_VERBOSE => 1,
);
echo "<pre>";
echo $url;
print_r($header);
print_r($params);
echo $data;
echo "</pre>";
$result = $this->execCurl($url, $data, $header, $addition_data);
var_dump($result);exit;
return $result;
}
protected function get_access_token()
{
$endpoint = '/v1/oauth2/token';
$url = $this -> PP_SERVER['PP_CHECKOUT_HOST'] . $endpoint;
$data = array(
'grant_type' => 'client_credentials'
);
$header = array(
'Accept' => 'application/json',
'Accept-Language' => 'en_US'
);
$addition_data = array(
CURLOPT_USERPWD => $this->API_USER['CLIENT_ID'] . ':' . $this->API_USER['SECRET'],
CURLOPT_VERBOSE => 1,
);
$result = $this -> execCurl($url, $data, $header, $addition_data);
$data = json_decode($result, true);
if($data) {
$access_token = $data['access_token'];
$nonce = $data['nonce'];
$token_type = $data['token_type'];
$app_id = $data['app_id'];
$expires_in = $data['expires_in'];
} else {
// something wrong ...
return false;
}
return $access_token;
}
protected function execCurl($url, $data, $header=array(), $addition_data=array()) {
if(is_array($data)) {
$data = http_build_query($data, '', "&");
}
$defaults = array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 0,
CURLOPT_HTTPHEADER => $header,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $data,
CURLOPT_CUSTOMREQUEST => 'POST'
);
if($addition_data) {
$defaults = $addition_data + $defaults;
}
$ch = curl_init();
curl_setopt_array($ch, $defaults);
$result = curl_exec($ch);
if (!$result) {
var_dump($defaults);
var_dump($result);
exit('error: ' . curl_error($ch) . 'cURL failed');
}
curl_close($ch);
return $result;
}
} I have checked my sandbox credentials and they are perfectly okay. I am stuck with the paypal integration since a week . Anyone have a proper integration option or any idea regarding this issue?
... View more