app/Plugin/Paid4/Controller/MypagePaidEntryController.php line 69

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright(c) 2020 RACCOON HOLDINGS, Inc. All rights reserved.
  4.  * https://paid.jp/
  5.  */
  6. namespace Plugin\Paid4\Controller;
  7. use Eccube\Common\EccubeConfig;
  8. use Eccube\Controller\AbstractController;
  9. use Eccube\Entity\Customer;
  10. use Eccube\Repository\CustomerRepository;
  11. use \Exception;
  12. use Plugin\Paid4\Entity\PaidCustomer;
  13. use Plugin\Paid4\Form\Type\PaidEntryType;
  14. use Plugin\Paid4\Service\PaidApi\PaidHelperMember;
  15. use Plugin\Paid4\Service\PaidCustomerService;
  16. use Plugin\Paid4\Util\PaidUtil;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class MypagePaidEntryController extends AbstractController
  22. {
  23.     /**
  24.      * @var CustomerRepository
  25.      */
  26.     protected $customerRepository;
  27.     /**
  28.      * @var PaidCustomerService
  29.      */
  30.     protected $paidCustomerService;
  31.     /**
  32.      * @var PaidHelperMember
  33.      */
  34.     private $paidHelperMember;
  35.     /**
  36.      * EntryController constructor.
  37.      *
  38.      * @param EccubeConfig $eccubeConfig
  39.      * @param CustomerRepository $customerRepository
  40.      * @param PaidCustomerService $paidCustomerService
  41.      * @param PaidHelperMember $paidHelperMember
  42.      */
  43.     public function __construct(
  44.         EccubeConfig $eccubeConfig,
  45.         CustomerRepository $customerRepository,
  46.         PaidCustomerService $paidCustomerService,
  47.         PaidHelperMember $paidHelperMember
  48.     )
  49.     {
  50.         $this->eccubeConfig $eccubeConfig;
  51.         $this->customerRepository $customerRepository;
  52.         $this->paidCustomerService $paidCustomerService;
  53.         $this->paidHelperMember $paidHelperMember;
  54.     }
  55.     /**
  56.      * Paid登録画面を表示する.
  57.      *
  58.      * @Route("/mypage/paid_entry", name="paid_mypage_entry")
  59.      * @Template("@Paid4/default/Mypage/paid_mypage_entry.twig")
  60.      */
  61.     public function index(Request $request)
  62.     {
  63.         if (!$this->isGranted('ROLE_USER')) {
  64.             $this->serLogMessage('未ログインなのでトップへリダイレクト');
  65.             return $this->redirectToRoute('homepage');
  66.         }
  67.         /** @var Customer $Customer */
  68.         $Customer $this->getUser();
  69.         $PaidCustomer $Customer->getPaidCustomer();
  70.         // 登録済みユーザー等はredirect
  71.         if (!is_null($PaidCustomer)) {
  72.             // 登録済み、paid登録内容確認画面へリダイレクト
  73.             if ($this->paidCustomerService->checkPaidRegistered($PaidCustomer) == true) {
  74.                 $redirect_message trans('paid4.mypage.paid_information.redirect_message');
  75.                 return $this->redirectToRoute(
  76.                     'paid_mypage_information',
  77.                     array('redirect_message' => $redirect_message)
  78.                 );
  79.                 // paid利用不可会員
  80.             } else if ($this->paidCustomerService->checkPaidUseAvailable($PaidCustomer) == false) {
  81.                 return $this->redirectToRoute('homepage');
  82.             }
  83.         } else {
  84.             $PaidCustomer = new PaidCustomer();
  85.             // ユーザー情報の一部をpaid用入力フォームに表示する
  86.             $PaidCustomer $this->paidCustomerService->passCustomerData($Customer$PaidCustomer);
  87.         }
  88.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  89.         $builder $this->formFactory->createBuilder(PaidEntryType::class, $PaidCustomer, ['newPaidCustomer' => true]);
  90.         /* @var $form \Symfony\Component\Form\FormInterface */
  91.         $form $builder->getForm();
  92.         $form->handleRequest($request);
  93.         if ($form->isSubmitted() && $form->isValid()) {
  94.             switch ($request->get('mode')) {
  95.                 case 'confirm':
  96.                     $this->serLogMessage('Paid会員確認開始');
  97.                     return $this->render(
  98.                         '@Paid4/default/Mypage/paid_mypage_entry_confirm.twig',
  99.                         [
  100.                             'form' => $form->createView(),
  101.                         ]
  102.                     );
  103.                 case 'complete':
  104.                     $this->serLogMessage('Paid会員登録開始');
  105.                     $error_title trans('paid4.mypage.paid_entry.register_error_title');
  106.                     $error_message trans('paid4.mypage.paid_entry.register_error_message1');
  107.                     try {
  108.                         $PaidCustomer
  109.                             ->setCustomer($Customer)
  110.                             // 郵便番号にハイフンを入れる
  111.                             ->setZipCode(substr_replace($PaidCustomer->getZipCode(), '-'30))
  112.                             // 1:paid未登録, 2:paid登録済み
  113.                             ->setPaidRegister($this->paidCustomerService->getRegisterUnregisteredId())
  114.                             // 1:審査中, 2:利用可能-取引中, 3:利用可能-取引不可, 4:利用不可
  115.                             ->setPaidConfirm($this->paidCustomerService->getConfirmUnderReviewId())
  116.                             // Eccube運営者による利用可否設定, 1:利用可能, 2:利用不可
  117.                             ->setPaidUse($this->paidCustomerService->getUseAvailableId());
  118.                         $this->entityManager->persist($PaidCustomer);
  119.                         $this->entityManager->flush();
  120.                     } catch (Exception $e) {
  121.                         $this->serLogMessage('Paid会員登録失敗 - Exception['$e ']');
  122.                         return $this->renderErrorView($error_title$error_message);
  123.                     }
  124.                     $this->serLogMessage('Paid会員登録完了');
  125.                     $this->serLogMessage('Paid会員API開始');
  126.                     if ($this->paidHelperMember->registerMember($PaidCustomer)) {
  127.                         $this->serLogMessage('Paid会員API完了');
  128.                         $this->serLogMessage('Paid会員ステータス更新開始');
  129.                         try {
  130.                             // paid登録ステータスを「登録済み」に更新
  131.                             $PaidCustomer->setPaidRegister($this->paidCustomerService->getRegisterRegisteredId());
  132.                             $this->entityManager->persist($PaidCustomer);
  133.                             $this->entityManager->flush();
  134.                             $this->serLogMessage('Paid会員ステータス更新完了');
  135.                         } catch (Exception $e) {
  136.                             $this->serLogMessage('Paid会員ステータス更新失敗 - Exception['$e ']');
  137.                             return $this->renderErrorView($error_title$error_message$this->paidHelperMember->getErrorMessage());
  138.                         }
  139.                         return $this->redirect($this->generateUrl('paid_mypage_entry_complete'));
  140.                     } else {
  141.                         $this->serLogMessage('Paid会員API失敗');
  142.                         return $this->renderErrorView($error_title$error_message$this->paidHelperMember->getErrorMessage());
  143.                     }
  144.             }
  145.         }
  146.         return [
  147.             'paid_form' => $form->createView(),
  148.         ];
  149.     }
  150.     /**
  151.      * 会員登録完了画面.
  152.      *
  153.      * @Route("/mypage/paid_entry_complete", name="paid_mypage_entry_complete")
  154.      * @Template("@Paid4/default/Mypage/paid_mypage_entry_complete.twig")
  155.      */
  156.     public function complete()
  157.     {
  158.         return [];
  159.     }
  160.     /**
  161.      * エラー画面を表示
  162.      * @param string $title
  163.      * @param string $message
  164.      * @param string $api_message
  165.      * @return Response
  166.      */
  167.     private function renderErrorView($title ''$message '' $api_message "" )
  168.     {
  169.         $this->serLogMessage('Paid登録失敗画面表示開始');
  170.         return $this->render(
  171.             '@Paid4/error.twig',
  172.             [
  173.                 'error_title' => $title,
  174.                 'error_message' => $message,
  175.                 'error_api_message' => $api_message,
  176.             ]
  177.         );
  178.     }
  179.     /**
  180.      * 会員情報を検索する.
  181.      *
  182.      * @Route("/mypage/paid_entry/search/customer/id", name="front_paid_search_customer_by_id", methods={"POST"})
  183.      *
  184.      * @param Request $request
  185.      *
  186.      * @param $error_nos
  187.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  188.      */
  189.     public function searchCustomerById(Request $request)
  190.     {
  191.         if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
  192.             /** @var Customer $Customer */
  193.             $Customer $this->getUser();
  194.             if (is_null($Customer)) {
  195.                 return $this->json([], 404);
  196.             }
  197.             $data $this->paidCustomerService->getTrimText($Customer);
  198.             if (is_null($data)) {
  199.                 return $this->json([], 404);
  200.             }
  201.             return $this->json($data);
  202.         }
  203.     }
  204.     /**
  205.      * @param string $Message
  206.      */
  207.     private function serLogMessage($Message '')
  208.     {
  209.         PaidUtil::logInfo(__NAMESPACE__ get_class($this) .' - '$Message);
  210.     }
  211. }