You are here

public function CreditCardTerminalForm::buildForm in Ubercart 8.4

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

payment/uc_credit/src/Form/CreditCardTerminalForm.php, line 67

Class

CreditCardTerminalForm
Displays the credit card terminal form for administrators.

Namespace

Drupal\uc_credit\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL, PaymentMethodInterface $uc_payment_method = NULL) {
  $this->order = $uc_order;
  $this->paymentMethod = $uc_payment_method;

  // Get the transaction types available to our default gateway.
  $types = $this->paymentMethod
    ->getPlugin()
    ->getTransactionTypes();
  $balance = uc_payment_balance($this->order);
  $form['order_total'] = [
    '#prefix' => '<div><strong>',
    '#markup' => $this
      ->t('Order total: @total', [
      '@total' => uc_currency_format($this->order
        ->getTotal()),
    ]),
    '#suffix' => '</div></strong>',
  ];
  $form['balance'] = [
    '#prefix' => '<div><strong>',
    '#markup' => $this
      ->t('Balance: @balance', [
      '@balance' => uc_currency_format($balance),
    ]),
    '#suffix' => '</div></strong>',
  ];

  // Let the administrator set the amount to charge.
  $form['amount'] = [
    '#type' => 'uc_price',
    '#title' => $this
      ->t('Charge Amount'),
    '#default_value' => $balance > 0 ? uc_currency_format($balance, FALSE, FALSE, '.') : 0,
  ];

  // Build a credit card form.
  $form['specify_card'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Credit card details'),
    '#description' => $this
      ->t('Use the available buttons in this fieldset to process with the specified card details.'),
    '#open' => TRUE,
  ];
  $form['specify_card']['cc_data'] = [
    '#tree' => TRUE,
    '#prefix' => '<div class="clearfix">',
    '#suffix' => '</div>',
  ];
  $form['specify_card']['cc_data'] += $this->paymentMethod
    ->getPlugin()
    ->cartDetails($this->order, [], $form_state);
  unset($form['specify_card']['cc_data']['cc_policy']);
  $form['specify_card']['actions'] = [
    '#type' => 'actions',
  ];

  // If available, let the card be charged now.
  if (in_array(UC_CREDIT_AUTH_CAPTURE, $types)) {
    $form['specify_card']['actions']['charge_card'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Charge amount'),
    ];
  }

  // If available, let the amount be authorized.
  if (in_array(UC_CREDIT_AUTH_ONLY, $types)) {
    $form['specify_card']['actions']['authorize_card'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Authorize amount only'),
    ];
  }

  // If available, create a reference at the gateway.
  if (in_array(UC_CREDIT_REFERENCE_SET, $types)) {
    $form['specify_card']['actions']['reference_set'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Set a reference only'),
    ];
  }

  // If available, create a reference at the gateway.
  if (in_array(UC_CREDIT_CREDIT, $types)) {
    $form['specify_card']['actions']['credit_card'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Credit amount to this card'),
    ];
  }

  // Find any uncaptured authorizations.
  $options = [];
  if (isset($this->order->data->cc_txns['authorizations'])) {
    foreach ($this->order->data->cc_txns['authorizations'] as $auth_id => $data) {
      if (empty($data['captured'])) {
        $options[$auth_id] = $this
          ->t('@auth_id - @date - @amount authorized', [
          '@auth_id' => strtoupper($auth_id),
          '@date' => $this->dateFormatter
            ->format($data['authorized'], 'short'),
          '@amount' => uc_currency_format($data['amount']),
        ]);
      }
    }
  }

  // If any authorizations existed...
  if (!empty($options)) {

    // Display fieldset with the authorizations and available action buttons.
    $form['authorizations'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Prior authorizations'),
      '#description' => $this
        ->t('Use the available buttons in this fieldset to select and act on a prior authorization. The charge amount specified above will be captured against the authorization listed below. Only one capture is possible per authorization, and a capture for more than the amount of the authorization may result in additional fees to you.'),
      '#open' => TRUE,
    ];
    $form['authorizations']['select_auth'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Select authorization'),
      '#options' => $options,
    ];
    $form['authorizations']['actions'] = [
      '#type' => 'actions',
    ];

    // If available, capture a prior authorization.
    if (in_array(UC_CREDIT_PRIOR_AUTH_CAPTURE, $types)) {
      $form['authorizations']['actions']['auth_capture'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Capture amount to this authorization'),
      ];
    }

    // If available, void a prior authorization.
    if (in_array(UC_CREDIT_VOID, $types)) {
      $form['authorizations']['actions']['auth_void'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Void authorization'),
      ];
    }

    // Collapse this fieldset if no actions are available.
    if (!isset($form['authorizations']['actions']['auth_capture']) && !isset($form['authorizations']['actions']['auth_void'])) {
      $form['authorizations']['#open'] = FALSE;
    }
  }

  // Find any uncaptured authorizations.
  $options = [];
  if (isset($this->order->data->cc_txns['references'])) {
    foreach ($this->order->data->cc_txns['references'] as $ref_id => $data) {
      $options[$ref_id] = $this
        ->t('@ref_id - @date - (Last 4) @card', [
        '@ref_id' => strtoupper($ref_id),
        '@date' => $this->dateFormatter
          ->format($data['created'], 'short'),
        '@card' => $data['card'],
      ]);
    }
  }

  // If any references existed...
  if (!empty($options)) {

    // Display fieldset with the authorizations and available action buttons.
    $form['references'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Customer references'),
      '#description' => $this
        ->t('Use the available buttons in this fieldset to select and act on a customer reference.'),
      '#open' => TRUE,
    ];
    $form['references']['select_ref'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Select references'),
      '#options' => $options,
    ];
    $form['references']['actions'] = [
      '#type' => 'actions',
    ];

    // If available, capture a prior references.
    if (in_array(UC_CREDIT_REFERENCE_TXN, $types)) {
      $form['references']['actions']['ref_capture'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Charge amount to this reference'),
      ];
    }

    // If available, remove a previously stored reference.
    if (in_array(UC_CREDIT_REFERENCE_REMOVE, $types)) {
      $form['references']['actions']['ref_remove'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Remove reference'),
      ];
    }

    // If available, remove a previously stored reference.
    if (in_array(UC_CREDIT_REFERENCE_CREDIT, $types)) {
      $form['references']['actions']['ref_credit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Credit amount to this reference'),
      ];
    }

    // Collapse this fieldset if no actions are available.
    if (!isset($form['references']['actions']['ref_capture']) && !isset($form['references']['actions']['ref_remove']) && !isset($form['references']['actions']['ref_credit'])) {
      $form['references']['#open'] = FALSE;
    }
  }
  $form['#attached']['library'][] = 'uc_credit/uc_credit.styles';
  return $form;
}