app/Plugin/YamatoPayment42/YamatoPaymentEvent.php line 250

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.lockon.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\YamatoPayment42;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Repository\ProductRepository;
  16. use Eccube\Repository\OrderRepository;
  17. use Eccube\Repository\PaymentRepository;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Plugin\YamatoPayment42\Repository\ConfigRepository;
  23. use Plugin\YamatoPayment42\Repository\YamatoOrderRepository;
  24. use Plugin\YamatoPayment42\Repository\YamatoPaymentMethodRepository;
  25. use Plugin\YamatoPayment42\Service\Client\CreditClientService;
  26. use Plugin\YamatoPayment42\Service\Client\CvsClientService;
  27. use Plugin\YamatoPayment42\Service\Method AS YamatoMethod;
  28. class YamatoPaymentEvent implements EventSubscriberInterface
  29. {
  30.     protected $eccubeConfig;
  31.     protected $paymentRepository;
  32.     protected $yamatoPluginRepository;
  33.     protected $yamatoPaymentMethodRepository;
  34.     protected $yamatoOrderRepository;
  35.     protected $productRepository;
  36.     protected $customerRepository;
  37.     protected $router;
  38.     protected $client;
  39.     public function __construct(
  40.         EccubeConfig $eccubeConfig,
  41.         ProductRepository $productRepository,
  42.         OrderRepository $orderRepository,
  43.         PaymentRepository $paymentRepository,
  44.         ConfigRepository $yamatoPluginRepository,
  45.         YamatoPaymentMethodRepository $yamatoPaymentMethodRepository,
  46.         YamatoOrderRepository $yamatoOrderRepository,
  47.         RouterInterface $router,
  48.         RequestStack $requestStack
  49.     ) {
  50.         $this->eccubeConfig $eccubeConfig;
  51.         $this->productRepository $productRepository;
  52.         $this->orderRepository $orderRepository;
  53.         $this->paymentRepository $paymentRepository;
  54.         $this->yamatoPluginRepository $yamatoPluginRepository;
  55.         $this->yamatoPaymentMethodRepository $yamatoPaymentMethodRepository;
  56.         $this->yamatoOrderRepository $yamatoOrderRepository;
  57.         $this->router $router;
  58.         $this->requestStack $requestStack;
  59.         $this->client = new CreditClientService(
  60.             $this->eccubeConfig,
  61.             $this->productRepository,
  62.             $this->orderRepository,
  63.             $this->yamatoPluginRepository,
  64.             $this->yamatoPaymentMethodRepository,
  65.             $this->yamatoOrderRepository,
  66.             $this->router
  67.         );
  68.     }
  69.     /**
  70.      * リッスンしたいサブスクライバのイベント名の配列を返します。
  71.      * 配列のキーはイベント名、値は以下のどれかをしてします。
  72.      * - 呼び出すメソッド名
  73.      * - 呼び出すメソッド名と優先度の配列
  74.      * - 呼び出すメソッド名と優先度の配列の配列
  75.      * 優先度を省略した場合は0
  76.      *
  77.      * 例:
  78.      * - ['eventName' => 'methodName']
  79.      * - ['eventName' => ['methodName', $priority]]
  80.      * - ['eventName' => [['methodName1', $priority], ['methodName2']]]
  81.      *
  82.      * {@inheritdoc}
  83.      */
  84.     public static function getSubscribedEvents()
  85.     {
  86.         return [
  87.             '@admin/Setting/Shop/payment_edit.twig' => 'onAdminSettingShopPaymentEditTwig',
  88.             '@admin/Product/product.twig' => 'onAdminProductProductTwig',
  89.             'Shopping/index.twig' => 'onShoppingIndexTwig',
  90.             'Shopping/confirm.twig' => 'onShoppingConfirmTwig',
  91.             'Mypage/index.twig' => 'onMypageNaviTwig',
  92.             'Mypage/favorite.twig' => 'onMypageNaviTwig',
  93.             'Mypage/change.twig' => 'onMypageNaviTwig',
  94.             'Mypage/withdraw.twig' => 'onMypageNaviTwig',
  95.             'Mypage/delivery.twig' => 'onMypageNaviTwig',
  96.             'Mypage/delivery_edit.twig' => 'onMypageNaviTwig',
  97.             '@YamatoPayment42/mypage/credit.twig' => 'onMypageNaviTwig',
  98.             '@IplPeriodicPurchase42/mypage/index.twig' => 'onMypageNaviTwig',
  99.             '@IplPeriodicPurchase42/mypage/history.twig' => 'onMypageNaviTwig',
  100.             '@IplPeriodicPurchase42/mypage/cycle.twig' => 'onMypageNaviTwig',
  101.             '@IplPeriodicPurchase42/mypage/next_shipping.twig' => 'onMypageNaviTwig',
  102.             '@IplPeriodicPurchase42/mypage/shipping.twig' => 'onMypageNaviTwig',
  103.             '@IplPeriodicPurchase42/mypage/products.twig' => 'onMypageNaviTwig',
  104.             '@IplPeriodicPurchase42/mypage/skip.twig' => 'onMypageNaviTwig',
  105.             '@IplPeriodicPurchase42/mypage/suspend.twig' => 'onMypageNaviTwig',
  106.             '@IplPeriodicPurchase42/mypage/resume.twig' => 'onMypageNaviTwig',
  107.             '@IplPeriodicPurchase42/mypage/cancel.twig' => 'onMypageNaviTwig',
  108.             '@IplPeriodicPurchase42/mypage/complete.twig' => 'onMypageNaviTwig',
  109.         ];
  110.     }
  111.     public function onAdminSettingShopPaymentEditTwig(TemplateEvent $event)
  112.     {
  113.         $event->addSnippet('@YamatoPayment42/admin/payment_register.twig');
  114.         $moduleSettings $this->client->getUserSettings();
  115.         $event->setParameter('moduleSettings'$moduleSettings);
  116.     }
  117.     public function onAdminProductProductTwig(TemplateEvent $event)
  118.     {
  119.         /* 予約商品出荷予定日項目追加 */
  120.         $snipet file_get_contents(__DIR__ '/Resource/template/admin/Product/product_edit.twig');
  121.         // HTML の書き換え
  122.         $search '{# エンティティ拡張の自動出力 #}';
  123.         $replace $snipet $search;
  124.         $source $event->getSource();
  125.         $source str_replace($search$replace$source);
  126.         $event->setSource($source);
  127.         /* パラメータの追加 */
  128.         $parameters $event->getParameters();
  129.         // 追加パラメータを取得
  130.         $moduleSettings $this->client->getUserSettings();
  131.         $addParams = [
  132.             'use_option' => $moduleSettings['use_option'],
  133.             'advance_sale' => $moduleSettings['advance_sale']
  134.         ];
  135.         // パラメータ追加
  136.         $parameters array_merge($parameters$addParams);
  137.         $event->setParameters($parameters);
  138.     }
  139.     public function onShoppingIndexTwig(TemplateEvent $event) {
  140.         $moduleSettings "";
  141.         $creditCardList "";
  142.         $Order $event->getParameter('Order');
  143.         if (!empty($this->eccubeConfig['SALE_TYPE_ID_PERIODIC'])) {
  144.             if ($Order->getSaleTypes()[0]->getId() === $this->eccubeConfig['SALE_TYPE_ID_PERIODIC']) {
  145.                 $event->setParameter('isPeriodic'true);
  146.             } else {
  147.                 $event->setParameter('isPeriodic'false);
  148.             }
  149.         } else {
  150.             $event->setParameter('isPeriodic'false);
  151.         }
  152.         $Payment $Order->getPayment();
  153.         // 配送方法を変えると支払方法が未設定になるケースがある.
  154.         if($Payment === null) return;
  155.         /**
  156.          * 購入フローからの遷移時に 選択された決済方法 を$eventからうまく受け取れないため、
  157.          * requestStackを混ぜた無理矢理な記述になっている。
  158.          * 購入フローからの遷移時(confirm, redirect_to)にelseに入り、requestStackから決済方法を受け取る。
  159.          */
  160.         $request $this->requestStack->getCurrentRequest();
  161.         $dataBag $request->request->get('_shopping_order');
  162.         if($dataBag === null || !array_key_exists('Payment'$dataBag)) {
  163.             $paymentId $Payment->getId();
  164.         } else {
  165.             $paymentId $dataBag['Payment'];
  166.         }
  167.         // クレジットカード決済
  168.         $Credit $this->paymentRepository->findOneBy(['method_class' => YamatoMethod\Credit::class]);
  169.         if (($Credit !== null) && ($paymentId == $Credit->getId())) {
  170.             $this->client->setSetting($Order);
  171.             $moduleSettings $this->client->getModuleSetting();
  172.             if($Order->getCustomer()) {
  173.                 $result $this->client->doGetCard($Order->getCustomer()->getId());
  174.                 if($result) {
  175.                     $creditCardList $this->client::getArrCardInfo($this->client->results);
  176.                 }
  177.             }
  178.             if($creditCardList == "") {
  179.                 $creditCardList = ['cardData' => [], 'cardUnit' => '0'];
  180.             }
  181.             /*
  182.              * 予約商品販売機能の使用有無を判定する
  183.              *     設定ファイルの「予約販売」の利用の有無を取得
  184.              *     カート内商品が「予約販売対象商品」か判定する
  185.              */
  186.             //予約商品存在確認
  187.             $tpl_is_reserv_service $this->client->isReservedOrder($Order);
  188.         
  189.             $yamatoPaymentMethod $this->yamatoPaymentMethodRepository->findOneBy(['Payment' => $Payment]);
  190.             $methodParam $yamatoPaymentMethod->getMemo05();
  191.             $event->addSnippet('@YamatoPayment42/credit.twig');
  192.             $event->setParameter('creditCardList'$creditCardList);
  193.             $event->setParameter('moduleSettings'$moduleSettings);
  194.             $event->setParameter('autoRegist'$methodParam['autoRegist']);
  195.             $event->setParameter('reservService'$tpl_is_reserv_service);
  196.             $event->addSnippet('@YamatoPayment42/kuronekoToken.twig');
  197.         }
  198.         // クロネコ代金後払いサービス(スマホタイプ)
  199.         $DeferredSms $this->paymentRepository->findOneBy(['method_class' => YamatoMethod\DeferredSms::class]);
  200.         if(($DeferredSms !== null) && ($paymentId == $DeferredSms->getId())) {
  201.             $event->addSnippet('@YamatoPayment42/shopping/deferred/sms/deferred_sms.twig');
  202.         }
  203.         // コンビニ決済
  204.         $Cvs $this->paymentRepository->findOneBy(['method_class' => YamatoMethod\Cvs::class]);
  205.         if ($Cvs !== null && $paymentId == $Cvs->getId()) {
  206.             $client = new CvsClientService(
  207.                 $this->eccubeConfig,
  208.                 $this->yamatoPluginRepository,
  209.                 $this->yamatoPaymentMethodRepository,
  210.                 $this->yamatoOrderRepository,
  211.                 $this->router
  212.             );
  213.             $client->setSetting($Order);
  214.             $yamatoPaymentMethod $this->yamatoPaymentMethodRepository->findOneBy(['Payment' => $Payment]);
  215.             $methodParam $yamatoPaymentMethod->getMemo05();
  216.             $event->addSnippet('@YamatoPayment42/cvs.twig');
  217.             $event->addSnippet('@YamatoPayment42/kuronekoToken.twig');
  218.         }
  219.     }
  220.     public function onShoppingConfirmTwig(TemplateEvent $event)
  221.     {
  222.         $Order $event->getParameter('Order');
  223.         if($Order->getPayment()->getMethodClass() === YamatoMethod\Credit::class) {
  224.             $event->addSnippet('@YamatoPayment42/credit_confirm.twig');
  225.         }
  226.         if($Order->getPayment()->getMethodClass() === YamatoMethod\DeferredSms::class) {
  227.             $event->addSnippet('@YamatoPayment42/shopping/deferred/sms/deferred_sms_confirm.twig');
  228.             $event->addSnippet('@YamatoPayment42/shopping/deferred/sms/sms_confirm_button.twig');
  229.         }
  230.         if($Order->getPayment()->getMethodClass() === YamatoMethod\Cvs::class) {
  231.             $event->addSnippet('@YamatoPayment42/cvs_confirm.twig');
  232.         }
  233.     }
  234.     public function onMypageNaviTwig(TemplateEvent $event)
  235.     {
  236.         $setting $this->client->getUserSettings();
  237.         if($setting['use_option'] == '0') {
  238.             // オプションサービス契約時のみ
  239.             $event->addSnippet('@YamatoPayment42/mypage/add_navi.twig');
  240.         }
  241.     }
  242. }