protected function PayPalCheckoutClient::acquireAccessToken in Commerce PayPal 7.2
Acquire an access token from PayPal.
Return value
string[] The API response JSON converted to an associative array.
Throws
PayPalCheckoutAuthenticationException
1 call to PayPalCheckoutClient::acquireAccessToken()
- PayPalCheckoutClient::getAccessToken in modules/
checkout/ lib/ PayPalCheckoutClient.php - Returns the access token stored locally if any, or get one from PayPal.
File
- modules/
checkout/ lib/ PayPalCheckoutClient.php, line 113 - Defines a class for consuming the PayPal Checkout API.
Class
- PayPalCheckoutClient
- Defines the PayPalCheckoutClient class.
Code
protected function acquireAccessToken() {
$ch = curl_init();
static::setDefaultCurlOptions($ch);
$headers = array(
'Authorization' => 'Basic ' . base64_encode($this->config['client_id'] . ':' . $this->config['secret']),
'Content-Type' => 'application/x-www-form-urlencoded',
) + $this->headers;
$url = $this
->baseUrl() . '/v1/oauth2/token';
curl_setopt($ch, CURLOPT_HTTPHEADER, static::formatHeaders($headers));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
// Submit the request to the PayPal server.
$response = curl_exec($ch);
// Ensure we got a successful response by inspecting the HTTP status code.
$response_info = curl_getinfo($ch);
if (is_resource($ch)) {
curl_close($ch);
}
$json = drupal_json_decode($response);
if ($response_info['http_code'] == 401) {
// Throw an exception indicating authentication failed.
$message = 'Authentication failed.';
if (isset($json['error_description'])) {
$message = sprintf('Error description: %s.', $json['error_description']);
}
throw new PayPalCheckoutAuthenticationException($message);
}
return $json;
}