src/Eccube/Controller/NonMemberShoppingController.php line 29

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("/shopping/customer", name="shopping_customer", methods={"POST"})
  116.      */
  117.     public function customer(Request $request)
  118.     {
  119.         if (!$request->isXmlHttpRequest()) {
  120.             return $this->json(['status' => 'NG'], 400);
  121.         }
  122.         $this->isTokenValid();
  123.         try {
  124.             log_info('非会員お客様情報変更処理開始');
  125.             $data $request->request->all();
  126.             // 入力チェック
  127.             $errors $this->customerValidation($data);
  128.             foreach ($errors as $error) {
  129.                 if ($error->count() != 0) {
  130.                     log_info('非会員お客様情報変更入力チェックエラー');
  131.                     return $this->json(['status' => 'NG'], 400);
  132.                 }
  133.             }
  134.             $pref $this->prefRepository->findOneBy(['name' => $data['customer_pref']]);
  135.             if (!$pref) {
  136.                 log_info('非会員お客様情報変更入力チェックエラー');
  137.                 return $this->json(['status' => 'NG'], 400);
  138.             }
  139.             $preOrderId $this->cartService->getPreOrderId();
  140.             $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  141.             if (!$Order) {
  142.                 log_info('受注が存在しません');
  143.                 $this->addError('front.shopping.order_error');
  144.                 return $this->redirectToRoute('shopping_error');
  145.             }
  146.             $Order
  147.                 ->setName01($data['customer_name01'])
  148.                 ->setName02($data['customer_name02'])
  149.                 ->setKana01($data['customer_kana01'])
  150.                 ->setKana02($data['customer_kana02'])
  151.                 ->setCompanyName($data['customer_company_name'])
  152.                 ->setPhoneNumber($data['customer_phone_number'])
  153.                 ->setPostalCode($data['customer_postal_code'])
  154.                 ->setPref($pref)
  155.                 ->setAddr01($data['customer_addr01'])
  156.                 ->setAddr02($data['customer_addr02'])
  157.                 ->setEmail($data['customer_email']);
  158.             $this->entityManager->flush();
  159.             $this->session->set(OrderHelper::SESSION_NON_MEMBER, [
  160.                 'name01' => $data['customer_name01'],
  161.                 'name02' => $data['customer_name02'],
  162.                 'kana01' => $data['customer_kana01'],
  163.                 'kana02' => $data['customer_kana02'],
  164.                 'company_name' => $data['customer_company_name'],
  165.                 'phone_number' => $data['customer_phone_number'],
  166.                 'postal_code' => $data['customer_postal_code'],
  167.                 'pref' => $pref,
  168.                 'addr01' => $data['customer_addr01'],
  169.                 'addr02' => $data['customer_addr02'],
  170.                 'email' => $data['customer_email'],
  171.             ]);
  172.             $event = new EventArgs(
  173.                 [
  174.                     'Order' => $Order,
  175.                     'data' => $data,
  176.                 ],
  177.                 $request
  178.             );
  179.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_CUSTOMER_INITIALIZE);
  180.             log_info('非会員お客様情報変更処理完了', [$Order->getId()]);
  181.             $message = ['status' => 'OK''kana01' => $data['customer_kana01'], 'kana02' => $data['customer_kana02']];
  182.             $response $this->json($message);
  183.         } catch (\Exception $e) {
  184.             log_error('予期しないエラー', [$e->getMessage()]);
  185.             $response $this->json(['status' => 'NG'], 500);
  186.         }
  187.         return $response;
  188.     }
  189.     /**
  190.      * 非会員でのお客様情報変更時の入力チェック
  191.      *
  192.      * @param array $data リクエストパラメータ
  193.      *
  194.      * @return \Symfony\Component\Validator\ConstraintViolationListInterface[]
  195.      */
  196.     protected function customerValidation(array &$data)
  197.     {
  198.         // 入力チェック
  199.         $errors = [];
  200.         $errors[] = $this->validator->validate(
  201.             $data['customer_name01'],
  202.             [
  203.                 new Assert\NotBlank(),
  204.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_name_len']]),
  205.                 new Assert\Regex(
  206.                     ['pattern' => '/^[^\s ]+$/u''message' => 'form_error.not_contain_spaces']
  207.                 ),
  208.             ]
  209.         );
  210.         $errors[] = $this->validator->validate(
  211.             $data['customer_name02'],
  212.             [
  213.                 new Assert\NotBlank(),
  214.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_name_len']]),
  215.                 new Assert\Regex(
  216.                     ['pattern' => '/^[^\s ]+$/u''message' => 'form_error.not_contain_spaces']
  217.                 ),
  218.             ]
  219.         );
  220.         $data['customer_kana01'] = mb_convert_kana($data['customer_kana01'], 'CV''utf-8');
  221.         $errors[] = $this->validator->validate(
  222.             $data['customer_kana01'],
  223.             [
  224.                 new Assert\NotBlank(),
  225.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
  226.                 new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
  227.             ]
  228.         );
  229.         $data['customer_kana02'] = mb_convert_kana($data['customer_kana02'], 'CV''utf-8');
  230.         $errors[] = $this->validator->validate(
  231.             $data['customer_kana02'],
  232.             [
  233.                 new Assert\NotBlank(),
  234.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_kana_len']]),
  235.                 new Assert\Regex(['pattern' => '/^[ァ-ヶヲ-゚ー]+$/u']),
  236.             ]);
  237.         $errors[] = $this->validator->validate(
  238.             $data['customer_company_name'],
  239.             [
  240.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
  241.             ]
  242.         );
  243.         $errors[] = $this->validator->validate(
  244.             $data['customer_phone_number'],
  245.             [
  246.                 new Assert\NotBlank(),
  247.                 new Assert\Type(['type' => 'digit''message' => 'form_error.numeric_only']),
  248.                 new Assert\Length(
  249.                     ['max' => $this->eccubeConfig['eccube_tel_len_max']]
  250.                 ),
  251.             ]
  252.         );
  253.         $errors[] = $this->validator->validate(
  254.             $data['customer_postal_code'],
  255.             [
  256.                 new Assert\NotBlank(),
  257.                 new Assert\Type(['type' => 'digit''message' => 'form_error.numeric_only']),
  258.                 new Assert\Length(
  259.                     ['max' => $this->eccubeConfig['eccube_postal_code']]
  260.                 ),
  261.             ]
  262.         );
  263.         $errors[] = $this->validator->validate(
  264.             $data['customer_addr01'],
  265.             [
  266.                 new Assert\NotBlank(),
  267.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_address1_len']]),
  268.             ]
  269.         );
  270.         $errors[] = $this->validator->validate(
  271.             $data['customer_addr02'],
  272.             [
  273.                 new Assert\NotBlank(),
  274.                 new Assert\Length(['max' => $this->eccubeConfig['eccube_address2_len']]),
  275.             ]
  276.         );
  277.         $errors[] = $this->validator->validate(
  278.             $data['customer_email'],
  279.             [
  280.                 new Assert\NotBlank(),
  281.                 new Email(nullnull$this->eccubeConfig['eccube_rfc_email_check'] ? 'strict' null),
  282.             ]
  283.         );
  284.         return $errors;
  285.     }
  286. }