app/Plugin/VeriTrans4G2/Service/Shopping/IndexExtensionService.php line 197

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) 2018 VeriTrans Inc., a Digital Garage company. All rights reserved.
  4.  * http://www.veritrans.co.jp/
  5.  */
  6. namespace Plugin\VeriTrans4G2\Service\Shopping;
  7. use Eccube\Common\Constant;
  8. use Eccube\Event\TemplateEvent;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Plugin\VeriTrans4G2\Entity\Vt4gPaymentMethod;
  11. use Plugin\VeriTrans4G2\Entity\Master\Vt4gSubscSaleType;
  12. use Symfony\Component\Form\FormError;
  13. /**
  14.  * ご注文手続き画面 拡張用クラス
  15.  */
  16. class IndexExtensionService
  17. {
  18.     /**
  19.      * コンテナ
  20.      */
  21.     protected $container;
  22.     /**
  23.      * エンティティーマネージャー
  24.      */
  25.     protected $em;
  26.     /**
  27.      * 汎用処理用サービス
  28.      */
  29.     protected $util;
  30.     /**
  31.      * VT用固定値配列
  32.      */
  33.     protected $vt4gConst;
  34.     /**
  35.      * コンストラクタ
  36.      *
  37.      * @param  ContainerInterface $container
  38.      * @return void
  39.      */
  40.     public function __construct(ContainerInterface $container)
  41.     {
  42.         $this->container $container;
  43.         $this->em        $container->get('doctrine.orm.entity_manager');
  44.         $this->util      $container->get('vt4g_plugin.service.util');
  45.         $this->vt4gConst $container->getParameter('vt4g_plugin.const');
  46.     }
  47.     /**
  48.      * ご注文手続き画面 レンダリング時のイベントリスナ
  49.      * @param  TemplateEvent $event イベントデータ
  50.      * @return void
  51.      */
  52.     public function onRenderBefore(TemplateEvent $event)
  53.     {
  54.         $excludePaymentIdList = [];
  55.         //plg_vt4g_payment_methodを取得
  56.         $vt4gPaymentMethods $this->em->getRepository(Vt4gPaymentMethod::class)->findAll();
  57.         //ご注文手続き画面のリクエストが存在する場合は削除する
  58.         if ($this->container->get('session')->has('vt4g_shopping_request')) {
  59.             $this->container->get('session')->remove('vt4g_shopping_request');
  60.         }
  61.         //支払情報の有無を確認する。
  62.         $paymentId '';
  63.         if (!empty($event->getParameter('Order')->getPayment())) {
  64.             $paymentId $event->getParameter('Order')->getPayment()->getId();
  65.         }
  66.         $paymentInfo null;
  67.         foreach ($vt4gPaymentMethods as $vt4gPaymentMethod) {
  68.             if ($vt4gPaymentMethod->getPaymentId() == $paymentId) {
  69.                 $paymentInfo $vt4gPaymentMethod;
  70.                 break;
  71.             }
  72.         }
  73.         // 注文商品の販売種別を取得
  74.         $orderSaleTypes $event->getParameter('Order')->getSaleTypes();
  75.         $curSaleTypeId = isset($orderSaleTypes[0]) ? $orderSaleTypes[0]->getId() : null;
  76.         // $subscSaleTypeがnullでなかったら継続課金商品
  77.         $subscSaleType $this->em->getRepository(Vt4gSubscSaleType::class)->findOneBy(['sale_type_id' => $curSaleTypeId]);
  78.         //plg_vt4g_pluginのサブデータから有効になっている決済内部IDを取得
  79.         $enablePayIdList $this->util->getEnablePayIdList();
  80.         // 継続課金対象の注文
  81.         if (isset($subscSaleType)) {
  82.             // 注文の顧客マスタ情報を取得
  83.             $customer $event->getParameter('Order')->getCustomer();
  84.             // 顧客マスタあり(ベリトランス会員)、かつ、決済内部IDにクレジットカード決済が含まれていたらクレジットカード決済のみ有効
  85.             if (in_array($this->vt4gConst['VT4G_PAYTYPEID_CREDIT'], $enablePayIdList) && isset($customer)) {
  86.                 $paymethodCredits $this->em->getRepository(Vt4gPaymentMethod::class)->findBy(['memo03' => $this->vt4gConst['VT4G_PAYTYPEID_CREDIT']]);
  87.                 $enablePayIdList = [$this->vt4gConst['VT4G_PAYTYPEID_CREDIT']];
  88.                 foreach ($paymethodCredits as $paymethodCredit) {
  89.                     $memo5Array = !is_null($paymethodCredit->getMemo05()) ? unserialize($paymethodCredit->getMemo05()) : [];
  90.                     $oneClickFlg = !empty($memo5Array['one_click_flg']) ? $memo5Array['one_click_flg'] : null;
  91.                     if (is_null($oneClickFlg) || $oneClickFlg != $this->vt4gConst['VT4G_CREDIT_ONE_CLICK']['VERITRANS_ID']) {
  92.                         $excludePaymentIdList[] = $paymethodCredit->getPaymentId();
  93.                     }
  94.                 }
  95.             } else {
  96.                 // 決済手段なし
  97.                 $enablePayIdList = [];
  98.             }
  99.         }
  100.         //有効な決済内部IDリストに存在しないplg_vt4g_payment_methodのpayment_idを対象外リストに追加
  101.         foreach ($vt4gPaymentMethods as $vt4gPaymentMethod) {
  102.             if (!in_array($vt4gPaymentMethod->getMemo03(), $enablePayIdList)) {
  103.                 $excludePaymentIdList[] = $vt4gPaymentMethod->getPaymentId();
  104.             }
  105.         }
  106.         // 継続課金注文の場合
  107.         if (isset($subscSaleType)) {
  108.             // plg_vt4g_payment_methodに含まれないdtb_paymentの支払方法も対象外リストに追加
  109.             $notExistsVt4gPaymentMethodList $this->em->getRepository(Vt4gPaymentMethod::class)->getNotExistsVt4gPaymentMethodList();
  110.             foreach ($notExistsVt4gPaymentMethodList as $notExistsVt4gPaymentMethod) {
  111.                 $excludePaymentIdList[] = $notExistsVt4gPaymentMethod['id'];
  112.             }
  113.         }
  114.         //お支払方法のFormViewから対象外リストにあるpayment_idを削除
  115.         $form $event->getParameter('form');
  116.         $paymentFormViews $form['Payment'];
  117.         foreach ($paymentFormViews as $key => $paymentFormView){
  118.             if (in_array($paymentFormView->vars['value'], $excludePaymentIdList)) {
  119.                 $paymentFormViews->offsetUnset($key);
  120.             }
  121.         }
  122.         // 有効な支払い方法がない場合
  123.         if ($paymentFormViews->count() == 0) {
  124.             // 「支払い情報を選択」する旨のエラーとなるのでそのメッセージを除去
  125.             $form['Payment']->vars['errors'] = [];
  126.             $form['Payment']->vars['errors'] = [new FormError(trans('front.shopping.payment_method_not_fount'))];
  127.             $paymentInfo null;
  128.         } else {
  129.             // お支払方法一覧にdtb_orderで指定中のpaymentIDが存在しない場合は、有効な支払方法の中から1件目の項目を選択済みとする
  130.             $formViesChildren $paymentFormViews->children;
  131.             if (!array_key_exists($paymentId,$formViesChildren)) {
  132.                 foreach ($paymentFormViews as $paymentId => $paymentFormView){
  133.                     $paymentFormView->vars['checked'] = true;
  134.                     $paymentInfo $this->util->getPaymentMethod($paymentId);
  135.                     break;
  136.                 }
  137.             }
  138.         }
  139.         $event->setParameter('form'$form);
  140.         //お支払方法に1件も表示する支払方法がない、ポイント利用によりベリトランス決済以外が選択された場合は処理を終了する
  141.         if(empty($paymentInfo)){
  142.             return;
  143.         }
  144.         $payTypeId $paymentInfo->getMemo03();
  145.         $pluginSetting $this->util->getPluginSetting();
  146.         if (empty($pluginSetting['use_recaptcha'])) {
  147.             $recaptchaAipUrl '';
  148.             $useRecaptcha false;
  149.         } else if ($pluginSetting['use_recaptcha'] === $this->vt4gConst['VT4G_RECAPTCHA_EXECUTE']['ONLY_CREDIT']) {
  150.             $recaptchaAipUrl $payTypeId == $this->vt4gConst['VT4G_PAYTYPEID_CREDIT']
  151.                             ? $this->vt4gConst['VT4G_RECAPTCHA_URL'].$pluginSetting['recaptcha_site_key']
  152.                             : '';
  153.             $useRecaptcha = empty($recaptchaAipUrl) ? false true;
  154.         } else {
  155.             $recaptchaAipUrl $this->vt4gConst['VT4G_RECAPTCHA_URL'].$pluginSetting['recaptcha_site_key'];
  156.             $useRecaptcha true;
  157.         }
  158.         // 支払方法に応じたフォームの表示処理
  159.         switch ($paymentInfo->getMemo03()) {
  160.             case $this->vt4gConst['VT4G_PAYTYPEID_CREDIT']:
  161.                 //クレジットカードを選択した場合の処理
  162.                 $credit $this->container->get('vt4g_plugin.service.payment_credit');
  163.                 $customer $event->getParameter('Order')->getCustomer();
  164.                 $paymentInfo $this->util->getPaymentMethodInfo($paymentId);
  165.                 // クレジットカード情報入力フォーム
  166.                 $cardForm $credit->createCreditForm($paymentInfo);
  167.                 // ベリトランス会員ID決済入力フォーム
  168.                 $accountForm $credit->createAccountForm($paymentInfo);
  169.                 // 再取引入力フォーム
  170.                 $oneClickForm $credit->createOneClickForm($paymentInfo);
  171.                 //バリデーションチェックエラーが存在したときのチェックを行う
  172.                 if ($this->container->get('session')->has('vt4g_shopping_error')) {
  173.                     $vt4gShoppingError $this->container->get('session')->get('vt4g_shopping_error');
  174.                     switch ($vt4gShoppingError['mode']) {
  175.                         case 'token':
  176.                             $cardForm $this->addFormError($cardForm);
  177.                             break;
  178.                         case 'account':
  179.                             $accountForm $this->addFormError($accountForm);
  180.                             break;
  181.                         case 'retrade':
  182.                             $oneClickForm $this->addFormError($oneClickForm);
  183.                             break;
  184.                         default:
  185.                             break;
  186.                     }
  187.                 }
  188.                 $paymentInfo['one_click_flg'] = $paymentInfo['one_click_flg'] ?? $this->vt4gConst['VT4G_CREDIT_ONE_CLICK']['DISABLED'];
  189.                 // ベリトランス会員ID決済の利用可否
  190.                 $useAccountPayment $paymentInfo['one_click_flg'] === $this->vt4gConst['VT4G_CREDIT_ONE_CLICK']['VERITRANS_ID'];
  191.                 // 登録済みカード情報を取得
  192.                 $isGranted $this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY');
  193.                 $accountCards $useAccountPayment && $isGranted && !empty($customer->vt4g_account_id)
  194.                     ? $this->container->get('vt4g_plugin.service.vt4g_account_id')->getAccountCards($customer->vt4g_account_id)
  195.                     : [];
  196.                 // 登録済みカード情報が存在しない場合
  197.                 if (empty($accountCards)) {
  198.                     $useAccountPayment false;
  199.                 }
  200.                 $cardsMax $paymentInfo['cardinfo_regist_max'] ?? $this->vt4gConst['VT4G_FORM']['DEFAULT']['CARDINFO_REGIST_MAX'];
  201.                 // 再取引の利用可否
  202.                 $useReTradePayment $paymentInfo['one_click_flg'] === $this->vt4gConst['VT4G_CREDIT_ONE_CLICK']['RETRADE'];
  203.                 // 再取引用の注文情報を取得
  204.                 $retrade $this->container->get('vt4g_plugin.service.vt4g_retrade');
  205.                 $reTradeCards $useReTradePayment
  206.                     $retrade->getReTradeCards($customer)
  207.                     : [];
  208.                 // 再取引に使用可能なカード情報が存在しない場合
  209.                 if (empty($reTradeCards)) {
  210.                     $useReTradePayment false;
  211.                 }
  212.                 // 継続課金商品の注文か判定
  213.                 $subscOrderFlg $this->em->getRepository(Vt4gSubscSaleType::class)->judgmentSubscSaleTypeByOrderId($event->getParameter('Order')->getId());
  214.                 $event->addSnippet(
  215.                     $this->container->get('twig')->render(
  216.                         "VeriTrans4G2/Resource/template/default/Shopping/vt4g_payment_credit_setting.twig",
  217.                         [
  218.                             'form'              => $cardForm->createView(),
  219.                             'accountForm'       => $accountForm->createView(),
  220.                             'oneClickForm'      => $oneClickForm->createView(),
  221.                             'paymentInfo'       => $paymentInfo,
  222.                             'title'             => $this->vt4gConst['VT4G_PAYNAME_PAYTYPEID_10'],
  223.                             'useAccountPayment' => $useAccountPayment,
  224.                             'accountCards'      => $accountCards,
  225.                             'useReTradePayment' => $useReTradePayment,
  226.                             'reTradeCards'      => $reTradeCards,
  227.                             'cardRegistFlg'     => $customer != null && $paymentInfo['one_click_flg'] === $this->vt4gConst['VT4G_CREDIT_ONE_CLICK']['VERITRANS_ID'],
  228.                             'cardRetradeFlg'    => $customer != null && $paymentInfo['one_click_flg'] === $this->vt4gConst['VT4G_CREDIT_ONE_CLICK']['RETRADE'],
  229.                             'isCardMaxOver'     => count($accountCards) >= $cardsMax,
  230.                             'subscOrderFlg'     => $subscOrderFlg,
  231.                             'tokenApiUrl'       => $this->vt4gConst['VT4G_PLUGIN_TOKEN_API_ENDPOINT'],
  232.                             'tokenApiKey'       => $pluginSetting['token_api_key'],
  233.                             'tokenJsPath'       => $this->util->getTokenJsPath(),
  234.                             'useRecaptcha'      => $useRecaptcha,
  235.                             'recaptchaSiteKey'  => $pluginSetting['recaptcha_site_key'],
  236.                         ],
  237.                         'text/html'
  238.                     ),
  239.                     false
  240.                 );
  241.                 break;
  242.             case $this->vt4gConst['VT4G_PAYTYPEID_CVS']:
  243.                 //コンビニを選択した場合の処理
  244.                 $cvs $this->container->get('vt4g_plugin.service.payment_cvs');
  245.                 $form $cvs->createCVSForm($this->util->getPaymentMethodInfo($paymentId));
  246.                 $form $this->addFormError($form);
  247.                 //ご注文手続きにコンビニ設定用のFormを追加する。
  248.                 $event->addSnippet(
  249.                     $this->container->get('twig')->render(
  250.                         "VeriTrans4G2/Resource/template/default/Shopping/vt4g_payment_cvs_setting.twig",
  251.                         [
  252.                             'title' => $this->vt4gConst['VT4G_PAYNAME_PAYTYPEID_20'],
  253.                             'form'  => $form->createView(),
  254.                         ],
  255.                         'text/html'
  256.                     ),
  257.                     false
  258.                 );
  259.                 break;
  260.             default:
  261.                 break;
  262.         }
  263.         $useThrottling Constant::VERSION >= $this->vt4gConst['VT4G_ECCUBE_VERSION_THROTTLING']
  264.                         ? !empty($pluginSetting['vt4g_shopping_index_rate_limit'])
  265.                         : false;
  266.         $customer $event->getParameter('Order')->getCustomer();
  267.         $event->addSnippet(
  268.             $this->container->get('twig')->render(
  269.                 "VeriTrans4G2/Resource/template/default/js/vt4g_js.twig",
  270.                 [
  271.                     'useThrottling'    => $useThrottling,
  272.                     'url'              => $this->util->generateUrl('vt4g_shopping_index_rate_limiter'),
  273.                     'customerId'       => is_null($customer) ? null $customer->getId(),
  274.                     'orderId'          => $event->getParameter('Order')->getId(),
  275.                     'payTypeId'        => $payTypeId,
  276.                     'useRecaptcha'     => $useRecaptcha,
  277.                     'recaptchaAipUrl'  => $recaptchaAipUrl,
  278.                     'recaptchaSiteKey' => $pluginSetting['recaptcha_site_key'],
  279.                 ],
  280.                 'text/html'
  281.             ),
  282.             false
  283.         );
  284.     }
  285.     /**
  286.      * ご注文手続き画面 拡張formにエラーを付与する
  287.      * @param  Form $form formデータ
  288.      * @return Form $form formデータ
  289.      */
  290.     private function addFormError($form) {
  291.         //sessionよりエラーの内容を取得し、formに付与する
  292.         if ($this->container->get('session')->has('vt4g_shopping_error')) {
  293.             $vt4gShoppingError $this->container->get('session')->get('vt4g_shopping_error');
  294.             $this->container->get('session')->remove('vt4g_shopping_error');
  295.             foreach($vt4gShoppingError as $name => $errorMsg){
  296.                 if($name === 'mode'){
  297.                     continue;
  298.                 }
  299.                 $form[$name]->addError(new FormError($errorMsg));
  300.             }
  301.         }
  302.         return $form;
  303.     }
  304. }