QueuedRefundUpdater.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Model;
  17. use Amazon\Core\Client\ClientFactoryInterface;
  18. use Amazon\Payment\Api\Data\PendingRefundInterface;
  19. use Amazon\Payment\Api\Data\PendingRefundInterfaceFactory;
  20. use Amazon\Payment\Domain\AmazonRefundDetailsResponseFactory;
  21. use Amazon\Payment\Domain\AmazonRefundStatus;
  22. use Amazon\Payment\Domain\Details\AmazonRefundDetails;
  23. use Magento\Framework\Notification\NotifierInterface;
  24. use Magento\Sales\Api\OrderPaymentRepositoryInterface;
  25. use Magento\Sales\Api\OrderRepositoryInterface;
  26. use Magento\Store\Model\StoreManagerInterface;
  27. use Psr\Log\LoggerInterface;
  28. class QueuedRefundUpdater
  29. {
  30. /**
  31. * @var OrderRepositoryInterface
  32. */
  33. private $orderRepository;
  34. /**
  35. * @var OrderPaymentRepositoryInterface
  36. */
  37. private $orderPaymentRepository;
  38. /**
  39. * @var ClientFactoryInterface
  40. */
  41. private $amazonHttpClientFactory;
  42. /**
  43. * @var AmazonRefundDetailsResponseFactory
  44. */
  45. private $amazonRefundDetailsResponseFactory;
  46. /**
  47. * @var NotifierInterface
  48. */
  49. private $adminNotifier;
  50. /**
  51. * @var PendingRefundInterfaceFactory
  52. */
  53. private $pendingRefundFactory;
  54. /**
  55. * @var StoreManagerInterface
  56. */
  57. private $storeManager;
  58. /**
  59. * @var LoggerInterface
  60. */
  61. private $logger;
  62. /**
  63. * @var bool
  64. */
  65. private $throwExceptions = false;
  66. /**
  67. * @param OrderRepositoryInterface $orderRepository
  68. * @param OrderPaymentRepositoryInterface $orderPaymentRepository
  69. * @param ClientFactoryInterface $amazonHttpClientFactory
  70. * @param AmazonRefundDetailsResponseFactory $amazonRefundDetailsResponseFactory
  71. * @param NotifierInterface $adminNotifier
  72. * @param PendingRefundInterfaceFactory $pendingRefundFactory
  73. * @param StoreManagerInterface $storeManager
  74. * @param LoggerInterface $logger
  75. */
  76. public function __construct(
  77. OrderRepositoryInterface $orderRepository,
  78. OrderPaymentRepositoryInterface $orderPaymentRepository,
  79. ClientFactoryInterface $amazonHttpClientFactory,
  80. AmazonRefundDetailsResponseFactory $amazonRefundDetailsResponseFactory,
  81. NotifierInterface $adminNotifier,
  82. PendingRefundInterfaceFactory $pendingRefundFactory,
  83. StoreManagerInterface $storeManager,
  84. LoggerInterface $logger
  85. ) {
  86. $this->orderRepository = $orderRepository;
  87. $this->orderPaymentRepository = $orderPaymentRepository;
  88. $this->amazonHttpClientFactory = $amazonHttpClientFactory;
  89. $this->amazonRefundDetailsResponseFactory = $amazonRefundDetailsResponseFactory;
  90. $this->adminNotifier = $adminNotifier;
  91. $this->pendingRefundFactory = $pendingRefundFactory;
  92. $this->storeManager = $storeManager;
  93. $this->logger = $logger;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function setThrowExceptions($throwExceptions)
  99. {
  100. $this->throwExceptions = $throwExceptions;
  101. return $this;
  102. }
  103. /**
  104. * @param int $pendingRefundId
  105. *
  106. * @return void
  107. */
  108. public function checkAndUpdateRefund($pendingRefundId, AmazonRefundDetails $refundDetails = null)
  109. {
  110. try {
  111. $pendingRefund = $this->pendingRefundFactory->create();
  112. $pendingRefund->getResource()->beginTransaction();
  113. $pendingRefund->setLockOnLoad(true);
  114. $pendingRefund->load($pendingRefundId);
  115. if ($pendingRefund->getRefundId()) {
  116. $order = $this->orderRepository->get($pendingRefund->getOrderId());
  117. $storeId = $order->getStoreId();
  118. $this->storeManager->setCurrentStore($storeId);
  119. if (null === $refundDetails) {
  120. $responseParser = $this->amazonHttpClientFactory->create($storeId)->getRefundDetails([
  121. 'amazon_refund_id' => $pendingRefund->getRefundId()
  122. ]);
  123. $response = $this->amazonRefundDetailsResponseFactory->create(['response' => $responseParser]);
  124. $refundDetails = $response->getDetails();
  125. }
  126. $status = $refundDetails->getRefundStatus();
  127. switch ($status->getState()) {
  128. case AmazonRefundStatus::STATE_COMPLETED:
  129. $pendingRefund->delete();
  130. break;
  131. case AmazonRefundStatus::STATE_DECLINED:
  132. $this->triggerAdminNotificationForDeclinedRefund($pendingRefund);
  133. $pendingRefund->delete();
  134. break;
  135. }
  136. }
  137. $pendingRefund->getResource()->commit();
  138. } catch (\Exception $e) {
  139. $this->logger->error($e);
  140. $pendingRefund->getResource()->rollBack();
  141. if ($this->throwExceptions) {
  142. throw $e;
  143. }
  144. }
  145. }
  146. /**
  147. * @param PendingRefundInterface $pendingRefund
  148. *
  149. * @return void
  150. */
  151. protected function triggerAdminNotificationForDeclinedRefund(PendingRefundInterface $pendingRefund)
  152. {
  153. $this->adminNotifier->addMajor(
  154. 'Amazon Pay has declined a refund',
  155. "Refund ID {$pendingRefund->getRefundId()} for Order ID {$pendingRefund->getOrderId()} " .
  156. "has been declined by Amazon Pay."
  157. );
  158. }
  159. }