You are here

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

Class

PaymentMethodAddForm

Namespace

Drupal\commerce_authnet\PluginForm\AcceptJs

Code

public function buildCreditCardForm(array $element, FormStateInterface $form_state) {

  // Alter the form with AuthorizeNet Accept JS specific needs.
  $element['#attributes']['class'][] = 'authorize-net-accept-js-form';

  /** @var \Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway\AcceptJs $plugin */
  $plugin = $this->plugin;
  if ($plugin
    ->getMode() == 'test') {
    $element['#attached']['library'][] = 'commerce_authnet/accept-js-sandbox';
  }
  else {
    $element['#attached']['library'][] = 'commerce_authnet/accept-js-production';
  }

  // @todo Remove this line when
  // https://www.drupal.org/project/commerce/issues/2986599 gets fixed.
  $element['#attached']['library'][] = 'commerce_authnet/form-accept';
  $element['#attached']['drupalSettings']['commerceAuthorizeNet'] = [
    'clientKey' => $plugin
      ->getConfiguration()['client_key'],
    'apiLoginID' => $plugin
      ->getConfiguration()['api_login'],
    'paymentMethodType' => 'credit_card',
    'ccaStatus' => 0,
    'mode' => $plugin
      ->getMode(),
    'gatewayId' => $this
      ->getEntity()
      ->getPaymentGatewayId(),
  ];

  // Fields placeholder to be built by the JS.
  $element['number'] = [
    '#type' => 'textfield',
    '#title' => t('Card number'),
    '#attributes' => [
      'placeholder' => '•••• •••• •••• ••••',
      'autocomplete' => 'off',
      'autocorrect' => 'off',
      'autocapitalize' => 'none',
      'id' => 'credit-card-number',
      'required' => 'required',
    ],
    '#label_attributes' => [
      'class' => [
        'js-form-required',
        'form-required',
      ],
    ],
    '#maxlength' => 20,
    '#size' => 20,
  ];
  $element['expiration'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'credit-card-form__expiration',
      ],
    ],
  ];
  $element['expiration']['month'] = [
    '#type' => 'textfield',
    '#title' => t('Month'),
    '#attributes' => [
      'placeholder' => 'MM',
      'autocomplete' => 'off',
      'autocorrect' => 'off',
      'autocapitalize' => 'none',
      'id' => 'expiration-month',
      'required' => 'required',
    ],
    '#label_attributes' => [
      'class' => [
        'js-form-required',
        'form-required',
      ],
    ],
    '#maxlength' => 2,
    '#size' => 3,
  ];
  $element['expiration']['divider'] = [
    '#type' => 'item',
    '#title' => '',
    '#markup' => '<span class="credit-card-form__divider">/</span>',
  ];
  $element['expiration']['year'] = [
    '#type' => 'textfield',
    '#title' => t('Year'),
    '#attributes' => [
      'placeholder' => 'YY',
      'autocomplete' => 'off',
      'autocorrect' => 'off',
      'autocapitalize' => 'none',
      'id' => 'expiration-year',
      'required' => 'required',
    ],
    '#label_attributes' => [
      'class' => [
        'js-form-required',
        'form-required',
      ],
    ],
    '#maxlength' => 2,
    '#size' => 3,
  ];
  $element['security_code'] = [
    '#type' => 'textfield',
    '#title' => t('CVV'),
    '#attributes' => [
      'placeholder' => '•••',
      'autocomplete' => 'off',
      'autocorrect' => 'off',
      'autocapitalize' => 'none',
      'id' => 'cvv',
      'required' => 'required',
    ],
    '#label_attributes' => [
      'class' => [
        'js-form-required',
        'form-required',
      ],
    ],
    '#maxlength' => 4,
    '#size' => 4,
  ];

  // To display validation errors.
  $element['payment_errors'] = [
    '#type' => 'markup',
    '#markup' => '<div id="payment-errors"></div>',
    '#weight' => -200,
  ];

  // Populated by the JS library after receiving a response from AuthorizeNet.
  $element['data_descriptor'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'class' => [
        'accept-js-data-descriptor',
      ],
    ],
  ];
  $element['data_value'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'class' => [
        'accept-js-data-value',
      ],
    ],
  ];
  $element['last4'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'class' => [
        'accept-js-data-last4',
      ],
    ],
  ];
  $element['expiration_month'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'class' => [
        'accept-js-data-month',
      ],
    ],
  ];
  $element['expiration_year'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'class' => [
        'accept-js-data-year',
      ],
    ],
  ];

  /** @var \Drupal\commerce_order\Entity\Order $order */
  if ($order = $this->routeMatch
    ->getParameter('commerce_order')) {
    if ($plugin
      ->getConfiguration()['cca_status']) {
      $element['cca_jwt_token'] = [
        '#type' => 'hidden',
        '#attributes' => [
          'class' => [
            'accept-js-data-cca-jwt-token',
          ],
        ],
        '#value' => (string) $this
          ->generateJwt(),
      ];
      $element['cca_jwt_response_token'] = [
        '#type' => 'hidden',
        '#attributes' => [
          'class' => [
            'accept-js-data-cca-jwt-response-token',
          ],
        ],
      ];
      $element['#attached']['drupalSettings']['commerceAuthorizeNet']['orderId'] = $order
        ->id();
      $element['#attached']['drupalSettings']['commerceAuthorizeNet']['orderAmount'] = $this
        ->toMinorUnits($order
        ->getTotalPrice());
      $element['#attached']['drupalSettings']['commerceAuthorizeNet']['orderCurrency'] = $order
        ->getTotalPrice()
        ->getCurrencyCode();
      $element['#attached']['drupalSettings']['commerceAuthorizeNet']['ccaStatus'] = 1;
    }
  }
  return $element;
}