src/Storefront/Controller/AccountPaymentController.php line 35

  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\Log\Package;
  7. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  8. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedHook;
  11. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoader;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @internal
  18.  */
  19. #[Route(defaults: ['_routeScope' => ['storefront']])]
  20. #[Package('storefront')]
  21. class AccountPaymentController extends StorefrontController
  22. {
  23.     /**
  24.      * @internal
  25.      */
  26.     public function __construct(private readonly AccountPaymentMethodPageLoader $paymentMethodPageLoader, private readonly AbstractChangePaymentMethodRoute $changePaymentMethodRoute)
  27.     {
  28.     }
  29.     #[Route(path'/account/payment'name'frontend.account.payment.page'options: ['seo' => false], defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  30.     #[Route(path'/account/payment'name'frontend.account.payment.page'options: ['seo' => false], defaults: ['_noStore' => true], methods: ['GET'])]
  31.     public function paymentOverview(Request $requestSalesChannelContext $context): Response
  32.     {
  33.         $page $this->paymentMethodPageLoader->load($request$context);
  34.         $this->hook(new AccountPaymentMethodPageLoadedHook($page$context));
  35.         return $this->renderStorefront('@Storefront/storefront/page/account/payment/index.html.twig', ['page' => $page]);
  36.     }
  37.     #[Route(path'/account/payment'name'frontend.account.payment.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  38.     public function savePayment(RequestDataBag $requestDataBagSalesChannelContext $contextCustomerEntity $customer): Response
  39.     {
  40.         try {
  41.             $paymentMethodId $requestDataBag->getAlnum('paymentMethodId');
  42.             $this->changePaymentMethodRoute->change(
  43.                 $paymentMethodId,
  44.                 $requestDataBag,
  45.                 $context,
  46.                 $customer
  47.             );
  48.         } catch (UnknownPaymentMethodException InvalidUuidException $exception) {
  49.             $this->addFlash(self::DANGER$this->trans('error.' $exception->getErrorCode()));
  50.             return $this->forwardToRoute('frontend.account.payment.page', ['success' => false]);
  51.         }
  52.         $this->addFlash(self::SUCCESS$this->trans('account.paymentSuccess'));
  53.         return new RedirectResponse($this->generateUrl('frontend.account.payment.page'));
  54.     }
  55. }