app/Plugin/EccubePaymentLite42/Controller/Front/Shopping/EpsilonPaymentCompleteController.php line 267

Open in your IDE?
  1. <?php
  2. namespace Plugin\EccubePaymentLite42\Controller\Front\Shopping;
  3. use Eccube\Common\Constant;
  4. use Eccube\Controller\AbstractController;
  5. use Eccube\Entity\Master\OrderStatus;
  6. use Eccube\Entity\Order;
  7. use Eccube\Repository\MailHistoryRepository;
  8. use Eccube\Repository\Master\OrderStatusRepository;
  9. use Eccube\Repository\OrderRepository;
  10. use Eccube\Service\CartService;
  11. use Eccube\Service\MailService;
  12. use Eccube\Service\OrderHelper;
  13. use Eccube\Service\Payment\PaymentMethodInterface;
  14. use Plugin\EccubePaymentLite42\Service\GmoEpsilonRequest\RequestGetSales2Service;
  15. use Plugin\EccubePaymentLite42\Service\UpdateGmoEpsilonOrderService;
  16. use Plugin\EccubePaymentLite42\Service\UpdateRegularOrderService;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  24. class EpsilonPaymentCompleteController extends AbstractController
  25. {
  26.     /**
  27.      * @var OrderRepository
  28.      */
  29.     private $orderRepository;
  30.     /**
  31.      * @var OrderStatusRepository
  32.      */
  33.     private $orderStatusRepository;
  34.     /**
  35.      * @var RequestGetSales2Service
  36.      */
  37.     private $requestGetSales2Service;
  38.     /**
  39.      * @var CsrfTokenManagerInterface
  40.      */
  41.     private $csrfTokenManager;
  42.     /**
  43.      * @var UpdateRegularOrderService
  44.      */
  45.     private $updateRegularOrderService;
  46.     /**
  47.      * @var UpdateGmoEpsilonOrderService
  48.      */
  49.     private $updateGmoEpsilonOrderService;
  50.     /**
  51.      * @var CartService
  52.      */
  53.     private $cartService;
  54.     /**
  55.      * @var MailService
  56.      */
  57.     private $mailService;
  58.     /**
  59.      * @var MailHistoryRepository
  60.      */
  61.     private $mailHistoryRepository;
  62.     /**
  63.      * @var ContainerInterface
  64.      */
  65.     protected $serviceContainer;
  66.     public function __construct(
  67.         OrderRepository $orderRepository,
  68.         OrderStatusRepository $orderStatusRepository,
  69.         RequestGetSales2Service $requestGetSales2Service,
  70.         CsrfTokenManagerInterface $csrfTokenManager,
  71.         UpdateRegularOrderService $updateRegularOrderService,
  72.         UpdateGmoEpsilonOrderService $updateGmoEpsilonOrderService,
  73.         CartService $cartService,
  74.         MailService $mailService,
  75.         MailHistoryRepository $mailHistoryRepository,
  76.         ContainerInterface $serviceContainer
  77.     ) {
  78.         $this->orderRepository $orderRepository;
  79.         $this->orderStatusRepository $orderStatusRepository;
  80.         $this->requestGetSales2Service $requestGetSales2Service;
  81.         $this->csrfTokenManager $csrfTokenManager;
  82.         $this->updateRegularOrderService $updateRegularOrderService;
  83.         $this->updateGmoEpsilonOrderService $updateGmoEpsilonOrderService;
  84.         $this->cartService $cartService;
  85.         $this->mailService $mailService;
  86.         $this->mailHistoryRepository $mailHistoryRepository;
  87.         $this->serviceContainer $serviceContainer;
  88.     }
  89.     /**
  90.      * 購入完了処理(commit)を行う
  91.      * コンビニ決済はこのRouteを利用しない
  92.      *
  93.      * @Route(
  94.      *     "/shopping/epsilon_payment/complete",
  95.      *     name="eccube_payment_lite42_payment_complete"
  96.      * )
  97.      */
  98.     public function complete(Request $request)
  99.     {
  100.         $transCode '';
  101.         $orderNumber '';
  102.         if ($request->query->get('trans_code')) {
  103.             $transCode $request->query->get('trans_code');
  104.         }
  105.         if ($request->query->get('order_number')) {
  106.             $orderNumber $request->query->get('order_number');
  107.         }
  108.         /** @var Order $Order */
  109.         $Order $this
  110.             ->orderRepository
  111.             ->find((int) explode('x'$orderNumber)[0]);
  112.         if (!$Order) {
  113.             throw new NotFoundHttpException();
  114.         }
  115.         // Check the order was completed
  116.         if ($Order->getGmoEpsilonOrderNo()) {
  117.             log_info('[注文確認] 注文が完了しました.', [$Order->getId()]);
  118.             return $this->redirectToRoute('shopping_error');
  119.         }
  120.         $results $this
  121.             ->requestGetSales2Service
  122.             ->handle($transCode$orderNumber);
  123.         if ($results['status'] === 'NG') {
  124.             throw new NotFoundHttpException();
  125.         }
  126.         // 受注登録・決済情報登録ボタンより処理を行った場合は、受注登録画面にリダイレクトさせる
  127.         if ($results['route'] === 'eccube_payment_lite42_admin_create_reg_credit_order') {
  128.             return $this->processingWhenCreatingOrder(
  129.                 $Order,
  130.                 $results['trans_code'],
  131.                 $results['order_number']
  132.             );
  133.         }
  134.         // 定期一覧より受注一括作成を行った場合は、もう一度定期受注作成へ
  135.         if ($results['route'] === 'eccube_payment_lite42_admin_regular_index_add') {
  136.             return $this->processingWhenCreatingRegularOrderIndex(
  137.                 $Order,
  138.                 $results['trans_code'],
  139.                 $results['order_number']
  140.             );
  141.         }
  142.         // マイページよりクレジットカードの登録を行った場合は、カード編集画面にリダイレクトさせる
  143.         if ($results['route'] === 'eccube_payment_lite42_mypage_credit_card_edit' ||
  144.             $results['route'] === 'eccube_payment_lite42_mypage_credit_card_new') {
  145.             return $this->redirectToRoute('eccube_payment_lite42_mypage_credit_card_complete');
  146.         }
  147.         // コマンドで定期受注作成時の処理
  148.         if ($results['route'] === 'eccube_payment_lite42_regular_create_command') {
  149.             return $this
  150.                 ->processingWhenCreatingOrderInBatch(
  151.                     $Order,
  152.                     $results['trans_code'],
  153.                     $results['order_number']
  154.                 );
  155.         }
  156.         // 決済方法に応じたインスタンスを取得
  157.         /** @var PaymentMethodInterface $PaymentMethod */
  158.         if (!is_null($Order->getPayment())) {
  159.             $PaymentMethod $this->serviceContainer->get($Order->getPayment()->getMethodClass());
  160.         }
  161.         $PaymentMethod->setOrder($Order);
  162.         if (!is_null($Order->getPayment()) && (preg_match('/Maillink/'$Order->getPayment()->getMethodClass()))) {
  163.             return $this->paymentMaillinkCompletionProcess($Order$PaymentMethod$results['payment_code']);
  164.         }
  165.         return $this->paymentCompletionProcess($Order$PaymentMethod$request);
  166.     }
  167.     private function processingWhenCreatingOrder(Order $Order$transCode$gmoEpsilonOrderNo): Response
  168.     {
  169.         logs('gmo_epsilon')->info('受注登録より登録した受注の購入完了処理(commit) start.');
  170.         $this
  171.             ->updateGmoEpsilonOrderService
  172.             ->updateAfterMakingPayment(
  173.                 $Order,
  174.                 $transCode,
  175.                 $gmoEpsilonOrderNo
  176.             );
  177.         $this->addSuccess(
  178.             'イプシロン決済サービスに受注ID'.$gmoEpsilonOrderNo.'の決済情報を登録しました''admin'
  179.         );
  180.         logs('gmo_epsilon')->info('受注登録より登録した受注の購入完了処理(commit) end.');
  181.         return $this->redirectToRoute('admin_order_edit', ['id' => $Order->getId()]);
  182.     }
  183.     private function processingWhenCreatingRegularOrderIndex(Order $Order$transCode$gmoEpsilonOrderNo): Response
  184.     {
  185.         logs('gmo_epsilon')->info('定期受注一覧より登録した受注の購入完了処理(commit) start.');
  186.         $this
  187.             ->updateGmoEpsilonOrderService
  188.             ->updateAfterMakingPayment(
  189.                 $Order,
  190.                 $transCode,
  191.                 $gmoEpsilonOrderNo
  192.             );
  193.         $this->updateRegularOrderService->updateAfterMakingPayment($Order->getRegularOrder());
  194.         $ids $this->session->get('eccube.admin.regular.order.createRegularIds');
  195.         logs('gmo_epsilon')->info('定期受注一覧より登録した受注の購入完了処理(commit) end.');
  196.         $this->entityManager->commit();
  197.         return $this->forwardToRoute(
  198.             'eccube_payment_lite42_admin_regular_index_add',
  199.             [],
  200.             [
  201.                 'ids' => $ids,
  202.                 Constant::TOKEN_NAME => $this->csrfTokenManager->getToken(Constant::TOKEN_NAME)->getValue(),
  203.             ]
  204.         );
  205.     }
  206.     private function processingWhenCreatingOrderInBatch($Order$transCode$gmoEpsilonOrderNo): Response
  207.     {
  208.         logs('gmo_epsilon')->info('定期受注作成バッチより登録した受注の購入完了処理(commit) start.');
  209.         $this
  210.             ->updateGmoEpsilonOrderService
  211.             ->updateAfterMakingPayment(
  212.                 $Order,
  213.                 $transCode,
  214.                 $gmoEpsilonOrderNo
  215.             );
  216.         $this->updateRegularOrderService->updateAfterMakingPayment($Order->getRegularOrder());
  217.         logs('gmo_epsilon')->info('定期受注作成バッチより登録した受注の購入完了処理(commit) end.');
  218.         return new Response(1);
  219.     }
  220.     private function paymentMaillinkCompletionProcess(Order $Order$PaymentMethod$paymentCode): Response
  221.     {
  222.         logs('gmo_epsilon')->info('メールリンク決済の購入完了処理(commit) start.');
  223.         $PaymentMethod->compProcess($paymentCode);
  224.         logs('gmo_epsilon')->info('メールリンク決済の購入完了処理(commit) end.');
  225.         return new Response(1);
  226.     }
  227.     private function paymentCompletionProcess(Order $Order$PaymentMethodRequest $request): Response
  228.     {
  229.         logs('gmo_epsilon')->info('購入完了処理(commit) start.');
  230.         logs('gmo_epsilon')->info('POST param argument '.print_r($request->getContent(), true));
  231.         // 購入完了処理
  232.         if ($response $this->executeCheckout($PaymentMethod)) {
  233.             return $response;
  234.         }
  235.         $this->cartService->clear();
  236.         // メール送信
  237.         $MailHistory $this->mailHistoryRepository->findBy(['Order' => $Order]);
  238.         // メール送信の処理は必ず入れるが、メール履歴(dtb_mail_history)を確認して無ければ送信の判断を入れる。
  239.         if (!$MailHistory) {
  240.             logs('gmo_epsilon')->info('send order mail');
  241.             $this->mailService->sendOrderMail($Order);
  242.         }
  243.         $this->session->set(OrderHelper::SESSION_ORDER_ID$Order->getId());
  244.         $this->entityManager->flush();
  245.         return $this->redirectToRoute('shopping_complete');
  246.     }
  247.     /**
  248.      * @return RedirectResponse|Response|void
  249.      */
  250.     private function executeCheckout(PaymentMethodInterface $paymentMethod)
  251.     {
  252.         $PaymentResult $paymentMethod->checkout();
  253.         $response $PaymentResult->getResponse();
  254.         // PaymentResultがresponseを保持している場合はresponseを返す
  255.         if ($response instanceof Response && ($response->isRedirection() || $response->isSuccessful())) {
  256.             $this->entityManager->flush();
  257.             logs('gmo_epsilon')->info('購入完了処理(commit): PaymentMethod::checkoutより、responseを返却.');
  258.             return $response;
  259.         }
  260.         // エラー時はロールバックして購入エラーとする.
  261.         if (!$PaymentResult->isSuccess()) {
  262.             $this->entityManager->rollback();
  263.             foreach ($PaymentResult->getErrors() as $error) {
  264.                 $this->addError($error);
  265.             }
  266.             logs('gmo_epsilon')->info('[注文処理] PaymentMethod::checkoutのエラーのため, 購入エラー画面へ遷移します.', [$PaymentResult->getErrors()]);
  267.             return $this->redirectToRoute('shopping_error');
  268.         }
  269.     }
  270. }