custom/plugins/MolliePayments/src/Controller/Storefront/Account/AccountControllerBase.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kiener\MolliePayments\Controller\Storefront\Account;
  3. use Kiener\MolliePayments\Components\Subscription\Page\Account\SubscriptionPageLoader;
  4. use Kiener\MolliePayments\Components\Subscription\SubscriptionManager;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
  8. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Controller\StorefrontController;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\Generator\UrlGenerator;
  16. class AccountControllerBase extends StorefrontController
  17. {
  18.     /**
  19.      * @var SubscriptionPageLoader
  20.      */
  21.     private $pageLoader;
  22.     /**
  23.      * @var SubscriptionManager
  24.      */
  25.     private $subscriptionManager;
  26.     /**
  27.      * @var LoggerInterface
  28.      */
  29.     private $logger;
  30.     /**
  31.      * @param SubscriptionPageLoader $pageLoader
  32.      * @param SubscriptionManager $subscriptionManager
  33.      * @param LoggerInterface $logger
  34.      */
  35.     public function __construct(SubscriptionPageLoader $pageLoaderSubscriptionManager $subscriptionManagerLoggerInterface $logger)
  36.     {
  37.         $this->pageLoader $pageLoader;
  38.         $this->subscriptionManager $subscriptionManager;
  39.         $this->logger $logger;
  40.     }
  41.     /**
  42.      * @Route("/account/mollie/subscriptions", name="frontend.account.mollie.subscriptions.page", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  43.      */
  44.     public function subscriptionsList(Request $requestSalesChannelContext $salesChannelContext): Response
  45.     {
  46.         if (!$this->isLoggedIn($salesChannelContext)) {
  47.             return $this->redirectToLoginPage();
  48.         }
  49.         $page $this->pageLoader->load($request$salesChannelContext);
  50.         return $this->renderStorefront(
  51.             '@Storefront/storefront/page/account/subscriptions/index.html.twig',
  52.             [
  53.                 'page' => $page
  54.             ]
  55.         );
  56.     }
  57.     /**
  58.      * @Route("/account/mollie/subscriptions/{subscriptionId}/billing/update", name="frontend.account.mollie.subscriptions.billing.update", methods={"POST"})
  59.      *
  60.      * @param string $subscriptionId
  61.      * @param RequestDataBag $data
  62.      * @param SalesChannelContext $salesChannelContext
  63.      * @return Response
  64.      */
  65.     public function updateBilling(string $subscriptionIdRequestDataBag $dataSalesChannelContext $salesChannelContext): Response
  66.     {
  67.         if (!$this->isLoggedIn($salesChannelContext)) {
  68.             return $this->redirectToLoginPage();
  69.         }
  70.         try {
  71.             $address $data->get('address'null);
  72.             if (!$address instanceof RequestDataBag) {
  73.                 throw new \Exception('Missing address data in request');
  74.             }
  75.             $salutationId $address->get('salutationId''');
  76.             $title $address->get('title''');
  77.             $firstName $address->get('firstName''');
  78.             $lastName $address->get('lastName''');
  79.             $company $address->get('company''');
  80.             $department $address->get('department''');
  81.             $additionalField1 $address->get('additionalField1''');
  82.             $additionalField2 $address->get('additionalField2''');
  83.             $phoneNumber $address->get('phoneNumber''');
  84.             $street $address->get('street''');
  85.             $zipcode $address->get('zipcode''');
  86.             $city $address->get('city''');
  87.             # COUNTRY change not allowed for billing
  88.             $countryStateId $address->get('countryStateId''');
  89.             $this->subscriptionManager->updateBillingAddress(
  90.                 $subscriptionId,
  91.                 $salutationId,
  92.                 $title,
  93.                 $firstName,
  94.                 $lastName,
  95.                 $company,
  96.                 $department,
  97.                 $additionalField1,
  98.                 $additionalField2,
  99.                 $phoneNumber,
  100.                 $street,
  101.                 $zipcode,
  102.                 $city,
  103.                 $countryStateId,
  104.                 $salesChannelContext->getContext()
  105.             );
  106.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successUpdateAddress'));
  107.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  108.         } catch (\Throwable $exception) {
  109.             return $this->routeToErrorPage(
  110.                 'molliePayments.subscriptions.account.errorUpdateAddress',
  111.                 'Error when updating billing address of subscription ' $subscriptionId ': ' $exception->getMessage()
  112.             );
  113.         }
  114.     }
  115.     /**
  116.      * @Route("/account/mollie/subscriptions/{subscriptionId}/shipping/update", name="frontend.account.mollie.subscriptions.shipping.update", methods={"POST"})
  117.      *
  118.      * @param string $subscriptionId
  119.      * @param RequestDataBag $data
  120.      * @param SalesChannelContext $salesChannelContext
  121.      * @return Response
  122.      */
  123.     public function updateShipping(string $subscriptionIdRequestDataBag $dataSalesChannelContext $salesChannelContext): Response
  124.     {
  125.         if (!$this->isLoggedIn($salesChannelContext)) {
  126.             return $this->redirectToLoginPage();
  127.         }
  128.         try {
  129.             $address $data->get('address'null);
  130.             if (!$address instanceof RequestDataBag) {
  131.                 throw new \Exception('Missing address data in request');
  132.             }
  133.             $salutationId $address->get('salutationId''');
  134.             $title $address->get('title''');
  135.             $firstName $address->get('firstName''');
  136.             $lastName $address->get('lastName''');
  137.             $company $address->get('company''');
  138.             $department $address->get('department''');
  139.             $additionalField1 $address->get('additionalField1''');
  140.             $additionalField2 $address->get('additionalField2''');
  141.             $phoneNumber $address->get('phoneNumber''');
  142.             $street $address->get('street''');
  143.             $zipcode $address->get('zipcode''');
  144.             $city $address->get('city''');
  145.             # COUNTRY change not allowed for billing
  146.             $countryStateId $address->get('countryStateId''');
  147.             $this->subscriptionManager->updateShippingAddress(
  148.                 $subscriptionId,
  149.                 $salutationId,
  150.                 $title,
  151.                 $firstName,
  152.                 $lastName,
  153.                 $company,
  154.                 $department,
  155.                 $additionalField1,
  156.                 $additionalField2,
  157.                 $phoneNumber,
  158.                 $street,
  159.                 $zipcode,
  160.                 $city,
  161.                 $countryStateId,
  162.                 $salesChannelContext->getContext()
  163.             );
  164.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successUpdateAddress'));
  165.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  166.         } catch (\Throwable $exception) {
  167.             return $this->routeToErrorPage(
  168.                 'molliePayments.subscriptions.account.errorUpdateAddress',
  169.                 'Error when updating shipping address of subscription ' $subscriptionId ': ' $exception->getMessage()
  170.             );
  171.         }
  172.     }
  173.     /**
  174.      * @Route("/account/mollie/subscriptions/{swSubscriptionId}/payment/update", name="frontend.account.mollie.subscriptions.payment.update", methods={"POST"})
  175.      *
  176.      * @param string $swSubscriptionId
  177.      * @param SalesChannelContext $salesChannelContext
  178.      * @return Response
  179.      */
  180.     public function updatePaymentStart(string $swSubscriptionIdSalesChannelContext $salesChannelContext): Response
  181.     {
  182.         if (!$this->isLoggedIn($salesChannelContext)) {
  183.             return $this->redirectToLoginPage();
  184.         }
  185.         try {
  186.             $redirectUrl $this->generateUrl(
  187.                 'frontend.account.mollie.subscriptions.payment.update-success',
  188.                 [
  189.                     'swSubscriptionId' => $swSubscriptionId
  190.                 ],
  191.                 UrlGenerator::ABSOLUTE_URL
  192.             );
  193.             $checkoutUrl $this->subscriptionManager->updatePaymentMethodStart($swSubscriptionId$redirectUrl$salesChannelContext->getContext());
  194.             return $this->redirect($checkoutUrl);
  195.         } catch (\Throwable $exception) {
  196.             return $this->routeToErrorPage(
  197.                 'molliePayments.subscriptions.account.errorUpdatePayment',
  198.                 'Error when updating payment method of subscription ' $swSubscriptionId ': ' $exception->getMessage()
  199.             );
  200.         }
  201.     }
  202.     /**
  203.      * @Route("/account/mollie/subscriptions/{swSubscriptionId}/payment/update/finish", name="frontend.account.mollie.subscriptions.payment.update-success", methods={"GET", "POST"})
  204.      *
  205.      * @param string $swSubscriptionId
  206.      * @param SalesChannelContext $salesChannelContext
  207.      * @return Response
  208.      */
  209.     public function updatePaymentFinish(string $swSubscriptionIdSalesChannelContext $salesChannelContext): Response
  210.     {
  211.         if (!$this->isLoggedIn($salesChannelContext)) {
  212.             return $this->redirectToLoginPage();
  213.         }
  214.         try {
  215.             $this->subscriptionManager->updatePaymentMethodConfirm($swSubscriptionId$salesChannelContext->getContext());
  216.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successUpdatePayment'));
  217.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  218.         } catch (\Throwable $exception) {
  219.             return $this->routeToErrorPage(
  220.                 'molliePayments.subscriptions.account.errorUpdatePayment',
  221.                 'Error when updating payment method of subscription ' $swSubscriptionId ': ' $exception->getMessage()
  222.             );
  223.         }
  224.     }
  225.     /**
  226.      * @Route("/account/mollie/subscriptions/{swSubscriptionId}/pause", name="frontend.account.mollie.subscriptions.pause", methods={"POST"})
  227.      * @param string $swSubscriptionId
  228.      */
  229.     public function pauseSubscription(string $swSubscriptionIdSalesChannelContext $context): Response
  230.     {
  231.         if (!$this->isLoggedIn($context)) {
  232.             return $this->redirectToLoginPage();
  233.         }
  234.         try {
  235.             $this->subscriptionManager->pauseSubscription($swSubscriptionId$context->getContext());
  236.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successPause'));
  237.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  238.         } catch (\Throwable $exception) {
  239.             return $this->routeToErrorPage(
  240.                 'molliePayments.subscriptions.account.errorPause',
  241.                 'Error when pausing subscription ' $swSubscriptionId ': ' $exception->getMessage()
  242.             );
  243.         }
  244.     }
  245.     /**
  246.      * @Route("/account/mollie/subscriptions/{swSubscriptionId}/skip", name="frontend.account.mollie.subscriptions.skip", methods={"POST"})
  247.      * @param string $swSubscriptionId
  248.      */
  249.     public function skipSubscription(string $swSubscriptionIdSalesChannelContext $context): Response
  250.     {
  251.         if (!$this->isLoggedIn($context)) {
  252.             return $this->redirectToLoginPage();
  253.         }
  254.         try {
  255.             $this->subscriptionManager->skipSubscription($swSubscriptionId1$context->getContext());
  256.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successSkip'));
  257.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  258.         } catch (\Throwable $exception) {
  259.             return $this->routeToErrorPage(
  260.                 'molliePayments.subscriptions.account.errorSkip',
  261.                 'Error when skipping subscription ' $swSubscriptionId ': ' $exception->getMessage()
  262.             );
  263.         }
  264.     }
  265.     /**
  266.      * @Route("/account/mollie/subscriptions/{swSubscriptionId}/resume", name="frontend.account.mollie.subscriptions.resume", methods={"POST"})
  267.      * @param string $swSubscriptionId
  268.      */
  269.     public function resumeSubscription(string $swSubscriptionIdSalesChannelContext $context): Response
  270.     {
  271.         if (!$this->isLoggedIn($context)) {
  272.             return $this->redirectToLoginPage();
  273.         }
  274.         try {
  275.             $this->subscriptionManager->resumeSubscription($swSubscriptionId$context->getContext());
  276.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successResume'));
  277.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  278.         } catch (\Throwable $exception) {
  279.             return $this->routeToErrorPage(
  280.                 'molliePayments.subscriptions.account.errorResume',
  281.                 'Error when resuming subscription ' $swSubscriptionId ': ' $exception->getMessage()
  282.             );
  283.         }
  284.     }
  285.     /**
  286.      * @Route("/account/mollie/subscriptions/{subscriptionId}/cancel", name="frontend.account.mollie.subscriptions.cancel", methods={"POST"})
  287.      * @param mixed $subscriptionId
  288.      */
  289.     public function cancelSubscription($subscriptionIdSalesChannelContext $context): Response
  290.     {
  291.         if (!$this->isLoggedIn($context)) {
  292.             return $this->redirectToLoginPage();
  293.         }
  294.         try {
  295.             $this->subscriptionManager->cancelSubscription($subscriptionId$context->getContext());
  296.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.cancelSubscription'));
  297.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  298.         } catch (\Throwable $exception) {
  299.             return $this->routeToErrorPage(
  300.                 'molliePayments.subscriptions.account.errorCancelSubscription',
  301.                 'Error when canceling subscription ' $subscriptionId ': ' $exception->getMessage()
  302.             );
  303.         }
  304.     }
  305.     /**
  306.      * @param string $errorSnippetKey
  307.      * @param string $logMessage
  308.      * @return RedirectResponse
  309.      */
  310.     private function routeToErrorPage(string $errorSnippetKeystring $logMessage): RedirectResponse
  311.     {
  312.         $this->logger->error($logMessage);
  313.         $this->addFlash(self::DANGER$this->trans($errorSnippetKey));
  314.         return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  315.     }
  316.     /**
  317.      * @return RedirectResponse
  318.      */
  319.     private function redirectToLoginPage(): RedirectResponse
  320.     {
  321.         return new RedirectResponse($this->generateUrl('frontend.account.login'), 302);
  322.     }
  323.     /**
  324.      * @param SalesChannelContext $context
  325.      * @return bool
  326.      */
  327.     private function isLoggedIn(SalesChannelContext $context): bool
  328.     {
  329.         return ($context->getCustomer() instanceof CustomerEntity);
  330.     }
  331. }