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

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