custom/plugins/SwagPayPal/src/Storefront/Controller/PayPalController.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\Storefront\Controller;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Checkout\Cart\SalesChannel\AbstractCartDeleteRoute;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\Routing\Annotation\Since;
  12. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  13. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  14. use Shopware\Core\System\SalesChannel\ContextTokenResponse;
  15. use Shopware\Core\System\SalesChannel\NoContentResponse;
  16. use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Shopware\Storefront\Controller\StorefrontController;
  19. use Swag\PayPal\Checkout\ExpressCheckout\SalesChannel\AbstractExpressCreateOrderRoute;
  20. use Swag\PayPal\Checkout\ExpressCheckout\SalesChannel\AbstractExpressPrepareCheckoutRoute;
  21. use Swag\PayPal\Checkout\PUI\SalesChannel\AbstractPUIPaymentInstructionsRoute;
  22. use Swag\PayPal\Checkout\PUI\SalesChannel\PUIPaymentInstructionsResponse;
  23. use Swag\PayPal\Checkout\SalesChannel\AbstractCreateOrderRoute;
  24. use Swag\PayPal\Checkout\SalesChannel\AbstractMethodEligibilityRoute;
  25. use Swag\PayPal\Checkout\TokenResponse;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. /**
  30.  * @Route(defaults={"_routeScope"={"storefront"}})
  31.  *
  32.  * @internal
  33.  */
  34. class PayPalController extends StorefrontController
  35. {
  36.     private AbstractCreateOrderRoute $createOrderRoute;
  37.     private AbstractMethodEligibilityRoute $methodEligibilityRoute;
  38.     private AbstractPUIPaymentInstructionsRoute $puiPaymentInstructionsRoute;
  39.     private AbstractExpressPrepareCheckoutRoute $expressPrepareCheckoutRoute;
  40.     private AbstractExpressCreateOrderRoute $expressCreateOrderRoute;
  41.     private LoggerInterface $logger;
  42.     private AbstractContextSwitchRoute $contextSwitchRoute;
  43.     private AbstractCartDeleteRoute $cartDeleteRoute;
  44.     /**
  45.      * @internal
  46.      */
  47.     public function __construct(
  48.         AbstractCreateOrderRoute $createOrderRoute,
  49.         AbstractMethodEligibilityRoute $methodEligibilityRoute,
  50.         AbstractPUIPaymentInstructionsRoute $puiPaymentInstructionsRoute,
  51.         AbstractExpressPrepareCheckoutRoute $expressPrepareCheckoutRoute,
  52.         AbstractExpressCreateOrderRoute $expressCreateOrderRoute,
  53.         AbstractContextSwitchRoute $contextSwitchRoute,
  54.         AbstractCartDeleteRoute $cartDeleteRoute,
  55.         LoggerInterface $logger
  56.     ) {
  57.         $this->createOrderRoute $createOrderRoute;
  58.         $this->methodEligibilityRoute $methodEligibilityRoute;
  59.         $this->puiPaymentInstructionsRoute $puiPaymentInstructionsRoute;
  60.         $this->expressPrepareCheckoutRoute $expressPrepareCheckoutRoute;
  61.         $this->expressCreateOrderRoute $expressCreateOrderRoute;
  62.         $this->contextSwitchRoute $contextSwitchRoute;
  63.         $this->cartDeleteRoute $cartDeleteRoute;
  64.         $this->logger $logger;
  65.     }
  66.     /**
  67.      * @Since("6.0.0")
  68.      *
  69.      * @Route(
  70.      *     "/paypal/create-order",
  71.      *     name="frontend.paypal.create_order",
  72.      *     methods={"POST"},
  73.      *     defaults={"XmlHttpRequest"=true, "csrf_protected"=false}
  74.      * )
  75.      */
  76.     public function createOrder(SalesChannelContext $salesChannelContextRequest $request): TokenResponse
  77.     {
  78.         return $this->createOrderRoute->createPayPalOrder($salesChannelContext$request);
  79.     }
  80.     /**
  81.      * @Since("6.0.0")
  82.      *
  83.      * @Route(
  84.      *     "/paypal/payment-method-eligibility",
  85.      *     name="frontend.paypal.payment-method-eligibility",
  86.      *     methods={"POST"},
  87.      *     defaults={"XmlHttpRequest"=true, "csrf_protected"=false}
  88.      * )
  89.      */
  90.     public function paymentMethodEligibility(Request $requestContext $context): Response
  91.     {
  92.         return $this->methodEligibilityRoute->setPaymentMethodEligibility($request$context);
  93.     }
  94.     /**
  95.      * @Since("6.0.0")
  96.      *
  97.      * @Route(
  98.      *     "/paypal/pui/payment-instructions/{transactionId}",
  99.      *     name="frontend.paypal.pui.payment_instructions",
  100.      *     methods={"GET"},
  101.      *     defaults={"_loginRequired"=true, "_loginRequiredAllowGuest"=true, "XmlHttpRequest"=true, "csrf_protected"=false}
  102.      * )
  103.      */
  104.     public function puiPaymentInstructions(string $transactionIdSalesChannelContext $salesChannelContext): PUIPaymentInstructionsResponse
  105.     {
  106.         return $this->puiPaymentInstructionsRoute->getPaymentInstructions($transactionId$salesChannelContext);
  107.     }
  108.     /**
  109.      * @Since("6.0.0")
  110.      *
  111.      * @Route(
  112.      *     "/paypal/express/prepare-checkout",
  113.      *     name="frontend.paypal.express.prepare_checkout",
  114.      *     methods={"POST"},
  115.      *     defaults={"XmlHttpRequest"=true, "csrf_protected"=false}
  116.      * )
  117.      */
  118.     public function expressPrepareCheckout(Request $requestSalesChannelContext $context): ContextTokenResponse
  119.     {
  120.         return $this->expressPrepareCheckoutRoute->prepareCheckout($context$request);
  121.     }
  122.     /**
  123.      * @Since("6.0.0")
  124.      *
  125.      * @Route(
  126.      *     "/paypal/express/create-order",
  127.      *     name="frontend.paypal.express.create_order",
  128.      *     methods={"POST"},
  129.      *     defaults={"XmlHttpRequest"=true, "csrf_protected"=false}
  130.      * )
  131.      */
  132.     public function expressCreateOrder(SalesChannelContext $context): TokenResponse
  133.     {
  134.         return $this->expressCreateOrderRoute->createPayPalOrder($context);
  135.     }
  136.     /**
  137.      * @Since("6.0.0")
  138.      *
  139.      * @Route(
  140.      *     "/paypal/express/prepare-cart",
  141.      *     name="frontend.paypal.express.prepare_cart",
  142.      *     methods={"POST"},
  143.      *     defaults={"XmlHttpRequest"=true, "csrf_protected"=false}
  144.      * )
  145.      */
  146.     public function expressPrepareCart(Request $requestSalesChannelContext $context): Response
  147.     {
  148.         $this->contextSwitchRoute->switchContext(new RequestDataBag([
  149.             SalesChannelContextService::PAYMENT_METHOD_ID => $request->get('paymentMethodId'),
  150.         ]), $context);
  151.         if ($request->request->getBoolean('deleteCart')) {
  152.             $this->cartDeleteRoute->delete($context);
  153.         }
  154.         return new NoContentResponse();
  155.     }
  156.     /**
  157.      * @Since("6.0.0")
  158.      *
  159.      * @Route(
  160.      *     "/paypal/error",
  161.      *     name="frontend.paypal.error",
  162.      *     methods={"POST"},
  163.      *     defaults={"XmlHttpRequest"=true, "csrf_protected"=false}
  164.      * )
  165.      */
  166.     public function addErrorMessage(Request $request): Response
  167.     {
  168.         if ($request->request->getBoolean('cancel')) {
  169.             $this->addFlash(self::DANGER$this->trans('paypal.general.paymentCancel'));
  170.             $this->logger->notice('Storefront checkout cancellation');
  171.         } else {
  172.             $this->addFlash(self::DANGER$this->trans('paypal.general.paymentError'));
  173.             $this->logger->notice('Storefront checkout error', ['error' => $request->request->get('error')]);
  174.         }
  175.         return new NoContentResponse();
  176.     }
  177. }