You are here

public function Stripe::validateConfigurationForm in Commerce Stripe 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides PaymentGatewayBase::validateConfigurationForm

File

src/Plugin/Commerce/PaymentGateway/Stripe.php, line 173

Class

Stripe
Provides the Stripe payment gateway.

Namespace

Drupal\commerce_stripe\Plugin\Commerce\PaymentGateway

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  parent::validateConfigurationForm($form, $form_state);
  if (!$form_state
    ->getErrors()) {
    $values = $form_state
      ->getValue($form['#parents']);

    // Validate the secret key.
    $expected_livemode = $values['mode'] == 'live' ? TRUE : FALSE;
    if (!empty($values['secret_key'])) {
      try {
        StripeLibrary::setApiKey($values['secret_key']);

        // Make sure we use the right mode for the secret keys.
        if (Balance::retrieve()
          ->offsetGet('livemode') != $expected_livemode) {
          $form_state
            ->setError($form['secret_key'], $this
            ->t('The provided secret key is not for the selected mode (@mode).', [
            '@mode' => $values['mode'],
          ]));
        }
      } catch (ApiErrorException $e) {
        $form_state
          ->setError($form['secret_key'], $this
          ->t('Invalid secret key.'));
      }
    }
  }
}