custom/plugins/MolliePayments/src/Controller/Storefront/AccountController.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kiener\MolliePayments\Controller\Storefront;
  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\Framework\Context;
  7. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Controller\StorefrontController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @RouteScope(scopes={"storefront"})
  17.  */
  18. class AccountController extends StorefrontController
  19. {
  20.     /**
  21.      * @var SubscriptionPageLoader
  22.      */
  23.     private $pageLoader;
  24.     /**
  25.      * @var SubscriptionManager
  26.      */
  27.     private $subscriptionManager;
  28.     /**
  29.      * @var LoggerInterface
  30.      */
  31.     private $logger;
  32.     /**
  33.      * @param SubscriptionPageLoader $pageLoader
  34.      * @param SubscriptionManager $subscriptionManager
  35.      * @param LoggerInterface $logger
  36.      */
  37.     public function __construct(SubscriptionPageLoader $pageLoaderSubscriptionManager $subscriptionManagerLoggerInterface $logger)
  38.     {
  39.         $this->pageLoader $pageLoader;
  40.         $this->subscriptionManager $subscriptionManager;
  41.         $this->logger $logger;
  42.     }
  43.     /**
  44.      * @LoginRequired()
  45.      * @Route("/account/mollie/subscriptions", name="frontend.account.mollie.subscriptions.page", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  46.      */
  47.     public function subscriptionsList(Request $requestSalesChannelContext $salesChannelContext): Response
  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.      * @LoginRequired()
  59.      * @Route("/account/mollie/subscriptions/{subscriptionId}/billing/update", name="frontend.account.mollie.subscriptions.billing.update", methods={"POST"})
  60.      *
  61.      * @param string $subscriptionId
  62.      * @param RequestDataBag $data
  63.      * @param SalesChannelContext $salesChannelContext
  64.      * @return Response
  65.      */
  66.     public function updateBilling(string $subscriptionIdRequestDataBag $dataSalesChannelContext $salesChannelContext): Response
  67.     {
  68.         try {
  69.             $address $data->get('address'null);
  70.             if (!$address instanceof RequestDataBag) {
  71.                 throw new \Exception('Missing address data in request');
  72.             }
  73.             $salutationId $address->get('salutationId''');
  74.             $title $address->get('title''');
  75.             $firstName $address->get('firstName''');
  76.             $lastName $address->get('lastName''');
  77.             $company $address->get('company''');
  78.             $department $address->get('department''');
  79.             $additionalField1 $address->get('additionalField1''');
  80.             $additionalField2 $address->get('additionalField2''');
  81.             $phoneNumber $address->get('phoneNumber''');
  82.             $street $address->get('street''');
  83.             $zipcode $address->get('zipcode''');
  84.             $city $address->get('city''');
  85.             # COUNTRY change not allowed for billing
  86.             $countryStateId $address->get('countryStateId''');
  87.             $this->subscriptionManager->updateBillingAddress(
  88.                 $subscriptionId,
  89.                 $salutationId,
  90.                 $title,
  91.                 $firstName,
  92.                 $lastName,
  93.                 $company,
  94.                 $department,
  95.                 $additionalField1,
  96.                 $additionalField2,
  97.                 $phoneNumber,
  98.                 $street,
  99.                 $zipcode,
  100.                 $city,
  101.                 $countryStateId,
  102.                 $salesChannelContext->getContext()
  103.             );
  104.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successUpdateAddress'));
  105.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  106.         } catch (\Throwable $exception) {
  107.             $this->logger->error('Error when updating billing address of subscription ' $subscriptionId ': ' $exception->getMessage());
  108.             $this->addFlash(self::DANGER$this->trans('molliePayments.subscriptions.account.errorUpdateAddress'));
  109.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  110.         }
  111.     }
  112.     /**
  113.      * @LoginRequired()
  114.      * @Route("/account/mollie/subscriptions/{subscriptionId}/shipping/update", name="frontend.account.mollie.subscriptions.shipping.update", methods={"POST"})
  115.      *
  116.      * @param string $subscriptionId
  117.      * @param RequestDataBag $data
  118.      * @param SalesChannelContext $salesChannelContext
  119.      * @return Response
  120.      */
  121.     public function updateShipping(string $subscriptionIdRequestDataBag $dataSalesChannelContext $salesChannelContext): Response
  122.     {
  123.         try {
  124.             $address $data->get('address'null);
  125.             if (!$address instanceof RequestDataBag) {
  126.                 throw new \Exception('Missing address data in request');
  127.             }
  128.             $salutationId $address->get('salutationId''');
  129.             $title $address->get('title''');
  130.             $firstName $address->get('firstName''');
  131.             $lastName $address->get('lastName''');
  132.             $company $address->get('company''');
  133.             $department $address->get('department''');
  134.             $additionalField1 $address->get('additionalField1''');
  135.             $additionalField2 $address->get('additionalField2''');
  136.             $phoneNumber $address->get('phoneNumber''');
  137.             $street $address->get('street''');
  138.             $zipcode $address->get('zipcode''');
  139.             $city $address->get('city''');
  140.             # COUNTRY change not allowed for billing
  141.             $countryStateId $address->get('countryStateId''');
  142.             $this->subscriptionManager->updateShippingAddress(
  143.                 $subscriptionId,
  144.                 $salutationId,
  145.                 $title,
  146.                 $firstName,
  147.                 $lastName,
  148.                 $company,
  149.                 $department,
  150.                 $additionalField1,
  151.                 $additionalField2,
  152.                 $phoneNumber,
  153.                 $street,
  154.                 $zipcode,
  155.                 $city,
  156.                 $countryStateId,
  157.                 $salesChannelContext->getContext()
  158.             );
  159.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successUpdateAddress'));
  160.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  161.         } catch (\Throwable $exception) {
  162.             $this->logger->error('Error when updating shipping address of subscription ' $subscriptionId ': ' $exception->getMessage());
  163.             $this->addFlash(self::DANGER$this->trans('molliePayments.subscriptions.account.errorUpdateAddress'));
  164.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  165.         }
  166.     }
  167.     /**
  168.      * @LoginRequired()
  169.      * @Route("/account/mollie/subscriptions/{subscriptionId}/payment/update", name="frontend.account.mollie.subscriptions.payment.update", methods={"POST"})
  170.      *
  171.      * @param string $subscriptionId
  172.      * @param SalesChannelContext $salesChannelContext
  173.      * @return Response
  174.      */
  175.     public function updatePaymentStart(string $subscriptionIdSalesChannelContext $salesChannelContext): Response
  176.     {
  177.         try {
  178.             $checkoutUrl $this->subscriptionManager->updatePaymentMethodStart($subscriptionId$salesChannelContext->getContext());
  179.             return $this->redirect($checkoutUrl);
  180.         } catch (\Throwable $exception) {
  181.             $this->logger->error('Error when updating payment method of subscription ' $subscriptionId ': ' $exception->getMessage());
  182.             $this->addFlash(self::DANGER$this->trans('molliePayments.subscriptions.account.errorUpdatePayment'));
  183.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  184.         }
  185.     }
  186.     /**
  187.      * @LoginRequired()
  188.      * @Route("/account/mollie/subscriptions/{subscriptionId}/payment/update/finish", name="frontend.account.mollie.subscriptions.payment.update-success", methods={"GET", "POST"})
  189.      *
  190.      * @param string $subscriptionId
  191.      * @param SalesChannelContext $salesChannelContext
  192.      * @return Response
  193.      */
  194.     public function updatePaymentFinish(string $subscriptionIdSalesChannelContext $salesChannelContext): Response
  195.     {
  196.         try {
  197.             $this->subscriptionManager->updatePaymentMethodConfirm($subscriptionId$salesChannelContext->getContext());
  198.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.successUpdatePayment'));
  199.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  200.         } catch (\Throwable $exception) {
  201.             $this->logger->error('Error when updating payment method of subscription ' $subscriptionId ': ' $exception->getMessage());
  202.             $this->addFlash(self::DANGER$this->trans('molliePayments.subscriptions.account.errorUpdatePayment'));
  203.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  204.         }
  205.     }
  206.     /**
  207.      * @LoginRequired()
  208.      * @Route("/account/mollie/subscriptions/{subscriptionId}/cancel", name="frontend.account.mollie.subscriptions.cancel", methods={"POST"})
  209.      * @param mixed $subscriptionId
  210.      */
  211.     public function cancelSubscription($subscriptionIdSalesChannelContext $context): Response
  212.     {
  213.         try {
  214.             $this->subscriptionManager->cancelSubscription($subscriptionId$context->getContext());
  215.             $this->addFlash(self::SUCCESS$this->trans('molliePayments.subscriptions.account.cancelSubscription'));
  216.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  217.         } catch (\Throwable $exception) {
  218.             $this->logger->error('Error when canceling subscription ' $subscriptionId ': ' $exception->getMessage());
  219.             $this->addFlash(self::DANGER$this->trans('molliePayments.subscriptions.account.errorCancelSubscription'));
  220.             return $this->redirectToRoute('frontend.account.mollie.subscriptions.page');
  221.         }
  222.     }
  223. }