You are here

public function Mollie_API_Client::performHttpCall in Commerce Mollie 7

Perform an http call. This method is used by the resource specific classes. Please use the $payments property to perform operations on payments.

@codeCoverageIgnore

Parameters

$http_method:

$api_method:

$http_body:

Return value

string

Throws

Mollie_API_Exception

See also

$payments

$isuers

File

Mollie/API/Client.php, line 166

Class

Mollie_API_Client
Copyright (c) 2013, Mollie B.V. All rights reserved.

Code

public function performHttpCall($http_method, $api_method, $http_body = NULL) {
  if (empty($this->api_key)) {
    throw new Mollie_API_Exception("You have not set an api key. Please use setApiKey() to set the API key.");
  }
  $url = $this->api_endpoint . "/" . self::API_VERSION . "/" . $api_method;
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_ENCODING, "");
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $user_agent = join(' ', $this->versionStrings);
  $request_headers = array(
    "Accept: application/json",
    "Authorization: Bearer {$this->api_key}",
    "User-Agent: {$user_agent}",
    "X-Mollie-Client-Info: " . php_uname(),
  );
  if ($http_body !== NULL) {
    $request_headers[] = "Content-Type: application/json";
    if ($http_method == self::HTTP_POST) {
      curl_setopt($ch, CURLOPT_POST, 1);
    }
    else {
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, $http_body);
  }
  curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
  $body = curl_exec($ch);
  if (curl_errno($ch) == CURLE_SSL_CACERT || curl_errno($ch) == CURLE_SSL_PEER_CERTIFICATE || curl_errno($ch) == 77) {

    /*
     * On some servers, the list of installed certificates is outdated or not present at all (the ca-bundle.crt
     * is not installed). So we tell cURL which certificates we trust. Then we retry the requests.
     */
    $request_headers[] = "X-Mollie-Debug: used shipped root certificates";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__) . "/cacert.pem"));
    $body = curl_exec($ch);
  }
  if (strpos(curl_error($ch), "certificate subject name 'mollie.nl' does not match target host") !== FALSE) {

    /*
     * On some servers, the wildcard SSL certificate is not processed correctly. This happens with OpenSSL 0.9.7
     * from 2003.
     */
    $request_headers[] = "X-Mollie-Debug: old OpenSSL found";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $body = curl_exec($ch);
  }
  if (curl_errno($ch)) {
    throw new Mollie_API_Exception("Unable to communicate with Mollie (" . curl_errno($ch) . "): " . curl_error($ch));
  }
  return $body;
}