You are here

class PaymentMethodAddForm in Commerce Braintree 8

Provides the HostedFields payment method add form.

Hierarchy

Expanded class hierarchy of PaymentMethodAddForm

File

src/PluginForm/HostedFields/PaymentMethodAddForm.php, line 11

Namespace

Drupal\commerce_braintree\PluginForm\HostedFields
View source
class PaymentMethodAddForm extends BasePaymentMethodAddForm {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $payment_method = $this->entity;
    if ($payment_method
      ->bundle() === 'paypal_credit') {
      $form['payment_details'] = $this
        ->buildPayPalForm($form['payment_details'], $form_state);
    }
    return $form;
  }

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

    /** @var \Drupal\commerce_braintree\Plugin\Commerce\PaymentGateway\HostedFieldsInterface $plugin */
    $plugin = $this->plugin;
    $element['#attached']['library'][] = 'commerce_braintree/paypal';
    $element['#attached']['drupalSettings']['commerceBraintree'] = [
      'clientToken' => $plugin
        ->generateClientToken(),
      'integration' => 'paypal',
      'paypalButton' => 'paypal-button',
      'environment' => $plugin
        ->getMode() == 'test' ? 'sandbox' : 'production',
      'paymentMethodType' => $this->entity
        ->bundle(),
    ];
    $element['#attributes']['class'][] = 'braintree-form';
    $element['paypal_button'] = [
      '#type' => 'container',
      '#id' => 'paypal-button',
    ];

    // Populated by the JS library.
    $element['payment_method_nonce'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'braintree-nonce',
        ],
      ],
    ];

    // Put the PayPal button below the billing address.
    $element['#weight'] = 50;
    return $element;
  }

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

    /** @var \Drupal\commerce_braintree\Plugin\Commerce\PaymentGateway\HostedFieldsInterface $plugin */
    $plugin = $this->plugin;
    $element['#attached']['library'][] = 'commerce_braintree/hosted-fields';
    $element['#attached']['drupalSettings']['commerceBraintree'] = [
      'clientToken' => $plugin
        ->generateClientToken(),
      'integration' => 'custom',
      'hostedFields' => [
        'number' => [
          'selector' => '#card-number',
        ],
        'cvv' => [
          'selector' => '#cvv',
        ],
        'expirationMonth' => [
          'selector' => '#expiration-month',
        ],
        'expirationYear' => [
          'selector' => '#expiration-year',
        ],
      ],
    ];
    $element['#attributes']['class'][] = 'braintree-form';

    // Populated by the JS library.
    $element['payment_method_nonce'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'braintree-nonce',
        ],
      ],
    ];
    $element['card_type'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'braintree-card-type',
        ],
      ],
    ];
    $element['last2'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'braintree-last2',
        ],
      ],
    ];
    $element['number'] = [
      '#type' => 'item',
      '#title' => t('Card number'),
      '#markup' => '<div id="card-number" class="braintree-hosted-field"></div>',
    ];
    $element['expiration'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'credit-card-form__expiration',
        ],
      ],
    ];
    $element['expiration']['month'] = [
      '#type' => 'item',
      '#title' => t('Month'),
      '#markup' => '<div id="expiration-month" class="braintree-hosted-field"></div>',
    ];
    $element['expiration']['divider'] = [
      '#type' => 'item',
      '#title' => '',
      '#markup' => '<span class="credit-card-form__divider">/</span>',
    ];
    $element['expiration']['year'] = [
      '#type' => 'item',
      '#title' => t('Year'),
      '#markup' => '<div id="expiration-year" class="braintree-hosted-field"></div>',
    ];
    $element['cvv'] = [
      '#type' => 'item',
      '#title' => t('CVV'),
      '#markup' => '<div id="cvv" class="braintree-hosted-field"></div>',
    ];
    return $element;
  }

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

    // The JS library performs its own validation.
  }

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

    // The JS library performs its own validation.
  }

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

    // The payment gateway plugin will process the submitted payment details.
  }

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

    // The payment gateway plugin will process the submitted payment details.
  }

}

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::buildConfigurationForm public function Form constructor. Overrides PaymentMethodAddForm::buildConfigurationForm
PaymentMethodAddForm::buildCreditCardForm public function Builds the credit card form. Overrides PaymentMethodAddForm::buildCreditCardForm
PaymentMethodAddForm::buildPayPalForm public function Builds the PayPal form. Overrides PaymentMethodAddForm::buildPayPalForm
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 public function Handles the submission of the PayPal form. Overrides PaymentMethodAddForm::submitPayPalForm
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. Overrides PaymentMethodAddForm::validatePayPalForm
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.