AbstractOperation.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Magento\Backend\Model\UrlInterface;
  18. use Magento\Framework\Api\SearchCriteriaBuilderFactory;
  19. use Magento\Framework\Event\ManagerInterface;
  20. use Magento\Framework\Exception\NoSuchEntityException;
  21. use Magento\Framework\Notification\NotifierInterface;
  22. use Magento\Payment\Model\InfoInterface as PaymentInfoInterface;
  23. use Magento\Sales\Api\Data\InvoiceInterface;
  24. use Magento\Sales\Api\Data\OrderInterface;
  25. use Magento\Sales\Api\Data\OrderPaymentInterface;
  26. use Magento\Sales\Api\Data\TransactionInterface;
  27. use Magento\Sales\Api\InvoiceRepositoryInterface;
  28. use Magento\Sales\Api\OrderPaymentRepositoryInterface;
  29. use Magento\Sales\Api\OrderRepositoryInterface;
  30. use Magento\Sales\Api\TransactionRepositoryInterface;
  31. use Magento\Sales\Model\Order;
  32. use Magento\Sales\Model\Order\Payment\Transaction;
  33. use Magento\Store\Model\ScopeInterface;
  34. use Magento\Store\Model\StoreManagerInterface;
  35. abstract class AbstractOperation
  36. {
  37. /**
  38. * @var SearchCriteriaBuilderFactory
  39. */
  40. private $searchCriteriaBuilderFactory;
  41. /**
  42. * @var UrlInterface
  43. */
  44. private $urlBuilder;
  45. /**
  46. * @var NotifierInterface
  47. */
  48. private $notifier;
  49. /**
  50. * @var InvoiceRepositoryInterface
  51. */
  52. private $invoiceRepository;
  53. public function __construct(
  54. NotifierInterface $notifier,
  55. UrlInterface $urlBuilder,
  56. SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
  57. InvoiceRepositoryInterface $invoiceRepository
  58. ) {
  59. $this->notifier = $notifier;
  60. $this->urlBuilder = $urlBuilder;
  61. $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
  62. $this->invoiceRepository = $invoiceRepository;
  63. }
  64. protected function getInvoice($transactionId, OrderInterface $order)
  65. {
  66. $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
  67. $searchCriteriaBuilder->addFilter(
  68. InvoiceInterface::TRANSACTION_ID,
  69. $transactionId
  70. );
  71. $searchCriteriaBuilder->addFilter(
  72. InvoiceInterface::ORDER_ID,
  73. $order->getId()
  74. );
  75. $searchCriteria = $searchCriteriaBuilder
  76. ->setPageSize(1)
  77. ->setCurrentPage(1)
  78. ->create();
  79. $invoiceList = $this->invoiceRepository->getList($searchCriteria);
  80. if (count($items = $invoiceList->getItems())) {
  81. $invoice = current($items);
  82. $invoice->setOrder($order);
  83. return $invoice;
  84. }
  85. throw new NoSuchEntityException();
  86. }
  87. protected function getInvoiceAndSetPaid($transactionId, OrderInterface $order)
  88. {
  89. $invoice = $this->getInvoice($transactionId, $order);
  90. $invoice->pay();
  91. $order->addRelatedObject($invoice);
  92. return $invoice;
  93. }
  94. protected function getInvoiceAndSetCancelled($transactionId, OrderInterface $order)
  95. {
  96. $invoice = $this->getInvoice($transactionId, $order);
  97. $invoice->cancel();
  98. $order->addRelatedObject($invoice);
  99. return $invoice;
  100. }
  101. protected function setOnHold(OrderInterface $order)
  102. {
  103. $this->setOrderState($order, Order::STATE_HOLDED);
  104. }
  105. protected function setProcessing(OrderInterface $order)
  106. {
  107. $this->setOrderState($order, Order::STATE_PROCESSING);
  108. }
  109. protected function setPaymentReview(OrderInterface $order)
  110. {
  111. $this->setOrderState($order, Order::STATE_PAYMENT_REVIEW);
  112. }
  113. protected function setOrderState(OrderInterface $order, $state)
  114. {
  115. $status = $order->getConfig()->getStateDefaultStatus($state);
  116. $order->setState($state)->setStatus($status);
  117. }
  118. protected function addCaptureDeclinedNotice(OrderInterface $order)
  119. {
  120. $orderUrl = $this->urlBuilder->getUrl('sales/order/view', ['order_id' => $order->getId()]);
  121. $this->notifier->addNotice(
  122. __('Capture declined'),
  123. __('Capture declined for Order #%1', $order->getIncrementId()),
  124. $orderUrl
  125. );
  126. }
  127. }