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

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