PaymentManagement.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Payment\Api\Data\PendingAuthorizationInterfaceFactory;
  18. use Amazon\Payment\Api\Data\PendingCaptureInterfaceFactory;
  19. use Amazon\Payment\Api\Data\PendingRefundInterfaceFactory;
  20. use Amazon\Payment\Domain\Details\AmazonAuthorizationDetails;
  21. use Amazon\Payment\Domain\Details\AmazonCaptureDetails;
  22. use Amazon\Payment\Domain\Details\AmazonRefundDetails;
  23. use Magento\Framework\Api\SearchCriteriaBuilderFactory;
  24. use Magento\Framework\Exception\NoSuchEntityException;
  25. use Magento\Payment\Model\InfoInterface as PaymentInfoInterface;
  26. use Magento\Sales\Api\Data\OrderInterface;
  27. use Magento\Sales\Api\Data\TransactionInterface;
  28. use Magento\Sales\Api\OrderPaymentRepositoryInterface;
  29. use Magento\Sales\Api\OrderRepositoryInterface;
  30. use Magento\Sales\Api\TransactionRepositoryInterface;
  31. /**
  32. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  33. */
  34. class PaymentManagement
  35. {
  36. /**
  37. * @var PendingCaptureInterfaceFactory
  38. */
  39. private $pendingCaptureFactory;
  40. /**
  41. * @var PendingAuthorizationInterfaceFactory
  42. */
  43. private $pendingAuthorizationFactory;
  44. /**
  45. * @var PendingRefundInterfaceFactory
  46. */
  47. private $pendingRefundFactory;
  48. /**
  49. * @var SearchCriteriaBuilderFactory
  50. */
  51. private $searchCriteriaBuilderFactory;
  52. /**
  53. * @var OrderPaymentRepositoryInterface
  54. */
  55. private $orderPaymentRepository;
  56. /**
  57. * @var OrderRepositoryInterface
  58. */
  59. private $orderRepository;
  60. /**
  61. * @var TransactionRepositoryInterface
  62. */
  63. private $transactionRepository;
  64. /**
  65. * PaymentManagement constructor.
  66. *
  67. * @param PendingCaptureInterfaceFactory $pendingCaptureFactory
  68. * @param PendingAuthorizationInterfaceFactory $pendingAuthorizationFactory
  69. * @param PendingRefundInterfaceFactory $pendingRefundFactory
  70. * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
  71. * @param OrderPaymentRepositoryInterface $orderPaymentRepository
  72. * @param OrderRepositoryInterface $orderRepository
  73. * @param TransactionRepositoryInterface $transactionRepository
  74. */
  75. public function __construct(
  76. PendingCaptureInterfaceFactory $pendingCaptureFactory,
  77. PendingAuthorizationInterfaceFactory $pendingAuthorizationFactory,
  78. PendingRefundInterfaceFactory $pendingRefundFactory,
  79. SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
  80. OrderPaymentRepositoryInterface $orderPaymentRepository,
  81. OrderRepositoryInterface $orderRepository,
  82. TransactionRepositoryInterface $transactionRepository
  83. ) {
  84. $this->pendingCaptureFactory = $pendingCaptureFactory;
  85. $this->pendingAuthorizationFactory = $pendingAuthorizationFactory;
  86. $this->pendingRefundFactory = $pendingRefundFactory;
  87. $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
  88. $this->orderPaymentRepository = $orderPaymentRepository;
  89. $this->orderRepository = $orderRepository;
  90. $this->transactionRepository = $transactionRepository;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function queuePendingCapture(AmazonCaptureDetails $details, $paymentId, $orderId)
  96. {
  97. $this->pendingCaptureFactory->create()
  98. ->setCaptureId($details->getTransactionId())
  99. ->setPaymentId($paymentId)
  100. ->setOrderId($orderId)
  101. ->save();
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function queuePendingAuthorization(AmazonAuthorizationDetails $details, OrderInterface $order)
  107. {
  108. $pendingAuthorization = $this->pendingAuthorizationFactory->create()
  109. ->setAuthorizationId($details->getAuthorizeTransactionId());
  110. if ($details->hasCapture()) {
  111. $pendingAuthorization->setCaptureId($details->getCaptureTransactionId())
  112. ->setCapture(true);
  113. }
  114. $order->addRelatedObject($pendingAuthorization);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function queuePendingRefund(AmazonRefundDetails $details, PaymentInfoInterface $payment)
  120. {
  121. $this->pendingRefundFactory->create()
  122. ->setRefundId($details->getRefundId())
  123. ->setPaymentId($payment->getId())
  124. ->setOrderId($payment->getOrder()->getId())
  125. ->save();
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function closeTransaction($transactionId, PaymentInfoInterface $payment, OrderInterface $order)
  131. {
  132. $this->getTransaction($transactionId, $payment, $order)->setIsClosed(1)->save();
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function getTransaction($transactionId, PaymentInfoInterface $payment, OrderInterface $order)
  138. {
  139. $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
  140. $searchCriteriaBuilder->addFilter(
  141. TransactionInterface::TXN_ID,
  142. $transactionId
  143. );
  144. $searchCriteriaBuilder->addFilter(
  145. TransactionInterface::ORDER_ID,
  146. $order->getId()
  147. );
  148. $searchCriteriaBuilder->addFilter(
  149. TransactionInterface::PAYMENT_ID,
  150. $payment->getId()
  151. );
  152. $searchCriteria = $searchCriteriaBuilder
  153. ->setPageSize(1)
  154. ->setCurrentPage(1)
  155. ->create();
  156. $transactionList = $this->transactionRepository->getList($searchCriteria);
  157. if (count($items = $transactionList->getItems())) {
  158. $transaction = current($items);
  159. $transaction
  160. ->setPayment($payment)
  161. ->setOrder($order);
  162. return $transaction;
  163. }
  164. throw new NoSuchEntityException();
  165. }
  166. }