vendor/shopware/platform/src/Storefront/Controller/AccountPaymentController.php line 45

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