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

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