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

  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.  * Do not use direct or indirect repository calls in a controller. Always use a store-api route to get or put data
  19.  */
  20. #[Route(defaults: ['_routeScope' => ['storefront']])]
  21. #[Package('storefront')]
  22. class AccountPaymentController extends StorefrontController
  23. {
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(
  28.         private readonly AccountPaymentMethodPageLoader $paymentMethodPageLoader,
  29.         private readonly AbstractChangePaymentMethodRoute $changePaymentMethodRoute
  30.     ) {
  31.     }
  32.     #[Route(path'/account/payment'name'frontend.account.payment.page'options: ['seo' => false], defaults: ['_loginRequired' => true'_noStore' => true], methods: ['GET'])]
  33.     #[Route(path'/account/payment'name'frontend.account.payment.page'options: ['seo' => false], defaults: ['_noStore' => true], methods: ['GET'])]
  34.     public function paymentOverview(Request $requestSalesChannelContext $context): Response
  35.     {
  36.         $page $this->paymentMethodPageLoader->load($request$context);
  37.         $this->hook(new AccountPaymentMethodPageLoadedHook($page$context));
  38.         return $this->renderStorefront('@Storefront/storefront/page/account/payment/index.html.twig', ['page' => $page]);
  39.     }
  40.     #[Route(path'/account/payment'name'frontend.account.payment.save'defaults: ['_loginRequired' => true], methods: ['POST'])]
  41.     public function savePayment(RequestDataBag $requestDataBagSalesChannelContext $contextCustomerEntity $customer): Response
  42.     {
  43.         try {
  44.             $paymentMethodId $requestDataBag->getAlnum('paymentMethodId');
  45.             $this->changePaymentMethodRoute->change(
  46.                 $paymentMethodId,
  47.                 $requestDataBag,
  48.                 $context,
  49.                 $customer
  50.             );
  51.         } catch (UnknownPaymentMethodException InvalidUuidException $exception) {
  52.             $this->addFlash(self::DANGER$this->trans('error.' $exception->getErrorCode()));
  53.             return $this->forwardToRoute('frontend.account.payment.page', ['success' => false]);
  54.         }
  55.         $this->addFlash(self::SUCCESS$this->trans('account.paymentSuccess'));
  56.         return new RedirectResponse($this->generateUrl('frontend.account.payment.page'));
  57.     }
  58. }