You are here

class PaymentMethodAddForm in Commerce Square Connect 8

Payment method add form for Square.

Hierarchy

Expanded class hierarchy of PaymentMethodAddForm

File

src/PluginForm/Square/PaymentMethodAddForm.php, line 13

Namespace

Drupal\commerce_square\PluginForm\Square
View source
class PaymentMethodAddForm extends BasePaymentMethodAddForm {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    /** @var \Drupal\commerce_square\Plugin\Commerce\PaymentGateway\Square $plugin */
    $plugin = $this->plugin;
    if ($plugin
      ->collectsBillingInformation()) {
      $billing_information_inline_form = $form['billing_information']['#inline_form'];
      assert($billing_information_inline_form instanceof CustomerProfile);
      $billing_profile = $billing_information_inline_form
        ->getEntity();
      if (!$billing_profile
        ->get('address')
        ->isEmpty()) {
        $address = $billing_profile
          ->get('address')
          ->first();
        assert($address instanceof AddressItem);
        $form['#attached']['drupalSettings']['commerceSquare']['customerPostalCode'] = $address
          ->getPostalCode();
      }
    }
    return $form;
  }

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

    /** @var \Drupal\commerce_square\Plugin\Commerce\PaymentGateway\Square $plugin */
    $plugin = $this->plugin;
    $configuration = $plugin
      ->getConfiguration();
    $config = \Drupal::config('commerce_square.settings');
    $api_mode = $configuration['mode'] == 'test' ? 'sandbox' : 'production';
    $element['#attached']['library'][] = 'commerce_square/form';
    $element['#attached']['drupalSettings']['commerceSquare'] = [
      'applicationId' => $config
        ->get($api_mode . '_app_id'),
      'apiMode' => $api_mode,
      'drupalSelector' => 'edit-' . str_replace('_', '-', implode('-', $element['#parents'])),
    ];
    $element['#attributes']['class'][] = 'square-form';

    // Populated by the JS library.
    $element['payment_method_nonce'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'square-nonce',
        ],
      ],
    ];
    $element['card_type'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'square-card-type',
        ],
      ],
    ];
    $element['last4'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'square-last4',
        ],
      ],
    ];
    $element['exp_month'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'square-exp-month',
        ],
      ],
    ];
    $element['exp_year'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'square-exp-year',
        ],
      ],
    ];
    $element['number'] = [
      '#type' => 'item',
      '#title' => t('Card number'),
      '#markup' => '<div id="square-card-number"></div>',
    ];
    $element['details'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'credit-card-form__expiration',
        ],
      ],
    ];
    $element['details']['expiration'] = [
      '#type' => 'item',
      '#title' => t('Expiration'),
      '#markup' => '<div id="square-expiration-date"></div>',
    ];
    $element['details']['cvv'] = [
      '#type' => 'item',
      '#title' => t('CVV'),
      '#markup' => '<div id="square-cvv"></div>',
    ];
    $element['details']['postal-code'] = [
      '#type' => 'item',
      '#markup' => '<div id="square-postal-code"></div>',
    ];
    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) {

    // 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 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::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.