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

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