You are here

function commerce_stripe_cardonfile_update_form in Commerce Stripe 7.3

Card on file callback: Update form

1 string reference to 'commerce_stripe_cardonfile_update_form'
commerce_stripe_commerce_payment_method_info_alter in ./commerce_stripe.module
Implements hook_commerce_payment_method_info_alter().

File

./commerce_stripe.module, line 1300
This module provides Stripe (http://stripe.com/) payment gateway integration to Commerce. Commerce Stripe offers a PCI-compliant way to process payments straight from you Commerce shop.

Code

function commerce_stripe_cardonfile_update_form($form, &$form_state, $op, $card_data) {
  $account = user_load($form_state['build_info']['args'][1]->uid);

  // @todo: Check for a customer_id we can reuse from Stripe.
  $form['card_data'] = array(
    '#type' => 'value',
    '#value' => $card_data,
  );
  $form['op'] = array(
    '#type' => 'value',
    '#value' => $op,
  );
  $form['user'] = array(
    '#type' => 'value',
    '#value' => $account,
  );
  $form['errors'] = array(
    '#markup' => '<div id="card-errors"></div>',
  );

  // Add the active card's details so the user knows which one she's updating.
  $card['number'] = t('xxxxxx ' . $card_data->card_number);
  $card['owner'] = t($card_data->card_name);
  $card['exp_month'] = strlen($card_data->card_exp_month) == 1 ? '0' . $card_data->card_exp_month : $card_data->card_exp_month;
  $card['exp_year'] = $card_data->card_exp_year;

  // Because this is an update operation we implement the payment form directly.
  $fields = array(
    'owner' => $card['owner'],
    'code' => '',
    'exp_month' => $card['exp_month'],
    'exp_year' => $card['exp_year'],
  );

  // Because this is an Update form we need to implement the payment form directly.
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
  $form += commerce_payment_credit_card_form($fields, $card);

  // Disable the card number field.
  $form['credit_card']['number']['#disabled'] = TRUE;
  $payment_method = commerce_payment_method_instance_load($card_data->instance_id);
  $stored_cards = commerce_cardonfile_load_multiple_by_uid($account->uid, $payment_method['instance_id']);
  $valid_cards = array();

  // If have stored cards ...
  if (!empty($stored_cards)) {
    $valid_cards = array_filter($stored_cards, 'commerce_cardonfile_validate_card_expiration');
  }
  $card_count = count($valid_cards);
  $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' => empty($valid_cards) && $card_count == 1 ? TRUE : $card_data->instance_default,
    '#disabled' => empty($valid_cards) && $card_count == 1 ? TRUE : $card_data->instance_default,
  );
  $wrapper = entity_metadata_wrapper('commerce_cardonfile', $card_data);

  // Tweak the form to remove the fieldset from the address field if there
  // is only one on this profile.
  $addressfields = array();
  $langcode = LANGUAGE_NONE;
  foreach (commerce_info_fields('addressfield', 'commerce_customer_profile') as $field_name => $field) {
    if (!empty($form[$field_name]['#language'])) {
      $langcode = $form[$field_name]['#language'];

      // Only consider this addressfield if it's represented on the form.
      if (!empty($form[$field_name][$langcode])) {
        $addressfields[] = array(
          $field_name,
          $langcode,
        );
      }
    }
  }

  // Load the billing profile associated with this card and populate the address form.
  if ($wrapper
    ->__isset('commerce_cardonfile_profile') && $wrapper->commerce_cardonfile_profile
    ->value()) {
    $profile = $wrapper->commerce_cardonfile_profile
      ->value();
  }

  // If the returned wrapper value is actually empty, create a new one.
  if (empty($profile)) {
    $profile = commerce_customer_profile_new('billing', $account->uid);

    // Prepare an addressfield array to set to the customer profile.
    $field = field_info_field('commerce_customer_address');
    $instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', 'billing');
    $address = addressfield_default_values($field, $instance);
    $profile->commerce_customer_address[$langcode][] = $address;
  }

  // Add the entity context of the current cart order.
  $profile->entity_context = array(
    'entity_type' => 'commerce_cardonfile',
    'entity_id' => $card_data->card_id,
  );
  $form['commerce_customer_profile'] = array(
    '#type' => 'value',
    '#value' => $profile,
  );

  // Add the field widgets for the profile.
  field_attach_form('commerce_customer_profile', $profile, $form, $form_state);

  // Add a validation callback so that we can call field_attach functions.
  $form['#validate'][] = 'commerce_stripe_cardonfile_update_validate';

  // Check to ensure only one addressfield was found on the form.
  if (count($addressfields) == 1) {
    list($field_name, $langcode) = array_shift($addressfields);
    foreach (element_children($form['address'][$field_name][$langcode]) as $delta) {

      // Don't mess with the "Add another item" button that could be present.
      if ($form[$field_name][$langcode][$delta]['#type'] != 'submit') {
        $form[$field_name][$langcode][$delta]['#type'] = 'container';
      }
    }
  }
  commerce_stripe_set_addressfield_class_names($form[$field_name][$langcode][0]);
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update card'),
  );
  return $form;
}