custom/plugins/SwagPayPal/src/Checkout/SalesChannel/MethodEligibilityRoute.php line 107

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 Psr\Log\LoggerInterface;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  12. use Shopware\Core\Framework\Routing\Annotation\Since;
  13. use Shopware\Core\Framework\Routing\Exception\InvalidRequestParameterException;
  14. use Shopware\Core\System\SalesChannel\NoContentResponse;
  15. use Swag\PayPal\Checkout\Payment\Method\ACDCHandler;
  16. use Swag\PayPal\Checkout\Payment\Method\PayLaterHandler;
  17. use Swag\PayPal\Checkout\Payment\Method\SEPAHandler;
  18. use Swag\PayPal\Checkout\Payment\Method\VenmoHandler;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\Session\Session;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. /**
  24.  * @Route(defaults={"_routeScope"={"store-api"}})
  25.  */
  26. class MethodEligibilityRoute extends AbstractMethodEligibilityRoute
  27. {
  28.     public const REMOVABLE_PAYMENT_HANDLERS = [
  29.         'CARD' => ACDCHandler::class,
  30.         'SEPA' => SEPAHandler::class,
  31.         'VENMO' => VenmoHandler::class,
  32.         'PAYLATER' => PayLaterHandler::class,
  33.     ];
  34.     public const SESSION_KEY 'payPalIneligiblePaymentMethods';
  35.     private LoggerInterface $logger;
  36.     /**
  37.      * @internal
  38.      */
  39.     public function __construct(LoggerInterface $logger)
  40.     {
  41.         $this->logger $logger;
  42.     }
  43.     public function getDecorated(): AbstractErrorRoute
  44.     {
  45.         throw new DecorationPatternException(self::class);
  46.     }
  47.     /**
  48.      * @Since("5.1.0")
  49.      *
  50.      * @OA\Post(
  51.      *     path="/store-api/paypal/payment-method-eligibility",
  52.      *     description="Sets ineligible payment methods to be removed from the session",
  53.      *     operationId="setPaymentMethodEligibility",
  54.      *     tags={"Store API", "PayPal"},
  55.      *
  56.      *     @OA\RequestBody(
  57.      *
  58.      *         @OA\JsonContent(
  59.      *
  60.      *             @OA\Property(
  61.      *                 property="paymentMethods",
  62.      *                 type="array",
  63.      *
  64.      *                 @OA\Items(
  65.      *                     type="string",
  66.      *                 ),
  67.      *                 description="List of PayPal payment method identifiers according to constant REMOVABLE_PAYMENT_HANDLERS"
  68.      *             )
  69.      *         )
  70.      *     ),
  71.      *
  72.      *     @OA\Response(
  73.      *          response="204"
  74.      *     )
  75.      * )
  76.      *
  77.      * @Route(
  78.      *     "/store-api/paypal/payment-method-eligibility",
  79.      *     name="store-api.paypal.payment-method-eligibility",
  80.      *     methods={"POST"},
  81.      *     defaults={"XmlHttpRequest"=true}
  82.      * )
  83.      */
  84.     public function setPaymentMethodEligibility(Request $requestContext $context): Response
  85.     {
  86.         /** @var mixed|array $paymentMethods */
  87.         $paymentMethods $request->request->all()['paymentMethods'] ?? null;
  88.         if (!\is_array($paymentMethods)) {
  89.             throw new InvalidRequestParameterException('paymentMethods');
  90.         }
  91.         $handlers = [];
  92.         foreach ($paymentMethods as $paymentMethod) {
  93.             if (self::REMOVABLE_PAYMENT_HANDLERS[$paymentMethod] ?? null) {
  94.                 $handlers[] = self::REMOVABLE_PAYMENT_HANDLERS[$paymentMethod];
  95.             }
  96.         }
  97.         $request->getSession()->set(self::SESSION_KEY$handlers);
  98.         $this->logger->info('Removed ineligible PayPal payment methods from session', ['handlers' => $handlers]);
  99.         return new NoContentResponse();
  100.     }
  101. }