commerce-8.x-2.8/modules/payment/commerce_payment.module

modules/payment/commerce_payment.module
<?php

/**
 * @file
 * Provides payment functionality.
 */

use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\HasPaymentInstructionsInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;

/**
 * Implements hook_commerce_checkout_pane_info_alter().
 */
function commerce_payment_commerce_checkout_pane_info_alter(&$definitions) {
  // The payment_information pane replaces the billing_information one.
  unset($definitions['billing_information']);
}

/**
 * Implements hook_entity_base_field_info().
 */
function commerce_payment_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'commerce_order') {
    $fields['payment_gateway'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Payment gateway'))
      ->setDescription(t('The payment gateway.'))
      ->setSetting('target_type', 'commerce_payment_gateway');

    $fields['payment_method'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Payment method'))
      ->setDescription(t('The payment method.'))
      ->setSetting('target_type', 'commerce_payment_method');

    return $fields;
  }
}

/**
 * Implements hook_entity_operation().
 */
function commerce_payment_entity_operation(EntityInterface $entity) {
  if ($entity->getEntityTypeId() === 'commerce_order') {
    if (\Drupal::currentUser()->hasPermission('administer commerce_payment')) {
      $operations = [];
      $operations['payments'] = [
        'title' => t('Payments'),
        'url' => Url::fromRoute('entity.commerce_payment.collection', [
          'commerce_order' => $entity->id(),
        ]),
        'weight' => 50,
      ];
      return $operations;
    }
  }
}

/**
 * Implements hook_theme().
 */
function commerce_payment_theme() {
  return [
    'commerce_payment_method' => [
      'render element' => 'elements',
    ],
    'commerce_payment_method__credit_card' => [
      'base hook' => 'commerce_payment_method',
      'render element' => 'elements',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_commerce_payment_method().
 */
function commerce_payment_theme_suggestions_commerce_payment_method(array $variables) {
  return _commerce_entity_theme_suggestions('commerce_payment_method', $variables);
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function commerce_payment_commerce_payment_method_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($entity->bundle() == 'credit_card') {
    $build['#attached']['library'][] = 'commerce_payment/payment_method_icons';
  }
}

/**
 * Prepares variables for payment method templates.
 *
 * Default template: commerce-payment-method.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing rendered fields.
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_commerce_payment_method(array &$variables) {
  /** @var Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = $variables['elements']['#commerce_payment_method'];

  $variables['payment_method_entity'] = $payment_method;
  $variables['payment_method'] = [
    // The label is generated dynamically, so it's not present in 'elements'.
    'label' => [
      '#markup' => $payment_method->label(),
    ],
  ];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['payment_method'][$key] = $variables['elements'][$key];
  }
}

/**
 * Implements hook_preprocess_commerce_order().
 */
function commerce_payment_preprocess_commerce_order(&$variables) {
  /** @var Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $variables['elements']['#commerce_order'];
  if (!$order->get('payment_method')->isEmpty()) {
    $variables['payment_method'] = [
      '#markup' => $order->get('payment_method')->first()->entity->label(),
    ];
  }
}

/**
 * Implements hook_preprocess_commerce_checkout_completion_message().
 */
function commerce_payment_preprocess_commerce_checkout_completion_message(&$variables) {
  /** @var Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $variables['order_entity'];
  if ($order->get('payment_gateway')->isEmpty()) {
    return;
  }

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = $order->get('payment_gateway')->entity;
  /** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\HasPaymentInstructionsInterface $payment_gateway_plugin */
  $payment_gateway_plugin = $payment_gateway->getPlugin();
  if ($payment_gateway_plugin instanceof HasPaymentInstructionsInterface) {
    $payment_storage = \Drupal::entityTypeManager()->getStorage('commerce_payment');
    $payments = $payment_storage->loadMultipleByOrder($order);
    $payments = array_filter($payments, function ($payment) use ($payment_gateway) {
      return $payment->getPaymentGatewayId() == $payment_gateway->id();
    });
    $payment = reset($payments);
    if ($payment) {
      $variables['payment_instructions'] = $payment_gateway_plugin->buildPaymentInstructions($payment);
    }
  }
}

/**
 * Implements hook_preprocess_commerce_order_receipt().
 */
function commerce_payment_preprocess_commerce_order_receipt(&$variables) {
  /** @var Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $variables['order_entity'];
  if (!$order->get('payment_method')->isEmpty()) {
    $variables['payment_method'] = [
      '#markup' => $order->get('payment_method')->first()->entity->label(),
    ];
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc