You are here

class PaymentMethodAddForm in Commerce Stripe 8

Provides payment form for Stripe.

Hierarchy

Expanded class hierarchy of PaymentMethodAddForm

File

src/PluginForm/Stripe/PaymentMethodAddForm.php, line 15

Namespace

Drupal\commerce_stripe\PluginForm\Stripe
View source
class PaymentMethodAddForm extends BasePaymentMethodAddForm implements TrustedCallbackInterface {

  /**
   * {@inheritdoc}
   */
  public function buildCreditCardForm(array $element, FormStateInterface $form_state) {

    // Alter the form with Stripe specific needs.
    $element['#attributes']['class'][] = 'stripe-form';

    // Set our key to settings array.

    /** @var \Drupal\commerce_stripe\Plugin\Commerce\PaymentGateway\StripeInterface $plugin */
    $plugin = $this->plugin;

    /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
    $payment_method = $this->entity;
    $payment_method_owner = $payment_method
      ->getOwner();

    // @todo Replace with setting check from https://www.drupal.org/project/commerce/issues/2871483.
    // @todo Simplify check after https://www.drupal.org/project/commerce/issues/3073942.
    $client_secret = NULL;
    if ($payment_method_owner instanceof UserInterface && $payment_method_owner
      ->isAuthenticated()) {
      $route_match = \Drupal::routeMatch();

      // @todo Use context passed by parent element after https://www.drupal.org/project/commerce/issues/3077783.
      if ($route_match
        ->getRouteName() === 'entity.commerce_payment_method.add_form') {

        // A SetupIntent is required if this is being created for off-session
        // usage (for instance, outside of checkout where there is no payment
        // intent that will be authenticated.)
        $setup_intent = SetupIntent::create([
          'usage' => 'off_session',
        ]);
        $client_secret = $setup_intent->client_secret;
      }
    }
    $element['#attached']['library'][] = 'commerce_stripe/stripe';
    $element['#attached']['library'][] = 'commerce_stripe/form';
    $element['#attached']['drupalSettings']['commerceStripe'] = [
      'publishableKey' => $plugin
        ->getPublishableKey(),
      'clientSecret' => $client_secret,
    ];

    // Populated by the JS library.
    $element['stripe_payment_method_id'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'id' => 'stripe-payment-method-id',
      ],
    ];
    $element['card_number'] = [
      '#type' => 'item',
      '#title' => t('Card number'),
      '#required' => TRUE,
      '#validated' => TRUE,
      '#markup' => '<div id="card-number-element" class="form-text"></div>',
    ];
    $element['expiration'] = [
      '#type' => 'item',
      '#title' => t('Expiration date'),
      '#required' => TRUE,
      '#validated' => TRUE,
      '#markup' => '<div id="expiration-element"></div>',
    ];
    $element['security_code'] = [
      '#type' => 'item',
      '#title' => t('CVC'),
      '#required' => TRUE,
      '#validated' => TRUE,
      '#markup' => '<div id="security-code-element"></div>',
    ];

    // To display validation errors.
    $element['payment_errors'] = [
      '#type' => 'markup',
      '#markup' => '<div id="payment-errors"></div>',
      '#weight' => -200,
    ];
    $cacheability = new CacheableMetadata();
    $cacheability
      ->addCacheableDependency($this->entity);
    $cacheability
      ->setCacheMaxAge(0);
    $cacheability
      ->applyTo($element);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  protected function validateCreditCardForm(array &$element, FormStateInterface $form_state) {

    // The JS library performs its own validation.
  }

