You are here

function commerce_amex_cardonfile_update_form in Commerce American Express Payment Gateway (Amex) 7

Builds the form for updating cardonfile data.

Parameters

$card_data: The card on file entity.

File

./commerce_amex.module, line 1251
Implements American Express payment gateway for use in Drupal Commerce.

Code

function commerce_amex_cardonfile_update_form($form, &$form_state, $card_data) {

  // Load the credit card helper functions from the Payment module.
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
  $payment_method = commerce_payment_method_instance_load($card_data->instance_id);
  $session = _commerce_amex_request_session($payment_method);
  if ($session->result == "SUCCESS") {
    $session_id = $session->session;
    $form['#action'] = $payment_method['settings']['txn_url'] . '/form/' . $session_id;
    $form['#method'] = "post";
    $form['card_data'] = array(
      '#type' => 'value',
      '#value' => $card_data,
    );
    $default = array(
      'exp_month' => $card_data->card_exp_month,
      'exp_year' => $card_data->card_exp_year,
    );
    $current_year_2 = date('y');
    $current_year_4 = date('Y');
    $form['card_holder'] = array(
      '#type' => 'item',
      '#title' => t('Card holder'),
      '#markup' => $card_data->card_name,
    );
    $form['card_type'] = array(
      '#type' => 'item',
      '#title' => t('Card Type'),
      '#markup' => !empty($card_types[$card_data->card_type]) ? $card_types[$card_data->card_type] : $card_data->card_type,
    );
    $form['card_number'] = array(
      '#type' => 'item',
      '#title' => t('Card Number'),
      '#markup' => 'XXXX - XXXX - XXXX - ' . $card_data->card_number,
    );

    // Always add fields for the credit card expiration date.
    $form['gatewayCardExpiryDateMonth'] = array(
      '#type' => 'select',
      '#title' => t('Expiration'),
      '#options' => drupal_map_assoc(array_keys(commerce_months())),
      '#default_value' => strlen($default['exp_month']) == 1 ? '0' . $default['exp_month'] : $default['exp_month'],
      '#required' => TRUE,
      '#prefix' => '<div class="commerce-credit-card-expiration">',
      '#suffix' => '<span class="commerce-month-year-divider">/</span>',
      // Attache the CSS from commerce_payment module for nice formatting.
      '#attached' => array(
        'css' => array(
          drupal_get_path('module', 'commerce_payment') . '/theme/commerce_payment.theme.css',
        ),
      ),
    );

    // Build a year select list that uses a 4 digit key with a 2 digit value.
    $options = array();
    for ($i = 0; $i < 20; $i++) {
      $options[$current_year_4 + $i] = str_pad($current_year_2 + $i, 2, '0', STR_PAD_LEFT);
    }
    $form['gatewayCardExpiryDateYear'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $default['exp_year'],
      '#suffix' => '</div>',
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Update card data'),
      '#suffix' => l(t('Cancel'), 'user/' . $card_data->uid . '/cards'),
    );
  }
  else {
    $form['error'] = array(
      '#type' => 'markup',
      '#markup' => t('Unable to contact payment gateway'),
    );
  }
  return $form;
}