You are here

public function PaymentMethodAddForm::buildCreditCardForm in Commerce Square Connect 8

Builds the credit card form.

Parameters

array $element: The target element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the complete form.

Return value

array The built credit card form.

Overrides PaymentMethodAddForm::buildCreditCardForm

File

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

Class

PaymentMethodAddForm
Payment method add form for Square.

Namespace

Drupal\commerce_square\PluginForm\Square

Code

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;
}