You are here

function commerce_authnet_aim_request in Commerce Authorize.Net 7

Submits an AIM API request to Authorize.Net.

Parameters

$payment_method: The payment method instance array associated with this API request.

4 calls to commerce_authnet_aim_request()
commerce_authnet_aim_capture_form_submit in includes/commerce_authnet.admin.inc
Submit handler: process a prior authorization capture via AIM.
commerce_authnet_aim_credit_form_submit in includes/commerce_authnet.admin.inc
Submit handler: process a credit via AIM.
commerce_authnet_aim_submit_form_submit in ./commerce_authnet.module
Payment method callback: checkout form submission.
commerce_authnet_aim_void_form_submit in includes/commerce_authnet.admin.inc
Submit handler: process the void request.

File

./commerce_authnet.module, line 1743
Implements Authorize.Net payment services for use in Drupal Commerce.

Code

function commerce_authnet_aim_request($payment_method, $nvp = array()) {

  // Get the API endpoint URL for the method's transaction mode.
  $url = commerce_authnet_aim_server_url($payment_method['settings']['txn_mode']);

  // Add the default name-value pairs to the array.
  $nvp += array(
    // API credentials
    'x_login' => $payment_method['settings']['login'],
    'x_tran_key' => $payment_method['settings']['tran_key'],
    'x_version' => '3.1',
    // Extra administrative values
    'x_test_request' => $payment_method['settings']['txn_mode'] == AUTHNET_TXN_MODE_LIVE_TEST ? 'TRUE' : 'FALSE',
    'x_delim_data' => 'TRUE',
    'x_delim_char' => '|',
    'x_encap_char' => '"',
    'x_relay_response' => 'FALSE',
    'x_email_customer' => $payment_method['settings']['email_customer'],
    // Drupal Commerce Solution ID
    'x_solution_id' => 'A1000009',
  );

  // Allow modules to alter parameters of the API request.
  drupal_alter('commerce_authnet_aim_request', $nvp, $payment_method);

  // Log the request if specified.
  if ($payment_method['settings']['log']['request'] == 'request') {

    // Mask the credit card number and CVV.
    $log_nvp = $nvp;
    $log_nvp['x_login'] = str_repeat('X', strlen($log_nvp['x_login']));
    $log_nvp['x_tran_key'] = str_repeat('X', strlen($log_nvp['x_tran_key']));
    if (!empty($log_nvp['x_card_num'])) {
      $log_nvp['x_card_num'] = str_repeat('X', strlen($log_nvp['x_card_num']) - 4) . substr($log_nvp['x_card_num'], -4);
    }
    if (!empty($log_nvp['x_card_code'])) {
      $log_nvp['x_card_code'] = str_repeat('X', strlen($log_nvp['x_card_code']));
    }
    watchdog('commerce_authnet', 'Authorize.Net AIM request to @url: !param', array(
      '@url' => $url,
      '!param' => '<pre>' . check_plain(print_r($log_nvp, TRUE)) . '</pre>',
    ), WATCHDOG_DEBUG);
  }

  // Prepare the name-value pair array to be sent as a string.
  $pairs = array();
  foreach ($nvp as $key => $value) {
    $pairs[] = $key . '=' . urlencode($value);
  }

  // Setup the cURL request.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_VERBOSE, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $pairs));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  $result = curl_exec($ch);

  // Commerce Authnet requires SSL peer verification, which may prevent out of
  // date servers from successfully processing API requests. If you get an error
  // related to peer verification, you may need to:
  // * Update libraries like openssl and libcurl.
  // * Use https://github.com/paragonie/certainty to get updated certificates.
  // * Update your php.ini to point to your CA certificate bundle with the
  //   curl.cainfo setting.
  // * Download the CA certificate bundle file from
  //   http://curl.haxx.se/docs/caextract.html, place it in a safe location on
  //   your web server, and update your settings.php to set the
  //   commerce_authnet_cacert variable to contain the absolute path of the
  //   file.
  if (variable_get('commerce_authnet_cacert', FALSE)) {
    curl_setopt($ch, CURLOPT_CAINFO, variable_get('commerce_authnet_cacert', ''));
  }

  // Log any errors to the watchdog.
  if ($error = curl_error($ch)) {
    watchdog('commerce_authnet', 'cURL error: @error', array(
      '@error' => $error,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  curl_close($ch);

  // Make the response an array and trim off the encapsulating characters.
  $response = explode('|', $result);
  for ($i = 0; $i < count($response); $i++) {
    $response[$i] = substr($response[$i], 1, strlen($response[$i]) - 2);
  }

  // Log the response if specified.
  if ($payment_method['settings']['log']['response'] == 'response') {
    watchdog('commerce_authnet', 'Authorize.Net AIM response: !param', array(
      '!param' => '<pre>' . check_plain(print_r($response, TRUE)) . '</pre>',
      WATCHDOG_DEBUG,
    ));
  }
  return $response;
}