You are here

public function ConfigSettingsForm::validateForm in Drupal Commerce Connector for AvaTax 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/ConfigSettingsForm.php, line 255

Class

ConfigSettingsForm
Configuration form for AvaTax settings.

Namespace

Drupal\commerce_avatax\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $values = $form_state
    ->getValues();
  try {
    $client_factory = \Drupal::service('commerce_avatax.client_factory');
    $client = $client_factory
      ->createInstance($values);
    $ping_request = $client
      ->get('/api/v2/utilities/ping', [
      'headers' => [
        'Authorization' => 'Basic ' . base64_encode($values['account_id'] . ':' . $values['license_key']),
      ],
    ]);
    $ping_request = Json::decode($ping_request
      ->getBody()
      ->getContents());
    if (!empty($ping_request['authenticated']) && $ping_request['authenticated'] === TRUE) {
      $this->messenger
        ->addMessage($this
        ->t('AvaTax response confirmed using the account and license key above.'));
    }
    else {
      $form_state
        ->setError($form['configuration']['account_id'], $this
        ->t('Could not confirm the provided credentials.'));
      $form_state
        ->setError($form['configuration']['license_key'], $this
        ->t('Could not confirm the provided credentials.'));
    }
  } catch (\Exception $e) {
    $form_state
      ->setError($form['configuration'], $e
      ->getMessage());
  }
}