app/Plugin/PayPalCheckout42/Event.php line 158

Open in your IDE?
  1. <?php
  2. namespace Plugin\PayPalCheckout42;
  3. use Eccube\Common\EccubeConfig;
  4. use Eccube\Entity\Cart;
  5. use Eccube\Entity\Order;
  6. use Eccube\Entity\Payment;
  7. use Eccube\Entity\Shipping;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Event\EventArgs;
  10. use Eccube\Event\TemplateEvent;
  11. use Eccube\Service\CartService;
  12. use Eccube\Repository\PaymentRepository;
  13. use Eccube\Util\CacheUtil;
  14. use Plugin\PayPalCheckout42\Entity\Config;
  15. use Plugin\PayPalCheckout42\Exception\OtherPaymentMethodException;
  16. use Plugin\PayPalCheckout42\Repository\ConfigRepository;
  17. use Plugin\PayPalCheckout42\Service\LoggerService;
  18. use Plugin\PayPalCheckout42\Service\Method\CreditCard;
  19. use Plugin\PayPalCheckout42\Service\Method\InlineGuest;
  20. use Plugin\PayPalCheckout42\Service\Method\Acdc;
  21. use Plugin\PayPalCheckout42\Service\PayPalAcdcService;
  22. use Plugin\PayPalCheckout42\Service\PayPalOrderService;
  23. use Plugin\PayPalCheckout42\Service\PayPalService;
  24. use Plugin\PayPalCheckout42\Service\LoginService;
  25. use Symfony\Component\DependencyInjection\ContainerInterface;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  28. /**
  29.  * Class Event
  30.  * @package Plugin\PayPalCheckout42
  31.  */
  32. class Event implements EventSubscriberInterface
  33. {
  34.     /**
  35.      * @var CartService
  36.      */
  37.     protected $cartService;
  38.     /**
  39.      * @var PayPalService
  40.      */
  41.     protected $paypalService;
  42.     /**
  43.      * @var PayPalOrderService
  44.      */
  45.     protected $paypalOrderService;
  46.     /**
  47.      * @var PayPalAcdcService
  48.      */
  49.     protected $paypalAcdcService;
  50.     /**
  51.      * @var ContainerInterface
  52.      */
  53.     protected $container;
  54.     /**
  55.      * @var EccubeConfig
  56.      */
  57.     protected $eccubeConfig;
  58.     /**
  59.      * @var Config
  60.      */
  61.     protected $Config;
  62.     /**
  63.      * @var LoggerService
  64.      */
  65.     protected $logger;
  66.     /**
  67.      * @var SessionInterface
  68.      */
  69.     protected $session;
  70.     /**
  71.      * @var PaymentRepository
  72.      */
  73.     protected $paymentRepository;
  74.     /**
  75.      * @var CacheUtil
  76.      */
  77.     protected $cacheUtil;
  78.     /**
  79.      * @var LoginService
  80.      */
  81.     protected $loginService;
  82.     /**
  83.      * PayPalCheckoutEvent constructor.
  84.      *
  85.      * @param CartService $cartService
  86.      * @param PayPalOrderService $paypalOrderService
  87.      * @param PayPalAcdcService $paypalAcdcService
  88.      * @param EccubeConfig $eccubeConfig
  89.      * @param ConfigRepository $configRepository
  90.      * @param ContainerInterface $container
  91.      * @param LoggerService $loggerService
  92.      * @param SessionInterface $session
  93.      * @param PaymentRepository $paymentRepository
  94.      * @param LoginService $loginService
  95.      */
  96.     public function __construct(
  97.         CartService $cartService,
  98.         PayPalService $payPalService,
  99.         PayPalOrderService $paypalOrderService,
  100.         PayPalAcdcService $paypalAcdcService,
  101.         EccubeConfig $eccubeConfig,
  102.         ConfigRepository $configRepository,
  103.         ContainerInterface $container,
  104.         LoggerService $loggerService,
  105.         SessionInterface $session,
  106.         PaymentRepository $paymentRepository,
  107.         CacheUtil $cacheUtil,
  108.         LoginService $loginService)
  109.     {
  110.         $this->cartService $cartService;
  111.         $this->paypalService $payPalService;
  112.         $this->paypalOrderService $paypalOrderService;
  113.         $this->paypalAcdcService $paypalAcdcService;
  114.         $this->container $container;
  115.         $this->eccubeConfig $eccubeConfig;
  116.         $this->Config $configRepository->get();
  117.         $this->logger $loggerService;
  118.         $this->loginService $loginService;
  119.         $this->session $session;
  120.         $this->paymentRepository $paymentRepository;
  121.         $this->cacheUtil $cacheUtil;
  122.     }
  123.     /**
  124.      * @return array
  125.      */
  126.     public static function getSubscribedEvents(): array
  127.     {
  128.         return [
  129.             'Shopping/login.twig' => 'onDefaultShoppingLoginTwig',
  130.             'Shopping/index.twig' => 'onDefaultShoppingIndexTwig',
  131.             'Shopping/confirm.twig' => 'onDefaultShoppingConfirmTwig',
  132.             'Cart/index.twig' => 'onDefaultCartIndexTwig',
  133.             'Block/paypal_logo.twig' => 'onDefaultPayPalLogoTwig',
  134.             EccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE => 'onChangedShippingAddress',
  135.         ];
  136.     }
  137.     /**
  138.      * @param TemplateEvent $event
  139.      * @throws Exception\PayPalCheckoutException
  140.      */
  141.     public function onDefaultShoppingLoginTwig(TemplateEvent $event): void
  142.     {
  143.         $Carts $this->cartService->getCart();
  144.         if (is_null($Carts)) {
  145.             return;
  146.         }
  147.         if ($this->paypalService::existsSubscriptionProductInCart($Carts)) {
  148.             return;
  149.         }
  150.         if (!$this->paypalService->useExpressBtn()) {
  151.             return;
  152.         }
  153.         // カート金額が決済可能金額でない場合、PayPalボタンを表示しない
  154.         $amount $this->paypalService::getCartAmount($Carts);
  155.         if(!$this->isPayableAmount($amount)) {
  156.             return;
  157.         }
  158.         list($snippet$parameters) = $this->paypalOrderService->generateFrontEndParametersOnShoppingLoginPage($Carts);
  159.         $parameters['PayPalCheckout']['Config'] = $this->Config;
  160.         $this->logger->debug('Generate PayPal frontend parameters on /shipping/login page: ', [
  161.             'snippet' => $snippet,
  162.             'parameters' => $parameters,
  163.         ]);
  164.         $event->addSnippet($snippet);
  165.         $event->addAsset('@PayPalCheckout42/default/head.twig');
  166.         $event->setParameters(array_merge($event->getParameters(), $parameters));
  167.     }
  168.     /**
  169.      * @param TemplateEvent $event
  170.      * @throws Exception\PayPalCheckoutException
  171.      */
  172.     public function onDefaultCartIndexTwig(TemplateEvent $event): void
  173.     {
  174. //        $this->session->remove(OrderHelper::SESSION_NON_MEMBER);
  175. //        $this->session->remove(OrderHelper::SESSION_NON_MEMBER_ADDRESSES);
  176.         /** @var array $parameters */
  177.         $parameters $event->getParameters();
  178.         if (empty($parameters['Carts'])) {
  179.             return;
  180.         }
  181.         if (!$this->paypalService->useExpressBtn()) {
  182.             return;
  183.         }
  184.         /** @var Cart $cart */
  185.         $cart $parameters['Carts'][0];
  186.         // カート金額が決済可能金額でない場合、PayPalボタンを表示しない
  187.         $amount $this->paypalService::getCartAmount($cart);
  188.         if(!$this->isPayableAmount($amount)) {
  189.             return;
  190.         }
  191.         list($snippet$parameters) = $this->paypalOrderService->generateFrontEndParametersOnCartIndexPage($cart);
  192.         $parameters['PayPalCheckout']['Config'] = $this->Config;
  193.         $this->logger->debug('Generate PayPal frontend parameters on /cart page: ', [
  194.             'snippet' => $snippet,
  195.             'parameters' => $parameters,
  196.         ]);
  197.         $event->addSnippet($snippet);
  198.         $event->addAsset('@PayPalCheckout42/default/head.twig');
  199.         $event->setParameters(array_merge($event->getParameters(), $parameters));
  200.     }
  201.     /**
  202.      * @param TemplateEvent $event
  203.      * @throws \Exception
  204.      */
  205.     public function onDefaultShoppingIndexTwig(TemplateEvent $event): void
  206.     {
  207.         /** @var array $parameters */
  208.         $parameters $event->getParameters();
  209.         if (empty($parameters['Order'])) {
  210.             return;
  211.         }
  212.         /** @var Payment $payment */
  213.         $payment $parameters['Order']->getPayment();
  214.         // 支払い方法が何も選択されてない場合は、処理しない
  215.         if (!isset($payment)) {
  216.             return;
  217.         }
  218.         list($snippet$parameters) = $this->paypalOrderService->generateFrontEndParametersOnShoppingIndexPage();
  219.         $parameters['PayPalCheckout']['Config'] = $this->Config;
  220.         $parameters['PayPalCheckout']['isLogin'] = $this->loginService->isLoginUser();
  221.         $this->logger->debug('Generate PayPal frontend parameters on /shopping page: ', [
  222.             'snippet' => $snippet,
  223.             'parameters' => $parameters,
  224.         ]);
  225.         $event->addSnippet($snippet);
  226.         $event->addAsset('@PayPalCheckout42/default/head.twig');
  227.         if ($payment->getMethodClass() === InlineGuest::class) {
  228.             // inline決済専用のコードを追加で読み込む
  229.             $event->addSnippet('@PayPalCheckout42/default/Shopping/index/inline_guest.twig');
  230.             // inline決済の場合、入力が完了するまで「確認する」ボタンを非表示にしておく
  231.             $source $event->getSource();
  232.             $pattern '#<button[^>]*class="ec-blockBtn--action"[^>]*>[^<]+</button>#';
  233.             $replacedSource preg_replace($pattern'<button class="ec-blockBtn--action" disabled>確認する</button>'$source);
  234.             if (!is_null($replacedSource)) {
  235.                 $event->setSource($replacedSource);
  236.             }
  237.         }
  238.         if ($payment->getMethodClass() === Acdc::class) {
  239.             // Acdc決済専用のコードを追加で読み込む
  240.             $event->addSnippet('@PayPalCheckout42/default/Shopping/index/acdc.twig');
  241.             // クライアントからAPIアクセスするためのトークンを生成
  242.             $useVault $this->paypalAcdcService->canUseVault();
  243.             $parameters['PayPalCheckout']['acdcClientToken'] = $this->paypalAcdcService->getClientToken($useVault);
  244.             $parameters['PayPalCheckout']['acdcShowVault'] = $useVault;
  245.             $parameters['PayPalCheckout']['acdcUse3dsecure'] = $this->Config->getUse3dsecure();
  246.             $parameters['PayPalCheckout']['acdcFraudNetSessionIdentifier'] = $this->paypalAcdcService->createAndSaveFraudNetSessionIdentifierToSession();
  247.             $parameters['PayPalCheckout']['acdcSourceWebsiteIdentifier'] = $this->paypalAcdcService->getSourceWebsiteIdentifier();
  248.             // Vault で保存されたクレジットカード情報があれば取得する
  249.             if ($useVault) {
  250.                 $vault $this->paypalAcdcService->getVaults();
  251.                 // vault が2件以上存在した場合、不整合なので安全を考慮し全ての vault を削除する
  252.                 if (count($vault) >= 2) {
  253.                     $this->paypalAcdcService->bulkDeleteExistingVault();
  254.                     $vault = [];
  255.                 }
  256.                 $parameters['PayPalCheckout']['acdcVault'] = $vault[0] ?? [];
  257.             }
  258.         }
  259.         $event->setParameters(array_merge($event->getParameters(), $parameters));
  260.     }
  261.     /**
  262.      * @param TemplateEvent $event
  263.      */
  264.     public function onDefaultShoppingConfirmTwig(TemplateEvent $event): void
  265.     {
  266.         /** @var Order $order */
  267.         $order $event->getParameter('Order');
  268.         $payment $order->getPayment();
  269.         $isProcessingShortcutPayment $this->paypalOrderService->isProcessingShortcutPayment();
  270.         try {
  271.             if ($isProcessingShortcutPayment) {
  272.                 $this->session->remove(PayPalOrderService::SESSION_SHORTCUT);
  273.                 $snippet '@PayPalCheckout42/default/Shopping/confirm/shortcut.twig';
  274.             } else if ($payment->getMethodClass() === InlineGuest::class) {
  275.                 $snippet '@PayPalCheckout42/default/Shopping/confirm/inline_guest.twig';
  276.             } else {
  277.                 list($snippet$parameters) = $this->paypalOrderService->generateFrontEndParametersOnShoppingConfirmPage($order);
  278.             }
  279.             $parameters['PayPalCheckout']['Config'] = $this->Config;
  280.         } catch (OtherPaymentMethodException $e) {
  281.             // PayPal決済以外は処理終了
  282.             return;
  283.         }
  284.         // 「注文する」ボタンが一瞬表示されてしまうので、あらかじめ非表示にしておく。
  285.         // 逆にショートカット決済、クレジットカード決済の場合は「注文する」ボタンを使うので処理しない。
  286.         if (!$isProcessingShortcutPayment
  287.             && $payment->getMethodClass() !== InlineGuest::class
  288.             && $payment->getMethodClass() !== Acdc::class) {
  289.             $source $event->getSource();
  290.             $pattern '#<button[^>]*class="ec-blockBtn--action"[^>]*>[^<]+</button>#';
  291.             $replacedSource preg_replace($pattern'<button class="ec-blockBtn--action" style="opacity: 0" disabled></button>'$source);
  292.             if (!is_null($replacedSource)) {
  293.                 $event->setSource($replacedSource);
  294.             }
  295.         }
  296.         $this->logger->debug('Generate PayPal frontend parameters on /shopping/confirm page: ', [
  297.             'custom_data' => [
  298.                 'snippet' => $snippet,
  299.                 'parameters' => $parameters,
  300.             ],
  301.         ]);
  302.         $event->addSnippet($snippet);
  303.         $event->addAsset('@PayPalCheckout42/default/head.twig');
  304.         $event->setParameters(array_merge($event->getParameters(), $parameters));
  305.     }
  306.     /**
  307.      * @param EventArgs $event
  308.      */
  309.     public function onChangedShippingAddress(EventArgs $event): void
  310.     {
  311.         /** @var Shipping $shipping */
  312.         $shipping $event->getArgument('Shipping');
  313.         $this->paypalOrderService->setShippingAddress($shipping);
  314.     }
  315.     /**
  316.      * @param TemplateEvent $event
  317.      */
  318.     public function onDefaultPayPalLogoTwig(TemplateEvent $event): void
  319.     {
  320.         /** @var string $selectedNumber */
  321.         $selectedNumber $this->Config->getPaypalLogo();
  322.         $parameters['PayPalCheckout']['paypal_logo'] = $this->eccubeConfig->get("paypal.paypal_express_paypal_logo_${selectedNumber}");
  323.         $event->setParameters(array_merge($event->getParameters(), $parameters));
  324.     }
  325.     private function isPayableAmount($amount): bool
  326.     {
  327.         $payment $this->paymentRepository->findOneBy(['method_class' => CreditCard::class]);
  328.         if (!empty($payment)) {
  329.             $minRule $payment->getRuleMin();
  330.             $maxRule $payment->getRuleMax();
  331.         }
  332.         if(!isset($minRule) || $minRule PluginManager::MIN_AMOUNT)
  333.             $minRule PluginManager::MIN_AMOUNT;
  334.         if(!isset($maxRule) || $maxRule PluginManager::MAX_AMOUNT)
  335.             $maxRule PluginManager::MAX_AMOUNT;
  336.         if($amount $minRule || $amount $maxRule) {
  337.             return false;
  338.         }
  339.         return true;
  340.     }
  341. }