* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ namespace PrestaShop\Module\PrestashopCheckout\Order\QueryHandler; use Configuration; use Order; use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException; use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException; use PrestaShop\Module\PrestashopCheckout\Order\Exception\OrderNotFoundException; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentPendingQuery; use PrestaShop\Module\PrestashopCheckout\Order\Query\GetOrderForPaymentPendingQueryResult; use PrestaShop\Module\PrestashopCheckout\Order\State\OrderStateConfigurationKeys; use PrestaShop\Module\PrestashopCheckout\Repository\PsCheckoutCartRepository; use PrestaShopCollection; use PrestaShopDatabaseException; use PrestaShopException; use PsCheckoutCart; use Validate; class GetOrderForPaymentPendingQueryHandler { /** * @var PsCheckoutCartRepository */ private $psCheckoutCartRepository; public function __construct(PsCheckoutCartRepository $psCheckoutCartRepository) { $this->psCheckoutCartRepository = $psCheckoutCartRepository; } /** * @param GetOrderForPaymentPendingQuery $query * * @return GetOrderForPaymentPendingQueryResult * * @throws PsCheckoutException * @throws PrestaShopDatabaseException * @throws PrestaShopException */ public function handle(GetOrderForPaymentPendingQuery $query) { /** @var PsCheckoutCart|false $psCheckoutCart */ $psCheckoutCart = $this->psCheckoutCartRepository->findOneByPayPalOrderId($query->getOrderPayPalId()->getValue()); if (!$psCheckoutCart) { throw new CartNotFoundException('No PrestaShop Cart associated to this PayPal Order at this time.'); } $orders = new PrestaShopCollection(Order::class); $orders->where('id_cart', '=', $psCheckoutCart->getIdCart()); if (!$orders->count()) { throw new OrderNotFoundException('No PrestaShop Order associated to this PayPal Order at this time.'); } /** @var Order $order */ $order = $orders->getFirst(); if (!Validate::isLoadedObject($order)) { throw new OrderNotFoundException('No PrestaShop Order associated to this PayPal Order at this time.'); } return new GetOrderForPaymentPendingQueryResult( (int) $order->id, $this->isInPending($order) ); } /** * @param Order $order * * @return bool */ private function isInPending(Order $order) { if (count($order->getHistory($order->id_lang, (int) Configuration::getGlobalValue(OrderStateConfigurationKeys::PS_CHECKOUT_STATE_PENDING)))) { return true; } // Check deprecated states $deprecatedStates = [ OrderStateConfigurationKeys::PS_CHECKOUT_STATE_WAITING_CREDIT_CARD_PAYMENT, OrderStateConfigurationKeys::PS_CHECKOUT_STATE_WAITING_LOCAL_PAYMENT, OrderStateConfigurationKeys::PS_CHECKOUT_STATE_WAITING_PAYPAL_PAYMENT, ]; foreach ($deprecatedStates as $deprecatedState) { $deprecatedStateId = (int) Configuration::getGlobalValue($deprecatedState); if ($deprecatedStateId && count($order->getHistory($order->id_lang, $deprecatedStateId))) { return true; } } return false; } }