You are here

function uc_cybersource_hop_form in Ubercart 7.3

Same name and namespace in other branches
  1. 6.2 payment/uc_cybersource/uc_cybersource.module \uc_cybersource_hop_form()

Defines values to be posted to CyberSource.

Return value

Transaction data arrays are returned as hidden form values.

1 string reference to 'uc_cybersource_hop_form'
uc_cybersource_page_alter in payment/uc_cybersource/uc_cybersource.module
Implements hook_page_alter().

File

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

Code

function uc_cybersource_hop_form($form, $form_state, $order) {
  if (!uc_cybersource_hop_include()) {
    drupal_set_message(t('Hosted Order Page requires the HOP.php provided by CyberSource.'));

    // TODO - Does returning false here make sense?
    return array(
      'success' => FALSE,
    );
  }
  $billing_country = uc_get_country_data(array(
    'country_id' => $order->billing_country,
  ));
  $delivery_country = uc_get_country_data(array(
    'country_id' => $order->delivery_country,
  ));
  $data = 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' => $billing_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,
    'billTo_phoneNumber' => $order->billing_phone,
  );
  if (uc_order_is_shippable($order)) {
    $data += array(
      'shipTo_firstName' => $order->delivery_first_name,
      'shipTo_lastName' => $order->delivery_last_name,
      'shipTo_street1' => $order->delivery_street1,
      'shipTo_street2' => $order->delivery_street2,
      'shipTo_city' => $order->delivery_city,
      'shipTo_country' => $delivery_country[0]['country_iso_code_2'],
      'shipTo_state' => uc_get_zone_code($order->delivery_zone),
      'shipTo_postalCode' => $order->delivery_postal_code,
    );
  }
  $shipping = 0;
  foreach ($order->line_items as $item) {
    if ($item['type'] == 'shipping') {
      $shipping += $item['amount'];
    }
  }
  $tax = 0;
  if (module_exists('uc_taxes')) {
    foreach (uc_taxes_calculate($order) as $tax_item) {
      $tax += $tax_item->amount;
    }
  }
  $amount = $order->order_total - $shipping - $tax;
  $currency = variable_get('uc_cybersource_hop_currency', 'USD');
  $merchantID = getMerchantID();
  $timestamp = getmicrotime();
  $datax = $merchantID . $amount . $currency . $timestamp;
  $pub = function_exists('getSharedSecret') ? getSharedSecret() : getPublicKey();
  $serialNumber = getSerialNumber();
  $pub_digest = hopHash($datax, $pub);
  $data['amount'] = $amount;
  $data['currency'] = $currency;
  $data['merchantID'] = $merchantID;
  $data['orderNumber'] = $order->order_id;
  $data['orderPage_timestamp'] = $timestamp;
  $data['orderPage_ignoreAVS'] = variable_get('uc_cybersource_hop_avs', 'true') == 'true' ? 'false' : 'true';
  $data['orderPage_signaturePublic'] = $pub_digest;
  $data['orderPage_version'] = '4';
  $data['orderPage_serialNumber'] = $serialNumber;
  $data['orderPage_transactionType'] = variable_get('uc_cybersource_hop_transaction_type', 'sale');
  $data['orderPage_sendMerchantReceiptEmail'] = variable_get('uc_cybersource_hop_merchant_receipt_email', 'true');
  $data['orderPage_sendMerchantURLPost'] = 'true';

  // CyberSource posts payment confirmation to this URL.
  $data['orderPage_merchantURLPostAddress'] = url('cybersource/hop-post', array(
    'absolute' => TRUE,
  ));
  $data['orderPage_buyButtonText'] = t('Checkout');
  $receipt_url = url('cybersource/hop-complete/' . $order->order_id, array(
    'absolute' => TRUE,
  ));
  $data['orderPage_receiptResponseURL'] = $receipt_url;
  $data['orderPage_buyButtonText'] = variable_get('uc_cybersource_cs_hop_button_text', t('Process payment'));
  $comments = t('Order @order-id at @store-name', array(
    '@order-id' => $order->order_id,
    '@store-name' => uc_store_name(),
  ));
  $alter_data['order'] = $order;
  $alter_data['comments'] = $comments;
  $alter_data['merchant_fields'] = array();

  // Allow other modules to alter the comment & merchant field data stored
  // with CyberSource.
  drupal_alter('uc_cybersource_data', $alter_data);
  $data['comments'] = $alter_data['comments'];
  if (!empty($alter_data['merchant_fields'])) {
    foreach ($alter_data['merchant_fields'] as $key => $value) {
      $data[$key] = $value;
    }
  }
  foreach ($data as $name => $value) {
    if (!empty($value)) {
      $form[$name] = array(
        '#type' => 'hidden',
        '#value' => $value,
      );
    }
  }
  $form['#action'] = variable_get('uc_cybersource_hop_server', 'https://orderpagetest.ic3.com/hop/orderform.jsp');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit order'),
  );
  return $form;
}