You are here

function _commerce_sagepay_request_post in Drupal Commerce SagePay Integration 7

Send a POST request to SagePay and return the response as an array.

Parameters

string $url: The url to POST to.

array $data: The data to post.

Return value

array The response from Sage Pay.

16 calls to _commerce_sagepay_request_post()
commerce_sagepay_abort_transaction in includes/commerce_sagepay_abort.inc
commerce_sagepay_authorise_form_submit in includes/commerce_sagepay_authorise.inc
Submit handler: process a prior authorization capture.
commerce_sagepay_cancel_transaction in includes/commerce_sagepay_cancel.inc
commerce_sagepay_direct_submit_form_submit in includes/commerce_sagepay_direct.inc
Payment method callback: checkout form submission.
commerce_sagepay_paypal_handle_callback in modules/commerce_sagepay_paypal/includes/commerce_sagepay_paypal.inc
Handle a POST callback from Pay Pal to confirm the transaction.

... See full list

File

includes/commerce_sagepay_common.inc, line 840
Common utility functions shared by all SagePay modules.

Code

function _commerce_sagepay_request_post($url, $data) {
  set_time_limit(60);
  $output = array();
  $curl_session = curl_init();
  curl_setopt($curl_session, CURLOPT_URL, $url);
  curl_setopt($curl_session, CURLOPT_HEADER, 0);
  curl_setopt($curl_session, CURLOPT_POST, 1);
  curl_setopt($curl_session, CURLOPT_POSTFIELDS, $data);
  curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl_session, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl_session, CURLOPT_SSL_VERIFYHOST, 2);

  /* Support for peer verification - as per https://drupal.org/node/1931760#comment-7365072

      If you get an error related to peer verification, you may need to download a CA certificate
      bundle file and place it in a safe location on your web server, and update your settings.php to set the
      commerce_sagepay_cacert variable to contain the absolute path of the file.

      Alternately, you may be able to update your php.ini to point to the file with the curl.cainfo setting.
    */
  $ca_cert_path = variable_get('commerce_sagepay_cacert', FALSE);
  if (!empty($ca_cert_path)) {
    curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($curl_session, CURLOPT_CAINFO, $ca_cert_path);
  }
  else {
    curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, 0);
  }
  $rawresponse = curl_exec($curl_session);
  $response = explode(chr(10), $rawresponse);
  if (curl_error($curl_session)) {
    $output['Status'] = "FAIL";
    $output['StatusDetail'] = curl_error($curl_session);
  }
  curl_close($curl_session);
  for ($i = 0; $i < count($response); $i++) {
    $split_at = strpos($response[$i], "=");
    $output[trim(substr($response[$i], 0, $split_at))] = trim(substr($response[$i], $split_at + 1));
  }
  return $output;
}