app/Plugin/ZeusPayment42/ZeusPaymentEvent.php line 270

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 Plugin\ZeusPayment42\Service\Method\EbankPayment;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Plugin\ZeusPayment42\Repository\ConfigRepository;
  10. use Plugin\ZeusPayment42\Service\ZeusPaymentService;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  14. use Eccube\Service\CartService;
  15. use Eccube\Service\OrderHelper;
  16. use Eccube\Repository\OrderRepository;
  17. use Plugin\ZeusPayment42\Service\Method\CvsPayment;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. /*
  22.  * イベント処理
  23.  */
  24. class ZeusPaymentEvent implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var ConfigRepository
  28.      */
  29.     protected $configRepository;
  30.     protected $paymentService;
  31.     protected $eccubeConfig;
  32.     protected $cartService;
  33.     protected $orderHelper;
  34.     protected $orderRepository;
  35.     protected $entityManager;
  36.     protected $router;
  37.     protected $session;
  38.     
  39.     public function __construct(
  40.         ConfigRepository $configRepository,
  41.         ZeusPaymentService $paymentService,
  42.         EccubeConfig $eccubeConfig,
  43.         CartService $cartService,
  44.         OrderHelper $orderHelper,
  45.         OrderRepository $orderRepository,
  46.         EntityManagerInterface $entityManager,
  47.         RouterInterface $router,
  48.         SessionInterface $session
  49.         )
  50.     {
  51.         $this->configRepository $configRepository;
  52.         $this->paymentService $paymentService;
  53.         $this->eccubeConfig $eccubeConfig;
  54.         $this->cartService $cartService;
  55.         $this->orderHelper $orderHelper;
  56.         $this->orderRepository $orderRepository;
  57.         $this->entityManager $entityManager;
  58.         $this->router $router;
  59.         $this->session $session;
  60.     }
  61.     
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             'Shopping/index.twig' => 'onShoppingIndexTwig',
  66.             'Shopping/confirm.twig' => 'onShoppingConfirmTwig',
  67.             '@admin/Order/index.twig' => 'adminOrderIndexTwig',
  68.             '@admin/Order/edit.twig' => 'adminOrderEditTwig',
  69.             KernelEvents::CONTROLLER => array('onKernelController'130)
  70.         ];
  71.     }
  72.     
  73.     public function onShoppingIndexTwig(TemplateEvent $event)
  74.     {
  75.         $event->addSnippet('@ZeusPayment42/credit_input_move_point.twig');
  76.         
  77.         $config $this->configRepository->get();
  78.         if (!$config || !$config->getCreditPayment()) {
  79.             return;
  80.         }
  81.         
  82.         $parameters $event->getParameters();
  83.         
  84.         
  85.         $zeus_credit = array();
  86.         $zeus_credit['credit_class_name'] = \Plugin\ZeusPayment42\Service\Method\CreditPayment::class;
  87.         $zeus_credit['config'] = $config;
  88.         
  89.         $order $parameters['Order'];
  90.         $payment $order->getPayment();
  91.         if (!$payment || $payment->getMethodClass() != $zeus_credit['credit_class_name']) {
  92.             return;
  93.         }
  94.         $zeus_credit['payment_id'] = $payment->getId();
  95.         $shippings $payment->getPaymentOptions();
  96.         $zeus_credit['shippings'] = "[]";
  97.         if ($shippings) {
  98.             $aShippings = [];
  99.             foreach ($shippings as $shipping) {
  100.                 $aShippings[] = $shipping->getDeliveryId();
  101.             }
  102.             $zeus_credit['shippings'] = "[" implode(','$aShippings) . "]";
  103.         }
  104.         
  105.         //if this field not exists,do not display
  106.         if (!$parameters['form']['ZeusCreditPaymentCardNo']) {
  107.             return;
  108.         }
  109.         
  110.         $event->addSnippet('@ZeusPayment42/credit_input.twig');
  111.         $parameters $event->getParameters();
  112.         
  113.         $quickOK $this->paymentService->isQuickChargeOK($order$config->getCreditPayment());
  114.         if ($quickOK) {
  115.             $lastMaskedCard $this->paymentService->fetchMaskedCard($order$config);
  116.             $zeus_credit['lastMaskedCard'] = $lastMaskedCard;
  117.         } else {
  118.             $zeus_credit['lastMaskedCard'] = '';
  119.         }
  120.         $zeus_credit['isQuickChargeOK'] = $quickOK;
  121.         $parameters['zeus_credit'] = $zeus_credit;
  122.         $event->setParameters($parameters);
  123.     }
  124.     
  125.     public function onShoppingConfirmTwig(TemplateEvent $event)
  126.     {
  127.         $event->addSnippet('@ZeusPayment42/credit_confirm_move_point.twig');
  128.         
  129.         $config $this->configRepository->get();
  130.         
  131.         if (!$config || !$config->getCreditPayment()) {
  132.             return;
  133.         }
  134.         
  135.         $this->csvRenameButton($event,$config);
  136.         $this->ebankRenameButton($event,$config);
  137.         
  138.         $parameters $event->getParameters();
  139.         $zeus_credit = array();
  140.         $zeus_credit['credit_class_name'] = \Plugin\ZeusPayment42\Service\Method\CreditPayment::class;
  141.         $zeus_credit['config'] = $config;
  142.         
  143.         $order $parameters['Order'];
  144.         $payment $order->getPayment();
  145.         if (!$payment || $payment->getMethodClass() != $zeus_credit['credit_class_name']) {
  146.             return;
  147.         }
  148.         $zeus_credit['payment_method'] = $payment->getMethod();
  149.         
  150.         $event->addSnippet('@ZeusPayment42/credit_confirm.twig');
  151.         $parameters $event->getParameters();
  152.         $formview $parameters['form'];
  153.         $zeus_credit['mask_cark_no'] = $this->paymentService->getMaskedCard($formview['ZeusCreditPaymentCardNo']->vars['value']);
  154.         
  155.         $paras $this->eccubeConfig['zeus_credit_options'];
  156.         $methods $paras['payment_choices'];
  157.         $paymentMethod $formview['ZeusCreditPaymentMethod']->vars['value'];
  158.         $zeus_credit['payment_method_name'] = $methods[$paymentMethod];
  159.         
  160.         
  161.         
  162.         $quickOK $this->paymentService->isQuickChargeOK($order$config->getCreditPayment());
  163.         if ($quickOK) {
  164.             $lastMaskedCard $this->paymentService->fetchMaskedCard($order$config);
  165.             $zeus_credit['lastMaskedCard'] = $lastMaskedCard;
  166.         } else {
  167.             $zeus_credit['lastMaskedCard'] = '';
  168.         }
  169.         $zeus_credit['isQuickChargeOK'] = $quickOK;
  170.         
  171.         $parameters['zeus_credit'] = $zeus_credit;
  172.         
  173.         $event->setParameters($parameters);
  174.     }
  175.     
  176.     
  177.     private function csvRenameButton($event$config){
  178.         
  179.         $parameters $event->getParameters();
  180.         $order $parameters['Order'];
  181.         
  182.         $payment $order->getPayment();
  183.         if (!$payment || $payment->getMethodClass() != \Plugin\ZeusPayment42\Service\Method\CvsPayment::class) {
  184.             return;
  185.         }
  186.         
  187.         $event->addSnippet('@ZeusPayment42/cvs_confirm_rename_button.twig');
  188.     }
  189.     
  190.     private function ebankRenameButton($event$config){
  191.         
  192.         $parameters $event->getParameters();
  193.         $order $parameters['Order'];
  194.         
  195.         $payment $order->getPayment();
  196.         if (!$payment || $payment->getMethodClass() != \Plugin\ZeusPayment42\Service\Method\EbankPayment::class) {
  197.             return;
  198.         }
  199.         
  200.         $event->addSnippet('@ZeusPayment42/ebank_confirm_rename_button.twig');
  201.     }
  202.     
  203.     public function onKernelController(ControllerEvent $event)
  204.     {
  205.         $request $event->getRequest();
  206.         
  207.         ///only front page
  208.         if(strpos($request->getRequestUri(),$this->eccubeConfig['eccube_admin_route'])!==false){
  209.             return;
  210.         }
  211.         
  212.         $preOrderId $this->cartService->getPreOrderId();
  213.         $order $this->orderRepository->findOneBy([
  214.             'pre_order_id' => $preOrderId
  215.             //'OrderStatus' => OrderStatus::PENDING,
  216.         ]);
  217.         if(!$order){
  218.             return;
  219.         }
  220.         $payment $order->getPayment();
  221.         if(!$payment){
  222.             return;
  223.         }
  224.         if($payment->getMethodClass()!=CvsPayment::class && $payment->getMethodClass()!=EbankPayment::class){
  225.             return;
  226.         }
  227.         
  228.         if($order->getOrderStatus()->getId()!=OrderStatus::PENDING && $order->getOrderStatus()->getId()!=OrderStatus::PROCESSING){
  229.             
  230.             $saveOrderErr "注文情報作成失敗しました。";
  231.             $note $order->getNote();
  232.             $saveOrderFailed = ($saveOrderErr === substr($note, - strlen($saveOrderErr)));
  233.             
  234.             log_info('[注文処理] カートをクリアします.', [$order->getId()]);
  235.             $this->cartService->clear();
  236.             
  237.             if($saveOrderFailed){
  238.                 
  239.                 $msg '';
  240.                 if (strlen($order->getNote()) > 0) {
  241.                     $str $order->getNote() . "\r\n";
  242.                 } else {
  243.                     $str "";
  244.                 }
  245.                 
  246.                 $msg "支払手続きは完了している可能性がありますので、サイトまでお問い合わせください。";
  247.                 $order->setNote($str "メッセージ表示中:" $msg);
  248.                 
  249.                 $order->setOrderStatus($this->entityManager->getRepository(OrderStatus::class)->find(OrderStatus::PENDING));
  250.                 
  251.                 $this->entityManager->persist($order);
  252.                 $this->entityManager->flush();
  253.                 $this->entityManager->commit();
  254.                 
  255.                 $this->session->getFlashBag()->add('eccube.front.error''購入処理でエラーが発生しました。' $msg);
  256.                 
  257.                 //$event->setResponse(new RedirectResponse($this->router->generate('shopping_error')));
  258.                 $redirect = new RedirectResponse($this->router->generate('shopping_error'));
  259.                 $redirect->send();
  260.                 
  261.             }
  262.         }
  263.         
  264.     }
  265.     
  266.     public function adminOrderIndexTwig(TemplateEvent $event)
  267.     {
  268.         $event->addSnippet('@ZeusPayment42/admin/order_index_js.twig');
  269.     }
  270.     
  271.     public function adminOrderEditTwig(TemplateEvent $event)
  272.     {
  273.         $event->addSnippet('@ZeusPayment42/admin/order_edit_js.twig');
  274.     }
  275. }