You are here

public function CreditCardTerminalForm::submitForm in Ubercart 8.4

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

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

Class

CreditCardTerminalForm
Displays the credit card terminal form for administrators.

Namespace

Drupal\uc_credit\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Get the data from the form and replace masked data from the order.
  $cc_data = $form_state
    ->getValue('cc_data');
  if (strpos($cc_data['cc_number'], (string) $this
    ->t('(Last 4) ')) === 0) {
    $cc_data['cc_number'] = $this->order->payment_details['cc_number'];
  }
  if (isset($cc_data['cc_cvv']) && isset($this->order->payment_details['cc_cvv'])) {
    if ($cc_data['cc_cvv'] == str_repeat('-', strlen($cc_data['cc_cvv']))) {
      $cc_data['cc_cvv'] = $this->order->payment_details['cc_cvv'];
    }
  }

  // Cache the values for use during processing.
  uc_credit_cache($cc_data, FALSE);

  // Build the data array passed on to the payment gateway.
  $txn_type = NULL;
  $reference = NULL;
  switch ($form_state
    ->getValue('op')) {
    case $this
      ->t('Charge amount'):
      $txn_type = UC_CREDIT_AUTH_CAPTURE;
      break;
    case $this
      ->t('Authorize amount only'):
      $txn_type = UC_CREDIT_AUTH_ONLY;
      break;
    case $this
      ->t('Set a reference only'):
      $txn_type = UC_CREDIT_REFERENCE_SET;
      break;
    case $this
      ->t('Credit amount to this card'):
      $txn_type = UC_CREDIT_CREDIT;
      break;
    case $this
      ->t('Capture amount to this authorization'):
      $txn_type = UC_CREDIT_PRIOR_AUTH_CAPTURE;
      $reference = $form_state
        ->getValue('select_auth');
      break;
    case $this
      ->t('Void authorization'):
      $txn_type = UC_CREDIT_VOID;
      $reference = $form_state
        ->getValue('select_auth');
      break;
    case $this
      ->t('Charge amount to this reference'):
      $txn_type = UC_CREDIT_REFERENCE_TXN;
      $reference = $form_state
        ->getValue('select_ref');
      break;
    case $this
      ->t('Remove reference'):
      $txn_type = UC_CREDIT_REFERENCE_REMOVE;
      $reference = $form_state
        ->getValue('select_ref');
      break;
    case $this
      ->t('Credit amount to this reference'):
      $txn_type = UC_CREDIT_REFERENCE_CREDIT;
      $reference = $form_state
        ->getValue('select_ref');
  }
  $plugin = $this->paymentMethod
    ->getPlugin();
  $result = $plugin
    ->processPayment($this->order, $form_state
    ->getValue('amount'), $txn_type, $reference);
  $this->order->payment_details = uc_credit_cache();
  $plugin
    ->orderSave($this->order);
  if ($result) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('The credit card was processed successfully. See the admin comments for more details.'));
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('There was an error processing the credit card. See the admin comments for details.'));
  }
  $form_state
    ->setRedirect('entity.uc_order.canonical', [
    'uc_order' => $this->order
      ->id(),
  ]);
}