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

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