You are here

function stripe_element_process in Stripe 7

Process callback for this module elements.

TODO: Attach JS through a library defined via hook_library() implementation. TODO: Support form WITHOUT Stripe.js. This would require an configuration variable to be set to TRUE. This will have serious security implications. No UI should be provided to enable this, requiring either a clear and informed user action or usage of third party code.

Parameters

array $element: The processed element.

array $form_state: The form state.

array $complete_form: The complete form.

1 string reference to 'stripe_element_process'
stripe_element_info in ./stripe.module
Implements hook_element_info().

File

./stripe.module, line 115
stripe.module Drupal hooks used for integrating the Stripe service.

Code

function stripe_element_process($element, $form_state, $complete_form) {
  $live = variable_get('stripe_key_status', 'test') === 'live';
  $element['#type'] = 'fieldset';
  $element += element_info('fieldset');
  $element['#tree'] = TRUE;
  $element['stripe_token'] = array(
    '#type' => 'hidden',
    '#element_validate' => array(
      'stripe_token_element_validate',
    ),
    '#attributes' => array(
      'data-stripe' => 'token',
    ),
  );
  if ($element['#cardholder_name']) {
    if (is_bool($element['#cardholder_name']) && $element['#cardholder_name'] == TRUE) {

      // Embed our own cardholder name element.
      $element['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name on card'),
        '#description' => t('Name of the cardholder as displayed on the card.'),
        '#attributes' => array(
          'data-stripe' => 'name',
          'autocomplete' => 'cc-name',
          'placeholder' => variable_get('stripe_subscription_placeholder_name', 'John Doe'),
        ),
        '#element_validate' => array(
          'stripe_element_validate_empty',
        ),
        '#pre_render' => array(
          'stripe_element_remove_name',
        ),
      );
    }
    elseif (is_array($element['#cardholder_name'])) {

      // Use existing cardholder name element.
      $cardholder_name_element =& drupal_array_get_nested_value($form, $element['#cardholder_name']);
      if ($cardholder_name_element) {
        $cardholder_name_element['#attributes']['data-stripe'] = 'name';
        $cardholder_name_element['#pre_render'][] = 'stripe_element_remove_name';
      }
    }
  }
  if (!empty($element['#address'])) {
    require_once 'includes/locale.inc';
    if (is_numeric($element['#address']) && $element['#address'] == TRUE) {
      if ($element['#address_option'] == 1) {
        $element['address'] = array(
          '#type' => 'textfield',
          '#title' => t('Billing Street Address'),
          '#attributes' => array(
            'data-stripe' => 'address_line1',
          ),
          //          '#default_value' => !empty($element['#card_data']->address_line1) ? $element['#card_data']->address_line1 : '',
          '#pre_render' => array(
            'stripe_element_remove_name',
          ),
          '#description' => t('Street address associated with the credit card.'),
        );
      }
      else {

        // Embed our own billing address elements.
        $element['address'] = array(
          '#type' => 'fieldset',
          'thoroughfare' => array(
            '#type' => 'textfield',
            '#title' => t('Street Address Line 1'),
            '#attributes' => array(
              'data-stripe' => 'address_line1',
            ),
            //            '#default_value' => !empty($element['#card_data']->address_line1) ? $element['#card_data']->address_line1 : '',
            '#pre_render' => array(
              'stripe_element_remove_name',
            ),
          ),
          'premise' => array(
            '#type' => 'textfield',
            '#title' => t('Street Address Line 2'),
            '#attributes' => array(
              'data-stripe' => 'address_line2',
            ),
            '#pre_render' => array(
              'stripe_element_remove_name',
            ),
          ),
          'locality' => array(
            '#type' => 'textfield',
            '#title' => t('City'),
            '#attributes' => array(
              'data-stripe' => 'address_city',
            ),
            '#pre_render' => array(
              'stripe_element_remove_name',
            ),
          ),
          'administrative_area' => array(
            '#type' => 'textfield',
            '#title' => t('State/Province'),
            '#attributes' => array(
              'data-stripe' => 'address_state',
            ),
            '#pre_render' => array(
              'stripe_element_remove_name',
            ),
          ),
          'postal_code' => array(
            '#type' => 'textfield',
            '#title' => t('Postal code'),
            '#attributes' => array(
              'data-stripe' => 'address_zip',
            ),
            '#pre_render' => array(
              'stripe_element_remove_name',
            ),
          ),
          'country' => array(
            '#type' => 'select',
            '#title' => t('Country'),
            '#attributes' => array(
              'data-stripe' => 'address_country',
            ),
            '#options' => country_get_list(),
            '#default_value' => variable_get('stripe_default_country', 'US'),
            '#pre_render' => array(
              'stripe_element_remove_name',
            ),
          ),
        );
      }
    }
    elseif (is_array($element['#address'])) {

      // Use existing billing address elements.
      //      $address_element = &drupal_array_get_nested_value($form, $element['#address']);
      //      if ($address_element) {
      //        $sub_elements = array(
      //          'thoroughfare' => 'address_line1',
      //          'premise' => 'address_line2',
      //          'locality' => 'address_city',
      //          'administrative_area' => 'address_state',
      //          'postal_code' => 'address_zip',
      //          'country' => 'address_country',
      //        );
      //        foreach ($sub_elements as $name => $data_stripe) {
      //          if (isset($address_element[$name])) {
      //            $address_element[$name]['#attributes']['data-stripe'] = $data_stripe;
      //            $address_element[$name]['#element_validate'][] = 'stripe_element_validate_empty';
      //            $address_element[$name]['#pre_render'][] = 'stripe_element_remove_name';
      //          }
      //        }
      //      }
    }
  }

  //  // Coupon form.
  //  if ($element['#coupon'] == TRUE && $element['#new_subscription'] == TRUE) {
  //    $element['coupon'] = array(
  //      '#type' => 'textfield',
  //      '#title' => t('Coupon Code'),
  //      '#size' => 20,
  //      '#description' => t('If you have a coupon code, enter it here.'),
  //    );
  //  }
  $element['card'] = array(
    '#type' => 'item',
    '#markup' => '<label for="card-element">Card number</label><div autocomplete="cc-number" data-stripe="number" id="card-element"></div>',
  );
  $element['card_errors'] = array(
    '#type' => 'item',
    '#markup' => '<div data-stripe-errors="number" class="stripe-errors form-text" id="card-errors"></div>',
  );

  // Attach our javascript.
  $element['#attached']['js'][] = drupal_get_path('module', 'stripe') . '/stripe.js';

  // Attach the publishable key to the form element.
  $element['#attributes']['data-stripe-key'] = !empty($element['#publishable_key']) ? $element['#publishable_key'] : variable_get('stripe_publishable', '');

  // Set publishable key in Drupal.settings.stripe.publicKey.
  drupal_add_js(array(
    'stripe' => array(
      'publicKey' => variable_get('stripe_publishable', ''),
    ),
  ), 'setting');
  return $element;
}