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

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