custom/plugins/SwagPayPal/src/Checkout/SalesChannel/FilteredPaymentMethodRoute.php line 153

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Checkout\SalesChannel;
  8. use OpenApi\Annotations as OA;
  9. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  12. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  13. use Shopware\Core\Checkout\Payment\SalesChannel\PaymentMethodRouteResponse;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\Routing\Annotation\Entity;
  18. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  19. use Shopware\Core\Framework\Routing\Annotation\Since;
  20. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  21. use Swag\PayPal\Checkout\Cart\Service\CartPriceService;
  22. use Swag\PayPal\Checkout\Cart\Service\ExcludedProductValidator;
  23. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  24. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  25. use Swag\PayPal\Util\Availability\AvailabilityService;
  26. use Swag\PayPal\Util\Lifecycle\Method\PaymentMethodDataRegistry;
  27. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\RequestStack;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. /**
  32.  * @RouteScope(scopes={"store-api"})
  33.  */
  34. class FilteredPaymentMethodRoute extends AbstractPaymentMethodRoute
  35. {
  36.     private AbstractPaymentMethodRoute $decorated;
  37.     private PaymentMethodDataRegistry $methodDataRegistry;
  38.     private SettingsValidationServiceInterface $settingsValidationService;
  39.     private CartService $cartService;
  40.     private CartPriceService $cartPriceService;
  41.     private RequestStack $requestStack;
  42.     private ExcludedProductValidator $excludedProductValidator;
  43.     private AvailabilityService $availabilityService;
  44.     private EntityRepositoryInterface $orderRepository;
  45.     public function __construct(
  46.         AbstractPaymentMethodRoute $decorated,
  47.         PaymentMethodDataRegistry $methodDataRegistry,
  48.         SettingsValidationServiceInterface $settingsValidationService,
  49.         CartService $cartService,
  50.         CartPriceService $cartPriceService,
  51.         ExcludedProductValidator $excludedProductValidator,
  52.         RequestStack $requestStack,
  53.         AvailabilityService $availabilityService,
  54.         EntityRepositoryInterface $orderRepository
  55.     ) {
  56.         $this->decorated $decorated;
  57.         $this->methodDataRegistry $methodDataRegistry;
  58.         $this->settingsValidationService $settingsValidationService;
  59.         $this->cartService $cartService;
  60.         $this->cartPriceService $cartPriceService;
  61.         $this->excludedProductValidator $excludedProductValidator;
  62.         $this->requestStack $requestStack;
  63.         $this->availabilityService $availabilityService;
  64.         $this->orderRepository $orderRepository;
  65.     }
  66.     public function getDecorated(): AbstractPaymentMethodRoute
  67.     {
  68.         return $this->decorated;
  69.     }
  70.     /**
  71.      * @Since("6.2.0.0")
  72.      * @Entity("payment_method")
  73.      * @OA\Post (
  74.      *      path="/payment-method",
  75.      *      summary="Loads all available payment methods",
  76.      *      operationId="readPaymentMethod",
  77.      *      tags={"Store API", "Payment Method"},
  78.      *      @OA\Parameter(name="Api-Basic-Parameters"),
  79.      *      @OA\RequestBody(
  80.      *          required=true,
  81.      *          @OA\JsonContent(
  82.      *              @OA\Property(property="onlyAvailable", description="List only available", type="boolean")
  83.      *          )
  84.      *      ),
  85.      *      @OA\Response(
  86.      *          response="200",
  87.      *          description="",
  88.      *          @OA\JsonContent(type="object",
  89.      *              @OA\Property(
  90.      *                  property="total",
  91.      *                  type="integer",
  92.      *                  description="Total amount"
  93.      *              ),
  94.      *              @OA\Property(
  95.      *                  property="aggregations",
  96.      *                  type="object",
  97.      *                  description="aggregation result"
  98.      *              ),
  99.      *              @OA\Property(
  100.      *                  property="elements",
  101.      *                  type="array",
  102.      *                  @OA\Items(ref="#/components/schemas/PaymentMethod")
  103.      *              )
  104.      *       )
  105.      *    )
  106.      * )
  107.      * @Route("/store-api/payment-method", name="store-api.payment.method", methods={"GET", "POST"})
  108.      */
  109.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): PaymentMethodRouteResponse
  110.     {
  111.         $response $this->getDecorated()->load($request$context$criteria);
  112.         if (!$request->query->getBoolean('onlyAvailable') && !$request->request->getBoolean('onlyAvailable')) {
  113.             return $response;
  114.         }
  115.         try {
  116.             $this->settingsValidationService->validate($context->getSalesChannelId());
  117.         } catch (PayPalSettingsInvalidException $e) {
  118.             $this->removeAllPaymentMethods($response->getPaymentMethods());
  119.             return $response;
  120.         }
  121.         $cart $this->cartService->getCart($context->getToken(), $context);
  122.         if ($this->cartPriceService->isZeroValueCart($cart)) {
  123.             $this->removeAllPaymentMethods($response->getPaymentMethods());
  124.             return $response;
  125.         }
  126.         if ($this->excludedProductValidator->cartContainsExcludedProduct($cart$context)) {
  127.             $this->removeAllPaymentMethods($response->getPaymentMethods());
  128.             return $response;
  129.         }
  130.         try {
  131.             $ineligiblePaymentMethods $this->requestStack->getSession()->get(MethodEligibilityRoute::SESSION_KEY);
  132.             if (\is_array($ineligiblePaymentMethods)) {
  133.                 $this->removePaymentMethods($response->getPaymentMethods(), $ineligiblePaymentMethods);
  134.             }
  135.         } catch (SessionNotFoundException $e) {
  136.         }
  137.         $order $this->checkOrder($request$context->getContext());
  138.         if ($order !== null) {
  139.             $this->removePaymentMethods(
  140.                 $response->getPaymentMethods(),
  141.                 $this->availabilityService->filterPaymentMethodsByOrder($response->getPaymentMethods(), $cart$order$context)
  142.             );
  143.             return $response;
  144.         }
  145.         $this->removePaymentMethods(
  146.             $response->getPaymentMethods(),
  147.             $this->availabilityService->filterPaymentMethods($response->getPaymentMethods(), $cart$context)
  148.         );
  149.         return $response;
  150.     }
  151.     /**
  152.      * @param string[] $handlers
  153.      */
  154.     private function removePaymentMethods(PaymentMethodCollection $paymentMethods, array $handlers): void
  155.     {
  156.         foreach ($paymentMethods as $paymentMethod) {
  157.             if (\in_array($paymentMethod->getHandlerIdentifier(), $handlerstrue)) {
  158.                 $paymentMethods->remove($paymentMethod->getId());
  159.             }
  160.         }
  161.     }
  162.     private function removeAllPaymentMethods(PaymentMethodCollection $paymentMethods): void
  163.     {
  164.         foreach ($paymentMethods as $paymentMethod) {
  165.             if ($this->methodDataRegistry->isPayPalPaymentMethod($paymentMethod)) {
  166.                 $paymentMethods->remove($paymentMethod->getId());
  167.             }
  168.         }
  169.     }
  170.     private function checkOrder(Request $requestContext $context): ?OrderEntity
  171.     {
  172.         $orderId $request->attributes->getAlnum('orderId');
  173.         if ($orderId) {
  174.             return $this->orderRepository->search(new Criteria([$orderId]), $context)->first();
  175.         }
  176.         $actualRequest $this->requestStack->getCurrentRequest();
  177.         if (!$actualRequest) {
  178.             return null;
  179.         }
  180.         $orderId $actualRequest->attributes->getAlnum('orderId');
  181.         if (!$orderId) {
  182.             return null;
  183.         }
  184.         return $this->orderRepository->search(new Criteria([$orderId]), $context)->first();
  185.     }
  186. }