<?php
/*
* Copyright(c) 2020 RACCOON HOLDINGS, Inc. All rights reserved.
* https://paid.jp/
*/
namespace Plugin\Paid4\Controller;
use Eccube\Common\EccubeConfig;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Customer;
use Eccube\Repository\CustomerRepository;
use \Exception;
use Plugin\Paid4\Entity\PaidCustomer;
use Plugin\Paid4\Form\Type\PaidEntryType;
use Plugin\Paid4\Service\PaidApi\PaidHelperMember;
use Plugin\Paid4\Service\PaidCustomerService;
use Plugin\Paid4\Util\PaidUtil;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class MypagePaidEntryController extends AbstractController
{
/**
* @var CustomerRepository
*/
protected $customerRepository;
/**
* @var PaidCustomerService
*/
protected $paidCustomerService;
/**
* @var PaidHelperMember
*/
private $paidHelperMember;
/**
* EntryController constructor.
*
* @param EccubeConfig $eccubeConfig
* @param CustomerRepository $customerRepository
* @param PaidCustomerService $paidCustomerService
* @param PaidHelperMember $paidHelperMember
*/
public function __construct(
EccubeConfig $eccubeConfig,
CustomerRepository $customerRepository,
PaidCustomerService $paidCustomerService,
PaidHelperMember $paidHelperMember
)
{
$this->eccubeConfig = $eccubeConfig;
$this->customerRepository = $customerRepository;
$this->paidCustomerService = $paidCustomerService;
$this->paidHelperMember = $paidHelperMember;
}
/**
* Paid登録画面を表示する.
*
* @Route("/mypage/paid_entry", name="paid_mypage_entry")
* @Template("@Paid4/default/Mypage/paid_mypage_entry.twig")
*/
public function index(Request $request)
{
if (!$this->isGranted('ROLE_USER')) {
$this->serLogMessage('未ログインなのでトップへリダイレクト');
return $this->redirectToRoute('homepage');
}
/** @var Customer $Customer */
$Customer = $this->getUser();
$PaidCustomer = $Customer->getPaidCustomer();
// 登録済みユーザー等はredirect
if (!is_null($PaidCustomer)) {
// 登録済み、paid登録内容確認画面へリダイレクト
if ($this->paidCustomerService->checkPaidRegistered($PaidCustomer) == true) {
$redirect_message = trans('paid4.mypage.paid_information.redirect_message');
return $this->redirectToRoute(
'paid_mypage_information',
array('redirect_message' => $redirect_message)
);
// paid利用不可会員
} else if ($this->paidCustomerService->checkPaidUseAvailable($PaidCustomer) == false) {
return $this->redirectToRoute('homepage');
}
} else {
$PaidCustomer = new PaidCustomer();
// ユーザー情報の一部をpaid用入力フォームに表示する
$PaidCustomer = $this->paidCustomerService->passCustomerData($Customer, $PaidCustomer);
}
/* @var $builder \Symfony\Component\Form\FormBuilderInterface */
$builder = $this->formFactory->createBuilder(PaidEntryType::class, $PaidCustomer, ['newPaidCustomer' => true]);
/* @var $form \Symfony\Component\Form\FormInterface */
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
switch ($request->get('mode')) {
case 'confirm':
$this->serLogMessage('Paid会員確認開始');
return $this->render(
'@Paid4/default/Mypage/paid_mypage_entry_confirm.twig',
[
'form' => $form->createView(),
]
);
case 'complete':
$this->serLogMessage('Paid会員登録開始');
$error_title = trans('paid4.mypage.paid_entry.register_error_title');
$error_message = trans('paid4.mypage.paid_entry.register_error_message1');
try {
$PaidCustomer
->setCustomer($Customer)
// 郵便番号にハイフンを入れる
->setZipCode(substr_replace($PaidCustomer->getZipCode(), '-', 3, 0))
// 1:paid未登録, 2:paid登録済み
->setPaidRegister($this->paidCustomerService->getRegisterUnregisteredId())
// 1:審査中, 2:利用可能-取引中, 3:利用可能-取引不可, 4:利用不可
->setPaidConfirm($this->paidCustomerService->getConfirmUnderReviewId())
// Eccube運営者による利用可否設定, 1:利用可能, 2:利用不可
->setPaidUse($this->paidCustomerService->getUseAvailableId());
$this->entityManager->persist($PaidCustomer);
$this->entityManager->flush();
} catch (Exception $e) {
$this->serLogMessage('Paid会員登録失敗 - Exception['. $e . ']');
return $this->renderErrorView($error_title, $error_message);
}
$this->serLogMessage('Paid会員登録完了');
$this->serLogMessage('Paid会員API開始');
if ($this->paidHelperMember->registerMember($PaidCustomer)) {
$this->serLogMessage('Paid会員API完了');
$this->serLogMessage('Paid会員ステータス更新開始');
try {
// paid登録ステータスを「登録済み」に更新
$PaidCustomer->setPaidRegister($this->paidCustomerService->getRegisterRegisteredId());
$this->entityManager->persist($PaidCustomer);
$this->entityManager->flush();
$this->serLogMessage('Paid会員ステータス更新完了');
} catch (Exception $e) {
$this->serLogMessage('Paid会員ステータス更新失敗 - Exception['. $e . ']');
return $this->renderErrorView($error_title, $error_message, $this->paidHelperMember->getErrorMessage());
}
return $this->redirect($this->generateUrl('paid_mypage_entry_complete'));
} else {
$this->serLogMessage('Paid会員API失敗');
return $this->renderErrorView($error_title, $error_message, $this->paidHelperMember->getErrorMessage());
}
}
}
return [
'paid_form' => $form->createView(),
];
}
/**
* 会員登録完了画面.
*
* @Route("/mypage/paid_entry_complete", name="paid_mypage_entry_complete")
* @Template("@Paid4/default/Mypage/paid_mypage_entry_complete.twig")
*/
public function complete()
{
return [];
}
/**
* エラー画面を表示
* @param string $title
* @param string $message
* @param string $api_message
* @return Response
*/
private function renderErrorView($title = '', $message = '' , $api_message = "" )
{
$this->serLogMessage('Paid登録失敗画面表示開始');
return $this->render(
'@Paid4/error.twig',
[
'error_title' => $title,
'error_message' => $message,
'error_api_message' => $api_message,
]
);
}
/**
* 会員情報を検索する.
*
* @Route("/mypage/paid_entry/search/customer/id", name="front_paid_search_customer_by_id", methods={"POST"})
*
* @param Request $request
*
* @param $error_nos
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function searchCustomerById(Request $request)
{
if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
/** @var Customer $Customer */
$Customer = $this->getUser();
if (is_null($Customer)) {
return $this->json([], 404);
}
$data = $this->paidCustomerService->getTrimText($Customer);
if (is_null($data)) {
return $this->json([], 404);
}
return $this->json($data);
}
}
/**
* @param string $Message
*/
private function serLogMessage($Message = '')
{
PaidUtil::logInfo(__NAMESPACE__ . get_class($this) .' - '. $Message);
}
}