TaxInvoice.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Magento\Framework\Message\ManagerInterface;
  9. use Magento\Framework\Phrase;
  10. use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
  11. use Magento\Sales\Model\Order;
  12. use Magento\Store\Model\ScopeInterface;
  13. use Vertex\Exception\ApiException;
  14. use Vertex\Services\Invoice\RequestInterface;
  15. use Vertex\Tax\Api\InvoiceInterface;
  16. /**
  17. * Service for sending Tax Invoices to the Vertex API
  18. */
  19. class TaxInvoice
  20. {
  21. const REQUEST_TYPE = 'invoice';
  22. const REQUEST_TYPE_REFUND = 'invoice_refund';
  23. /** @var InvoiceInterface */
  24. private $invoice;
  25. /** @var ExceptionLogger */
  26. private $logger;
  27. /** @var ManagerInterface */
  28. private $messageManager;
  29. /** @var OrderStatusHistoryRepositoryInterface */
  30. private $orderStatusRepository;
  31. /**
  32. * @param ExceptionLogger $logger
  33. * @param ManagerInterface $messageManager
  34. * @param OrderStatusHistoryRepositoryInterface $orderStatusRepository
  35. * @param InvoiceInterface $invoice
  36. */
  37. public function __construct(
  38. ExceptionLogger $logger,
  39. ManagerInterface $messageManager,
  40. OrderStatusHistoryRepositoryInterface $orderStatusRepository,
  41. InvoiceInterface $invoice
  42. ) {
  43. $this->logger = $logger;
  44. $this->messageManager = $messageManager;
  45. $this->orderStatusRepository = $orderStatusRepository;
  46. $this->invoice = $invoice;
  47. }
  48. /**
  49. * Send the Invoice Request to the API
  50. *
  51. * @param RequestInterface $request
  52. * @param Order $order
  53. * @return null|\Vertex\Services\Invoice\ResponseInterface
  54. */
  55. public function sendInvoiceRequest(RequestInterface $request, Order $order)
  56. {
  57. try {
  58. $response = $this->invoice->record($request, $order->getStoreId(), ScopeInterface::SCOPE_STORE);
  59. } catch (\Exception $e) {
  60. $this->logger->error($e);
  61. $this->addErrorMessage(__('Could not submit invoice to Vertex.'), $e);
  62. return null;
  63. }
  64. $totalTax = $response->getTotalTax();
  65. $comment = $order->addStatusHistoryComment(
  66. 'Vertex Invoice sent successfully. Amount: $' . number_format($totalTax, 2)
  67. );
  68. try {
  69. $this->orderStatusRepository->save($comment);
  70. } catch (\Exception $originalException) {
  71. $exception = new \Exception('Could not save Vertex invoice comment', 0, $originalException);
  72. $this->logger->critical($exception);
  73. }
  74. return $response;
  75. }
  76. /**
  77. * Send the Creditmemo Request to the API
  78. *
  79. * @param RequestInterface $request
  80. * @param Order|null $order
  81. * @return null|\Vertex\Services\Invoice\ResponseInterface
  82. */
  83. public function sendRefundRequest(RequestInterface $request, Order $order)
  84. {
  85. try {
  86. $response = $this->invoice->record($request, $order->getStoreId(), ScopeInterface::SCOPE_STORE);
  87. } catch (\Exception $e) {
  88. $this->addErrorMessage(__('Could not submit refund to Vertex.'), $e);
  89. return null;
  90. }
  91. $totalTax = $response->getTotalTax();
  92. $comment = $order->addStatusHistoryComment(
  93. 'Vertex Invoice refunded successfully. Amount: $' . number_format($totalTax, 2)
  94. );
  95. try {
  96. $this->orderStatusRepository->save($comment);
  97. } catch (\Exception $originalException) {
  98. $exception = new CouldNotSaveException(
  99. __('Could not save Vertex invoice refund comment'),
  100. $originalException
  101. );
  102. $this->logger->critical($exception);
  103. }
  104. return $response;
  105. }
  106. private function addErrorMessage(Phrase $friendlyPhrase, $exception = null)
  107. {
  108. $friendlyMessage = $friendlyPhrase->render();
  109. $errorMessage = null;
  110. $exceptionClass = $exception !== null ? get_class($exception) : null;
  111. switch ($exceptionClass) {
  112. case ApiException\AuthenticationException::class:
  113. $errorMessage = __(
  114. '%1 Please verify your configured Company Code and Trusted ID are correct.',
  115. $friendlyMessage
  116. );
  117. break;
  118. case ApiException\ConnectionFailureException::class:
  119. $errorMessage = __(
  120. '%1 Vertex could not be reached. Please verify your configuration.',
  121. $friendlyMessage
  122. );
  123. break;
  124. case ApiException::class:
  125. default:
  126. $errorMessage = __('%1 Error has been logged.', $friendlyMessage);
  127. break;
  128. }
  129. $this->messageManager->addErrorMessage($errorMessage);
  130. }
  131. }