You are here

public function VisaCheckoutForm::buildConfigurationForm in Commerce Authorize.Net 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PaymentOffsiteForm::buildConfigurationForm

File

src/PluginForm/VisaCheckoutForm.php, line 13

Class

VisaCheckoutForm

Namespace

Drupal\commerce_authnet\PluginForm

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);

  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
  $payment = $this->entity;
  $form['authnet_visa']['#type'] = 'container';
  $form['authnet_visa']['#attributes']['class'][] = 'authorize-net-accept-js-form';

  /** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\PaymentGatewayBase $plugin */
  $plugin = $this->plugin;
  $form['authnet_visa']['#attached']['library'][] = 'commerce_authnet/form-visa-checkout';
  if ($plugin
    ->getMode() == 'test') {
    $form['authnet_visa']['#attached']['library'][] = 'commerce_authnet/form-visa-checkout-sandbox';
    $image_src = 'https://sandbox.secure.checkout.visa.com/wallet-services-web/xo/button.png';
  }
  else {
    $form['authnet_visa']['#attached']['library'][] = 'commerce_authnet/form-visa-checkout-production';
    $image_src = 'https://secure.checkout.visa.com/wallet-services-web/xo/button.png';
  }
  $order = $payment
    ->getOrder();
  $current_checkout_step = $order
    ->get('checkout_step')
    ->getValue()[0]['value'];

  /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */
  $checkout_flow = $order
    ->get('checkout_flow')->entity;
  $next_step = $checkout_flow
    ->getPlugin()
    ->getNextStepId($current_checkout_step);
  $form['authnet_visa']['#attached']['drupalSettings']['commerceAuthorizeNet'] = [
    'visaApiKey' => $plugin
      ->getConfiguration()['visa_checkout_api_key'],
    'paymentMethodType' => 'authnet_visa',
    'currencyCode' => $payment
      ->getAmount()
      ->getCurrencyCode(),
    'number' => $payment
      ->getAmount()
      ->getNumber(),
    'successUrl' => $form['#return_url'],
    'errorUrl' => $form['#return_url'],
    'cancelUrl' => $form['#cancel_url'],
    'nextCheckoutStepUrl' => '/checkout/' . $order
      ->id() . '/' . $next_step,
  ];
  $form['authnet_visa']['visa_button'] = [
    '#type' => 'html_tag',
    '#tag' => 'img',
    '#attributes' => [
      'alt' => 'Visa Checkout',
      'role' => 'button',
      'class' => [
        'v-button',
      ],
      'src' => $image_src,
    ],
  ];
  return $form;
}