clientFactory = $clientFactory; $this->pendingCaptureFactory = $pendingCaptureFactory; $this->amazonCaptureDetailsResponseFactory = $amazonCaptureDetailsResponseFactory; $this->orderPaymentRepository = $orderPaymentRepository; $this->orderRepository = $orderRepository; $this->transactionRepository = $transactionRepository; $this->storeManager = $storeManager; $this->paymentManagement = $paymentManagement; $this->logger = $logger; parent::__construct($notifier, $urlBuilder, $searchCriteriaBuilderFactory, $invoiceRepository); } /** * {@inheritdoc} */ public function setThrowExceptions($throwExceptions) { $this->throwExceptions = $throwExceptions; return $this; } /** * {@inheritdoc} */ public function updateCapture($pendingCaptureId, AmazonCaptureDetails $captureDetails = null) { try { $pendingCapture = $this->pendingCaptureFactory->create(); $pendingCapture->getResource()->beginTransaction(); $pendingCapture->setLockOnLoad(true); $pendingCapture->load($pendingCaptureId); if ($pendingCapture->getCaptureId()) { $order = $this->orderRepository->get($pendingCapture->getOrderId()); $payment = $this->orderPaymentRepository->get($pendingCapture->getPaymentId()); $order->setPayment($payment); $order->setData(OrderInterface::PAYMENT, $payment); $storeId = $order->getStoreId(); $this->storeManager->setCurrentStore($storeId); if (null === $captureDetails) { $responseParser = $this->clientFactory->create($storeId)->getCaptureDetails( [ 'amazon_capture_id' => $pendingCapture->getCaptureId() ] ); $response = $this->amazonCaptureDetailsResponseFactory->create(['response' => $responseParser]); $captureDetails = $response->getDetails(); } $this->processUpdateCaptureResponse($captureDetails, $pendingCapture, $payment, $order); } $pendingCapture->getResource()->commit(); } catch (Exception $e) { $this->logger->error($e); $pendingCapture->getResource()->rollBack(); if ($this->throwExceptions) { throw $e; } } } protected function processUpdateCaptureResponse( AmazonCaptureDetails $details, PendingCaptureInterface $pendingCapture, OrderPaymentInterface $payment, OrderInterface $order ) { $status = $details->getStatus(); switch ($status->getState()) { case AmazonCaptureStatus::STATE_COMPLETED: $this->completePendingCapture($pendingCapture, $payment, $order); break; case AmazonCaptureStatus::STATE_DECLINED: $this->declinePendingCapture($pendingCapture, $payment, $order); break; } } protected function completePendingCapture( PendingCaptureInterface $pendingCapture, OrderPaymentInterface $payment, OrderInterface $order ) { $transactionId = $pendingCapture->getCaptureId(); $transaction = $this->paymentManagement->getTransaction($transactionId, $payment, $order); $invoice = $this->getInvoice($transactionId, $order); $formattedAmount = $order->getBaseCurrency()->formatTxt($invoice->getBaseGrandTotal()); $message = __('Captured amount of %1 online', $formattedAmount); $this->getInvoiceAndSetPaid($transactionId, $order); $payment->setDataUsingMethod('base_amount_paid_online', $invoice->getBaseGrandTotal()); $this->setProcessing($order); $payment->addTransactionCommentsToOrder($transaction, $message); $order->save(); $this->paymentManagement->closeTransaction($transactionId, $payment, $order); $pendingCapture->delete(); } protected function declinePendingCapture( PendingCaptureInterface $pendingCapture, OrderPaymentInterface $payment, OrderInterface $order ) { $transactionId = $pendingCapture->getCaptureId(); $transaction = $this->paymentManagement->getTransaction($transactionId, $payment, $order); $invoice = $this->getInvoice($transactionId, $order); $formattedAmount = $order->getBaseCurrency()->formatTxt($invoice->getBaseGrandTotal()); $message = __('Declined amount of %1 online', $formattedAmount); $this->getInvoiceAndSetCancelled($transactionId, $order); $this->setOnHold($order); $payment->addTransactionCommentsToOrder($transaction, $message); $order->save(); $this->paymentManagement->closeTransaction($transactionId, $payment, $order); $pendingCapture->delete(); $this->addCaptureDeclinedNotice($order); } }