You are here

function uc_stripe_settings_form_validate in Ubercart Stripe 7.3

Same name and namespace in other branches
  1. 6.2 uc_stripe.module \uc_stripe_settings_form_validate()
  2. 7.2 uc_stripe.module \uc_stripe_settings_form_validate()

Validation function and normalize keys (trim spaces)

_state

Parameters

$form:

1 string reference to 'uc_stripe_settings_form_validate'
uc_stripe_form_uc_payment_method_settings_form_alter in ./uc_stripe.module
Implements hook_form_FORMID_alter()

File

./uc_stripe.module, line 532
A stripe.js PCI-compliant payment gateway Forked from Bitcookie's work (thanks!) which was posted at http://bitcookie.com/blog/pci-compliant-ubercart-and-stripe-js from discussion in the uc_stripe issue queue, https://www.drupal.org/node/1467886

Code

function uc_stripe_settings_form_validate($form, &$form_state) {
  $elements = array(
    'uc_stripe_api_key_test_secret',
    'uc_stripe_api_key_test_publishable',
    'uc_stripe_api_key_live_secret',
    'uc_stripe_api_key_live_publishable',
  );
  if ($form_state['values']['uc_pg_uc_stripe_enabled']) {
    foreach ($elements as $element_name) {
      $form_state['values'][$element_name] = _uc_stripe_sanitize_key($form_state['values'][$element_name]);
      if (!_uc_stripe_validate_key($form_state['values'][$element_name])) {
        form_set_error($element_name, t('@name does not appear to be a valid stripe key', array(
          '@name' => $element_name,
        )));
      }
    }
  }

  // Make sure they haven't tried to validate credit card numbers, as uc_stripe will not provide a real one.
  if (!empty($form_state['values']['uc_credit_validate_numbers'])) {
    form_set_error('uc_credit_validate_numbers', t('When used with Ubercart Stripe, "Validate credit card number at checkout" must be unchecked.'));
  }

  // Validate advanced or basic styling.
  if ($form_state['values']['uc_stripe_element_styles_mode']) {

    // Advanced - Validate JSON-encoded style settings.
    $element_styles = $form_state['values']['uc_stripe_element_styles_json'];
    if (empty($element_styles)) {
      form_set_error('uc_stripe_element_styles_json', t('Empty Style Settings for Stripe Card Element (Advanced) is not a valid JSON. Set to {} or switch off Advanced styling.'));
    }
    else {
      json_decode($element_styles);
      if (json_last_error() !== JSON_ERROR_NONE) {
        form_set_error('uc_stripe_element_styles_json', t('Style Settings for Stripe Card Element (Advanced) JSON does not validate:') . ' ' . json_last_error_msg());
      }
    }
  }
  else {

    // Basic - Make sure that stripe style settings do not have an extra comma at the end.
    $element_styles = $form_state['values']['uc_stripe_element_styles'];
    if (!empty($element_styles)) {
      if (substr($element_styles, -1) == ',') {
        form_set_error('uc_stripe_element_styles', t('Stripe Element Styles list should not end with a comma.'));
      }
    }
  }
}