Capture.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
  8. class Capture extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View
  9. {
  10. /**
  11. * Capture invoice action
  12. *
  13. * @return \Magento\Framework\Controller\ResultInterface
  14. */
  15. public function execute()
  16. {
  17. $invoice = $this->getInvoice();
  18. if (!$invoice) {
  19. /** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
  20. $resultForward = $this->resultForwardFactory->create();
  21. $resultForward->forward('noroute');
  22. return $resultForward;
  23. }
  24. try {
  25. $invoiceManagement = $this->_objectManager->get(\Magento\Sales\Api\InvoiceManagementInterface::class);
  26. $invoiceManagement->setCapture($invoice->getEntityId());
  27. $invoice->getOrder()->setIsInProcess(true);
  28. $this->_objectManager->create(
  29. \Magento\Framework\DB\Transaction::class
  30. )->addObject(
  31. $invoice
  32. )->addObject(
  33. $invoice->getOrder()
  34. )->save();
  35. $this->messageManager->addSuccessMessage(__('The invoice has been captured.'));
  36. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  37. $this->messageManager->addErrorMessage($e->getMessage());
  38. } catch (\Exception $e) {
  39. $this->messageManager->addErrorMessage(__('Invoice capturing error'));
  40. }
  41. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  42. $resultRedirect = $this->resultRedirectFactory->create();
  43. $resultRedirect->setPath('sales/*/view', ['invoice_id' => $invoice->getId()]);
  44. return $resultRedirect;
  45. }
  46. }