You are here

public function Checkout::validateConfigurationForm in Commerce PayPal 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/Checkout.php, line 323

Class

Checkout
Provides the PayPal Checkout payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  parent::validateConfigurationForm($form, $form_state);
  if ($form_state
    ->getErrors()) {
    return;
  }
  $values = $form_state
    ->getValue($form['#parents']);
  if (empty($values['client_id']) || empty($values['secret'])) {
    return;
  }
  $sdk = $this->checkoutSdkFactory
    ->get($values);

  // Make sure we query for a fresh access token.
  $this->state
    ->delete('commerce_paypal.oauth2_token');
  try {
    $sdk
      ->getAccessToken();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Connectivity to PayPal successfully verified.'));
  } catch (BadResponseException $exception) {
    $this
      ->messenger()
      ->addError($this
      ->t('Invalid client_id or secret specified.'));
    $form_state
      ->setError($form['credentials']['client_id']);
    $form_state
      ->setError($form['credentials']['secret']);
  }
}