commandPool = $commandPool; $this->transactionRepository = $transactionRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->filterBuilder = $filterBuilder; $this->coreHelper = $coreHelper; $this->orderAdapterFactory = $orderAdapterFactory; } /** * @inheritdoc */ public function execute(array $commandSubject) { if (isset($commandSubject['payment'])) { $paymentDO = $commandSubject['payment']; $paymentInfo = $paymentDO->getPayment(); // The magento order adapter doesn't expose everything we need to send a request to the AP API so we // need to use our own version with the details we need exposed in custom methods. $orderAdapter = $this->orderAdapterFactory->create( ['order' => $paymentInfo->getOrder()] ); $commandSubject['partial_capture'] = false; $commandSubject['amazon_order_id'] = $orderAdapter->getAmazonOrderID(); $commandSubject['multicurrency'] = $orderAdapter->getMulticurrencyDetails($commandSubject['amount']); ContextHelper::assertOrderPayment($paymentInfo); $command = $this->getCommand($paymentInfo); if ($command) { if ($command == self::PARTIAL_CAPTURE) { $commandSubject['partial_capture'] = true; $command = self::SALE; } $this->commandPool->get($command)->execute($commandSubject); } } } /** * Get execution command name - if there's an authorization, this is just a settlement, if not, could be * a partial capture situation where we need to completely auth and capture again against the same order * * @param OrderPaymentInterface $payment * @return string */ private function getCommand(OrderPaymentInterface $payment) { $isCaptured = $this->captureTransactionExists($payment); // If an authorization exists, we're going to settle it with a capture if (!$isCaptured && $payment->getAuthorizationTransaction()) { return self::CAPTURE; } // Item has already been captured - need to reauthorize and capture (partial capture) if ($isCaptured) { return self::PARTIAL_CAPTURE; } // We're in a situation where we need a reauth and capture. return self::SALE; } /** * Check if capture transaction already exists * * @param OrderPaymentInterface $payment * @return bool */ private function captureTransactionExists(OrderPaymentInterface $payment) { $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 (boolean)$count; } }