You are here

public static function Stripe::processStripe in Stripe 8

Processes a stripe element.

File

src/Element/Stripe.php, line 69

Class

Stripe
Provides a form element that will be rendered by stripe elements.

Namespace

Drupal\stripe\Element

Code

public static function processStripe(&$element, FormStateInterface $form_state, &$complete_form) {
  $config = \Drupal::config('stripe.settings');
  $element['#attached']['library'][] = 'stripe/stripe.js';
  $apikey = $config
    ->get('apikey.' . $config
    ->get('environment') . '.public');
  $element['#attached']['drupalSettings']['stripe']['apiKey'] = $apikey;
  if (empty($apikey)) {
    $settings_uri = Url::fromRoute('stripe.settings')
      ->toString();
    \Drupal::messenger()
      ->addError(t('You must <a href="@settings_uri">configure</a> the <b>Publishable API Key</b> on <b>%environment</b> for the stripe elements to work.', [
      '%environment' => $config
        ->get('environment'),
      '@settings_uri' => $settings_uri,
    ]));
  }
  $settings = [];
  $element['#attached']['drupalSettings']['stripe']['elements'][$element['#id']] = $settings;
  return $element;
}