orderRepository = $orderRepository; $this->orderPaymentRepository = $orderPaymentRepository; $this->amazonHttpClientFactory = $amazonHttpClientFactory; $this->amazonRefundDetailsResponseFactory = $amazonRefundDetailsResponseFactory; $this->adminNotifier = $adminNotifier; $this->pendingRefundFactory = $pendingRefundFactory; $this->storeManager = $storeManager; $this->logger = $logger; } /** * {@inheritdoc} */ public function setThrowExceptions($throwExceptions) { $this->throwExceptions = $throwExceptions; return $this; } /** * @param int $pendingRefundId * * @return void */ public function checkAndUpdateRefund($pendingRefundId, AmazonRefundDetails $refundDetails = null) { try { $pendingRefund = $this->pendingRefundFactory->create(); $pendingRefund->getResource()->beginTransaction(); $pendingRefund->setLockOnLoad(true); $pendingRefund->load($pendingRefundId); if ($pendingRefund->getRefundId()) { $order = $this->orderRepository->get($pendingRefund->getOrderId()); $storeId = $order->getStoreId(); $this->storeManager->setCurrentStore($storeId); if (null === $refundDetails) { $responseParser = $this->amazonHttpClientFactory->create($storeId)->getRefundDetails([ 'amazon_refund_id' => $pendingRefund->getRefundId() ]); $response = $this->amazonRefundDetailsResponseFactory->create(['response' => $responseParser]); $refundDetails = $response->getDetails(); } $status = $refundDetails->getRefundStatus(); switch ($status->getState()) { case AmazonRefundStatus::STATE_COMPLETED: $pendingRefund->delete(); break; case AmazonRefundStatus::STATE_DECLINED: $this->triggerAdminNotificationForDeclinedRefund($pendingRefund); $pendingRefund->delete(); break; } } $pendingRefund->getResource()->commit(); } catch (\Exception $e) { $this->logger->error($e); $pendingRefund->getResource()->rollBack(); if ($this->throwExceptions) { throw $e; } } } /** * @param PendingRefundInterface $pendingRefund * * @return void */ protected function triggerAdminNotificationForDeclinedRefund(PendingRefundInterface $pendingRefund) { $this->adminNotifier->addMajor( 'Amazon Pay has declined a refund', "Refund ID {$pendingRefund->getRefundId()} for Order ID {$pendingRefund->getOrderId()} " . "has been declined by Amazon Pay." ); } }