public function PayPalCheckoutClient::submitRequest in Commerce PayPal 7.2
Submits an API request to the PayPal server.
Parameters
string $method: The HTTP method to use. One of: 'GET', 'POST', 'PATCH', 'PUT', DELETE'.
string $path: The remote path. The base URL will be automatically appended.
array $parameters: An array of parameters to include with the request. Optional.
Return value
string[] The API response JSON converted to an associative array.
Throws
PayPalCheckoutAuthenticationException if the request fails authentication.
PayPalCheckoutHttpServerErrorException if the response status code is 5xx.
PayPalCheckoutHttpClientErrorException if the response status code is 4xx.
PayPalCheckoutHttpRedirectionException if the response status code is 3xx.
PayPalCheckoutInvalidResponseJsonException if the response is not valid JSON.
9 calls to PayPalCheckoutClient::submitRequest()
- PayPalCheckoutClient::authorizeOrder in modules/
checkout/ lib/ PayPalCheckoutClient.php - Authorize payment for order.
- PayPalCheckoutClient::captureOrder in modules/
checkout/ lib/ PayPalCheckoutClient.php - Capture payment for order.
- PayPalCheckoutClient::capturePayment in modules/
checkout/ lib/ PayPalCheckoutClient.php - Captures an authorized payment, by ID.
- PayPalCheckoutClient::createOrder in modules/
checkout/ lib/ PayPalCheckoutClient.php - Creates an order in PayPal.
- PayPalCheckoutClient::getOrder in modules/
checkout/ lib/ PayPalCheckoutClient.php - Get an existing order from PayPal.
File
- modules/
checkout/ lib/ PayPalCheckoutClient.php, line 313 - Defines a class for consuming the PayPal Checkout API.
Class
- PayPalCheckoutClient
- Defines the PayPalCheckoutClient class.
Code
public function submitRequest($method, $path, $parameters = array()) {
$this->headers['Authorization'] = 'Bearer ' . $this
->getAccessToken();
$url = $this
->baseUrl() . '/' . $path;
$ch = curl_init();
static::setDefaultCurlOptions($ch);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($parameters)) {
if ($this->headers['Content-Type'] == 'application/json') {
// JSON encode the fields and set them to the request body.
curl_setopt($ch, CURLOPT_POSTFIELDS, drupal_json_encode($parameters));
}
else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters, '', '&'));
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, static::formatHeaders($this->headers));
// Submit the request to the PayPal server.
$this
->logMessage(sprintf('Request URL: %s', $url));
curl_setopt($ch, CURLOPT_URL, $url);
$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);
}
if ($response_info['http_code'] == 401) {
// Attempt to get a new access token if the authentication failed.
// This might happen if we're sending an expired access token to PayPal.
if ($this->retryCount < static::RETRY_LIMIT) {
$this->retryCount++;
// Ensure we get a fresh access token next time.
variable_del('commerce_paypal_checkout_access_token');
return $this
->submitRequest($method, $path, $parameters);
}
$json = drupal_json_decode($response);
// 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);
}
elseif ($response_info['http_code'] >= 500) {
// Throw an exception indicating a server error.
throw new PayPalCheckoutHttpServerErrorException('', $response_info['http_code']);
}
elseif ($response_info['http_code'] >= 400) {
// Throw an exception indicating a client error.
throw new PayPalCheckoutHttpClientErrorException('', $response_info['http_code']);
}
elseif ($response_info['http_code'] >= 300) {
// Throw an exception indicating a redirection that this library is not
// going to automatically follow.
throw new PayPalCheckoutHttpRedirectionException('', $response_info['http_code']);
}
// Attempt to convert the response body to an associative array.
try {
$json = drupal_json_decode($response);
} catch (\Exception $e) {
throw new PayPalCheckoutInvalidResponseJsonException('The API response string could not be parsed as JSON.');
}
return $json;
}