src/Eccube/Controller/NonMemberShoppingController.php line 77

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller;
  13. use Eccube\Event\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Form\Type\Front\NonMemberType;
  16. use Eccube\Form\Validator\Email;
  17. use Eccube\Repository\Master\PrefRepository;
  18. use Eccube\Service\CartService;
  19. use Eccube\Service\OrderHelper;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Validator\Constraints as Assert;
  24. use Symfony\Component\Validator\Validator\ValidatorInterface;
  25. class NonMemberShoppingController extends AbstractShoppingController
  26. {
  27.     /**
  28.      * @var ValidatorInterface
  29.      */
  30.     protected $validator;
  31.     /**
  32.      * @var PrefRepository
  33.      */
  34.     protected $prefRepository;
  35.     /**
  36.      * @var OrderHelper
  37.      */
  38.     protected $orderHelper;
  39.     /**
  40.      * @var CartService
  41.      */
  42.     protected $cartService;
  43.     /**
  44.      * NonMemberShoppingController constructor.
  45.      *
  46.      * @param ValidatorInterface $validator
  47.      * @param PrefRepository $prefRepository
  48.      * @param OrderHelper $orderHelper
  49.      * @param CartService $cartService
  50.      */
  51.     public function __construct(
  52.         ValidatorInterface $validator,
  53.         PrefRepository $prefRepository,
  54.         OrderHelper $orderHelper,
  55.         CartService $cartService
  56.     ) {
  57.         $this->validator $validator;
  58.         $this->prefRepository $prefRepository;
  59.         $this->orderHelper $orderHelper;
  60.         $this->cartService $cartService;
  61.     }
  62.     /**
  63.      * 非会員処理
  64.      *
  65.      * @Route("/shopping/nonmember", name="shopping_nonmember", methods={"GET", "POST"})
  66.      * @Template("Shopping/nonmember.twig")
  67.      */
  68.     public function index(Request $request)
  69.     {
  70.         // ログイン済みの場合は, 購入画面へリダイレクト.
  71.         if ($this->isGranted('ROLE_USER')) {
  72.             return $this->redirectToRoute('shopping');
  73.         }
  74.         // カートチェック.
  75.         $Cart $this->cartService->getCart();
  76.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  77.             return $this->redirectToRoute('cart');
  78.         }
  79.         $builder $this->formFactory->createBuilder(NonMemberType::class);
  80.         $event = new EventArgs(
  81.             [
  82.                 'builder' => $builder,
  83.             ],
  84.             $request
  85.         );
  86.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_NONMEMBER_INITIALIZE);
  87.         $form $builder->getForm();
  88.         $form->handleRequest($request);
  89.         if ($form->isSubmitted() && $form->isValid()) {
  90.             log_info('非会員お客様情報登録開始');
  91.             $data $form->getData();
  92.             // 非会員用セッションを作成
  93.             $this->session->set(OrderHelper::SESSION_NON_MEMBER$data);
  94.             $this->session->set(OrderHelper::SESSION_NON_MEMBER_ADDRESSESserialize([]));
  95.             $event = new EventArgs(
  96.                 [
  97.                     'form' => $form,
  98.                 ],
  99.                 $request
  100.             );
  101.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_NONMEMBER_COMPLETE);
  102.             if ($event->getResponse() !== null) {
  103.                 return $event->getResponse();
  104.             }
  105.             log_info('非会員お客様情報登録完了');
  106.             return $this->redirectToRoute('shopping');
  107.         }
  108.         return [
  109.             'form' => $form->createView(),
  110.         ];
  111.     }
  112.     /**
  113.      * 非会員処理
  114.      *
  115.      * @Route("/buyshopping/nonmember", name="buyshopping_nonmember", methods={"GET", "POST"})
  116.      * @Template("Shopping/nonmember_buy.twig")
  117.      */
  118.     public function buyindex(Request $request)
  119.     {
  120.         // ログイン済みの場合は, 購入画面へリダイレクト.
  121.         if ($this->isGranted('ROLE_USER')) {
  122.             return $this->redirectToRoute('buyshopping');
  123.         }
  124.         // カートチェック.
  125.         $Cart $this->cartService->getCart(false 1);
  126.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  127.             //return $this->redirectToRoute('buycart');
  128.         }
  129.         $builder $this->formFactory->createBuilder(NonMemberType::class);
  130.         $event = new EventArgs(
  131.             [
  132.                 'builder' => $builder,
  133.             ],
  134.             $request
  135.         );
  136.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_NONMEMBER_INITIALIZE);
  137.         $form $builder->getForm();
  138.         $form->handleRequest($request);
  139.         if ($form->isSubmitted() && $form->isValid()) {
  140.             log_info('非会員お客様情報登録開始');
  141.             $data $form->getData();
  142.             // 非会員用セッションを作成
  143.             $this->session->set(OrderHelper::SESSION_NON_MEMBER$data);
  144.             $this->session->set(OrderHelper::SESSION_NON_MEMBER_ADDRESSESserialize([]));
  145.             $event = new EventArgs(
  146.                 [
  147.                     'form' => $form,
  148.                 ],
  149.                 $request
  150.             );
  151.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_NONMEMBER_COMPLETE);
  152.             if ($event->getResponse() !== null) {
  153.                 return $event->getResponse();
  154.             }
  155.             log_info('非会員お客様情報登録完了');
  156.             return $this->redirectToRoute('buyshopping');
  157.         }
  158.         return [
  159.             'form' => $form->createView(),
  160.         ];
  161.     }
  162.     /**
  163.      * お客様情報の変更(非会員)
  164.      *
  165.      * @Route("/shopping/customer", name="shopping_customer", methods={"POST"})
  166.      */
  167.     public function customer(Request $request)
  168.     {
  169.         if (!$request->isXmlHttpRequest()) {
  170.             return $this->json(['status' => 'NG'], 400);
  171.         }
  172.         $this->isTokenValid();
  173.         try {
  174.             log_info('非会員お客様情報変更処理開始');
  175.             $data $request->request->all();
  176.             // 入力チェック
  177.             $errors $this->customerValidation($data);
  178.             foreach ($errors as $error) {
  179.                 if ($error->count() != 0) {
  180.                     log_info('非会員お客様情報変更入力チェックエラー');
  181.                     return $this->json(['status' => 'NG'], 400);
  182.                 }
  183.             }
  184.             $pref $this->prefRepository->findOneBy(['name' => $data['customer_pref']]);
  185.             if (!$pref) {
  186.                 log_info('非会員お客様情報変更入力チェックエラー');
  187.                 return $this->json(['status' => 'NG'], 400);
  188.             }
  189.             $preOrderId $this->cartService->getPreOrderId();
  190.             $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  191.             if (!$Order) {
  192.                 log_info('受注が存在しません');
  193.                 $this->addError('front.shopping.order_error');
  194.                 return $this->redirectToRoute('shopping_error');
  195.             }
  196.             $Order
  197.                 ->setName01($data['customer_name01'])
  198.                 ->setName02($data['customer_name02'])
  199.                 ->setKana01($data['customer_kana01'])
  200.                 ->setKana02($data['customer_kana02'])
  201.                 ->setCompanyName($data['customer_company_name'])
  202.                 ->setPhoneNumber($data['customer_phone_number'])
  203.                 ->setPostalCode($data['customer_postal_code'])
  204.                 ->setPref($pref)
  205.                 ->setAddr01($data['customer_addr01'])
  206.                 ->setAddr02($data['customer_addr02'])
  207.                 ->setEmail($data['customer_email']);
  208.             $this->entityManager->flush();
  209.             $this->session->set(OrderHelper::SESSION_NON_MEMBER, [
  210.                 'name01' => $data['customer_name01'],
  211.                 'name02' => $data['customer_name02'],
  212.                 'kana01' => $data['customer_kana01'],
  213.                 'kana02' => $data['customer_kana02'],
  214.                 'company_name' => $data['customer_company_name'],
  215.                 'phone_number' => $data['customer_phone_number'],
  216.                 'postal_code' => $data['customer_postal_code'],
  217.                 'pref' => $pref,
  218.                 'addr01' => $data['customer_addr01'],
  219.                 'addr02' => $data['customer_addr02'],
  220.                 'email' => $data['customer_email'],
  221.             ]);
  222.             $event = new EventArgs(
  223.                 [
  224.                     'Order' => $Order,
  225.                     'data' => $data,
  226.                 ],
  227.                 $request
  228.             );
  229.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_CUSTOMER_INITIALIZE);
  230.             log_info('非会員お客様情報変更処理完了', [$Order->getId()]);
  231.             $message = ['status' => 'OK''kana01' => $data['customer_kana01'], 'kana02' => $data['customer_kana02']];
  232.             $response $this->json($message);
  233.         } catch (\Exception $e) {
  234.             log_error('予期しないエラー', [$e->getMessage()]);
  235.             $response $this->json(['status' => 'NG'], 500);
  236.         }
  237.         return $response;
  238.     }
  239.     /**
  240.      * 非会員でのお客様情報変更時の入力チェック
  241.      *
  242.      * @param array $data リクエストパラメータ
  243.      *
  244.      * @return \Symfony\Component\Validator\ConstraintViolationListInterface[]
  245.      */
  246.     protected function customerValidation(array &$data)
  247.     {
  248.         // 入力チェック
  249.         $errors = [];
  250.         $errors[] = $this->validator->validate(
  251.             $data['customer_name01'],
  252.             [
  253.                 new Assert\NotBlank(),
  254.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_name_len']]),
  255.                 new Assert\Regex(
  256.                     ['pattern' => '/^[^\s ]+$/u''message' => 'form_error.not_contain_spaces']
  257.                 ),
  258.             ]
  259.         );
  260.         $errors[] = $this->validator->validate(
  261.             $data['customer_name02'],
  262.             [
  263.                 new Assert\NotBlank(),
  264.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_name_len']]),
  265.                 new Assert\Regex(
  266.                     ['pattern' => '/^[^\s ]+$/u''message' => 'form_error.not_contain_spaces']
  267.                 ),
  268.             ]
  269.         );
  270.         $data['customer_kana01'] = mb_convert_kana($data['customer_kana01'], 'CV''utf-8');
  271.         $errors[] = $this->validator->validate(
  272.             $data['customer_kana01'],
  273.             [
  274.                 new Assert\NotBlank(),
  275.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
  276.                 new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
  277.             ]
  278.         );
  279.         $data['customer_kana02'] = mb_convert_kana($data['customer_kana02'], 'CV''utf-8');
  280.         $errors[] = $this->validator->validate(
  281.             $data['customer_kana02'],
  282.             [
  283.                 new Assert\NotBlank(),
  284.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
  285.                 new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
  286.             ]);
  287.         $errors[] = $this->validator->validate(
  288.             $data['customer_company_name'],
  289.             [
  290.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
  291.             ]
  292.         );
  293.         $errors[] = $this->validator->validate(
  294.             $data['customer_phone_number'],
  295.             [
  296.                 new Assert\NotBlank(),
  297.                 new Assert\Type(['type' => 'digit''message' => 'form_error.numeric_only']),
  298.                 new Assert\Length(
  299.                     ['max' => $this->eccubeConfig['eccube_tel_len_max']]
  300.                 ),
  301.             ]
  302.         );
  303.         $errors[] = $this->validator->validate(
  304.             $data['customer_postal_code'],
  305.             [
  306.                 new Assert\NotBlank(),
  307.                 new Assert\Type(['type' => 'digit''message' => 'form_error.numeric_only']),
  308.                 new Assert\Length(
  309.                     ['max' => $this->eccubeConfig['eccube_postal_code']]
  310.                 ),
  311.             ]
  312.         );
  313.         $errors[] = $this->validator->validate(
  314.             $data['customer_addr01'],
  315.             [
  316.                 new Assert\NotBlank(),
  317.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_address1_len']]),
  318.             ]
  319.         );
  320.         $errors[] = $this->validator->validate(
  321.             $data['customer_addr02'],
  322.             [
  323.                 new Assert\NotBlank(),
  324.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_address2_len']]),
  325.             ]
  326.         );
  327.         $errors[] = $this->validator->validate(
  328.             $data['customer_email'],
  329.             [
  330.                 new Assert\NotBlank(),
  331.                 new Email(nullnull$this->eccubeConfig['eccube_rfc_email_check'] ? 'strict' null),
  332.             ]
  333.         );
  334.         return $errors;
  335.     }
  336. }