Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/Ecombricks/InventoryPaypal/Controller/Express/AbstractExpress/Cancel/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/Ecombricks/InventoryPaypal/Controller/Express/AbstractExpress/Cancel/SourceTrait.php
<?php
/**
 * Copyright © eComBricks. All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace Ecombricks\InventoryPaypal\Controller\Express\AbstractExpress\Cancel;

/**
 * Cancel paypal express controller source trait
 */
trait SourceTrait
{
    
    /**
     * Clear checkout session
     * 
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @return $this
     */
    protected function clearCheckoutSession($checkoutSession)
    {
        $checkoutSession->unsLastQuoteId();
        $checkoutSession->unsLastSuccessQuoteId();
        $checkoutSession->unsLastOrderId();
        $checkoutSession->unsLastRealOrderId();
        $checkoutSession->unsLastOrderStatus();
        $checkoutSession->unsLastOrderIds();
        $checkoutSession->unsLastRealOrderIds();
        $checkoutSession->unsLastOrderStatuses();
        return $this;
    }
    
    /**
     * Cancel order
     * 
     * @param \Magento\Sales\Api\Data\OrderInterface $order
     * @return $this
     */
    protected function cancelOrder($order)
    {
        if (!$order->getId() || $order->getQuoteId() != $this->_checkoutSession->getQuoteId()) {
            $this->messageManager->addSuccessMessage(__('Express Checkout has been canceled.'));
            return $this;
        }
        $order->cancel()->save();
        $this->messageManager->addSuccessMessage(__('Express Checkout and Order have been canceled.'));
        return $this;
    }
    
    /**
     * Get last orders
     * 
     * @return array
     */
    protected function getLastOrders()
    {
        $orders = [];
        $orderIds = $this->_checkoutSession->getLastOrderIds();
        if (empty($orderIds)) {
            return $orders;
        }
        foreach ($orderIds as $orderId) {
            $orders[] = $this->_orderFactory->create()->load($orderId);
        }
        return $orders;
    }
    
    /**
     * Execute
     * 
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function inventoryExecute()
    {
        try {
            $this->_initToken(false);
            foreach ($this->getLastOrders() as $order) {
                $this->cancelOrder($order);
            }
            $this->clearCheckoutSession($this->_checkoutSession);
            $this->_getSession()->unsQuoteId(); 
        } catch (\Magento\Framework\Exception\LocalizedException $exception) {
            $this->messageManager->addExceptionMessage($exception, $exception->getMessage());
        } catch (\Exception $exception) {
            $this->messageManager->addExceptionMessage($exception, __('Unable to cancel Express Checkout'));
        }
        return $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
            ->setPath('checkout/cart');
    }
    
}

Spamworldpro Mini