<?php declare(strict_types=1);
namespace AmMacTrade\Storefront\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Zeobv\CartRecovery\Service\CartRecoveryService;
use Shopware\Core\System\StateMachine\StateMachineRegistry;
use Shopware\Core\System\StateMachine\Transition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Symfony\Component\HttpFoundation\Request;
class CheckoutSubscriber implements EventSubscriberInterface
{
/**
* @var CartRecoveryService
*/
protected $cartRecoveryService;
protected $stateMachineRegistry;
private $orderTransactionRepository;
private $orderRepository;
private $connection;
/**
* CheckoutSubscriber constructor.
*
* @param CartRecoveryService $cartRecoveryService
*/
public function __construct(
CartRecoveryService $cartRecoveryService,
StateMachineRegistry $stateMachineRegistry,
EntityRepositoryInterface $orderTransactionRepository,
OrderTransactionStateHandler $orderTransactionStateHandler,
EntityRepositoryInterface $orderRepository,
Connection $connection
)
{
$this->cartRecoveryService = $cartRecoveryService;
$this->stateMachineRegistry = $stateMachineRegistry;
$this->orderTransactionRepository = $orderTransactionRepository;
$this->orderRepository = $orderRepository;
$this->connection = $connection;
$this->orderTransactionStateHandler = $orderTransactionStateHandler;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
AccountEditOrderPageLoadedEvent::class => 'onEditOrderPageLoaded',
'state_enter.order_transaction.state.cancel' => 'onOrderStateChanged',
CustomerRegisterEvent::class => 'onCustomerRegisterEvent',
];
}
public function onCustomerRegisterEvent(CustomerRegisterEvent $event): void
{
$request = Request::createFromGlobals();
//$mactradeAccpetGroup = $request->request->get('mactradeAccpetGroup');
$requestedGroupId = $request->request->get('requestedGroupId');
if($requestedGroupId == 'cfbd5018d38d41d8adca10d94fc8bdd6') {
return;
}
$customerId = $event->getCustomerId();
$customerId = strtoupper($customerId);
$getCustomer = $this->connection->fetchOne("SELECT requested_customer_group_id from `customer` WHERE `id` = UNHEX('$customerId')");
if($getCustomer && $requestedGroupId) {
$this->connection->executeStatement("UPDATE `customer` SET `requested_customer_group_id` = NULL
, `customer_group_id` = UNHEX('$requestedGroupId') WHERE `id` = UNHEX('$customerId');");
}
}
public function onEditOrderPageLoaded( AccountEditOrderPageLoadedEvent $event)
{
$order = $event->getPage()->getOrder();
$order_id = $order->getId();
if ( $this->paymentFailedForOrder($order) ) {
//exit();
$this->cartRecoveryService->recoverCartFromOrder( $order, $event->getSalesChannelContext() );
$order_id = $order->getId();
$state_id = $order->getStateId();
$order_number = $order->getOrderNumber();
$transactions = $order->getTransactions();
$deliveries = $order->getDeliveries();
$context = $event->getContext();
foreach ($transactions as $transaction){
$transactionId = $transaction->id;
}
foreach ($deliveries as $delivery){
$deliveryID = $delivery->id;
}
try {
$this->stateMachineRegistry->transition(new Transition(
OrderDefinition::ENTITY_NAME,
$order_id,
'cancel',
'stateId'
), $context);
} catch (\Throwable $th) {
$this->stateMachineRegistry->transition(new Transition(
OrderTransactionDefinition::ENTITY_NAME,
$transactionId,
'cancel',
'stateId'
), $context);
$this->stateMachineRegistry->transition(new Transition(
OrderDeliveryDefinition::ENTITY_NAME,
$deliveryID,
'cancel',
'stateId'
), $context);
}
} else {
$this->cartRecoveryService->cleanUpCartWaitingForRecoveryForOrder( $order, $event->getSalesChannelContext() );
}
$url = 'https://'.$_SERVER['SERVER_NAME'].'/checkout/confirm';
?>
<script type="text/javascript">
window.location = '<?php echo $url; ?>';
</script>
<?php
//$event->setResponse($response);
}
private function isPayeverPaymentMethod(OrderEntity $order): bool
{
$transaction = $order->getTransactions()->first();
if( $transaction->getPaymentMethod()->getShortName() == 'payever_payment' ){
return true;
}
return false;
}
public function onOrderStateChanged(StateMachineStateChangeEvent $event): void
{
echo 'here';
die;
if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {
return;
}
$order = $this->getOrder($event);
}
/**
* @param OrderEntity $order
*
* @return bool
*/
protected function paymentFailedForOrder(OrderEntity $order)
{
$latestTransactionStateName = $order->getTransactions()->last()->getStateMachineState()->getTechnicalName();
return in_array($latestTransactionStateName, [
'cancelled',
'denied'
]);
}
}