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

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