collectionFactory = $collectionFactory; $this->authorization = $authorization; $this->coreHelper = $coreHelper; $this->transactionRepository = $transactionRepository; $this->searchBuilder = $searchBuilder; $this->filterBuilder = $filterBuilder; $this->filterGroup = $filterGroup; $this->logger = $logger; $limit = (int)$limit; if ($limit < 1) { throw new \InvalidArgumentException('Limit must be greater than 1.'); } $this->limit = $limit; } /** * Since we can't get order or payment ID during gateway transaction, we make sure items in the * amazon_pending_authorization table have these IDs if they are not set by matching them to a transaction that * has matching transaction or parent transaction IDs. */ private function updateIds() { // only get items that have no order ID set since we don't want to have to keep repeating this $collection = $this->collectionFactory ->create() ->addFieldToFilter('order_id', ['eq' => 0]) ->addOrder(PendingAuthorizationInterface::UPDATED_AT, Collection::SORT_ORDER_ASC) ->setPageSize($this->limit) ->setCurPage(1); foreach ($collection->getItems() as $item) { if ($item) { $hasTransaction = false; $parent = $this->filterBuilder ->setField('parent_txn_id') ->setValue($item->getAuthorizationId()) ->setConditionType('eq') ->create(); $child = $this->filterBuilder ->setField('txn_id') ->setValue($item->getAuthorizationId()) ->setConditionType('eq') ->create(); $filterOr = $this->filterGroup->setFilters([$parent, $child]); $searchCriteria = $this->searchBuilder->setFilterGroups([$filterOr])->create(); $transactionList = $this->transactionRepository->getList($searchCriteria); foreach ($transactionList->getItems() as $transaction) { if ($transaction) { $item->setPaymentId($transaction->getPaymentId()); $item->setOrderId($transaction->getOrderId()); $item->save(); $hasTransaction = true; } } // If there's no match, get rid of this item in the table so the cron job will not error out. if (!$hasTransaction) { $item->delete(); } } } } /** * During cron, process payments if possible. */ public function execute() { if (UpdateMechanism::IPN === $this->coreHelper->getUpdateMechanism()) { return; } $this->updateIds(); $collection = $this->collectionFactory ->create() ->addOrder(PendingAuthorizationInterface::UPDATED_AT, Collection::SORT_ORDER_ASC) ->setPageSize($this->limit) ->setCurPage(1); $pendingAuthorizationIds = $collection->getIdGenerator(); foreach ($pendingAuthorizationIds as $pendingAuthorizationId) { try { $this->authorization->updateAuthorization($pendingAuthorizationId); } catch (\Exception $e) { $this->logger->error($e); } } } }