You are here

function _uc_cybersource_post_charge in Ubercart 6.2

Same name and namespace in other branches
  1. 5 payment/uc_cybersource/uc_cybersource.module \_uc_cybersource_post_charge()
  2. 7.3 payment/uc_cybersource/uc_cybersource.module \_uc_cybersource_post_charge()
1 call to _uc_cybersource_post_charge()
uc_cybersource_charge in payment/uc_cybersource/uc_cybersource.module

File

payment/uc_cybersource/uc_cybersource.module, line 597
A module used for CyberSource's Silent Order POST and Hosted Order Page methods of payment.

Code

function _uc_cybersource_post_charge($order, $amount, $data, $cc_type, $country) {

  // Include the HOP.php per the module instructions.
  if (!uc_cybersource_hop_include()) {
    drupal_set_message(t('Silent Order POST requires the HOP.php provided by CyberSource.'));
    return array(
      'success' => FALSE,
    );
  }
  $request = array(
    'billTo_firstName' => $order->billing_first_name,
    'billTo_lastName' => $order->billing_last_name,
    'billTo_street1' => $order->billing_street1,
    'billTo_city' => $order->billing_city,
    'billTo_country' => $country[0]['country_iso_code_2'],
    'billTo_state' => uc_get_zone_code($order->billing_zone),
    'billTo_postalCode' => $order->billing_postal_code,
    'billTo_email' => $order->primary_email,
    'card_accountNumber' => $order->payment_details['cc_number'],
    'card_cardType' => $cc_type,
    'card_expirationMonth' => $order->payment_details['cc_exp_month'],
    'card_expirationYear' => $order->payment_details['cc_exp_year'],
  );
  if (variable_get('uc_credit_cvv_enabled', TRUE)) {
    $request['card_cvNumber'] = $order->payment_details['cc_cvv'];
  }
  $currency = variable_get('uc_cybersource_currency', 'usd');
  $merchantID = getMerchantID();
  $timestamp = getmicrotime();
  $data = $merchantID . $amount . $currency . $timestamp;
  $pub = function_exists('getSharedSecret') ? getSharedSecret() : getPublicKey();
  $serialNumber = getSerialNumber();
  $pub_digest = hopHash($data, $pub);
  $request['amount'] = $amount;
  $request['currency'] = $currency;
  $request['merchantID'] = $merchantID;
  $request['orderNumber'] = $order->order_id;
  $request['orderPage_timestamp'] = $timestamp;
  $request['orderPage_ignoreAVS'] = variable_get('uc_cybersource_avs', 'true') == 'true' ? 'false' : 'true';
  $request['orderPage_signaturePublic'] = $pub_digest;
  $request['orderPage_version'] = '4';
  $request['orderPage_serialNumber'] = $serialNumber;
  $request['orderPage_transactionType'] = variable_get('uc_cybersource_hop_transaction_type', 'sale');
  $data = '';
  while (list($key, $value) = each($request)) {
    $data .= $key . '=' . urlencode(str_replace(',', '', $value)) . '&';
  }
  $data = substr($data, 0, -1);
  if (variable_get('uc_cybersource_server', 'test') == 'test') {
    $url = 'https://orderpagetest.ic3.com/hop/ProcessOrder.do';
  }
  else {
    $url = 'https://orderpage.ic3.com/hop/ProcessOrder.do';
  }
  $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, $data);
  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);
  $response = curl_exec($ch);
  if ($error = curl_error($ch)) {
    watchdog('uc_cybersource', '@error', array(
      '@error' => $error,
    ), WATCHDOG_ERROR);
  }
  curl_close($ch);
  if (preg_match_all('`name=".+" value=".+"`', $response, $pairs) > 0) {
    for ($i = 0; $i < count($pairs[0]); $i++) {
      list($name, $value) = explode('" value="', substr($pairs[0][$i], 6, strlen($pairs[0][$i]) - 7));
      $nvp[$name] = $value;
    }

    // Create the order and payment ledger comments.
    $context = array(
      'revision' => 'formatted-original',
      'type' => 'amount',
    );
    $o_comment = t('<b>Credit card !type:</b> !amount<br /><b>Decision: @decision</b><br /><b>Reason:</b> !reason', array(
      '!type' => variable_get('uc_cybersource_hop_transaction_type', 'sale'),
      '!amount' => uc_price($nvp['orderAmount'], $context),
      '@decision' => $nvp['decision'],
      '!reason' => _parse_cs_reason_code($nvp['reasonCode']),
    ));
    $p_comment = t('!id<br />!decision, Reason: !reason', array(
      '!id' => $nvp['orderPage_serialNumber'],
      '!decision' => $nvp['decision'],
      '!reason' => $nvp['reasonCode'],
    ));
    if (!empty($nvp['ccAuthReply_avsCode'])) {
      $o_comment .= t('<br /><b>AVS:</b> !avs', array(
        '!avs' => _parse_cs_avs_code($nvp['ccAuthReply_avsCode']),
      ));
      $p_comment .= t(', AVS: @avs', array(
        '@avs' => $nvp['ccAuthReply_avsCode'],
      ));
    }
    if (!empty($nvp['ccAuthReply_cvCode'])) {
      $o_comment .= t('<br /><b>CVV:</b> !cvv', array(
        '!cvv' => _parse_cs_cvv_code($nvp['ccAuthReply_cvCode']),
      ));
      $p_comment .= t(', CVV: @cvv', array(
        '@cvv' => $nvp['ccAuthReply_cvCode'],
      ));
    }
    uc_order_comment_save($order->order_id, $user->uid, $o_comment, 'admin');
    if ($nvp['decision'] == 'ACCEPT') {
      $result = array(
        'success' => TRUE,
        'comment' => $p_comment,
        'message' => $o_comment,
        'uid' => $user->uid,
      );
    }
    else {
      $result = array(
        'success' => FALSE,
        'comment' => $p_comment,
        'message' => $o_comment,
        'uid' => $user->uid,
      );
    }
  }
  else {
    $result = array(
      'success' => FALSE,
      'message' => t('No response returned from CyberSource.'),
    );
  }
  return $result;
}