You are here

function uc_credit_form_alter in Ubercart 5

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

Implementation of hook_form_alter().

File

payment/uc_credit/uc_credit.module, line 86
Defines the credit card payment method and hooks in payment gateways.

Code

function uc_credit_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'uc_payment_methods_form':
      if (empty($_POST) && uc_credit_encryption_key() === FALSE) {
        drupal_set_message(t('Credit card encryption must be configured to accept credit card payments.'), 'error');
      }
      $form['#validate']['uc_credit_settings_form_validate'] = array();
      $form['#submit']['uc_credit_settings_form_submit'] = array();
      break;
    case 'uc_cart_checkout_form':
      if (isset($_POST['cc_number'])) {
        $order = new stdClass();
        uc_payment_method_credit('cart-process', $order, TRUE);
      }

      // Cache the CC details for use in other functions.
      if (isset($_SESSION['sescrd'])) {
        uc_credit_cache('save', $_SESSION['sescrd']);

        // Store the encrypted details to the form for processing on submit.
        $form['payment_details_data'] = array(
          '#type' => 'hidden',
          '#value' => $_SESSION['sescrd'],
        );

        // Clear the session of the details.
        unset($_SESSION['sescrd']);
      }
      unset($_SESSION['cc_pay']);
      break;
    case 'uc_cart_checkout_review_form':

      // Check if the customer paid by CC and refreshed on the review page.
      if (isset($_SESSION['cc_pay']) && !isset($_SESSION['sescrd']) && empty($_POST['sescrd'])) {

        // Send them back to the checkout form to put in their details again.
        drupal_set_message(t('To protect our customers from identity theft, credit card details are erased when a browser refreshes on the checkout review page.  Please enter your card details again and re-submit the form.'), 'error');
        $_SESSION['clear_cc'] = TRUE;
        unset($_SESSION['cc_pay']);
        drupal_goto('cart/checkout');
      }

      // Cache the CC details for use in other functions.
      uc_credit_cache('save', $_SESSION['sescrd']);

      // Store the encrypted details to the form for processing on submit.
      $form['sescrd'] = array(
        '#type' => 'hidden',
        '#value' => $_SESSION['sescrd'],
      );
      $form['#submit'] = array(
        'uc_credit_cart_review_pre_form_submit' => array(),
      ) + $form['#submit'] + array(
        'uc_credit_cart_review_post_form_submit' => array(),
      );

      // Clear the session of the details.
      unset($_SESSION['sescrd']);
      break;
    case 'uc_payment_gateways_form':

      // Loop through each of the gateways on the form.
      foreach ((array) $form['gateways'] as $key => $value) {

        // Get the transaction types associated with this gateway.
        $gateway_types = uc_credit_gateway_txn_types($key);

        // Default to authorization plus capture if none are specified.
        if (empty($types) && !is_null(_payment_gateway_data($key, 'credit'))) {
          $types = array(
            UC_CREDIT_AUTH_CAPTURE,
          );
        }

        // Loop through all the available transaction types.
        $options = array();
        $txn_types = array(
          UC_CREDIT_AUTH_ONLY => t('Authorization only'),
          UC_CREDIT_AUTH_CAPTURE => t('Authorize and capture immediately'),
        );
        foreach ($txn_types as $type => $title) {

          // Add the current one to the options if the gateway supports it.
          if (in_array($type, $gateway_types)) {
            $options[$type] = $title;
          }
        }
        $form['gateways'][$key]['uc_pg_' . $key . '_cc_txn_type'] = array(
          '#type' => 'radios',
          '#title' => t('Default credit transaction type'),
          '#description' => t('Only available transaction types are listed. The default will be used unless an administrator chooses otherwise through the terminal.'),
          '#options' => $options,
          '#default_value' => variable_get('uc_pg_' . $key . '_cc_txn_type', UC_CREDIT_AUTH_CAPTURE),
          '#weight' => -5,
        );
      }
  }
}