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

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