vendor/shopware/core/Checkout/Payment/DataAbstractionLayer/PaymentHandlerIdentifierSubscriber.php line 30

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Payment\DataAbstractionLayer;
  3. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface;
  4. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\PreparedPaymentHandlerInterface;
  5. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\RefundPaymentHandlerInterface;
  6. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\SynchronousPaymentHandlerInterface;
  7. use Shopware\Core\Checkout\Payment\PaymentEvents;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  13. /**
  14.  * @internal
  15.  */
  16. #[Package('core')]
  17. class PaymentHandlerIdentifierSubscriber implements EventSubscriberInterface
  18. {
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             PaymentEvents::PAYMENT_METHOD_LOADED_EVENT => 'formatHandlerIdentifier',
  23.             'payment_method.partial_loaded' => 'formatHandlerIdentifier',
  24.         ];
  25.     }
  26.     public function formatHandlerIdentifier(EntityLoadedEvent $event): void
  27.     {
  28.         /** @var Entity $entity */
  29.         foreach ($event->getEntities() as $entity) {
  30.             $entity->assign([
  31.                 'shortName' => $this->getShortName($entity),
  32.                 'formattedHandlerIdentifier' => $this->getHandlerIdentifier($entity),
  33.                 'synchronous' => $this->isSynchronous($entity),
  34.                 'asynchronous' => $this->isAsynchronous($entity),
  35.                 'prepared' => $this->isPrepared($entity),
  36.                 'refundable' => $this->isRefundable($entity),
  37.             ]);
  38.         }
  39.     }
  40.     private function getHandlerIdentifier(Entity $entity): string
  41.     {
  42.         $explodedHandlerIdentifier explode('\\', (string) $entity->get('handlerIdentifier'));
  43.         if (\count($explodedHandlerIdentifier) < 2) {
  44.             return $entity->get('handlerIdentifier');
  45.         }
  46.         /** @var string|null $firstHandlerIdentifier */
  47.         $firstHandlerIdentifier array_shift($explodedHandlerIdentifier);
  48.         $lastHandlerIdentifier array_pop($explodedHandlerIdentifier);
  49.         if ($firstHandlerIdentifier === null || $lastHandlerIdentifier === null) {
  50.             return '';
  51.         }
  52.         return 'handler_'
  53.             mb_strtolower($firstHandlerIdentifier)
  54.             . '_'
  55.             mb_strtolower($lastHandlerIdentifier);
  56.     }
  57.     private function isSynchronous(Entity $entity): bool
  58.     {
  59.         if (($app $entity->get('appPaymentMethod')) !== null) {
  60.             /** @var Entity $app */
  61.             return !($app->get('payUrl') && $app->get('finalizeUrl'));
  62.         }
  63.         return \is_a($entity->get('handlerIdentifier'), SynchronousPaymentHandlerInterface::class, true);
  64.     }
  65.     private function isAsynchronous(Entity $entity): bool
  66.     {
  67.         if (($app $entity->get('appPaymentMethod')) !== null) {
  68.             /** @var Entity $app */
  69.             return $app->get('payUrl') && $app->get('finalizeUrl');
  70.         }
  71.         return \is_a($entity->get('handlerIdentifier'), AsynchronousPaymentHandlerInterface::class, true);
  72.     }
  73.     private function getShortName(Entity $entity): string
  74.     {
  75.         $explodedHandlerIdentifier explode('\\', (string) $entity->get('handlerIdentifier'));
  76.         $last $explodedHandlerIdentifier[\count($explodedHandlerIdentifier) - 1];
  77.         return (new CamelCaseToSnakeCaseNameConverter())->normalize($last);
  78.     }
  79.     private function isPrepared(Entity $entity): bool
  80.     {
  81.         if (($app $entity->get('appPaymentMethod')) !== null) {
  82.             /** @var Entity $app */
  83.             return $app->get('validateUrl') && $app->get('captureUrl');
  84.         }
  85.         return \is_a($entity->get('handlerIdentifier'), PreparedPaymentHandlerInterface::class, true);
  86.     }
  87.     private function isRefundable(Entity $entity): bool
  88.     {
  89.         if (($app $entity->get('appPaymentMethod')) !== null) {
  90.             /** @var Entity $app */
  91.             return $app->get('refundUrl') !== null;
  92.         }
  93.         return \is_a($entity->get('handlerIdentifier'), RefundPaymentHandlerInterface::class, true);
  94.     }
  95. }