You are here

class Payment in Ubercart 8.4

Specify and collect payment for an order.

Plugin annotation


@UbercartOrderPane(
  id = "payment",
  title = @Translation("Payment"),
  weight = 4,
)

Hierarchy

Expanded class hierarchy of Payment

3 string references to 'Payment'
uc_payment.info.yml in payment/uc_payment/uc_payment.info.yml
payment/uc_payment/uc_payment.info.yml
uc_payment.rules.events.yml in payment/uc_payment/uc_payment.rules.events.yml
payment/uc_payment/uc_payment.rules.events.yml
uc_payment_views_data in payment/uc_payment/uc_payment.views.inc
Implements hook_views_data().

File

payment/uc_payment/src/Plugin/Ubercart/OrderPane/Payment.php, line 23

Namespace

Drupal\uc_payment\Plugin\Ubercart\OrderPane
View source
class Payment extends EditableOrderPanePluginBase {

  /**
   * {@inheritdoc}
   */
  public function getClasses() {
    return [
      'pos-left',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function view(OrderInterface $order, $view_mode) {
    if ($view_mode != 'customer') {
      $build['balance'] = [
        '#markup' => $this
          ->t('Balance: @balance', [
          '@balance' => uc_currency_format(uc_payment_balance($order)),
        ]),
      ];
      $account = \Drupal::currentUser();
      if ($account
        ->hasPermission('view payments')) {
        $build['view_payments'] = [
          '#type' => 'link',
          '#prefix' => ' (',
          '#title' => $this
            ->t('View'),
          '#url' => Url::fromRoute('uc_payments.order_payments', [
            'uc_order' => $order
              ->id(),
          ]),
          '#suffix' => ')',
        ];
      }
      $method = \Drupal::service('plugin.manager.uc_payment.method')
        ->createFromOrder($order);
      $build['method'] = [
        '#markup' => $this
          ->t('Method: @payment_method', [
          '@payment_method' => $method
            ->cartReviewTitle(),
        ]),
        '#prefix' => '<br />',
      ];
      $method_output = $method
        ->orderView($order);
      if (!empty($method_output)) {
        $build['output'] = $method_output + [
          '#prefix' => '<br />',
        ];
      }
    }
    else {
      $method = \Drupal::service('plugin.manager.uc_payment.method')
        ->createFromOrder($order);
      $build['method'] = [
        '#markup' => $this
          ->t('Method: @payment_method', [
          '@payment_method' => $method
            ->cartReviewTitle(),
        ]),
      ];
      $method_output = $method
        ->customerView($order);
      if (!empty($method_output)) {
        $build['output'] = $method_output + [
          '#prefix' => '<br />',
        ];
      }
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(OrderInterface $order, array $form, FormStateInterface $form_state) {
    $options = [];
    $methods = PaymentMethod::loadMultiple();
    uasort($methods, 'Drupal\\uc_payment\\Entity\\PaymentMethod::sort');
    foreach ($methods as $method) {
      $options[$method
        ->id()] = $method
        ->label();
    }
    $form['payment_method'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Payment method'),
      '#default_value' => $order
        ->getPaymentMethodId(),
      '#options' => $options,
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxCallback',
        ],
        'progress' => [
          'type' => 'throbber',
        ],
        'wrapper' => 'payment-details',
      ],
    ];

    // An empty <div> for Ajax.
    $form['payment_details'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => 'payment-details',
      ],
      '#tree' => TRUE,
    ];
    $method = $form_state
      ->getValue('payment_method') ?: $order
      ->getPaymentMethodId();
    if ($method && ($details = PaymentMethod::load($method)
      ->getPlugin()
      ->orderEditDetails($order))) {
      if (is_array($details)) {
        $form['payment_details'] += $details;
      }
      else {
        $form['payment_details']['#markup'] = $details;
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(OrderInterface $order, array &$form, FormStateInterface $form_state) {
    $changes['payment_method'] = $form_state
      ->getValue('payment_method');
    $changes['payment_details'] = $form_state
      ->getValue('payment_details') ?: [];
    $order
      ->setPaymentMethodId($changes['payment_method']);
    $method = \Drupal::service('plugin.manager.uc_payment.method')
      ->createFromOrder($order);
    $return = $method
      ->orderEditProcess($order, $form, $form_state);
    if (is_array($return)) {
      $changes['payment_details'] = array_merge($changes['payment_details'], $return);
    }
    $order->payment_details = $changes['payment_details'];
  }

  /**
   * AJAX callback to render the payment method pane.
   */
  public function ajaxCallback($form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $response
      ->addCommand(new ReplaceCommand('#payment-details', trim(drupal_render($form['payment']['payment_details']))));
    $status_messages = [
      '#type' => 'status_messages',
    ];
    $response
      ->addCommand(new PrependCommand('#payment-details', drupal_render($status_messages)));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OrderPanePluginBase::calculateDependencies public function
OrderPanePluginBase::getTitle public function Returns the title of an order pane. Overrides OrderPanePluginInterface::getTitle 2
Payment::ajaxCallback public function AJAX callback to render the payment method pane.
Payment::buildForm public function Form constructor. Overrides EditableOrderPanePluginInterface::buildForm
Payment::getClasses public function Returns the classes used to wrap an order pane. Overrides OrderPanePluginBase::getClasses
Payment::submitForm public function Form submission handler. Overrides EditableOrderPanePluginInterface::submitForm
Payment::view public function Returns the contents of an order pane as a store administrator. Overrides OrderPanePluginInterface::view
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.