You are here

abstract class StripeBase in Stripe 2.x

Provides the base for our Stripe elements

Hierarchy

Expanded class hierarchy of StripeBase

File

src/Element/StripeBase.php, line 19

Namespace

Drupal\stripe\Element
View source
abstract class StripeBase extends FormElement {
  protected static $type = '';

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    $info = [
      '#process' => [
        [
          $class,
          'processStripe',
        ],
      ],
      '#theme_wrappers' => [
        'form_element' => [],
      ],
      '#stripe_submit_selector' => [],
      '#stripe_amount' => 100,
      '#stripe_label' => 'Total',
      '#stripe_currency' => 'usd',
      '#stripe_country' => 'US',
      '#stripe_paymentintent' => [],
      '#stripe_shared' => TRUE,
    ];
    return $info;
  }

  /**
   * Flatten the sub-elements.
   *
   * This is so that the "#tree" can be used but this elements is presented
   * as a regular 'composite element' to FAPI.
   *
   * @see https://www.drupal.org/project/drupal/issues/784874#comment-11420347
   */
  public static function processActions(&$element, FormStateInterface $form_state, &$complete_form) {
    array_pop($element['#parents']);
    array_pop($element['#parents']);
    return $element;
  }

  /**
   * Processes a stripe element.
   */
  public static function processStripe(&$element, FormStateInterface $form_state, &$complete_form) {
    if (!$form_state
      ->isProcessingInput() && !$form_state
      ->isExecuted() && $form_state
      ->get('drupal_stripe_' . static::$type)) {
      $error = t('Only one stripe element of type @type is allowed per form.', [
        '@type' => static::$type,
      ]);

      // \Drupal::messenger()->addError($error);
      $form_state
        ->setError($element, $error);
      $element['error'] = [
        '#markup' => "<div class=\"messages messages--error\">{$error}</div>",
        '#attributes' => [
          'class' => [
            'messages',
            'messages-error',
          ],
        ],
      ];
      return $element;
    }
    $form_state
      ->set('drupal_stripe_' . static::$type, TRUE);
    $config = \Drupal::config('stripe.settings');
    $apikey = $config
      ->get('apikey.' . $config
      ->get('environment') . '.public');
    $apikeySecret = $config
      ->get('apikey.' . $config
      ->get('environment') . '.secret');
    if (empty($apikey) || empty($apikeySecret)) {
      $settings_uri = Url::fromRoute('stripe.settings')
        ->toString();
      $error = t('You must <a href="@settings_uri">configure</a> the <b>API Keys</b> on <b>%environment</b> for the stripe element to work.', [
        '%environment' => $config
          ->get('environment'),
        '@settings_uri' => $settings_uri,
      ]);
      \Drupal::messenger()
        ->addError($error);
      $element['error'] = [
        '#markup' => $error,
      ];
      return $element;
    }
    $stripe = new \Stripe\StripeClient($apikeySecret);
    $id = $element['#id'];
    $wrapper_id = 'stripe-' . implode('-', $element['#parents']) . '-wrapper';
    $button_name = 'stripe-' . implode('-', $element['#parents']) . '-button';
    $element['#tree'] = TRUE;

    // $element['card'] = [
    //   '#markup' => "<div id=\"$id-stripe-card\"></div>",
    // ];
    // $element['paymentrequest'] = [
    //   '#markup' => "<div id=\"$id-stripe-paymentrequest\"></div>",
    //   '#access' => $element['#stripe_paymentrequest'],
    // ];
    $element['stripe'] = [
      '#type' => 'container',
      '#id' => $id,
    ];
    $element['stripe']['element'] = [
      '#markup' => "<div class=\"drupal-stripe-element\"></div>",
    ];
    $element['stripe']['errors'] = [
      '#markup' => "<div class=\"drupal-stripe-errors\" role=\"alert\"></div>",
    ];
    $element['stripe']['actions'] = [
      '#process' => [
        [
          static::class,
          'processActions',
        ],
      ],
    ];
    $element['stripe']['actions']['update'] = [
      '#type' => 'submit',
      '#value' => t('Update'),
      // We specifically don't us a validate ajax callback as we want to act
      // only when the whole form validation succeddes, not otherwise
      // '#validate' => [[get_called_class(), 'validateStripeElementCallback']],
      '#submit' => [
        [
          get_called_class(),
          'submitStripeElementCallback',
        ],
      ],
      '#ajax' => [
        'callback' => [
          get_called_class(),
          'ajaxStripeElementCallback',
        ],
        'progress' => [
          'type' => 'none',
        ],
      ],
      // Disable validation, hide button, add submit button trigger class.
      '#attributes' => [
        'formnovalidate' => 'formnovalidate',
        'class' => [
          'js-hide',
          'drupal-stripe-update',
        ],
      ],
      // Issue #1342066 Document that buttons with the same #value need a unique
      // #name for the Form API to distinguish them, or change the Form API to
      // assign unique #names automatically.
      '#name' => $button_name,
    ];
    $element['stripe']['actions']['#attached']['library'][] = 'stripe/stripe';
    $element['stripe']['actions']['#attached']['drupalSettings']['stripe']['apiKey'] = $apikey;
    $settings = [];
    $payment_intent_id = $element['#value']['payment_intent'] ?? NULL;

    // If the element is configured to share the same payment intent for all
    // stripe elements in the form, attempt to find it.
    if (!$payment_intent_id && $element['#stripe_shared']) {
      $payment_intent_id = $form_state
        ->get('stripe_paymentintent');
    }
    if (!$payment_intent_id) {
      $payment_intent_create = $element['#stripe_paymentintent'] ?? [];
      $payment_intent_create += [
        'amount' => is_numeric($element['#stripe_amount']) ? $element['#stripe_amount'] : 100,
        'currency' => $element['#stripe_currency'],
      ];
      $payment_intent = $stripe->paymentIntents
        ->create($payment_intent_create);
    }
    else {
      $payment_intent = $stripe->paymentIntents
        ->retrieve($payment_intent_id);
    }

    // If the element is configured to share the same payment intent for all
    // stripe elements in the form, store it
    if ($element['#stripe_shared']) {
      $form_state
        ->set('stripe_paymentintent', $payment_intent->id);
    }
    $element['stripe']['actions']['payment_intent'] = [
      '#type' => 'hidden',
      '#value' => $payment_intent->id,
      '#attributes' => [
        'id' => "{$id}-stripe-payment-intent",
        'data-payment-intent-status' => $payment_intent->status,
        'class' => [
          'drupal-stripe-payment-intent',
        ],
      ],
    ];
    $element['stripe']['actions']['client_secret'] = [
      '#type' => 'hidden',
      '#value' => $payment_intent->client_secret,
      '#attributes' => [
        'class' => [
          'drupal-stripe-client-secret',
        ],
      ],
    ];
    $element['stripe']['actions']['trigger'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'drupal-stripe-trigger',
        ],
      ],
    ];
    $settings['country'] = $element['#stripe_country'];
    $settings['currency'] = $element['#stripe_currency'];
    $settings['amount'] = $element['#stripe_amount'];
    $settings['label'] = $element['#stripe_label'];
    $settings['submit_selector'] = $element['#stripe_submit_selector'];
    $settings['type'] = static::$type;
    $element['stripe']['actions']['#attached']['drupalSettings']['stripe']['elements'][$element['#id']] = $settings;

    // Add validate callback.
    $element['stripe'] += [
      '#element_validate' => [],
    ];
    array_unshift($element['stripe']['#element_validate'], [
      get_called_class(),
      'validateStripeElement',
    ]);
    if (!empty($element['#value']['trigger'])) {
      $element['stripe']['actions']['processed'] = [
        '#type' => 'value',
        '#value' => TRUE,
      ];
    }
    return $element;
  }

  /**
   * Validates an other element.
   */
  public static function validateStripeElement(&$element, FormStateInterface $form_state, &$form) {
  }

  /**
   * Webform computed element submit callback.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public static function submitStripeElementCallback(array $form, FormStateInterface $form_state) {

    // Do nothing, but it's important to prevent other submission handlers
    // from being run
  }

  /**
   * Webform computed element Ajax callback.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The computed element element.
   */
  public static function ajaxStripeElementCallback(array $form, FormStateInterface $form_state) {
    $config = \Drupal::config('stripe.settings');
    $apikeySecret = $config
      ->get('apikey.' . $config
      ->get('environment') . '.secret');

    // Stripe form element from trigger
    $trigger = $form_state
      ->getTriggeringElement();
    $key = $trigger['#parents'][0];
    $payment_intent = $form_state
      ->getValue([
      $key,
      'payment_intent',
    ]);
    $response = new AjaxResponse();

    // Instantiate our event.
    $event = new StripePaymentEvent($form, $form_state, $key);

    // Dispatch our event to allow modules to update client-side elements of
    // the stripe elements
    $event_dispatcher = \Drupal::service('event_dispatcher');
    $event_dispatcher
      ->dispatch(StripeEvents::PAYMENT, $event);
    $settings = [
      'trigger' => $form_state
        ->getValue([
        $key,
        'trigger',
      ]),
      'error' => $form_state
        ->hasAnyErrors(),
    ];
    $total = $event
      ->getTotal();
    if (!empty($total)) {
      $settings['total'] = $total;
      if ($payment_intent) {
        $stripe = new \Stripe\StripeClient($apikeySecret);
        $stripe->paymentIntents
          ->update($payment_intent, [
          'amount' => $total['amount'],
        ]);
      }
    }
    $billing_details = $event
      ->getBillingDetails();
    if (!empty($billing_details)) {
      $settings['billing_details'] = $billing_details;
    }
    $response
      ->addCommand(new InvokeCommand(NULL, 'stripeUpdatePaymentIntent', [
      $settings,
    ]));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
StripeBase::$type protected static property 2
StripeBase::ajaxStripeElementCallback public static function Webform computed element Ajax callback.
StripeBase::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
StripeBase::processActions public static function Flatten the sub-elements.
StripeBase::processStripe public static function Processes a stripe element.
StripeBase::submitStripeElementCallback public static function Webform computed element submit callback.
StripeBase::validateStripeElement public static function Validates an other element.