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

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\Framework\Routing\Annotation\NoStore;
  14. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedHook;
  15. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoader;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * @RouteScope(scopes={"storefront"})
  22.  */
  23. class AccountPaymentController extends StorefrontController
  24. {
  25.     private AccountPaymentMethodPageLoader $paymentMethodPageLoader;
  26.     private AbstractChangePaymentMethodRoute $changePaymentMethodRoute;
  27.     public function __construct(
  28.         AccountPaymentMethodPageLoader $paymentMethodPageLoader,
  29.         AbstractChangePaymentMethodRoute $changePaymentMethodRoute
  30.     ) {
  31.         $this->paymentMethodPageLoader $paymentMethodPageLoader;
  32.         $this->changePaymentMethodRoute $changePaymentMethodRoute;
  33.     }
  34.     /**
  35.      * @Since("6.0.0.0")
  36.      * @LoginRequired()
  37.      * @Route("/account/payment", name="frontend.account.payment.page", options={"seo"="false"}, methods={"GET"})
  38.      * @NoStore
  39.      *
  40.      * @throws CustomerNotLoggedInException
  41.      */
  42.     public function paymentOverview(Request $requestSalesChannelContext $context): Response
  43.     {
  44.         $page $this->paymentMethodPageLoader->load($request$context);
  45.         $this->hook(new AccountPaymentMethodPageLoadedHook($page$context));
  46.         return $this->renderStorefront('@Storefront/storefront/page/account/payment/index.html.twig', ['page' => $page]);
  47.     }
  48.     /**
  49.      * @Since("6.0.0.0")
  50.      * @LoginRequired()
  51.      * @Route("/account/payment", name="frontend.account.payment.save", methods={"POST"})
  52.      */
  53.     public function savePayment(RequestDataBag $requestDataBagSalesChannelContext $contextCustomerEntity $customer): Response
  54.     {
  55.         try {
  56.             $paymentMethodId $requestDataBag->getAlnum('paymentMethodId');
  57.             $this->changePaymentMethodRoute->change(
  58.                 $paymentMethodId,
  59.                 $requestDataBag,
  60.                 $context,
  61.                 $customer
  62.             );
  63.         } catch (UnknownPaymentMethodException InvalidUuidException $exception) {
  64.             $this->addFlash(self::DANGER$this->trans('error.' $exception->getErrorCode()));
  65.             return $this->forwardToRoute('frontend.account.payment.page', ['success' => false]);
  66.         }
  67.         $this->addFlash(self::SUCCESS$this->trans('account.paymentSuccess'));
  68.         return new RedirectResponse($this->generateUrl('frontend.account.payment.page'));
  69.     }
  70. }