You are here

public function CreditCardPaymentMethodBase::cartDetails in Ubercart 8.4

Returns the form or render array to be displayed at checkout.

Parameters

\Drupal\uc_order\OrderInterface $order: The order which is being processed.

array $form: The checkout form array.

\Drupal\Core\Form\FormStateInterface $form_state: The checkout form state array.

Return value

array A form or render array.

Overrides PaymentMethodPluginBase::cartDetails

File

payment/uc_credit/src/CreditCardPaymentMethodBase.php, line 120

Class

CreditCardPaymentMethodBase
Defines a base credit card payment method plugin implementation.

Namespace

Drupal\uc_credit

Code

public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) {
  $build = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'uc-credit-form',
    ],
  ];
  $build['#attached']['library'][] = 'uc_credit/uc_credit.styles';
  $build['cc_policy'] = [
    '#prefix' => '<p>',
    '#markup' => $this
      ->t('Your billing information must match the billing address for the credit card entered below or we will be unable to process your payment.'),
    '#suffix' => '</p>',
  ];
  $order->payment_details = [];

  // Encrypted data in the session is from the user
  // returning from the review page.
  $session = \Drupal::service('session');
  if ($session
    ->has('sescrd')) {
    $order->payment_details = uc_credit_cache($session
      ->get('sescrd'));
    $build['payment_details_data'] = [
      '#type' => 'hidden',
      '#value' => base64_encode($session
        ->get('sescrd')),
    ];
    $session
      ->remove('sescrd');
  }
  elseif (isset($_POST['panes']['payment']['details']['payment_details_data'])) {

    // Copy any encrypted data that was POSTed in.
    $build['payment_details_data'] = [
      '#type' => 'hidden',
      '#value' => $_POST['panes']['payment']['details']['payment_details_data'],
    ];
  }
  $fields = $this
    ->getEnabledFields();
  if (!empty($fields['type'])) {
    $build['cc_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Card type'),
      '#options' => $this
        ->getEnabledTypes(),
      '#default_value' => isset($order->payment_details['cc_type']) ? $order->payment_details['cc_type'] : NULL,
    ];
  }
  if (!empty($fields['owner'])) {
    $build['cc_owner'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Card owner'),
      '#default_value' => isset($order->payment_details['cc_owner']) ? $order->payment_details['cc_owner'] : '',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#size' => 32,
      '#maxlength' => 64,
    ];
  }

  // Set up the default CC number on the credit card form.
  if (!isset($order->payment_details['cc_number'])) {
    $default_num = NULL;
  }
  elseif (!$this
    ->validateCardNumber($order->payment_details['cc_number'])) {

    // Display the number as-is if it does not validate,
    // so it can be corrected.
    $default_num = $order->payment_details['cc_number'];
  }
  else {

    // Otherwise default to the last 4 digits.
    $default_num = $this
      ->t('(Last 4) @digits', [
      '@digits' => substr($order->payment_details['cc_number'], -4),
    ]);
  }
  $build['cc_number'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Card number'),
    '#default_value' => $default_num,
    '#attributes' => [
      'autocomplete' => 'off',
    ],
    '#size' => 20,
    '#maxlength' => 19,
  ];
  if (!empty($fields['start'])) {
    $month = isset($order->payment_details['cc_start_month']) ? $order->payment_details['cc_start_month'] : NULL;
    $year = isset($order->payment_details['cc_start_year']) ? $order->payment_details['cc_start_year'] : NULL;
    $year_range = range(date('Y') - 10, date('Y'));
    $build['cc_start_month'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Start date'),
      '#options' => [
        1 => $this
          ->t('01 - January'),
        2 => $this
          ->t('02 - February'),
        3 => $this
          ->t('03 - March'),
        4 => $this
          ->t('04 - April'),
        5 => $this
          ->t('05 - May'),
        6 => $this
          ->t('06 - June'),
        7 => $this
          ->t('07 - July'),
        8 => $this
          ->t('08 - August'),
        9 => $this
          ->t('09 - September'),
        10 => $this
          ->t('10 - October'),
        11 => $this
          ->t('11 - November'),
        12 => $this
          ->t('12 - December'),
      ],
      '#default_value' => $month,
      '#required' => TRUE,
    ];
    $build['cc_start_year'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Start year'),
      '#title_display' => 'invisible',
      '#options' => array_combine($year_range, $year_range),
      '#default_value' => $year,
      '#field_suffix' => $this
        ->t('(if present)'),
      '#required' => TRUE,
    ];
  }
  $month = isset($order->payment_details['cc_exp_month']) ? $order->payment_details['cc_exp_month'] : 1;
  $year = isset($order->payment_details['cc_exp_year']) ? $order->payment_details['cc_exp_year'] : date('Y');
  $year_range = range(date('Y'), date('Y') + 20);
  $build['cc_exp_month'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Expiration date'),
    '#options' => [
      1 => $this
        ->t('01 - January'),
      2 => $this
        ->t('02 - February'),
      3 => $this
        ->t('03 - March'),
      4 => $this
        ->t('04 - April'),
      5 => $this
        ->t('05 - May'),
      6 => $this
        ->t('06 - June'),
      7 => $this
        ->t('07 - July'),
      8 => $this
        ->t('08 - August'),
      9 => $this
        ->t('09 - September'),
      10 => $this
        ->t('10 - October'),
      11 => $this
        ->t('11 - November'),
      12 => $this
        ->t('12 - December'),
    ],
    '#default_value' => $month,
    '#required' => TRUE,
  ];
  $build['cc_exp_year'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Expiration year'),
    '#title_display' => 'invisible',
    '#options' => array_combine($year_range, $year_range),
    '#default_value' => $year,
    '#field_suffix' => $this
      ->t('(if present)'),
    '#required' => TRUE,
  ];
  if (!empty($fields['issue'])) {

    // Set up the default Issue Number on the credit card form.
    if (empty($order->payment_details['cc_issue'])) {
      $default_card_issue = NULL;
    }
    elseif (!$this
      ->validateIssueNumber($order->payment_details['cc_issue'])) {

      // Display the Issue Number as is if it does not validate so it can be
      // corrected.
      $default_card_issue = $order->payment_details['cc_issue'];
    }
    else {

      // Otherwise mask it with dashes.
      $default_card_issue = str_repeat('-', strlen($order->payment_details['cc_issue']));
    }
    $build['cc_issue'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Issue number'),
      '#default_value' => $default_card_issue,
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#size' => 2,
      '#maxlength' => 2,
      '#field_suffix' => $this
        ->t('(if present)'),
    ];
  }
  if (!empty($fields['cvv'])) {

    // Set up the default CVV on the credit card form.
    if (empty($order->payment_details['cc_cvv'])) {
      $default_cvv = NULL;
    }
    elseif (!$this
      ->validateCvv($order->payment_details['cc_cvv'])) {

      // Display the CVV as is if it does not validate so it can be corrected.
      $default_cvv = $order->payment_details['cc_cvv'];
    }
    else {

      // Otherwise mask it with dashes.
      $default_cvv = str_repeat('-', strlen($order->payment_details['cc_cvv']));
    }
    $build['cc_cvv'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('CVV'),
      '#default_value' => $default_cvv,
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#size' => 4,
      '#maxlength' => 4,
      '#field_suffix' => [
        '#theme' => 'uc_credit_cvv_help',
        '#method' => $order
          ->getPaymentMethodId(),
      ],
    ];
  }
  if (!empty($fields['bank'])) {
    $build['cc_bank'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Issuing bank'),
      '#default_value' => isset($order->payment_details['cc_bank']) ? $order->payment_details['cc_bank'] : '',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#size' => 32,
      '#maxlength' => 64,
    ];
  }
  return $build;
}