Capture.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\PaymentManagement;
  17. use Amazon\Core\Client\ClientFactoryInterface;
  18. use Amazon\Payment\Api\Data\PendingCaptureInterface;
  19. use Amazon\Payment\Api\Data\PendingCaptureInterfaceFactory;
  20. use Amazon\Payment\Model\PaymentManagement\Capture;
  21. use Amazon\Payment\Model\PaymentManagement;
  22. use Amazon\Payment\Domain\AmazonCaptureDetailsResponseFactory;
  23. use Amazon\Payment\Domain\AmazonCaptureStatus;
  24. use Amazon\Payment\Domain\Details\AmazonCaptureDetails;
  25. use Exception;
  26. use Magento\Backend\Model\UrlInterface;
  27. use Magento\Framework\Api\SearchCriteriaBuilderFactory;
  28. use Magento\Framework\Notification\NotifierInterface;
  29. use Magento\Sales\Api\Data\OrderInterface;
  30. use Magento\Sales\Api\Data\OrderPaymentInterface;
  31. use Magento\Sales\Api\Data\TransactionInterface;
  32. use Magento\Sales\Api\InvoiceRepositoryInterface;
  33. use Magento\Sales\Api\OrderPaymentRepositoryInterface;
  34. use Magento\Sales\Api\OrderRepositoryInterface;
  35. use Magento\Sales\Api\TransactionRepositoryInterface;
  36. use Magento\Store\Model\StoreManagerInterface;
  37. use Psr\Log\LoggerInterface;
  38. /**
  39. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  40. */
  41. class Capture extends AbstractOperation
  42. {
  43. /**
  44. * @var ClientFactoryInterface
  45. */
  46. private $clientFactory;
  47. /**
  48. * @var PendingCaptureInterfaceFactory
  49. */
  50. private $pendingCaptureFactory;
  51. /**
  52. * @var AmazonCaptureDetailsResponseFactory
  53. */
  54. private $amazonCaptureDetailsResponseFactory;
  55. /**
  56. * @var OrderPaymentRepositoryInterface
  57. */
  58. private $orderPaymentRepository;
  59. /**
  60. * @var OrderRepositoryInterface
  61. */
  62. private $orderRepository;
  63. /**
  64. * @var TransactionRepositoryInterface
  65. */
  66. private $transactionRepository;
  67. /**
  68. * @var StoreManagerInterface
  69. */
  70. private $storeManager;
  71. /**
  72. * @var PaymentManagement
  73. */
  74. private $paymentManagement;
  75. /**
  76. * @var LoggerInterface
  77. */
  78. private $logger;
  79. /**
  80. * @var bool
  81. */
  82. private $throwExceptions = false;
  83. /**
  84. * Capture constructor.
  85. *
  86. * @param NotifierInterface $notifier
  87. * @param UrlInterface $urlBuilder
  88. * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
  89. * @param InvoiceRepositoryInterface $invoiceRepository
  90. * @param ClientFactoryInterface $clientFactory
  91. * @param PendingCaptureInterfaceFactory $pendingCaptureFactory
  92. * @param AmazonCaptureDetailsResponseFactory $amazonCaptureDetailsResponseFactory
  93. * @param OrderPaymentRepositoryInterface $orderPaymentRepository
  94. * @param OrderRepositoryInterface $orderRepository
  95. * @param TransactionRepositoryInterface $transactionRepository
  96. * @param StoreManagerInterface $storeManager
  97. * @param PaymentManagement $paymentManagement
  98. * @param LoggerInterface $logger
  99. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  100. */
  101. public function __construct(
  102. NotifierInterface $notifier,
  103. UrlInterface $urlBuilder,
  104. SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
  105. InvoiceRepositoryInterface $invoiceRepository,
  106. ClientFactoryInterface $clientFactory,
  107. PendingCaptureInterfaceFactory $pendingCaptureFactory,
  108. AmazonCaptureDetailsResponseFactory $amazonCaptureDetailsResponseFactory,
  109. OrderPaymentRepositoryInterface $orderPaymentRepository,
  110. OrderRepositoryInterface $orderRepository,
  111. TransactionRepositoryInterface $transactionRepository,
  112. StoreManagerInterface $storeManager,
  113. PaymentManagement $paymentManagement,
  114. LoggerInterface $logger
  115. ) {
  116. $this->clientFactory = $clientFactory;
  117. $this->pendingCaptureFactory = $pendingCaptureFactory;
  118. $this->amazonCaptureDetailsResponseFactory = $amazonCaptureDetailsResponseFactory;
  119. $this->orderPaymentRepository = $orderPaymentRepository;
  120. $this->orderRepository = $orderRepository;
  121. $this->transactionRepository = $transactionRepository;
  122. $this->storeManager = $storeManager;
  123. $this->paymentManagement = $paymentManagement;
  124. $this->logger = $logger;
  125. parent::__construct($notifier, $urlBuilder, $searchCriteriaBuilderFactory, $invoiceRepository);
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function setThrowExceptions($throwExceptions)
  131. {
  132. $this->throwExceptions = $throwExceptions;
  133. return $this;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function updateCapture($pendingCaptureId, AmazonCaptureDetails $captureDetails = null)
  139. {
  140. try {
  141. $pendingCapture = $this->pendingCaptureFactory->create();
  142. $pendingCapture->getResource()->beginTransaction();
  143. $pendingCapture->setLockOnLoad(true);
  144. $pendingCapture->load($pendingCaptureId);
  145. if ($pendingCapture->getCaptureId()) {
  146. $order = $this->orderRepository->get($pendingCapture->getOrderId());
  147. $payment = $this->orderPaymentRepository->get($pendingCapture->getPaymentId());
  148. $order->setPayment($payment);
  149. $order->setData(OrderInterface::PAYMENT, $payment);
  150. $storeId = $order->getStoreId();
  151. $this->storeManager->setCurrentStore($storeId);
  152. if (null === $captureDetails) {
  153. $responseParser = $this->clientFactory->create($storeId)->getCaptureDetails(
  154. [
  155. 'amazon_capture_id' => $pendingCapture->getCaptureId()
  156. ]
  157. );
  158. $response = $this->amazonCaptureDetailsResponseFactory->create(['response' => $responseParser]);
  159. $captureDetails = $response->getDetails();
  160. }
  161. $this->processUpdateCaptureResponse($captureDetails, $pendingCapture, $payment, $order);
  162. }
  163. $pendingCapture->getResource()->commit();
  164. } catch (Exception $e) {
  165. $this->logger->error($e);
  166. $pendingCapture->getResource()->rollBack();
  167. if ($this->throwExceptions) {
  168. throw $e;
  169. }
  170. }
  171. }
  172. protected function processUpdateCaptureResponse(
  173. AmazonCaptureDetails $details,
  174. PendingCaptureInterface $pendingCapture,
  175. OrderPaymentInterface $payment,
  176. OrderInterface $order
  177. ) {
  178. $status = $details->getStatus();
  179. switch ($status->getState()) {
  180. case AmazonCaptureStatus::STATE_COMPLETED:
  181. $this->completePendingCapture($pendingCapture, $payment, $order);
  182. break;
  183. case AmazonCaptureStatus::STATE_DECLINED:
  184. $this->declinePendingCapture($pendingCapture, $payment, $order);
  185. break;
  186. }
  187. }
  188. protected function completePendingCapture(
  189. PendingCaptureInterface $pendingCapture,
  190. OrderPaymentInterface $payment,
  191. OrderInterface $order
  192. ) {
  193. $transactionId = $pendingCapture->getCaptureId();
  194. $transaction = $this->paymentManagement->getTransaction($transactionId, $payment, $order);
  195. $invoice = $this->getInvoice($transactionId, $order);
  196. $formattedAmount = $order->getBaseCurrency()->formatTxt($invoice->getBaseGrandTotal());
  197. $message = __('Captured amount of %1 online', $formattedAmount);
  198. $this->getInvoiceAndSetPaid($transactionId, $order);
  199. $payment->setDataUsingMethod('base_amount_paid_online', $invoice->getBaseGrandTotal());
  200. $this->setProcessing($order);
  201. $payment->addTransactionCommentsToOrder($transaction, $message);
  202. $order->save();
  203. $this->paymentManagement->closeTransaction($transactionId, $payment, $order);
  204. $pendingCapture->delete();
  205. }
  206. protected function declinePendingCapture(
  207. PendingCaptureInterface $pendingCapture,
  208. OrderPaymentInterface $payment,
  209. OrderInterface $order
  210. ) {
  211. $transactionId = $pendingCapture->getCaptureId();
  212. $transaction = $this->paymentManagement->getTransaction($transactionId, $payment, $order);
  213. $invoice = $this->getInvoice($transactionId, $order);
  214. $formattedAmount = $order->getBaseCurrency()->formatTxt($invoice->getBaseGrandTotal());
  215. $message = __('Declined amount of %1 online', $formattedAmount);
  216. $this->getInvoiceAndSetCancelled($transactionId, $order);
  217. $this->setOnHold($order);
  218. $payment->addTransactionCommentsToOrder($transaction, $message);
  219. $order->save();
  220. $this->paymentManagement->closeTransaction($transactionId, $payment, $order);
  221. $pendingCapture->delete();
  222. $this->addCaptureDeclinedNotice($order);
  223. }
  224. }