You are here

public function PaymentMethodAddForm::buildCreditCardForm in Commerce Braintree 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/HostedFields/PaymentMethodAddForm.php, line 63

Class

PaymentMethodAddForm
Provides the HostedFields payment method add form.

Namespace

Drupal\commerce_braintree\PluginForm\HostedFields

Code

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