You are here

public function PaymentMethodPane::view in Ubercart 8.4

Returns the contents of a checkout pane.

Parameters

\Drupal\uc_order\OrderInterface $order: The order that 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 array, with an optional '#description' key to provide help text for the pane.

Overrides CheckoutPanePluginInterface::view

File

payment/uc_payment/src/Plugin/Ubercart/CheckoutPane/PaymentMethodPane.php, line 63

Class

PaymentMethodPane
Allows the user to select a payment method and preview the line items.

Namespace

Drupal\uc_payment\Plugin\Ubercart\CheckoutPane

Code

public function view(OrderInterface $order, array $form, FormStateInterface $form_state) {
  $contents['#attached']['library'][] = 'uc_payment/uc_payment.styles';
  if ($this->configuration['show_preview']) {
    $contents['line_items'] = [
      '#theme' => 'uc_payment_totals',
      '#order' => $order,
      '#weight' => -20,
    ];
  }

  // Ensure that the form builder uses #default_value to determine which
  // button should be selected after an ajax submission. This is
  // necessary because the previously selected value may have become
  // unavailable, which would result in an invalid selection.
  $input = $form_state
    ->getUserInput();
  unset($input['panes']['payment']['payment_method']);
  $form_state
    ->setUserInput($input);
  $options = [];
  $methods = PaymentMethod::loadMultiple();
  uasort($methods, 'Drupal\\uc_payment\\Entity\\PaymentMethod::sort');
  foreach ($methods as $method) {

    // $set = rules_config_load('uc_payment_method_' . $method['id']);
    // if ($set && !$set->execute($order)) {
    //   continue;
    // }
    if ($method
      ->status()) {
      $options[$method
        ->id()] = $method
        ->getDisplayLabel();
    }
  }
  \Drupal::moduleHandler()
    ->alter('uc_payment_method_checkout', $options, $order);
  if (!$options) {
    $contents['#description'] = $this
      ->t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.');
    $options[''] = $this
      ->t('No payment methods available');
  }
  elseif (count($options) > 1) {
    $contents['#description'] = $this
      ->t('Select a payment method from the following options.');
  }
  if (!$order
    ->getPaymentMethodId() || !isset($options[$order
    ->getPaymentMethodId()])) {
    $order
      ->setPaymentMethodId(key($options));
  }
  $contents['payment_method'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Payment method'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => $order
      ->getPaymentMethodId(),
    '#disabled' => count($options) == 1,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxRender',
      ],
      'wrapper' => 'payment-details',
      'progress' => [
        'type' => 'throbber',
      ],
    ],
  ];

  // If there are no payment methods available, this will be ''.
  if ($order
    ->getPaymentMethodId()) {
    $plugin = $this->paymentMethodManager
      ->createFromOrder($order);
    $definition = $plugin
      ->getPluginDefinition();
    $contents['details'] = [
      '#prefix' => '<div id="payment-details" class="clearfix ' . Html::cleanCssIdentifier('payment-details-' . $definition['id']) . '">',
      '#markup' => $this
        ->t('Continue with checkout to complete payment.'),
      '#suffix' => '</div>',
    ];
    try {
      $details = $plugin
        ->cartDetails($order, $form, $form_state);
      if ($details) {
        unset($contents['details']['#markup']);
        $contents['details'] += $details;
      }
    } catch (PluginException $e) {
    }
  }
  return $contents;
}