app/Plugin/AmazonPayV2/AmazonPayEvent.php line 182

Open in your IDE?
  1. <?php
  2. /*
  3.  * Amazon Pay V2 for EC-CUBE4
  4.  * Copyright(c) 2020 IPLOGIC CO.,LTD. All Rights Reserved.
  5.  *
  6.  * http://www.iplogic.co.jp/
  7.  *
  8.  * This program is not free software.
  9.  * It applies to terms of service.
  10.  *
  11.  */
  12. namespace Plugin\AmazonPayV2;
  13. use Eccube\Event\TemplateEvent;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Common\EccubeConfig;
  17. use Eccube\Repository\PaymentRepository;
  18. use Eccube\Repository\PluginRepository;
  19. use Eccube\Service\OrderHelper;
  20. use Eccube\Service\CartService;
  21. use Plugin\AmazonPayV2\Repository\ConfigRepository;
  22. use Plugin\AmazonPayV2\Service\AmazonRequestService;
  23. use Plugin\AmazonPayV2\Service\Method\AmazonPay;
  24. use Plugin\AmazonPayV2\phpseclib\Crypt\Random;
  25. use Symfony\Component\DependencyInjection\ContainerInterface;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\HttpFoundation\RequestStack;
  28. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  29. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  30. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  31. use Eccube\Repository\DeliveryRepository;
  32. use Eccube\Repository\PaymentOptionRepository;
  33. class AmazonPayEvent implements EventSubscriberInterface
  34. {
  35.     /**
  36.      * @var string プロファイル情報キー
  37.      */
  38.     private $sessionAmazonProfileKey 'amazon_pay_v2.profile';
  39.     /**
  40.      * @var string プロファイル情報キー
  41.      */
  42.     private $sessionAmazonCheckoutSessionIdKey 'amazon_pay_v2.checkout_session_id';
  43.     /**
  44.      * @var string Amazonログインステート
  45.      */
  46.     private $sessionAmazonLoginStateKey 'amazon_pay_v2.amazon_login_state';
  47.     /**
  48.      * @var EccubeConfig
  49.      */
  50.     protected $eccubeConfig;
  51.     /**
  52.      * @var UrlGeneratorInterface
  53.      */
  54.     private $router;
  55.     /**
  56.      * @var ConfigRepository
  57.      */
  58.     protected $configRepository;
  59.     /**
  60.      * @var AmazonRequestService
  61.      */
  62.     protected $amazonRequestService;
  63.     /**
  64.      * @var DeliveryRepository
  65.      */
  66.     protected $deliveryRepository;
  67.     /**
  68.      * @var DeliveryRepository
  69.      */
  70.     protected $paymentOptionRepository;
  71.     /**
  72.      * AmazonPayEvent
  73.      *
  74.      * @param eccubeConfig $eccubeConfig
  75.      * @param ConfigRepository $configRepository
  76.      */
  77.     public function __construct(
  78.         RequestStack $requestStack,
  79.         SessionInterface $session,
  80.         TokenStorageInterface $tokenStorage,
  81.         EccubeConfig $eccubeConfig,
  82.         UrlGeneratorInterface $router,
  83.         PaymentRepository $paymentRepository,
  84.         PluginRepository $pluginRepository,
  85.         ConfigRepository $configRepository,
  86.         ContainerInterface $container,
  87.         OrderHelper $orderHelper,
  88.         CartService $cartService,
  89.         AmazonRequestService $amazonRequestService,
  90.         DeliveryRepository $deliveryRepository,
  91.         PaymentOptionRepository $paymentOptionRepository
  92.     ) {
  93.         $this->requestStack $requestStack;
  94.         $this->session $session;
  95.         $this->tokenStorage $tokenStorage;
  96.         $this->eccubeConfig $eccubeConfig;
  97.         $this->router $router;
  98.         $this->paymentRepository $paymentRepository;
  99.         $this->pluginRepository $pluginRepository;
  100.         $this->configRepository $configRepository;
  101.         $this->container $container;
  102.         $this->orderHelper $orderHelper;
  103.         $this->cartService $cartService;
  104.         $this->amazonRequestService $amazonRequestService;
  105.         $this->deliveryRepository $deliveryRepository;
  106.         $this->paymentOptionRepository $paymentOptionRepository;
  107.     }
  108.     /**
  109.      * {@inheritdoc}
  110.      */
  111.     public static function getSubscribedEvents()
  112.     {
  113.         return [
  114.             EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE => 'amazon_cart_buystep',
  115.             'Cart/index.twig' => 'cart',
  116.             'Shopping/index.twig' => 'amazon_pay_shopping',
  117.             'Mypage/login.twig' => 'mypage_login',
  118.             'Shopping/confirm.twig' => 'amazon_pay_shopping_confirm',
  119.         ];
  120.     }
  121.     public function cart(TemplateEvent $event)
  122.     {
  123.         $parameters $event->getParameters();
  124.         if (empty($parameters['Carts'])) {
  125.             return;
  126.         }
  127.         $Config $this->configRepository->get();
  128.         if ($Config->getUseCartButton() == $this->eccubeConfig['amazon_pay_v2']['toggle']['off']) {
  129.             return;
  130.         }
  131.         // AmazonPayに紐づく商品種別の取得
  132.         $Payment $this->paymentRepository->findOneBy(['method_class' => AmazonPay::class]);
  133.         $AmazonDeliveries $this->paymentOptionRepository->findBy(['payment_id' => $Payment->getId()]);
  134.         $AmazonSaleTypes = [];
  135.         foreach ($AmazonDeliveries as $AmazonDelivery) {
  136.             $Delivery $this->deliveryRepository->findOneBy(['id' => $AmazonDelivery->getDelivery()->getId()]);
  137.             $AmazonSaleTypes[] = $Delivery->getSaleType()->getId();
  138.         }
  139.         $parameters['AmazonSaleTypes'] = $AmazonSaleTypes;
  140.         foreach ($parameters['Carts'] as $Cart) {
  141.             $cartKey $Cart->getCartKey();
  142.             $payload $this->amazonRequestService->createCheckoutSessionPayload($Cart->getCartKey());
  143.             $signature $this->amazonRequestService->signaturePayload($payload);
  144.             $parameters['cart'][$cartKey]['payload'] = $payload;
  145.             $parameters['cart'][$cartKey]['signature'] = $signature;
  146.         }
  147.         $parameters['AmazonPayV2Config'] = $Config;
  148.         if ($Config->getEnv() == $this->eccubeConfig['amazon_pay_v2']['env']['prod']) {
  149.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['prod'];
  150.         } else {
  151.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['sandbox'];
  152.         }
  153.         $event->setParameters($parameters);
  154.         $event->addSnippet('@AmazonPayV2/default/Cart/amazon_pay_js.twig');
  155.         if ($Config->getCartButtonPlace() == $this->eccubeConfig['amazon_pay_v2']['button_place']['auto']) {
  156.             $event->addSnippet('@AmazonPayV2/default/Cart/button.twig');
  157.         }
  158.     }
  159.     public function amazon_cart_buystep(EventArgs $event)
  160.     {
  161.         // Amazonログインによる仮会員情報がセッションにセットされていたら
  162.         if ($this->orderHelper->getNonmember() && $this->session->get($this->sessionAmazonProfileKey)) {
  163.             // 仮会員情報を削除
  164.             $this->session->remove(OrderHelper::SESSION_NON_MEMBER);
  165.             $this->session->remove($this->sessionAmazonProfileKey);
  166.             $this->cartService->setPreOrderId(null);
  167.             $this->cartService->save();
  168.         }
  169.     }
  170.     public function amazon_pay_shopping(TemplateEvent $event)
  171.     {
  172.         $request $this->requestStack->getMasterRequest();
  173.         $uri $request->getUri();
  174.         $parameters $event->getParameters();
  175.         if (preg_match('/shopping\/amazon_pay/'$uri) == false) {
  176.             $referer $request->headers->get('referer');
  177.             $Order $parameters['Order'];
  178.             $Payment $Order->getPayment();
  179.             // AmazonPay決済確認画面→クーポン入力画面→決済確認画面への遷移時にAmazonPay決済確認画面へ戻す
  180.             if ($Payment && $Payment->getMethodClass() === AmazonPay::class && preg_match('/shopping_coupon/'$referer)) {
  181.                 header("Location:" $this->container->get('router')->generate('amazon_pay_shopping'));
  182.                 exit;
  183.             }
  184.             return;
  185.         }
  186.         $Config $this->configRepository->get();
  187.         $event->addSnippet('@AmazonPayV2/default/Shopping/widgets.twig');
  188.         $event->addSnippet('@AmazonPayV2/default/Shopping/customer_regist_v2.twig');
  189.         // チェックアウトセッションIDを取得する
  190.         $amazonCheckoutSessionId $this->session->get($this->sessionAmazonCheckoutSessionIdKey);
  191.         $parameters $event->getParameters();
  192.         $parameters['amazonCheckoutSessionId'] = $amazonCheckoutSessionId;
  193.         $parameters['AmazonPayV2Config'] = $Config;
  194.         // メルマガプラグイン利用時はチェックボックスを追加
  195.         if (
  196.             $this->pluginRepository->findOneBy(['code' => 'MailMagazine4''enabled' => true])
  197.             || $this->pluginRepository->findOneBy(['code' => 'PostCarrier4''enabled' => true])
  198.         ) {
  199.             $useMailMagazine true;
  200.         } else {
  201.             $useMailMagazine false;
  202.         }
  203.         $parameters['useMailMagazine'] = $useMailMagazine;
  204.         if ($Config->getEnv() == $this->eccubeConfig['amazon_pay_v2']['env']['prod']) {
  205.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['prod'];
  206.         } else {
  207.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['sandbox'];
  208.         }
  209.         $event->setParameters($parameters);
  210.     }
  211.     public function amazon_pay_shopping_confirm(TemplateEvent $event)
  212.     {
  213.         $request $this->requestStack->getMasterRequest();
  214.         $uri $request->getUri();
  215.         if (preg_match('/shopping\/amazon_pay/'$uri) == false) {
  216.             return;
  217.         }
  218.         $Config $this->configRepository->get();
  219.         $event->addSnippet('@AmazonPayV2/default/Shopping/confirm_widgets.twig');
  220.         $event->addSnippet('@AmazonPayV2/default/Shopping/confirm_customer_regist_v2.twig');
  221.         $parameters $event->getParameters();
  222.         $parameters['AmazonPayV2Config'] = $Config;
  223.         // メルマガプラグイン利用時はチェックボックスを追加
  224.         if (
  225.             $this->pluginRepository->findOneBy(['code' => 'MailMagazine4''enabled' => true])
  226.             || $this->pluginRepository->findOneBy(['code' => 'PostCarrier4''enabled' => true])
  227.         ) {
  228.             $useMailMagazine true;
  229.         } else {
  230.             $useMailMagazine false;
  231.         }
  232.         $parameters['useMailMagazine'] = $useMailMagazine;
  233.         if ($Config->getEnv() == $this->eccubeConfig['amazon_pay_v2']['env']['prod']) {
  234.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['prod'];
  235.         } else {
  236.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['sandbox'];
  237.         }
  238.         $event->setParameters($parameters);
  239.     }
  240.     public function mypage_login(TemplateEvent $event)
  241.     {
  242.         $Config $this->configRepository->get();
  243.         if ($Config->getUseMypageLoginButton() == $this->eccubeConfig['amazon_pay_v2']['toggle']['off']) {
  244.             return;
  245.         }
  246.         $state $this->session->get($this->sessionAmazonLoginStateKey);
  247.         if (!$state) {
  248.             // stateが生成されていなければ、生成及びセッションセット
  249.             $state bin2hex(Random::string(16));
  250.             $this->session->set($this->sessionAmazonLoginStateKey$state);
  251.         }
  252.         $returnUrl $this->router->generate('login_with_amazon', ['state' => $state], UrlGeneratorInterface::ABSOLUTE_URL);
  253.         $parameters $event->getParameters();
  254.         $payload $this->amazonRequestService->createSigninPayload($returnUrl);
  255.         $signature $this->amazonRequestService->signaturePayload($payload);
  256.         $parameters['payload'] = $payload;
  257.         $parameters['signature'] = $signature;
  258.         $parameters['buttonColor'] = $Config->getMypageLoginButtonColor();
  259.         $parameters['AmazonPayV2Config'] = $Config;
  260.         if ($Config->getEnv() == $this->eccubeConfig['amazon_pay_v2']['env']['prod']) {
  261.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['prod'];
  262.         } else {
  263.             $parameters['AmazonPayV2Api'] = $this->eccubeConfig['amazon_pay_v2']['api']['sandbox'];
  264.         }
  265.         $event->setParameters($parameters);
  266.         if ($Config->getMypageLoginButtonPlace() == $this->eccubeConfig['amazon_pay_v2']['button_place']['auto']) {
  267.             $event->addSnippet('@AmazonPayV2/default/Mypage/login_page_button.twig');
  268.         }
  269.         $event->addSnippet('@AmazonPayV2/default/Mypage/amazon_login_js.twig');
  270.     }
  271. }