You are here

function commerce_stripe_cardonfile_create_form in Commerce Stripe 7.3

Same name and namespace in other branches
  1. 7 commerce_stripe.module \commerce_stripe_cardonfile_create_form()
  2. 7.2 commerce_stripe.module \commerce_stripe_cardonfile_create_form()

Card on file callback: create form

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

File

./commerce_stripe.module, line 1054
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_create_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.
  // Pass along information to the validate and submit handlers.
  $form['card_data'] = array(
    '#type' => 'value',
    '#value' => $card_data,
  );
  $form['op'] = array(
    '#type' => 'value',
    '#value' => $op,
  );
  $form['user'] = array(
    '#type' => 'value',
    '#value' => $account,
  );
  $stripe_integration = _commerce_stripe_load_setting('integration_type', COMMERCE_STRIPE_DEFAULT_INTEGRATION);
  if ($stripe_integration === 'stripejs') {
    _commerce_stripe_form_add_credit_card_form($form);
    drupal_add_js('https://js.stripe.com/v2/', 'external');
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Add card'),
    );
  }
  elseif ($stripe_integration === 'elements') {
    $form += _commerce_stripe_elements_form();
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Add card'),
    );
  }
  elseif ($stripe_integration === 'checkout') {

    // Stub this out so that making this the default card doesn't break.
    $form['credit_card'] = array(
      '#tree' => TRUE,
    );
    $order_total = array(
      'amount' => 0,
      'currency_code' => commerce_default_currency(),
    );
    $checkout_currency = _commerce_stripe_load_setting('stripe_currency');
    $email = $account->mail;

    // Add Checkout settings.
    $checkout_settings = _commerce_stripe_load_setting('checkout_settings');

    // Change the label to Save, since we are not charging any money.
    $checkout_settings['panelLabel'] = t('Save card');
    _commerce_stripe_form_configure_stripe_checkout($checkout_settings, $checkout_currency, $email, $order_total);
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Enter card details'),
      '#description' => t('Please enter your details in the pop-up that opens. They are sent directly to the payment processor and do not pass through this website.'),
    );
  }
  $public_key = _commerce_stripe_load_setting('public_key');
  $stripe_token = !empty($form_state['values']['stripe_token']) ? $form_state['values']['stripe_token'] : '';
  _commerce_stripe_form_configure_stripe_common($form, $stripe_token, $stripe_integration);
  $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']);
  $set_new_card_default = variable_get('commerce_cardonfile_set_new_card_default', 0);
  $valid_cards = array();
  if (!empty($stored_cards)) {
    $valid_cards = array_filter($stored_cards, 'commerce_cardonfile_validate_card_expiration');
  }
  $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) || $set_new_card_default ? TRUE : FALSE,
    '#disabled' => empty($valid_cards) || $set_new_card_default ? TRUE : FALSE,
  );

  // Create a billing profile object and add the address form.
  $profile = commerce_customer_profile_new('billing', $account->uid);

  // 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_create_validate';

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

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

  // 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['address'][$field_name][$langcode][$delta]['#type'] != 'submit') {
        $form['address'][$field_name][$langcode][$delta]['#type'] = 'container';
      }
    }
  }
  commerce_stripe_set_addressfield_class_names($form['address']);
  $form['errors'] = array(
    '#markup' => '<div id="card-errors"></div>',
  );
  return $form;
}