app/Plugin/komoju42/KomojuEvent.php line 215

Open in your IDE?
  1. <?php
  2. namespace Plugin\komoju42;
  3. use Eccube\Common\EccubeConfig;
  4. use Eccube\Event\TemplateEvent;
  5. use Eccube\Entity\BaseInfo;
  6. use Eccube\Entity\Payment;
  7. use Eccube\Entity\Master\OrderStatus;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Plugin\komoju42\Service\Method\KomojuMultiPay;
  13. use Plugin\komoju42\Entity\KomojuConfig;
  14. use Plugin\komoju42\Entity\KomojuPay;
  15. use Plugin\komoju42\Entity\KomojuOrder;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Event\EccubeEvents;
  18. use Plugin\komoju42\KomojuClient;
  19. class KomojuEvent implements EventSubscriberInterface{
  20.     private $container;
  21.     private $entityManager;
  22.     private $errorMessage;
  23.     private $eccubeConfig;
  24.     private $base_info;
  25.     const EVENT_KOMOJU_CONFIG_LOAD "PLUGIN.KOMOJU.CONFIG.LOAD";
  26.     public function __construct(
  27.         EccubeConfig $eccubeConfig,
  28.         ContainerInterface $container,
  29.         EntityManagerInterface $entityManager
  30.     ){
  31.         $this->eccubeConfig $eccubeConfig;
  32.         $this->container $container;
  33.         $this->entityManager $entityManager;        
  34.         $this->base_info $this->entityManager->getRepository(BaseInfo::class)->get();
  35.     }
  36.     /**
  37.      * @return array
  38.      */
  39.     public static function getSubscribedEvents(){
  40.         return [
  41.             'Shopping/confirm.twig'   =>  'onShoppingConfirmTwig',
  42.             'Shopping/index.twig'   =>  'onShoppingIndexTwig',
  43.             'front.shopping.complete.initialize'    =>  'onFrontShoppingCompleteInitialize',
  44.             '@admin/Order/index.twig'   =>  'onAdminOrderIndexTwig',
  45.             '@admin/Order/edit.twig'    =>  'onAdminOrderEditTwig',
  46.             // EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE   =>  'onOrderEditIndexComplete',
  47.         ];
  48.     }
  49.     /**
  50.      * @param TemplateEvent
  51.      */
  52.     public function onShoppingIndexTwig(TemplateEvent $event){
  53.         $paymentRepository $this->entityManager->getRepository(Payment::class);
  54.         $Payment $paymentRepository->findOneBy(['method_class' => KomojuMultiPay::class]);
  55.         if($Payment){
  56.             $payment_id $Payment->getId();
  57.             $event->setParameter("komoju_id"$payment_id);
  58.             $event->addSnippet('@komoju42/default/shopping/shopping.twig');
  59.         }
  60.     }
  61.     /**
  62.      * @param EventArgs
  63.      */
  64.     public function onOrderEditIndexComplete(EventArgs $args){
  65.         // $OriginOrder = $args->getArgument('OriginOrder');
  66.         // $TargetOrder = $args->getArgument('TargetOrder');
  67.         // if($OriginOrder->getPayment()->getId() == 
  68.         //     $this->entityManager->getRepository(Payment::class)->findOneBy(['method_class' => KomojuMultiPay::class])->getId()){
  69.             
  70.         //     $OldStatus = $OriginOrder->getOrderStatus();
  71.         //     $NewStatus = $TargetOrder->getOrderStatus();
  72.     
  73.         //     if ($TargetOrder->getId() && $OldStatus->getId() != $NewStatus->getId()) {
  74.         //         if($NewStatus->getId() == OrderStatus::CANCEL){
  75.         //             $komoju_order = $this->entityManager->getRepository(KomojuOrder::class)->findOneBy(['Order' => $TargetOrder]);
  76.         //             if(empty($komoju_order) || $komoju_order->isCaptured()){
  77.         //                 return;
  78.         //             }
  79.         //             $payment_id = $komoju_order->getKomojuPaymentId();
  80.         //             $config_service = $this->container->get("plg_komoju.service.config");
  81.         //             $config_data = $config_service->getConfigData($TargetOrder);
  82.         //             $komoju_client = new KomojuClient($config_data['secret_key']);
  83.         //             $payment_obj = $komoju_client->getPayment($payment_id);
  84.         //             if($komoju_client->getStatusCode() != 200 || empty($payment_obj)){
  85.         //                 return;
  86.         //             }
  87.         //             if($payment_obj['status'] == "pending" || $payment_obj['status'] == "authorized"){
  88.         //                 $res = $komoju_client->cancelPayment($payment_id);
  89.         //             }
  90.         //         }
  91.         //     }
  92.         // }
  93.     }
  94.     /**
  95.      * @param TemplateEvent $event
  96.      */
  97.     public function onShoppingConfirmTwig(TemplateEvent $event){
  98.         $Order $event->getParameter("Order");
  99.         if($Order){
  100.             if($Order->getPayment()->getMethodClass() === KomojuMultiPay::class){
  101.                 $config_service $this->container->get("plg_komoju.service.config");
  102.                 // $config_repo = $this->entityManager->getRepository(KomojuConfig::class);
  103.                 // $config = $config_repo->getConfigByOrder($Order);
  104.                 $config $config_service->getConfigData($Order);
  105.                 $total_amount $Order->getPaymentTotal();
  106.                 
  107.                 $order_items $Order->getProductOrderItems();
  108.                 
  109.                 $first_prod_name $order_items[0]->getProduct()->getName();
  110.                 $cnt count($order_items);
  111.                 if($cnt 1){
  112.                     $title $first_prod_name " and " . ($cnt 1) . " more";
  113.                 }else{
  114.                     $title $first_prod_name;
  115.                 }
  116.                 $description $this->base_info->getShopName();
  117.                 $methods $this->entityManager->getRepository(KomojuPay::class)->getEnabledMethodsString();
  118.                 // $currency = $Order->getCurrencyCode();
  119.                 $event->setParameter("publishable_key"$config['publishable_key']);
  120.                 $event->setParameter("total_amount"$total_amount);
  121.                 $event->setParameter("title"$title);
  122.                 $event->setParameter("description"$description);
  123.                 $event->setParameter("methods"$methods);
  124.                 // $event->setParameter("currency", $currency);
  125.                 $event->addSnippet('@komoju42/default/shopping/komoju_multipay.twig');
  126.             }
  127.         }
  128.     }
  129.     /**
  130.      * @param EventArgs $event
  131.      */
  132.     public function onFrontShoppingCompleteInitialize(EventArgs $event){
  133.         $Order=$event->getArgument('Order');
  134.         if($Order) {
  135.             if ($Order->getPayment()->getMethodClass() === KomojuMultiPay::class) {
  136.                 $komoju_order_repo $this->entityManager->getRepository(KomojuOrder::class);
  137.                 $komoju_order $komoju_order_repo->findOneBy(array('Order'=>$Order));
  138.                 if($komoju_order) {
  139.                     $payment_id $komoju_order->getKomojuPaymentId();
  140.                     if (!empty($payment_id) && $komoju_order->isCaptured()) {
  141.                         $Today = new \DateTime();
  142.                         $Order->setPaymentDate($Today);
  143.                         $OrderStatus $this->entityManager->getRepository(OrderStatus::class)->find(OrderStatus::PAID);
  144.                         $Order->setOrderStatus($OrderStatus);
  145.                         $this->entityManager->persist($Order);
  146.                         $this->entityManager->flush($Order);
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.     }
  152.     /**
  153.      * @param TemplateEvent $event
  154.      */
  155.     public function onAdminOrderIndexTwig(TemplateEvent $event){
  156.         $pagination $event->getParameter("pagination");
  157.         if (empty($pagination) || count($pagination) == 0)
  158.         {
  159.             return;
  160.         }
  161.         $OrderToSearch=array();
  162.         foreach ($pagination as $Order){
  163.             $OrderToSearch[] = $Order;
  164.         }
  165.         if (empty($OrderToSearch)) {
  166.             return;
  167.         }
  168.         $config_service $this->container->get("plg_komoju.service.config");
  169.         $komoju_order_repo $this->entityManager->getRepository(KomojuOrder::class);
  170.         $komoju_orders $komoju_order_repo->findBy(['Order'    =>  $OrderToSearch]);
  171.         if(empty($komoju_orders)){
  172.             return;
  173.         }
  174.         $komoju_orders_mapping = [];
  175.         $komoju_order_mapping = array();
  176.         foreach($komoju_orders as $komoju_order){
  177.             $Order $komoju_order->getOrder();
  178.             
  179.             if($komoju_order->getKomojuPaymentId()){
  180.                 $dashboard_url $this->getKomojuDashboardLink($komoju_order->getKomojuPaymentId());
  181.             }else{
  182.                 $dashboard_url null;
  183.             }
  184.             $order_edit_url $this->container->get('router')->generate('admin_order_edit', ['id' => $Order->getId(), UrlGeneratorInterface::ABSOLUTE_URL ]);
  185.             $komoju_order_mapping[] = (object)['order_edit_url' => str_replace("?0=0"""$order_edit_url), 'payment_id' => $komoju_order->getKomojuPaymentId(), 'dashboard_url' => $dashboard_url];            
  186.             
  187.         }
  188.         
  189.         $event->setParameter('komoju_order_mapping'$komoju_order_mapping);
  190.         $event->addAsset('@komoju42\admin\order_index.js.twig');
  191.     }
  192.     
  193.     /**
  194.      * @param TemplateEvent
  195.      */
  196.     public function onAdminOrderEditTwig(TemplateEvent $event){
  197.         $Order $event->getParameter("Order");
  198.         if(!$Order || empty($Order->getPayment())){
  199.             return;
  200.         }
  201.         if ($Order->getPayment()->getMethodClass() === KomojuMultiPay::class) {
  202.             $komoju_order $this->entityManager->getRepository(KomojuOrder::class)->findOneBy(['Order' => $Order]);
  203.             if(empty($komoju_order)  
  204.                 || empty($komoju_order->getKomojuPaymentId())
  205.                 // || $komoju_order->getType() !== "credit_card" 
  206.                 ){
  207.                 return ;
  208.             }
  209.             if(!$komoju_order->getIsChargeRefunded() && $komoju_order->getSelectedRefundOption() === && $komoju_order->getRefundedAmount() == 0){
  210.                 $komoju_order->setRefundedAmount($Order->getPaymentTotal());
  211.                 $this->entityManager->persist($komoju_order);
  212.                 $this->entityManager->flush();
  213.             }
  214.             $refund_full_option KomojuOrder::REFUND_FULL;
  215.             $refund_partial_option KomojuOrder::REFUND_PARTIAL;
  216.             $order_canceled $Order->getOrderStatus()->getId() == OrderStatus::CANCEL;
  217.             
  218.             $event->setParameter("komoju_order"$komoju_order);
  219.             $event->setParameter("order_canceled"$order_canceled);
  220.             $event->setParameter("komoju_dashboard_link"$this->getKomojuDashboardLink($komoju_order->getKomojuPaymentId()));
  221.             $event->setParameter('REFUND_FULL_OPTION',  $refund_full_option);
  222.             $event->setParameter('REFUND_PARTIAL_OPTION',  $refund_partial_option);
  223.             $event->addSnippet("@komoju42/admin/order_edit.twig");
  224.         }
  225.     }
  226.     private function getKomojuDashboardLink($komoju_payment_id){
  227.         return "https://komoju.com/admin/payments/$komoju_payment_id";
  228.     }
  229. }