app/Plugin/ZeusPayment42/ZeusPaymentEvent.php line 252

Open in your IDE?
  1. <?php
  2. namespace Plugin\ZeusPayment42;
  3. use Eccube\Common\EccubeConfig;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Eccube\Entity\Master\OrderStatus;
  6. use Eccube\Event\TemplateEvent;
  7. use Eccube\Event\EccubeEvents;
  8. use Eccube\Event\EventArgs;
  9. use Plugin\ZeusPayment42\Service\Method\EbankPayment;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Plugin\ZeusPayment42\Repository\ConfigRepository;
  12. use Plugin\ZeusPayment42\Service\ZeusPaymentService;
  13. use Plugin\ZeusPayment42\Entity\Config;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  16. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  17. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  18. use Eccube\Service\CartService;
  19. use Eccube\Service\OrderHelper;
  20. use Eccube\Repository\OrderRepository;
  21. use Plugin\ZeusPayment42\Service\Method\CvsPayment;
  22. use Symfony\Component\Routing\RouterInterface;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\HttpFoundation\RequestStack;
  25. use Eccube\Request\Context;
  26. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  27. /*
  28.  * イベント処理
  29.  */
  30. class ZeusPaymentEvent implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @var ConfigRepository
  34.      */
  35.     protected $configRepository;
  36.     protected $paymentService;
  37.     protected $eccubeConfig;
  38.     protected $cartService;
  39.     protected $orderHelper;
  40.     protected $orderRepository;
  41.     protected $entityManager;
  42.     protected $router;
  43.     protected $session;
  44.     private $context;
  45.     private $eventDispatcher;
  46.     
  47.     public function __construct(
  48.         ConfigRepository $configRepository,
  49.         ZeusPaymentService $paymentService,
  50.         EccubeConfig $eccubeConfig,
  51.         CartService $cartService,
  52.         OrderHelper $orderHelper,
  53.         OrderRepository $orderRepository,
  54.         EntityManagerInterface $entityManager,
  55.         RouterInterface $router,
  56.         RequestStack $requestStack,
  57.         Context $context,
  58.         EventDispatcherInterface $eventDispatcher
  59.         )
  60.     {
  61.         $this->configRepository $configRepository;
  62.         $this->paymentService $paymentService;
  63.         $this->eccubeConfig $eccubeConfig;
  64.         $this->cartService $cartService;
  65.         $this->orderHelper $orderHelper;
  66.         $this->orderRepository $orderRepository;
  67.         $this->entityManager $entityManager;
  68.         $this->router $router;
  69.         $this->session $requestStack->getSession();
  70.         $this->context $context;
  71.         $this->eventDispatcher $eventDispatcher;
  72.     }
  73.     
  74.     public static function getSubscribedEvents()
  75.     {
  76.         return [
  77.             'Shopping/index.twig' => 'onShoppingIndexTwig',
  78.             'Shopping/confirm.twig' => 'onShoppingConfirmTwig',
  79.             '@admin/Order/index.twig' => 'adminOrderIndexTwig',
  80.             '@admin/Order/edit.twig' => 'adminOrderEditTwig',
  81.             KernelEvents::CONTROLLER => array('onKernelController'130),
  82.             KernelEvents::CONTROLLER_ARGUMENTS => 'onKernelControllerArguments',
  83.         ];
  84.     }
  85.     
  86.     public function onShoppingIndexTwig(TemplateEvent $event)
  87.     {
  88.         $event->addSnippet('@ZeusPayment42/credit_input_move_point.twig');
  89.         
  90.         $config $this->configRepository->get();
  91.         if (!$config && !$config->getCreditPayment() && !$config->getLaterPayment()) {
  92.             return;
  93.         }
  94.         
  95.         $parameters $event->getParameters();
  96.         
  97.         
  98.         $zeus_credit = array();
  99.         $zeus_credit['credit_class_name'] = \Plugin\ZeusPayment42\Service\Method\CreditPayment::class;
  100.         $zeus_credit['config'] = $config;
  101.         $later = array();
  102.         $later['class_name'] = \Plugin\ZeusPayment42\Service\Method\LaterPayment::class;
  103.         $later['config'] = $config;
  104.         
  105.         $order $parameters['Order'];
  106.         $payment $order->getPayment();
  107.         if(!$payment){
  108.             return;
  109.         }
  110.         if($payment->getMethodClass() == $zeus_credit['credit_class_name']){
  111.             $zeus_credit['payment_id'] = $payment->getId();
  112.             $shippings $payment->getPaymentOptions();
  113.             $zeus_credit['shippings'] = "[]";
  114.             if ($shippings) {
  115.                 $aShippings = [];
  116.                 foreach ($shippings as $shipping) {
  117.                     $aShippings[] = $shipping->getDeliveryId();
  118.                 }
  119.                 $zeus_credit['shippings'] = "[" implode(','$aShippings) . "]";
  120.             }
  121.             
  122.             //if this field not exists,do not display
  123.             if (!$parameters['form']['ZeusCreditPaymentCardNo']) {
  124.                 return;
  125.             }
  126.             
  127.             $event->addSnippet('@ZeusPayment42/credit_input.twig');
  128.             $parameters $event->getParameters();
  129.             
  130.             $quickOK $this->paymentService->isQuickChargeOK($order$config->getCreditPayment());
  131.             if ($quickOK) {
  132.                 $lastMaskedCard $this->paymentService->fetchMaskedCard($order$config);
  133.                 $zeus_credit['lastMaskedCard'] = $lastMaskedCard;
  134.             } else {
  135.                 $zeus_credit['lastMaskedCard'] = '';
  136.             }
  137.             $zeus_credit['isQuickChargeOK'] = $quickOK;
  138.             $parameters['zeus_credit'] = $zeus_credit;
  139.             
  140.         }elseif($payment->getMethodClass() == $later['class_name']){
  141.             $later['payment_id'] = $payment->getId();
  142.             $event->addSnippet('@ZeusPayment42/later_input.twig');
  143.             $parameters $event->getParameters();
  144.             $parameters['later'] = $later;
  145.         }
  146.         $event->setParameters($parameters);
  147.     }
  148.     
  149.     public function onShoppingConfirmTwig(TemplateEvent $event)
  150.     {
  151.         $event->addSnippet('@ZeusPayment42/credit_confirm_move_point.twig');
  152.         
  153.         $config $this->configRepository->get();
  154.         
  155.         if (!$config && !$config->getCreditPayment() && !$config->getLaterPayment()) {
  156.             return;
  157.         }
  158.         
  159.         $this->csvRenameButton($event,$config);
  160.         $this->ebankRenameButton($event,$config);
  161.         $parameters $event->getParameters();
  162.         $zeus_credit = array();
  163.         $zeus_credit['credit_class_name'] = \Plugin\ZeusPayment42\Service\Method\CreditPayment::class;
  164.         $zeus_credit['config'] = $config;
  165.         $later = array();
  166.         $later['class_name'] = \Plugin\ZeusPayment42\Service\Method\LaterPayment::class;
  167.         $later['config'] = $config;
  168.         
  169.         $order $parameters['Order'];
  170.         $payment $order->getPayment();
  171.         
  172.         if ($payment && $payment->getMethodClass() == $zeus_credit['credit_class_name']) {
  173.             $zeus_credit['payment_method'] = $payment->getMethod();
  174.         
  175.             $event->addSnippet('@ZeusPayment42/credit_confirm.twig');
  176.             $parameters $event->getParameters();
  177.             $formview $parameters['form'];
  178.             $zeus_credit['mask_cark_no'] = $this->paymentService->getMaskedCard($formview['ZeusCreditPaymentCardNo']->vars['value']);
  179.             
  180.             $paras $this->eccubeConfig['zeus_credit_options'];
  181.             $methods $paras['payment_choices'];
  182.             $paymentMethod $formview['ZeusCreditPaymentMethod']->vars['value'];
  183.             $zeus_credit['payment_method_name'] = $methods[$paymentMethod];
  184.             
  185.             $quickOK $this->paymentService->isQuickChargeOK($order$config->getCreditPayment());
  186.             if ($quickOK) {
  187.                 $lastMaskedCard $this->paymentService->fetchMaskedCard($order$config);
  188.                 $zeus_credit['lastMaskedCard'] = $lastMaskedCard;
  189.             } else {
  190.                 $zeus_credit['lastMaskedCard'] = '';
  191.             }
  192.             $zeus_credit['isQuickChargeOK'] = $quickOK;
  193.             $parameters['zeus_credit'] = $zeus_credit;
  194.         }elseif($payment->getMethodClass() == $later['class_name']){
  195.             $later['payment_method'] = $payment->getMethod();
  196.         
  197.             $event->addSnippet('@ZeusPayment42/later_confirm.twig');
  198.             $parameters $event->getParameters();
  199.             $formview $parameters['form'];
  200.             $later['phone_number'] = $formview['later_phone_number']->vars['value'];
  201.             $parameters['later'] = $later;
  202.         }
  203.         
  204.         $event->setParameters($parameters);
  205.     }
  206.     
  207.     
  208.     private function csvRenameButton($event$config){
  209.         
  210.         $parameters $event->getParameters();
  211.         $order $parameters['Order'];
  212.         
  213.         $payment $order->getPayment();
  214.         if (!$payment || $payment->getMethodClass() != \Plugin\ZeusPayment42\Service\Method\CvsPayment::class) {
  215.             return;
  216.         }
  217.         
  218.         $event->addSnippet('@ZeusPayment42/cvs_confirm_rename_button.twig');
  219.     }
  220.     
  221.     private function ebankRenameButton($event$config){
  222.         
  223.         $parameters $event->getParameters();
  224.         $order $parameters['Order'];
  225.         
  226.         $payment $order->getPayment();
  227.         if (!$payment || $payment->getMethodClass() != \Plugin\ZeusPayment42\Service\Method\EbankPayment::class) {
  228.             return;
  229.         }
  230.         
  231.         $event->addSnippet('@ZeusPayment42/ebank_confirm_rename_button.twig');
  232.     }
  233.     
  234.     public function onKernelController(ControllerEvent $event)
  235.     {
  236.         $request $event->getRequest();
  237.         
  238.         ///only front page
  239.         if(strpos($request->getRequestUri(),$this->eccubeConfig['eccube_admin_route'])!==false){
  240.             return;
  241.         }
  242.         
  243.         $preOrderId $this->cartService->getPreOrderId();
  244.         $order $this->orderRepository->findOneBy([
  245.             'pre_order_id' => $preOrderId
  246.             //'OrderStatus' => OrderStatus::PENDING,
  247.         ]);
  248.         if(!$order){
  249.             return;
  250.         }
  251.         $payment $order->getPayment();
  252.         if(!$payment){
  253.             return;
  254.         }
  255.         if($payment->getMethodClass()!=CvsPayment::class && $payment->getMethodClass()!=EbankPayment::class){
  256.             return;
  257.         }
  258.         
  259.         if($order->getOrderStatus()->getId()!=OrderStatus::PENDING && $order->getOrderStatus()->getId()!=OrderStatus::PROCESSING){
  260.             
  261.             $saveOrderErr "注文情報作成失敗しました。";
  262.             $note $order->getNote();
  263.             $saveOrderFailed = ($saveOrderErr === substr($note, - strlen($saveOrderErr)));
  264.             
  265.             log_info('[注文処理] カートをクリアします.', [$order->getId()]);
  266.             $this->cartService->clear();
  267.             
  268.             if($saveOrderFailed){
  269.                 
  270.                 $msg '';
  271.                 if (strlen($order->getNote()) > 0) {
  272.                     $str $order->getNote() . "\r\n";
  273.                 } else {
  274.                     $str "";
  275.                 }
  276.                 
  277.                 $msg "支払手続きは完了している可能性がありますので、サイトまでお問い合わせください。";
  278.                 $order->setNote($str "メッセージ表示中:" $msg);
  279.                 
  280.                 $order->setOrderStatus($this->entityManager->getRepository(OrderStatus::class)->find(OrderStatus::PENDING));
  281.                 
  282.                 $this->entityManager->persist($order);
  283.                 $this->entityManager->flush();
  284.                 $this->entityManager->commit();
  285.                 
  286.                 $this->session->getFlashBag()->add('eccube.front.error''購入処理でエラーが発生しました。' $msg);
  287.                 
  288.                 //$event->setResponse(new RedirectResponse($this->router->generate('shopping_error')));
  289.                 $redirect = new RedirectResponse($this->router->generate('shopping_error'));
  290.                 $redirect->send();
  291.                 
  292.             }
  293.         }
  294.         
  295.     }
  296.     
  297.     public function adminOrderIndexTwig(TemplateEvent $event)
  298.     {
  299.         $event->addSnippet('@ZeusPayment42/admin/order_index_js.twig');
  300.     }
  301.     
  302.     public function adminOrderEditTwig(TemplateEvent $event)
  303.     {
  304.         $parameters $event->getParameters();
  305.         $order $parameters['Order'];
  306.         
  307.         if($order->getId() == null){
  308.             return;
  309.         }
  310.         $payment_id $order->getPayment()->getId();
  311.         $config $this->configRepository->get();
  312.         $payments $config->getPayments();
  313.         foreach ($payments as $payment) {
  314.             $paymentIds[] = $payment->getId();
  315.         }
  316.         $isNonZeusPayment = (!in_array($payment_id$paymentIds) || !$order->getZeusOrderId()) ? true false;
  317.         $event->addSnippet('@ZeusPayment42/admin/order_edit_js.twig');
  318.         $event->setParameter('config'$config);
  319.         $event->setParameter('isNonZeusPayment'$isNonZeusPayment);
  320.         
  321.         $event->setParameter('creditPayment'$config->getCreditPayment());
  322.         $event->setParameter('laterPayment'$config->getLaterPayment());
  323.         $event->setParameter('cvsPayment'$config->getCvsPayment());
  324.         $event->setParameter('ebankPayment'$config->getEbankPayment());
  325.         
  326.         $event->setParameter('zeusCreditSaleTypes'$config->getZeusSaleTypes());
  327.         $event->setParameter('zeusLaterSaleTypes'$config->getZeusLaterSaleTypes());
  328.         $event->setParameter('zeusCvsSaleTypes'$config->getZeusCvsSaleTypes());
  329.         $event->setParameter('zeusEbankSaleTypes'$config->getZeusEbankSaleTypes());
  330.         
  331.     }
  332.     public function onKernelControllerArguments(ControllerArgumentsEvent $event)
  333.     {
  334.         if (!$this->context->isAdmin()) {
  335.             return;
  336.         }
  337.         
  338.         if (!$event->getRequest()->attributes->has('_template')) {
  339.             return;
  340.         }
  341.         /* @var Template $template */
  342.         $template $event->getRequest()->attributes->get('_template');
  343.         
  344.         $this->eventDispatcher->addListener($template->getTemplate(), function (TemplateEvent $templateEvent) {
  345.             $templateEvent->addSnippet('@ZeusPayment42/admin/nav.twig');
  346.         });
  347.     }
  348. }