vendor/shopware/storefront/Controller/AccountPaymentController.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangePaymentMethodRoute;
  6. use Shopware\Core\Checkout\Payment\Exception\UnknownPaymentMethodException;
  7. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\Framework\Routing\Annotation\Since;
  10. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  11. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoader;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. /**
  19.  * @RouteScope(scopes={"storefront"})
  20.  */
  21. class AccountPaymentController extends StorefrontController
  22. {
  23.     /**
  24.      * @var AccountPaymentMethodPageLoader
  25.      */
  26.     private $paymentMethodPageLoader;
  27.     /**
  28.      * @var AbstractChangePaymentMethodRoute
  29.      */
  30.     private $changePaymentMethodRoute;
  31.     public function __construct(
  32.         AccountPaymentMethodPageLoader $paymentMethodPageLoader,
  33.         AbstractChangePaymentMethodRoute $changePaymentMethodRoute
  34.     ) {
  35.         $this->paymentMethodPageLoader $paymentMethodPageLoader;
  36.         $this->changePaymentMethodRoute $changePaymentMethodRoute;
  37.     }
  38.     /**
  39.      * @Since("6.0.0.0")
  40.      * @LoginRequired()
  41.      * @Route("/account/payment", name="frontend.account.payment.page", options={"seo"="false"}, methods={"GET"})
  42.      *
  43.      * @throws CustomerNotLoggedInException
  44.      */
  45.     public function paymentOverview(Request $requestSalesChannelContext $context): Response
  46.     {
  47.         $page $this->paymentMethodPageLoader->load($request$context);
  48.         return $this->renderStorefront('@Storefront/storefront/page/account/payment/index.html.twig', ['page' => $page]);
  49.     }
  50.     /**
  51.      * @Since("6.0.0.0")
  52.      * @LoginRequired()
  53.      * @Route("/account/payment", name="frontend.account.payment.save", methods={"POST"})
  54.      */
  55.     public function savePayment(RequestDataBag $requestDataBagSalesChannelContext $contextCustomerEntity $customer): Response
  56.     {
  57.         try {
  58.             $paymentMethodId $requestDataBag->getAlnum('paymentMethodId');
  59.             $this->changePaymentMethodRoute->change(
  60.                 $paymentMethodId,
  61.                 $requestDataBag,
  62.                 $context,
  63.                 $customer
  64.             );
  65.         } catch (UnknownPaymentMethodException InvalidUuidException $exception) {
  66.             $this->addFlash(self::DANGER$this->trans('error.' $exception->getErrorCode()));
  67.             return $this->forwardToRoute('frontend.account.payment.page', ['success' => false]);
  68.         }
  69.         $this->addFlash(self::SUCCESS$this->trans('account.paymentSuccess'));
  70.         return new RedirectResponse($this->generateUrl('frontend.account.payment.page'));
  71.     }
  72. }