PaymentGatewayStorage.php in Commerce Core 8.2
File
modules/payment/src/PaymentGatewayStorage.php
View source
<?php
namespace Drupal\commerce_payment;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Event\FilterPaymentGatewaysEvent;
use Drupal\commerce_payment\Event\PaymentEvents;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PaymentGatewayStorage extends ConfigEntityStorage implements PaymentGatewayStorageInterface {
protected $eventDispatcher;
public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EventDispatcherInterface $event_dispatcher) {
parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager, $memory_cache);
$this->eventDispatcher = $event_dispatcher;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('config.factory'), $container
->get('uuid'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('event_dispatcher'));
}
public function loadForUser(UserInterface $account) {
$payment_gateways = $this
->loadByProperties([
'status' => TRUE,
]);
$payment_gateways = array_filter($payment_gateways, function ($payment_gateway) {
return $payment_gateway
->getPlugin() instanceof SupportsStoredPaymentMethodsInterface;
});
$payment_gateway = reset($payment_gateways);
return $payment_gateway;
}
public function loadMultipleForOrder(OrderInterface $order) {
$payment_gateways = $this
->loadByProperties([
'status' => TRUE,
]);
$event = new FilterPaymentGatewaysEvent($payment_gateways, $order);
$this->eventDispatcher
->dispatch(PaymentEvents::FILTER_PAYMENT_GATEWAYS, $event);
$payment_gateways = $event
->getPaymentGateways();
foreach ($payment_gateways as $payment_gateway_id => $payment_gateway) {
if (!$payment_gateway
->applies($order)) {
unset($payment_gateways[$payment_gateway_id]);
}
}
uasort($payment_gateways, [
$this->entityType
->getClass(),
'sort',
]);
return $payment_gateways;
}
}