resourceConnection = $resourceConnection;
$this->orderRepository = $orderRepository;
$this->invoiceDocumentFactory = $invoiceDocumentFactory;
$this->paymentAdapter = $paymentAdapter;
$this->orderStateResolver = $orderStateResolver;
$this->config = $config;
$this->invoiceRepository = $invoiceRepository;
$this->invoiceOrderValidator = $invoiceOrderValidator;
$this->notifierInterface = $notifierInterface;
$this->logger = $logger;
$this->orderMutex = $orderMutex ?: ObjectManager::getInstance()->get(OrderMutexInterface::class);
}
/**
* Creates invoice for provided order ID
*
* @param int $orderId
* @param bool $capture
* @param array $items
* @param bool $notify
* @param bool $appendComment
* @param \Magento\Sales\Api\Data\InvoiceCommentCreationInterface|null $comment
* @param \Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface|null $arguments
* @return int
* @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
* @throws \Magento\Sales\Api\Exception\CouldNotInvoiceExceptionInterface
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \DomainException
*/
public function execute(
$orderId,
$capture = false,
array $items = [],
$notify = false,
$appendComment = false,
InvoiceCommentCreationInterface $comment = null,
InvoiceCreationArgumentsInterface $arguments = null
) {
return $this->orderMutex->execute(
(int) $orderId,
\Closure::fromCallable([$this, 'createInvoice']),
[
$orderId,
$capture,
$items,
$notify,
$appendComment,
$comment,
$arguments
]
);
}
/**
* Creates invoice for provided order ID
*
* @param int $orderId
* @param bool $capture
* @param array $items
* @param bool $notify
* @param bool $appendComment
* @param \Magento\Sales\Api\Data\InvoiceCommentCreationInterface|null $comment
* @param \Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface|null $arguments
* @return int
* @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
* @throws \Magento\Sales\Api\Exception\CouldNotInvoiceExceptionInterface
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \DomainException
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function createInvoice(
$orderId,
$capture = false,
array $items = [],
$notify = false,
$appendComment = false,
InvoiceCommentCreationInterface $comment = null,
InvoiceCreationArgumentsInterface $arguments = null
) {
$order = $this->orderRepository->get($orderId);
$invoice = $this->invoiceDocumentFactory->create(
$order,
$items,
$comment,
($appendComment && $notify),
$arguments
);
$errorMessages = $this->invoiceOrderValidator->validate(
$order,
$invoice,
$capture,
$items,
$notify,
$appendComment,
$comment,
$arguments
);
if ($errorMessages->hasMessages()) {
throw new \Magento\Sales\Exception\DocumentValidationException(
__("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages->getMessages()))
);
}
try {
$order = $this->paymentAdapter->pay($order, $invoice, $capture);
$order->setState(
$this->orderStateResolver->getStateForOrder($order, [OrderStateResolverInterface::IN_PROGRESS])
);
$order->setStatus($this->config->getStateDefaultStatus($order->getState()));
$invoice->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
$this->invoiceRepository->save($invoice);
$this->orderRepository->save($order);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new \Magento\Sales\Exception\CouldNotInvoiceException(
__('Could not save an invoice, see error log for details')
);
}
if ($notify) {
if (!$appendComment) {
$comment = null;
}
$this->notifierInterface->notify($order, $invoice, $comment);
}
return $invoice->getEntityId();
}
}