You are here

function commerce_cardonfile_form_alter in Commerce Card on File 7

Same name and namespace in other branches
  1. 7.2 commerce_cardonfile.module \commerce_cardonfile_form_alter()

Implements hook_form_alter().

This implementation alters any checkout form looking for the payment pane and seeing if its details are currently for a credit card payment method. If so, it adds the necessary form elements for Card on File payment, including a select element to use previously stored credit card information and a checkbox on the credit card data entry form to store the given credit card on file for future usage.

File

./commerce_cardonfile.module, line 212
Supports card on file functionality for credit card payment methods by associating card data reference IDs from payment gateways with user accounts.

Code

function commerce_cardonfile_form_alter(&$form, &$form_state, $form_id) {

  // If the current form ID is for a checkout form...
  if (strpos($form_id, 'commerce_checkout_form_') === 0) {

    // And it specifies a valid checkout page...
    if (commerce_checkout_page_load(substr($form_id, 23))) {

      // And the current page's form includes the payment checkout pane...
      if (!empty($form['commerce_payment'])) {

        // Check to see if the currently selected payment method is Card on File
        // enabled (via the cardonfile boolean in its info array).
        $payment_method = commerce_payment_method_instance_load($form['commerce_payment']['payment_method']['#default_value']);
        if (!empty($payment_method['cardonfile']) && !empty($form['commerce_payment']['payment_details']['credit_card'])) {

          // Add a checkbox to the credit card details container to store the
          // credit card for future use.
          $storage = variable_get('commerce_cardonfile_storage', 'opt-in');
          if (in_array($storage, array(
            'opt-in',
            'opt-out',
          ))) {
            $form['commerce_payment']['payment_details']['credit_card']['cardonfile_store'] = array(
              '#type' => 'checkbox',
              '#title' => t('Store this credit card on file for future use.'),
              '#default_value' => $storage == 'opt-out',
            );
          }
          else {
            $form['commerce_payment']['payment_details']['credit_card']['cardonfile_store'] = array(
              '#type' => 'value',
              '#value' => TRUE,
            );
          }
          if (!user_is_anonymous()) {

            // Load any existing card data for the current payment method instance
            // and user.
            $stored_cards = commerce_cardonfile_data_load_multiple($form_state['account']->uid, $payment_method['instance_id']);

            // Filter out expired cards.
            foreach ($stored_cards as $card_id => $card_data) {
              if ($card_data['card_exp_year'] < date('Y') || $card_data['card_exp_year'] == date('Y') && $card_data['card_exp_month'] < date('m')) {
                unset($stored_cards[$card_id]);
              }
            }

            // If we found any stored cards, show the options in the form.
            if (!empty($stored_cards)) {
              $element = variable_get('commerce_cardonfile_selector', 'radios');
              $options = commerce_cardonfile_options_list($stored_cards, $element);
              $form['commerce_payment']['payment_details']['cardonfile'] = array(
                '#type' => $element,
                '#title' => t('Select a stored credit card'),
                '#options' => $options,
                '#default_value' => key($options),
                '#weight' => -10,
                '#ajax' => array(
                  'callback' => 'commerce_payment_pane_checkout_form_details_refresh',
                  'wrapper' => 'payment-details',
                ),
              );

              // If the current value for the card selection element is not to use
              // a different credit card, then hide the credit card form elements.
              if (empty($form_state['values']) || $form_state['values']['commerce_payment']['payment_details']['cardonfile'] !== 'new') {
                $form['commerce_payment']['payment_details']['credit_card']['#access'] = FALSE;
              }

              // Add the CSS to hide a sole credit card icon if specified.
              if (variable_get('commerce_cardonfile_hide_cc_radio_button', TRUE)) {
                if (count($form['commerce_payment']['payment_method']['#options']) == 1) {
                  $form['commerce_payment']['payment_method']['#attached']['css'][] = drupal_get_path('module', 'commerce_cardonfile') . '/theme/commerce_cardonfile.checkout.css';
                }
              }
            }
          }
        }
      }
    }
  }
}