You are here

function commerce_square_payment_method_settings_form in Commerce Square Connect 7

Payment method form callback.

File

./commerce_square.module, line 102
Module file for Commerce Square.

Code

function commerce_square_payment_method_settings_form($settings) {
  $square_settings = variable_get('commerce_square_settings', commerce_square_default_settings()) + commerce_square_default_settings();
  $settings = $settings + commerce_square_payment_method_default_settings();
  libraries_load('square');
  $form['mode'] = array(
    '#type' => 'radios',
    '#title' => t('Mode'),
    '#options' => _commerce_square_get_supported_modes(),
    '#default_value' => empty($settings['mode']) ? 'test' : $settings['mode'],
    '#required' => TRUE,
  );
  $form['type'] = array(
    '#type' => 'radios',
    '#title' => t('Default credit card transaction type'),
    '#description' => t('The default will be used to process transactions during checkout.'),
    '#options' => array(
      COMMERCE_CREDIT_AUTH_CAPTURE => t('Authorization and capture'),
      COMMERCE_CREDIT_AUTH_ONLY => t('Authorization only (requires manual or automated capture after checkout)'),
    ),
    '#default_value' => $settings['type'],
  );
  foreach (_commerce_square_get_supported_modes() as $mode => $name) {
    $form[$mode . '_location_id'] = array(
      '#type' => 'select',
      '#title' => t('@mode Location', array(
        '@mode' => $name,
      )),
      '#description' => t('The location for the transactions.'),
      '#default_value' => $settings[$mode . '_location_id'],
      '#required' => TRUE,
    );
    $access_token = $square_settings[$mode . '_access_token'];
    if (!empty($access_token)) {
      $square_api = new SquareApi($access_token, $mode);
      $location_api = new LocationsApi($square_api
        ->getClient());
      try {
        $locations = $location_api
          ->listLocations();
        $location_options = $locations
          ->getLocations();
        $options = array();
        foreach ($location_options as $location_option) {
          $options[$location_option
            ->getId()] = $location_option
            ->getName();
        }
        $form[$mode . '_location_id']['#options'] = $options;
      } catch (\Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'error');
      }
    }
    else {
      $form[$mode][$mode . '_location_id']['#disabled'] = TRUE;
      $form[$mode][$mode . '_location_id']['#options'] = array(
        '_none' => 'Not configured',
      );
    }
  }
  if (module_exists('commerce_cardonfile')) {
    $form['cardonfile'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable Card on File functionality with this payment method.'),
      '#default_value' => $settings['cardonfile'],
    );
  }
  return $form;
}