You are here

function commerce_cardonfile_options_list in Commerce Card on File 7

Returns an options array for selecting a card on file or choosing to use a different credit card.

Parameters

$stored_cards: An array of stored card data arrays keyed by card_id.

$element: The form element the options array will be for, 'radios' or 'select'.

$different: Add an option to use a different credit card.

Return value

An options array for selecting a card on file.

1 call to commerce_cardonfile_options_list()
commerce_cardonfile_form_alter in ./commerce_cardonfile.module
Implements hook_form_alter().

File

./commerce_cardonfile.module, line 306
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_options_list($stored_cards, $element = 'radios', $different = TRUE) {

  // Load the credit card helper functions from the Payment module.
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
  $card_types = commerce_payment_credit_card_types();

  // Build an options array of stored credit cards.
  $options = array();
  foreach ($stored_cards as $card_id => $card_data) {
    $replacement = array(
      '@name' => $card_data['card_name'],
      '@number' => $card_data['card_number'],
      '@month' => str_pad($card_data['card_exp_month'], 2, '0', STR_PAD_LEFT),
      '@year' => $card_data['card_exp_year'],
    );
    if (!empty($card_types[$card_data['card_type']])) {
      $replacement['@type'] = $card_types[$card_data['card_type']];
    }
    else {
      $replacement['@type'] = 'Card';
    }

    // Use a longer format for radio button options.
    if ($element == 'radios') {
      $label = t('@type belonging to @name: Ends in @number, Expires @month/@year', $replacement);
    }
    else {
      $label = t('@type ending in @number, Exp. @month/@year', $replacement);
    }
    $options[$card_id] = $label;
  }

  // Add an option to use a different credit card if specified.
  if ($different) {
    $options['new'] = t('Use a different credit card');
  }
  return $options;
}