app/Customize/Plugin/ZeusPayment42/ZeusPaymentEvent.php line 254

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