You are here

class PaymentListBuilder in Commerce Core 8.2

Defines the list builder for payments.

Hierarchy

Expanded class hierarchy of PaymentListBuilder

File

modules/payment/src/PaymentListBuilder.php, line 18

Namespace

Drupal\commerce_payment
View source
class PaymentListBuilder extends EntityListBuilder {

  /**
   * The currency formatter.
   *
   * @var \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface
   */
  protected $currencyFormatter;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The date formatter.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * {@inheritdoc}
   */
  protected $entitiesKey = 'payments';

  /**
   * {@inheritdoc}
   *
   * Set limit to false so the list is not paginated.
   */
  protected $limit = FALSE;

  /**
   * The order.
   */
  protected $order;

  /**
   * Constructs a new PaymentListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface $currency_formatter
   *   The currency formatter.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrencyFormatterInterface $currency_formatter, RouteMatchInterface $route_match, DateFormatterInterface $date_formatter) {
    parent::__construct($entity_type, $storage);
    $this->currencyFormatter = $currency_formatter;
    $this->routeMatch = $route_match;
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('commerce_price.currency_formatter'), $container
      ->get('current_route_match'), $container
      ->get('date.formatter'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'commerce_payments';
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    $this->order = $this->routeMatch
      ->getParameter('commerce_order');
    return $this->storage
      ->loadMultipleByOrder($this->order);
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultOperations(EntityInterface $entity) {
    $operations = [];

    /** @var \Drupal\commerce_payment\Entity\PaymentInterface $entity */
    $payment_gateway = $entity
      ->getPaymentGateway();

    // Add the gateway-specific operations.
    if ($payment_gateway) {
      $operations = $payment_gateway
        ->getPlugin()
        ->buildPaymentOperations($entity);

      // Filter out operations that aren't allowed.
      $operations = array_filter($operations, function ($operation) {
        return !empty($operation['access']);
      });

      // Build the url for each operation.
      $base_route_parameters = [
        'commerce_payment' => $entity
          ->id(),
        'commerce_order' => $entity
          ->getOrderId(),
      ];
      foreach ($operations as $operation_id => $operation) {
        $route_parameters = $base_route_parameters + [
          'operation' => $operation_id,
        ];
        $operation['url'] = new Url('entity.commerce_payment.operation_form', $route_parameters);
        $operations[$operation_id] = $operation;
      }
    }

    // Add the non-gateway-specific operations.
    if ($entity
      ->access('delete')) {
      $operations['delete'] = [
        'title' => $this
          ->t('Delete'),
        'weight' => 100,
        'url' => $entity
          ->toUrl('delete-form'),
      ];
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this
      ->t('Payment');
    $header['state'] = $this
      ->t('State');
    $header['payment_gateway'] = $this
      ->t('Payment gateway');
    $header['authorized'] = $this
      ->t('Authorized');
    $header['completed'] = $this
      ->t('Completed');
    $header['avs_response'] = '';
    $header['remote_id'] = $this
      ->t('Remote ID');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /** @var \Drupal\commerce_payment\Entity\PaymentInterface $entity */
    $amount = $entity
      ->getAmount();
    $formatted_amount = $this->currencyFormatter
      ->format($amount
      ->getNumber(), $amount
      ->getCurrencyCode());
    $refunded_amount = $entity
      ->getRefundedAmount();
    if ($refunded_amount && !$refunded_amount
      ->isZero()) {
      $formatted_amount .= ' ' . $this
        ->t('Refunded:') . ' ';
      $formatted_amount .= $this->currencyFormatter
        ->format($refunded_amount
        ->getNumber(), $refunded_amount
        ->getCurrencyCode());
    }
    $payment_gateway = $entity
      ->getPaymentGateway();
    $row['label'] = $formatted_amount;
    $row['state'] = $entity
      ->getState()
      ->getLabel();
    $row['payment_gateway'] = $payment_gateway ? $payment_gateway
      ->label() : $this
      ->t('N/A');
    foreach ([
      'authorized',
      'completed',
    ] as $field) {
      if ($entity
        ->get($field)
        ->isEmpty()) {
        $row[$field] = '';
        continue;
      }
      $row[$field] = $this->dateFormatter
        ->format($entity
        ->get($field)->value, 'short');
    }

    // Add the AVS response code label beneath the gateway name if it exists.
    if ($avs_response_code = $entity
      ->getAvsResponseCode()) {
      $row['avs_response'] = $this
        ->t('AVS response: [@code] @label', [
        '@code' => $avs_response_code,
        '@label' => $entity
          ->getAvsResponseCodeLabel(),
      ]);
    }
    else {
      $row['avs_response'] = '';
    }
    $row['remote_id'] = $entity
      ->getRemoteId() ?: $this
      ->t('N/A');
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   *
   * Make the payment list page themeable. Add total_paid and order_balance
   * elements.
   */
  public function render() {
    $build = parent::render();
    $build['payment_total_summary'] = [
      '#theme' => 'commerce_payment_total_summary',
      '#order_entity' => $this->order,
    ];
    return $build;
  }

}

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
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. 4
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PaymentListBuilder::$currencyFormatter protected property The currency formatter.
PaymentListBuilder::$dateFormatter protected property The date formatter.
PaymentListBuilder::$entitiesKey protected property
PaymentListBuilder::$limit protected property Set limit to false so the list is not paginated. Overrides EntityListBuilder::$limit
PaymentListBuilder::$order protected property The order.
PaymentListBuilder::$routeMatch protected property The current route match.
PaymentListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
PaymentListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
PaymentListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
PaymentListBuilder::getDefaultOperations protected function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations
PaymentListBuilder::getFormId public function
PaymentListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilder::load
PaymentListBuilder::render public function Make the payment list page themeable. Add total_paid and order_balance elements. Overrides EntityListBuilder::render
PaymentListBuilder::__construct public function Constructs a new PaymentListBuilder object. Overrides EntityListBuilder::__construct
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.