  /**
   * {@inheritdoc}
   */
  public function submitCreditCardForm(array $element, FormStateInterface $form_state) {
    if ($email = $form_state
      ->getValue([
      'contact_information',
      'email',
    ])) {
      $email_parents = array_merge($element['#parents'], [
        'email',
      ]);
      $form_state
        ->setValue($email_parents, $email);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    if (isset($form['billing_information'])) {
      $form['billing_information']['#after_build'][] = [
        get_class($this),
        'addAddressAttributes',
      ];
    }
    return $form;
  }

  /**
   * Element #after_build callback: adds "data-stripe" to address properties.
   *
   * This allows our JavaScript to pass these values to Stripe as customer
   * information, enabling CVC, Zip, and Street checks.
   *
   * @param array $element
   *   The form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The modified form element.
   */
  public static function addAddressAttributes(array $element, FormStateInterface $form_state) {
    if (isset($element['address'])) {
      $element['address']['widget'][0]['address']['given_name']['#attributes']['data-stripe'] = 'given_name';
      $element['address']['widget'][0]['address']['family_name']['#attributes']['data-stripe'] = 'family_name';
      $element['address']['widget'][0]['address']['address_line1']['#attributes']['data-stripe'] = 'address_line1';
      $element['address']['widget'][0]['address']['address_line2']['#attributes']['data-stripe'] = 'address_line2';
      $element['address']['widget'][0]['address']['locality']['#attributes']['data-stripe'] = 'address_city';
      $element['address']['widget'][0]['address']['postal_code']['#attributes']['data-stripe'] = 'address_zip';

      // Country code is a sub-element and needs another callback.
      $element['address']['widget'][0]['address']['country_code']['#pre_render'][] = [
        get_called_class(),
        'addCountryCodeAttributes',
      ];
    }
    return $element;
  }

  /**
   * Element #pre_render callback: adds "data-stripe" to the country_code.
   *
   * This ensures data-stripe is on the hidden or select element for the country
   * code, so that it is properly passed to Stripe.
   *
   * @param array $element
   *   The form element.
   *
   * @return array
   *   The modified form element.
   */
  public static function addCountryCodeAttributes(array $element) {
    $element['country_code']['#attributes']['data-stripe'] = 'address_country';
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'addCountryCodeAttributes',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
PaymentGatewayFormBase::$entity protected property The form entity.
PaymentGatewayFormBase::getEntity public function Gets the form entity. Overrides PaymentGatewayFormInterface::getEntity
PaymentGatewayFormBase::setEntity public function Sets the form entity. Overrides PaymentGatewayFormInterface::setEntity
PaymentMethodAddForm::addAddressAttributes public static function Element #after_build callback: adds "data-stripe" to address properties.
PaymentMethodAddForm::addCountryCodeAttributes public static function Element #pre_render callback: adds "data-stripe" to the country_code.
PaymentMethodAddForm::buildConfigurationForm public function Form constructor. Overrides PaymentMethodAddForm::buildConfigurationForm
PaymentMethodAddForm::buildCreditCardForm public function Builds the credit card form. Overrides PaymentMethodAddForm::buildCreditCardForm
PaymentMethodAddForm::buildPayPalForm protected function Builds the PayPal form.
PaymentMethodAddForm::getErrorElement public function Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormBase::getErrorElement
PaymentMethodAddForm::submitConfigurationForm public function Form submission handler. Overrides PaymentMethodFormBase::submitConfigurationForm
PaymentMethodAddForm::submitCreditCardForm public function Handles the submission of the credit card form. Overrides PaymentMethodAddForm::submitCreditCardForm
PaymentMethodAddForm::submitPayPalForm protected function Handles the submission of the PayPal form.
PaymentMethodAddForm::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
PaymentMethodAddForm::validateConfigurationForm public function Form validation handler. Overrides PaymentMethodFormBase::validateConfigurationForm
PaymentMethodAddForm::validateCreditCardForm protected function Validates the credit card form. Overrides PaymentMethodAddForm::validateCreditCardForm
PaymentMethodAddForm::validatePayPalForm protected function Validates the PayPal form.
PaymentMethodFormBase::$currentStore protected property The current store.
PaymentMethodFormBase::$entityTypeManager protected property The entity type manager.
PaymentMethodFormBase::$inlineFormManager protected property The inline form manager.
PaymentMethodFormBase::$logger protected property The logger.
PaymentMethodFormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
PaymentMethodFormBase::__construct public function Constructs a new PaymentMethodFormBase.
PluginFormBase::$plugin protected property The plugin this form is for. 3
PluginFormBase::setPlugin public function Sets the plugin for this object. Overrides PluginAwareInterface::setPlugin 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.