Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
RefundInvoice Class Reference
Inheritance diagram for RefundInvoice:
RefundInvoiceInterface

Public Member Functions

 __construct (ResourceConnection $resourceConnection, OrderStateResolverInterface $orderStateResolver, OrderRepositoryInterface $orderRepository, InvoiceRepositoryInterface $invoiceRepository, RefundInvoiceValidator $validator, CreditmemoRepositoryInterface $creditmemoRepository, RefundAdapterInterface $refundAdapter, CreditmemoDocumentFactory $creditmemoDocumentFactory, NotifierInterface $notifier, OrderConfig $config, LoggerInterface $logger)
 
 execute ( $invoiceId, array $items=[], $isOnline=false, $notify=false, $appendComment=false, \Magento\Sales\Api\Data\CreditmemoCommentCreationInterface $comment=null, \Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface $arguments=null)
 

Detailed Description

Class RefundInvoice @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 25 of file RefundInvoice.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ResourceConnection  $resourceConnection,
OrderStateResolverInterface  $orderStateResolver,
OrderRepositoryInterface  $orderRepository,
InvoiceRepositoryInterface  $invoiceRepository,
RefundInvoiceValidator  $validator,
CreditmemoRepositoryInterface  $creditmemoRepository,
RefundAdapterInterface  $refundAdapter,
CreditmemoDocumentFactory  $creditmemoDocumentFactory,
NotifierInterface  $notifier,
OrderConfig  $config,
LoggerInterface  $logger 
)

RefundInvoice constructor.

Parameters
ResourceConnection$resourceConnection
OrderStateResolverInterface$orderStateResolver
OrderRepositoryInterface$orderRepository
InvoiceRepositoryInterface$invoiceRepository
RefundInvoiceValidator$validator
CreditmemoRepositoryInterface$creditmemoRepository
RefundAdapterInterface$refundAdapter
CreditmemoDocumentFactory$creditmemoDocumentFactory
NotifierInterface$notifier
OrderConfig$config
LoggerInterface$logger@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 98 of file RefundInvoice.php.

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  }
$invoiceRepository
$config
Definition: fraud_order.php:17
$orderRepository
Definition: order.php:69
$logger

Member Function Documentation

◆ execute()

execute (   $invoiceId,
array  $items = [],
  $isOnline = false,
  $notify = false,
  $appendComment = false,
\Magento\Sales\Api\Data\CreditmemoCommentCreationInterface  $comment = null,
\Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface  $arguments = null 
)

Create refund for invoice

Parameters
int$invoiceId

Implements RefundInvoiceInterface.

Definition at line 127 of file RefundInvoice.php.

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  }
$order
Definition: order.php:55
__()
Definition: __.php:13
$invoice
$arguments
$connection
Definition: bulk.php:13
$items

The documentation for this class was generated from the following file: