Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RefundInvoice.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Sales\Model;
7 
13 use Magento\Sales\Model\Order\Config as OrderConfig;
19 use Psr\Log\LoggerInterface;
20 
26 {
30  private $resourceConnection;
31 
35  private $orderStateResolver;
36 
40  private $orderRepository;
41 
45  private $invoiceRepository;
46 
50  private $creditmemoRepository;
51 
55  private $refundAdapter;
56 
60  private $creditmemoDocumentFactory;
61 
65  private $notifier;
66 
70  private $config;
71 
75  private $logger;
76 
80  private $validator;
81 
98  public function __construct(
99  ResourceConnection $resourceConnection,
100  OrderStateResolverInterface $orderStateResolver,
101  OrderRepositoryInterface $orderRepository,
102  InvoiceRepositoryInterface $invoiceRepository,
103  RefundInvoiceValidator $validator,
104  CreditmemoRepositoryInterface $creditmemoRepository,
105  RefundAdapterInterface $refundAdapter,
106  CreditmemoDocumentFactory $creditmemoDocumentFactory,
107  NotifierInterface $notifier,
108  OrderConfig $config,
109  LoggerInterface $logger
110  ) {
111  $this->resourceConnection = $resourceConnection;
112  $this->orderStateResolver = $orderStateResolver;
113  $this->orderRepository = $orderRepository;
114  $this->invoiceRepository = $invoiceRepository;
115  $this->validator = $validator;
116  $this->creditmemoRepository = $creditmemoRepository;
117  $this->refundAdapter = $refundAdapter;
118  $this->creditmemoDocumentFactory = $creditmemoDocumentFactory;
119  $this->notifier = $notifier;
120  $this->config = $config;
121  $this->logger = $logger;
122  }
123 
127  public function execute(
128  $invoiceId,
129  array $items = [],
130  $isOnline = false,
131  $notify = false,
132  $appendComment = false,
133  \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment = null,
134  \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments = null
135  ) {
136  $connection = $this->resourceConnection->getConnection('sales');
137  $invoice = $this->invoiceRepository->get($invoiceId);
138  $order = $this->orderRepository->get($invoice->getOrderId());
139  $creditmemo = $this->creditmemoDocumentFactory->createFromInvoice(
140  $invoice,
141  $items,
142  $comment,
143  ($appendComment && $notify),
144  $arguments
145  );
146 
147  $validationMessages = $this->validator->validate(
148  $invoice,
149  $order,
150  $creditmemo,
151  $items,
152  $isOnline,
153  $notify,
154  $appendComment,
155  $comment,
156  $arguments
157  );
158  if ($validationMessages->hasMessages()) {
159  throw new \Magento\Sales\Exception\DocumentValidationException(
160  __("Creditmemo Document Validation Error(s):\n" . implode("\n", $validationMessages->getMessages()))
161  );
162  }
163  $connection->beginTransaction();
164  try {
165  $creditmemo->setState(\Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED);
166  $order->setCustomerNoteNotify($notify);
167  $order = $this->refundAdapter->refund($creditmemo, $order, $isOnline);
168  $order->setState(
169  $this->orderStateResolver->getStateForOrder($order, [])
170  );
171  $order->setStatus($this->config->getStateDefaultStatus($order->getState()));
172  if (!$isOnline) {
173  $invoice->setIsUsedForRefund(true);
174  $invoice->setBaseTotalRefunded(
175  $invoice->getBaseTotalRefunded() + $creditmemo->getBaseGrandTotal()
176  );
177  }
178  $this->invoiceRepository->save($invoice);
179  $order = $this->orderRepository->save($order);
180  $creditmemo = $this->creditmemoRepository->save($creditmemo);
181  $connection->commit();
182  } catch (\Exception $e) {
183  $this->logger->critical($e);
184  $connection->rollBack();
185  throw new \Magento\Sales\Exception\CouldNotRefundException(
186  __('Could not save a Creditmemo, see error log for details')
187  );
188  }
189  if ($notify) {
190  if (!$appendComment) {
191  $comment = null;
192  }
193  $this->notifier->notify($order, $creditmemo, $comment);
194  }
195 
196  return $creditmemo->getEntityId();
197  }
198 }
$invoiceRepository
$config
Definition: fraud_order.php:17
$orderRepository
Definition: order.php:69
execute( $invoiceId, array $items=[], $isOnline=false, $notify=false, $appendComment=false, \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment=null, \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments=null)
$order
Definition: order.php:55
__()
Definition: __.php:13
$logger
$invoice
__construct(ResourceConnection $resourceConnection, OrderStateResolverInterface $orderStateResolver, OrderRepositoryInterface $orderRepository, InvoiceRepositoryInterface $invoiceRepository, RefundInvoiceValidator $validator, CreditmemoRepositoryInterface $creditmemoRepository, RefundAdapterInterface $refundAdapter, CreditmemoDocumentFactory $creditmemoDocumentFactory, NotifierInterface $notifier, OrderConfig $config, LoggerInterface $logger)
$arguments
$connection
Definition: bulk.php:13
$items