custom/plugins/SwagPayPal/src/Checkout/ExpressCheckout/SalesChannel/ExpressCategoryRoute.php line 103

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\ExpressCheckout\SalesChannel;
  8. use OpenApi\Annotations as OA;
  9. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  10. use Shopware\Core\Content\Category\SalesChannel\CategoryRouteResponse;
  11. use Shopware\Core\Framework\Routing\Annotation\Since;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Swag\PayPal\Checkout\ExpressCheckout\ExpressCheckoutSubscriber;
  15. use Swag\PayPal\Checkout\ExpressCheckout\Service\ExpressCheckoutDataServiceInterface;
  16. use Swag\PayPal\Setting\Exception\PayPalSettingsInvalidException;
  17. use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
  18. use Swag\PayPal\Setting\Settings;
  19. use Swag\PayPal\Util\PaymentMethodUtil;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. /**
  23.  * @Route(defaults={"_routeScope"={"store-api"}})
  24.  */
  25. class ExpressCategoryRoute extends AbstractCategoryRoute
  26. {
  27.     private AbstractCategoryRoute $inner;
  28.     private ExpressCheckoutDataServiceInterface $expressCheckoutDataService;
  29.     private SettingsValidationServiceInterface $settingsValidationService;
  30.     private SystemConfigService $systemConfigService;
  31.     private PaymentMethodUtil $paymentMethodUtil;
  32.     /**
  33.      * @internal
  34.      */
  35.     public function __construct(
  36.         AbstractCategoryRoute $inner,
  37.         ExpressCheckoutDataServiceInterface $expressCheckoutDataService,
  38.         SettingsValidationServiceInterface $settingsValidationService,
  39.         SystemConfigService $systemConfigService,
  40.         PaymentMethodUtil $paymentMethodUtil
  41.     ) {
  42.         $this->inner $inner;
  43.         $this->expressCheckoutDataService $expressCheckoutDataService;
  44.         $this->settingsValidationService $settingsValidationService;
  45.         $this->systemConfigService $systemConfigService;
  46.         $this->paymentMethodUtil $paymentMethodUtil;
  47.     }
  48.     public function getDecorated(): AbstractCategoryRoute
  49.     {
  50.         return $this->inner;
  51.     }
  52.     /**
  53.      * @Since("3.3.0")
  54.      *
  55.      * @OA\Post(
  56.      *     path="/category/{categoryId}",
  57.      *     summary="Fetch a single category",
  58.      *     description="This endpoint returns information about the category, as well as a fully resolved (hydrated with mapping values) CMS page, if one is assigned to the category. You can pass slots which should be resolved exclusively.",
  59.      *     operationId="readCategory",
  60.      *     tags={"Store API", "Category"},
  61.      *
  62.      *     @OA\Parameter(
  63.      *         name="categoryId",
  64.      *         description="Identifier of the category to be fetched",
  65.      *
  66.      *         @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  67.      *         in="path",
  68.      *         required=true
  69.      *     ),
  70.      *
  71.      *     @OA\Parameter(
  72.      *         name="slots",
  73.      *         description="Resolves only the given slot identifiers. The identifiers have to be seperated by a '|' character",
  74.      *
  75.      *         @OA\Schema(type="string"),
  76.      *         in="query",
  77.      *     ),
  78.      *
  79.      *     @OA\Parameter(name="Api-Basic-Parameters"),
  80.      *
  81.      *     @OA\Response(
  82.      *          response="200",
  83.      *          description="The loaded category with cms page",
  84.      *
  85.      *          @OA\JsonContent(ref="#/components/schemas/category_flat")
  86.      *     )
  87.      * )
  88.      *
  89.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  90.      */
  91.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  92.     {
  93.         $response $this->inner->load($navigationId$request$context);
  94.         $route $request->attributes->get('_route');
  95.         if (!\is_string($route) || empty($route)) {
  96.             return $response;
  97.         }
  98.         if ($route !== 'frontend.cms.navigation.page') {
  99.             return $response;
  100.         }
  101.         $cmsPage $response->getCategory()->getCmsPage();
  102.         if ($cmsPage === null) {
  103.             return $response;
  104.         }
  105.         $settings $this->checkSettings($context);
  106.         if ($settings === false) {
  107.             return $response;
  108.         }
  109.         $expressCheckoutButtonData $this->expressCheckoutDataService->buildExpressCheckoutButtonData($contexttrue);
  110.         if ($expressCheckoutButtonData === null) {
  111.             return $response;
  112.         }
  113.         $cmsPage->addExtension(
  114.             ExpressCheckoutSubscriber::PAYPAL_EXPRESS_CHECKOUT_BUTTON_DATA_EXTENSION_ID,
  115.             $expressCheckoutButtonData
  116.         );
  117.         return $response;
  118.     }
  119.     private function checkSettings(SalesChannelContext $context): bool
  120.     {
  121.         if ($this->paymentMethodUtil->isPaypalPaymentMethodInSalesChannel($context) === false) {
  122.             return false;
  123.         }
  124.         try {
  125.             $this->settingsValidationService->validate($context->getSalesChannelId());
  126.         } catch (PayPalSettingsInvalidException $e) {
  127.             return false;
  128.         }
  129.         if ($this->systemConfigService->getBool(Settings::ECS_LISTING_ENABLED$context->getSalesChannelId()) === false) {
  130.             return false;
  131.         }
  132.         return true;
  133.     }
  134. }