custom/plugins/AmMacTrade/src/Storefront/Subscriber/CheckoutSubscriber.php line 88

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace AmMacTrade\Storefront\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  6. use Zeobv\CartRecovery\Service\CartRecoveryService;
  7. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  8. use Shopware\Core\System\StateMachine\Transition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Doctrine\DBAL\Connection;
  11. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryDefinition;
  12. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
  13. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
  14. use Shopware\Core\Checkout\Order\OrderDefinition;
  15. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  16. use Symfony\Component\HttpFoundation\Request;
  17. class CheckoutSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var CartRecoveryService
  21.      */
  22.     protected $cartRecoveryService;
  23.     protected $stateMachineRegistry;
  24.     private $orderTransactionRepository;
  25.     private $orderRepository;
  26.     private $connection;
  27.     /**
  28.      * CheckoutSubscriber constructor.
  29.      *
  30.      * @param CartRecoveryService $cartRecoveryService
  31.      */
  32.     public function __construct(
  33.         CartRecoveryService $cartRecoveryService,
  34.         StateMachineRegistry $stateMachineRegistry,
  35.         EntityRepositoryInterface $orderTransactionRepository,
  36.         OrderTransactionStateHandler $orderTransactionStateHandler,
  37.         EntityRepositoryInterface $orderRepository,
  38.         Connection $connection
  39.     )
  40.     {
  41.         $this->cartRecoveryService $cartRecoveryService;
  42.         $this->stateMachineRegistry $stateMachineRegistry;
  43.         $this->orderTransactionRepository $orderTransactionRepository;
  44.         $this->orderRepository $orderRepository;
  45.         $this->connection $connection;
  46.         $this->orderTransactionStateHandler $orderTransactionStateHandler;
  47.     }
  48.     /**
  49.      * @return array
  50.      */
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             AccountEditOrderPageLoadedEvent::class => 'onEditOrderPageLoaded',
  55.             'state_enter.order_transaction.state.cancel' => 'onOrderStateChanged',
  56.             CustomerRegisterEvent::class => 'onCustomerRegisterEvent',
  57.         ];
  58.     }
  59.     public function onCustomerRegisterEvent(CustomerRegisterEvent $event): void
  60.     {
  61.         $request Request::createFromGlobals();
  62.         //$mactradeAccpetGroup = $request->request->get('mactradeAccpetGroup');
  63.         $requestedGroupId $request->request->get('requestedGroupId');
  64.         if($requestedGroupId == 'cfbd5018d38d41d8adca10d94fc8bdd6') {
  65.             return;
  66.         }
  67.         $customerId $event->getCustomerId();
  68.         $customerId strtoupper($customerId);
  69.         $getCustomer $this->connection->fetchOne("SELECT requested_customer_group_id from `customer` WHERE `id` = UNHEX('$customerId')");
  70.         if($getCustomer && $requestedGroupId) {
  71.             $this->connection->executeStatement("UPDATE `customer` SET `requested_customer_group_id` = NULL 
  72.             , `customer_group_id` =  UNHEX('$requestedGroupId') WHERE `id` = UNHEX('$customerId');");
  73.         }
  74.     }
  75.     public function onEditOrderPageLoadedAccountEditOrderPageLoadedEvent $event)
  76.     {
  77.         
  78.         $order $event->getPage()->getOrder();
  79.         $order_id $order->getId();
  80.         if ( $this->paymentFailedForOrder($order) ) {
  81.             //exit();
  82.             $this->cartRecoveryService->recoverCartFromOrder$order$event->getSalesChannelContext() );
  83.             $order_id $order->getId();
  84.             $state_id $order->getStateId();
  85.             $order_number $order->getOrderNumber();
  86.             $transactions $order->getTransactions();
  87.             $deliveries $order->getDeliveries();
  88.             $context $event->getContext();
  89.             foreach ($transactions as $transaction){
  90.                 $transactionId $transaction->id;
  91.             }
  92.             foreach ($deliveries as $delivery){
  93.                 $deliveryID $delivery->id;
  94.             }
  95.             try {
  96.                 $this->stateMachineRegistry->transition(new Transition(
  97.                     OrderDefinition::ENTITY_NAME,
  98.                     $order_id,
  99.                     'cancel',
  100.                     'stateId'
  101.                 ), $context);
  102.             } catch (\Throwable $th) {
  103.                 $this->stateMachineRegistry->transition(new Transition(
  104.                     OrderTransactionDefinition::ENTITY_NAME,
  105.                     $transactionId,
  106.                     'cancel',
  107.                     'stateId'
  108.                 ), $context);
  109.                 $this->stateMachineRegistry->transition(new Transition(
  110.                     OrderDeliveryDefinition::ENTITY_NAME,
  111.                     $deliveryID,
  112.                     'cancel',
  113.                     'stateId'
  114.                 ), $context);
  115.             }
  116.         } else {
  117.             $this->cartRecoveryService->cleanUpCartWaitingForRecoveryForOrder$order$event->getSalesChannelContext() );
  118.         }
  119.         $url 'https://'.$_SERVER['SERVER_NAME'].'/checkout/confirm';
  120.         ?>
  121.         <script type="text/javascript">
  122.             window.location = '<?php echo $url?>';
  123.         </script>
  124.         <?php
  125.         //$event->setResponse($response);
  126.     }
  127.     private function isPayeverPaymentMethod(OrderEntity $order): bool
  128.     {
  129.         $transaction $order->getTransactions()->first();
  130.         if( $transaction->getPaymentMethod()->getShortName() == 'payever_payment' ){
  131.             return true;
  132.         }
  133.         return false;
  134.     }
  135.     public function onOrderStateChanged(StateMachineStateChangeEvent $event): void
  136.     {
  137.         echo 'here';
  138.         die;
  139.         if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {
  140.             return;
  141.         }
  142.         $order $this->getOrder($event);
  143.     }
  144.     /**
  145.      * @param OrderEntity $order
  146.      *
  147.      * @return bool
  148.      */
  149.     protected function paymentFailedForOrder(OrderEntity $order)
  150.     {
  151.         $latestTransactionStateName $order->getTransactions()->last()->getStateMachineState()->getTechnicalName();
  152.         return in_array($latestTransactionStateName, [
  153.             'cancelled',
  154.             'denied'
  155.         ]);
  156.     }
  157. }