vendor/shopware/core/Checkout/Payment/Cart/PaymentHandler/PaymentHandlerRegistry.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Payment\Cart\PaymentHandler;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  5. use Shopware\Core\Framework\App\Payment\Handler\AppAsyncPaymentHandler;
  6. use Shopware\Core\Framework\App\Payment\Handler\AppSyncPaymentHandler;
  7. use Shopware\Core\Framework\Feature;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Symfony\Contracts\Service\ServiceProviderInterface;
  11. #[Package('checkout')]
  12. class PaymentHandlerRegistry
  13. {
  14.     /**
  15.      * @var array<string, PaymentHandlerInterface>
  16.      */
  17.     private array $handlers = [];
  18.     private Connection $connection;
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         ServiceProviderInterface $syncHandlers,
  24.         ServiceProviderInterface $asyncHandlers,
  25.         ServiceProviderInterface $preparedHandlers,
  26.         ServiceProviderInterface $refundHandlers,
  27.         Connection $connection
  28.     ) {
  29.         $this->connection $connection;
  30.         foreach (\array_keys($syncHandlers->getProvidedServices()) as $serviceId) {
  31.             $handler $syncHandlers->get($serviceId);
  32.             $this->handlers[(string) $serviceId] = $handler;
  33.         }
  34.         foreach (\array_keys($asyncHandlers->getProvidedServices()) as $serviceId) {
  35.             $handler $asyncHandlers->get($serviceId);
  36.             $this->handlers[(string) $serviceId] = $handler;
  37.         }
  38.         foreach (\array_keys($preparedHandlers->getProvidedServices()) as $serviceId) {
  39.             $handler $preparedHandlers->get($serviceId);
  40.             $this->handlers[(string) $serviceId] = $handler;
  41.         }
  42.         foreach (\array_keys($refundHandlers->getProvidedServices()) as $serviceId) {
  43.             $handler $refundHandlers->get($serviceId);
  44.             $this->handlers[(string) $serviceId] = $handler;
  45.         }
  46.     }
  47.     /**
  48.      * @deprecated tag:v6.5.0 - Will be removed. Use getPaymentMethodHandler instead.
  49.      *
  50.      * @return PaymentHandlerInterface|null
  51.      */
  52.     public function getHandler(string $handlerId)
  53.     {
  54.         Feature::triggerDeprecationOrThrow(
  55.             'v6.5.0.0',
  56.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getPaymentMethodHandler()')
  57.         );
  58.         if (!\array_key_exists($handlerId$this->handlers)) {
  59.             return null;
  60.         }
  61.         return $this->handlers[$handlerId];
  62.     }
  63.     /**
  64.      * @deprecated tag:v6.5.0 Will be removed. Use getPaymentMethodHandler instead.
  65.      *
  66.      * @return PaymentHandlerInterface|null
  67.      */
  68.     public function getHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod)
  69.     {
  70.         Feature::triggerDeprecationOrThrow(
  71.             'v6.5.0.0',
  72.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getPaymentMethodHandler()')
  73.         );
  74.         return $this->getPaymentMethodHandler($paymentMethod->getId());
  75.     }
  76.     public function getPaymentMethodHandler(
  77.         string $paymentMethodId,
  78.         ?string $expectedHandlerType null
  79.     ): ?PaymentHandlerInterface {
  80.         $statement $this->connection->createQueryBuilder()
  81.             ->select('
  82.                 payment_method.handler_identifier,
  83.                 app_payment_method.id as app_payment_method_id,
  84.                 app_payment_method.pay_url,
  85.                 app_payment_method.finalize_url,
  86.                 app_payment_method.capture_url,
  87.                 app_payment_method.validate_url,
  88.                 app_payment_method.refund_url
  89.             ')
  90.             ->from('payment_method')
  91.             ->leftJoin(
  92.                 'payment_method',
  93.                 'app_payment_method',
  94.                 'app_payment_method',
  95.                 'payment_method.id = app_payment_method.payment_method_id'
  96.             )
  97.             ->andWhere('payment_method.id = :paymentMethodId')
  98.             ->setParameter('paymentMethodId'Uuid::fromHexToBytes($paymentMethodId))
  99.             ->execute();
  100.         $result $statement->fetchAssociative();
  101.         if (!$result || !\array_key_exists('handler_identifier'$result)) {
  102.             return null;
  103.         }
  104.         // app payment method is set: we need to resolve an app handler
  105.         if (isset($result['app_payment_method_id'])) {
  106.             return $this->resolveAppPaymentMethodHandler($result$expectedHandlerType);
  107.         }
  108.         $handlerIdentifier $result['handler_identifier'];
  109.         if (!\array_key_exists($handlerIdentifier$this->handlers)) {
  110.             return null;
  111.         }
  112.         $handler $this->handlers[$handlerIdentifier];
  113.         // a specific handler type was requested
  114.         if ($expectedHandlerType !== null && !\is_a($handler$expectedHandlerTypetrue)) {
  115.             return null;
  116.         }
  117.         return $this->handlers[$handlerIdentifier];
  118.     }
  119.     public function getSyncPaymentHandler(string $paymentMethodId): ?SynchronousPaymentHandlerInterface
  120.     {
  121.         $handler $this->getPaymentMethodHandler($paymentMethodIdSynchronousPaymentHandlerInterface::class);
  122.         if (!$handler instanceof SynchronousPaymentHandlerInterface) {
  123.             return null;
  124.         }
  125.         return $handler;
  126.     }
  127.     public function getAsyncPaymentHandler(string $paymentMethodId): ?AsynchronousPaymentHandlerInterface
  128.     {
  129.         $handler $this->getPaymentMethodHandler($paymentMethodIdAsynchronousPaymentHandlerInterface::class);
  130.         if (!$handler instanceof AsynchronousPaymentHandlerInterface) {
  131.             return null;
  132.         }
  133.         return $handler;
  134.     }
  135.     public function getPreparedPaymentHandler(string $paymentMethodId): ?PreparedPaymentHandlerInterface
  136.     {
  137.         $handler $this->getPaymentMethodHandler($paymentMethodIdPreparedPaymentHandlerInterface::class);
  138.         if (!$handler instanceof PreparedPaymentHandlerInterface) {
  139.             return null;
  140.         }
  141.         return $handler;
  142.     }
  143.     public function getRefundPaymentHandler(string $paymentMethodId): ?RefundPaymentHandlerInterface
  144.     {
  145.         $handler $this->getPaymentMethodHandler($paymentMethodIdRefundPaymentHandlerInterface::class);
  146.         if (!$handler instanceof RefundPaymentHandlerInterface) {
  147.             return null;
  148.         }
  149.         return $handler;
  150.     }
  151.     /**
  152.      * @deprecated tag:v6.5.0 Will be removed. Use getSyncPaymentHandler instead.
  153.      */
  154.     public function getSyncHandler(string $handlerId): ?SynchronousPaymentHandlerInterface
  155.     {
  156.         Feature::triggerDeprecationOrThrow(
  157.             'v6.5.0.0',
  158.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getSyncPaymentHandler()')
  159.         );
  160.         $handler $this->getPaymentMethodHandler($handlerId);
  161.         if (!$handler || !$handler instanceof SynchronousPaymentHandlerInterface) {
  162.             return null;
  163.         }
  164.         return $handler;
  165.     }
  166.     /**
  167.      * @deprecated tag:v6.5.0 Will be removed. Use getAsyncPaymentHandler instead.
  168.      */
  169.     public function getAsyncHandler(string $handlerId): ?AsynchronousPaymentHandlerInterface
  170.     {
  171.         Feature::triggerDeprecationOrThrow(
  172.             'v6.5.0.0',
  173.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getAsyncPaymentHandler()')
  174.         );
  175.         $handler $this->getPaymentMethodHandler($handlerId);
  176.         if (!$handler || !$handler instanceof AsynchronousPaymentHandlerInterface) {
  177.             return null;
  178.         }
  179.         return $handler;
  180.     }
  181.     /**
  182.      * @deprecated tag:v6.5.0 Will be removed. Use getSyncPaymentHandler instead.
  183.      */
  184.     public function getSyncHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod): ?SynchronousPaymentHandlerInterface
  185.     {
  186.         Feature::triggerDeprecationOrThrow(
  187.             'v6.5.0.0',
  188.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getSyncPaymentHandler()')
  189.         );
  190.         return $this->getSyncPaymentHandler($paymentMethod->getId());
  191.     }
  192.     /**
  193.      * @deprecated tag:v6.5.0 Will be removed. Use getAsyncPaymentHandler instead.
  194.      */
  195.     public function getAsyncHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod): ?AsynchronousPaymentHandlerInterface
  196.     {
  197.         Feature::triggerDeprecationOrThrow(
  198.             'v6.5.0.0',
  199.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getAsyncPaymentHandler()')
  200.         );
  201.         return $this->getAsyncPaymentHandler($paymentMethod->getId());
  202.     }
  203.     /**
  204.      * @deprecated tag:v6.5.0 Will be removed. Use getPreparedPaymentHandler instead.
  205.      */
  206.     public function getPreparedHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod): ?PreparedPaymentHandlerInterface
  207.     {
  208.         Feature::triggerDeprecationOrThrow(
  209.             'v6.5.0.0',
  210.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getPreparedPaymentHandler()')
  211.         );
  212.         return $this->getPreparedPaymentHandler($paymentMethod->getId());
  213.     }
  214.     /**
  215.      * @deprecated tag:v6.5.0 Will be removed. Use getRefundPaymentHandler instead.
  216.      */
  217.     public function getRefundHandlerForPaymentMethod(PaymentMethodEntity $paymentMethod): ?RefundPaymentHandlerInterface
  218.     {
  219.         Feature::triggerDeprecationOrThrow(
  220.             'v6.5.0.0',
  221.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0''getRefundPaymentHandler()')
  222.         );
  223.         return $this->getRefundPaymentHandler($paymentMethod->getId());
  224.     }
  225.     private function resolveAppPaymentMethodHandler(
  226.         array $appPaymentMethod,
  227.         ?string $expectedHandlerType null
  228.     ): ?PaymentHandlerInterface {
  229.         // validate if prepared and refund handlers have all information set
  230.         if ($expectedHandlerType) {
  231.             if (\is_a(PreparedPaymentHandlerInterface::class, $expectedHandlerTypetrue)) {
  232.                 if (empty($appPaymentMethod['capture_url']) || empty($appPaymentMethod['validate_url'])) {
  233.                     return null;
  234.                 }
  235.             }
  236.             if (\is_a(RefundPaymentHandlerInterface::class, $expectedHandlerTypetrue)) {
  237.                 if (empty($appPaymentMethod['refund_url'])) {
  238.                     return null;
  239.                 }
  240.             }
  241.         }
  242.         if (empty($appPaymentMethod['finalize_url'])) {
  243.             return $this->handlers[AppSyncPaymentHandler::class] ?? null;
  244.         }
  245.         return $this->handlers[AppAsyncPaymentHandler::class] ?? null;
  246.     }
  247. }