You are here

protected function PaymentMethodEditForm::buildCreditCardForm in Commerce Core 8.2

Builds the credit card form.

Parameters

\Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method: The payment method.

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

Return value

array The built credit card form.

1 call to PaymentMethodEditForm::buildCreditCardForm()
PaymentMethodEditForm::buildConfigurationForm in modules/payment/src/PluginForm/PaymentMethodEditForm.php
Form constructor.

File

modules/payment/src/PluginForm/PaymentMethodEditForm.php, line 95

Class

PaymentMethodEditForm

Namespace

Drupal\commerce_payment\PluginForm

Code

protected function buildCreditCardForm(PaymentMethodInterface $payment_method, FormStateInterface $form_state) {

  // Build a month select list that shows months with a leading zero.
  $months = [];
  for ($i = 1; $i < 13; $i++) {
    $month = str_pad($i, 2, '0', STR_PAD_LEFT);
    $months[$month] = $month;
  }

  // Build a year select list that uses a 4 digit key with a 2 digit value.
  $current_year_4 = date('Y');
  $current_year_2 = date('y');
  $years = [];
  for ($i = 0; $i < 10; $i++) {
    $years[$current_year_4 + $i] = $current_year_2 + $i;
  }
  $element['#attached']['library'][] = 'commerce_payment/payment_method_icons';
  $element['#attributes']['class'][] = 'credit-card-form';
  $element['type'] = [
    '#type' => 'hidden',
    '#value' => $payment_method
      ->get('card_type')->value,
  ];
  $element['number'] = [
    '#type' => 'inline_template',
    '#template' => '<span class="payment-method-icon payment-method-icon--{{ type }}"></span>{{ label }}',
    '#context' => [
      'type' => $payment_method
        ->get('card_type')->value,
      'label' => $payment_method
        ->label(),
    ],
  ];
  $element['expiration'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'credit-card-form__expiration',
      ],
    ],
  ];
  $element['expiration']['month'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Month'),
    '#options' => $months,
    '#default_value' => str_pad($payment_method
      ->get('card_exp_month')->value, 2, '0', STR_PAD_LEFT),
    '#required' => TRUE,
  ];
  $element['expiration']['divider'] = [
    '#type' => 'item',
    '#title' => '',
    '#markup' => '<span class="credit-card-form__divider">/</span>',
  ];
  $element['expiration']['year'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Year'),
    '#options' => $years,
    '#default_value' => $payment_method
      ->get('card_exp_year')->value,
    '#required' => TRUE,
  ];
  return $element;
}