commandPool = $commandPool; $this->transactionRepository = $repository; $this->filterBuilder = $filterBuilder; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->subjectReader = $subjectReader; } /** * @inheritdoc */ public function execute(array $commandSubject): void { /** @var PaymentDataObjectInterface $paymentDO */ $paymentDO = $this->subjectReader->readPayment($commandSubject); $command = $this->getCommand($paymentDO); $this->commandPool->get($command) ->execute($commandSubject); } /** * Get execution command name. * * @param PaymentDataObjectInterface $paymentDO * @return string */ private function getCommand(PaymentDataObjectInterface $paymentDO): string { $payment = $paymentDO->getPayment(); ContextHelper::assertOrderPayment($payment); // If auth transaction does not exist then execute authorize&capture command $captureExists = $this->captureTransactionExists($payment); if (!$payment->getAuthorizationTransaction() && !$captureExists) { return self::SALE; } return self::CAPTURE; } /** * Check if capture transaction already exists * * @param OrderPaymentInterface $payment * @return bool */ private function captureTransactionExists(OrderPaymentInterface $payment): bool { $this->searchCriteriaBuilder->addFilters( [ $this->filterBuilder ->setField('payment_id') ->setValue($payment->getId()) ->create(), ] ); $this->searchCriteriaBuilder->addFilters( [ $this->filterBuilder ->setField('txn_type') ->setValue(TransactionInterface::TYPE_CAPTURE) ->create(), ] ); $searchCriteria = $this->searchCriteriaBuilder->create(); $count = $this->transactionRepository->getList($searchCriteria) ->getTotalCount(); return $count > 0; } }