You are here

function commerce_cardonfile_card_form in Commerce Card on File 7.2

Form callback: create or edit a card.

1 string reference to 'commerce_cardonfile_card_form'
commerce_cardonfile_card_form_page in includes/commerce_cardonfile.pages.inc
Returns the appropriate card form.

File

includes/commerce_cardonfile.pages.inc, line 95
User page callbacks and forms for Commerce Card on File.

Code

function commerce_cardonfile_card_form($form, &$form_state, $op, $card) {

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

  // Pass along information to the validate and submit handlers.
  $form_state['card'] = $card;
  $form_state['op'] = $op;
  $fields = array(
    'owner' => '',
    'code' => '',
    'type' => array(
      'visa',
      'mastercard',
      'amex',
    ),
  );
  $defaults = array();
  if ($op == 'update') {
    unset($fields['code']);
    $defaults = array(
      'type' => $card->card_type,
      'owner' => $card->card_name,
      'exp_month' => $card->card_exp_month,
      'exp_year' => $card->card_exp_year,
    );
  }
  $form += commerce_payment_credit_card_form($fields, $defaults);
  if ($op == 'update') {
    $form['credit_card']['type']['#access'] = FALSE;
    $form['credit_card']['number']['#access'] = FALSE;
  }
  $payment_method = commerce_payment_method_load($card->payment_method);
  $form['credit_card']['cardonfile_instance_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use as default card for payments with %method', array(
      '%method' => $payment_method['display_title'],
    )),
    '#default_value' => FALSE,
  );

  // Disable the checkbox if we are adding a new card and the user doesn't have
  // any other active cards with the same instance ID. Also disable it, if we
  // are updating the current default card, so the user can't uncheck the
  // checkbox.
  $existing_cards = commerce_cardonfile_load_multiple_by_uid($card->uid, $card->instance_id, TRUE);
  if ($op == 'create' && !$existing_cards || $op == 'update' && $card->instance_default) {
    $form['credit_card']['cardonfile_instance_default']['#default_value'] = TRUE;
    $form['credit_card']['cardonfile_instance_default']['#access'] = FALSE;
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $op == 'create' ? t('Add card') : t('Update card'),
    '#suffix' => l(t('Cancel'), 'user/' . $card->uid . '/cards'),
  );
  return $form;
}