Payment.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Authorizenet\Controller\Directpost;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Framework\App\Action\HttpPostActionInterface;
  10. use Magento\Payment\Block\Transparent\Iframe;
  11. use Magento\Framework\App\Action\Action;
  12. /**
  13. * DirectPost Payment Controller
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. * @deprecated 100.3.1 Authorize.net is removing all support for this payment method
  17. */
  18. abstract class Payment extends Action implements HttpGetActionInterface, HttpPostActionInterface
  19. {
  20. /**
  21. * Core registry
  22. *
  23. * @var \Magento\Framework\Registry
  24. */
  25. protected $_coreRegistry = null;
  26. /**
  27. * @var \Magento\Authorizenet\Helper\DataFactory
  28. */
  29. protected $dataFactory;
  30. /**
  31. * Constructor
  32. *
  33. * @param \Magento\Framework\App\Action\Context $context
  34. * @param \Magento\Framework\Registry $coreRegistry
  35. * @param \Magento\Authorizenet\Helper\DataFactory $dataFactory
  36. */
  37. public function __construct(
  38. \Magento\Framework\App\Action\Context $context,
  39. \Magento\Framework\Registry $coreRegistry,
  40. \Magento\Authorizenet\Helper\DataFactory $dataFactory
  41. ) {
  42. $this->_coreRegistry = $coreRegistry;
  43. $this->dataFactory = $dataFactory;
  44. parent::__construct($context);
  45. }
  46. /**
  47. * Get checkout model
  48. *
  49. * @return \Magento\Checkout\Model\Session
  50. */
  51. protected function _getCheckout()
  52. {
  53. return $this->_objectManager->get(\Magento\Checkout\Model\Session::class);
  54. }
  55. /**
  56. * Get session model
  57. *
  58. * @return \Magento\Authorizenet\Model\Directpost\Session
  59. */
  60. protected function _getDirectPostSession()
  61. {
  62. return $this->_objectManager->get(\Magento\Authorizenet\Model\Directpost\Session::class);
  63. }
  64. /**
  65. * Response action.
  66. *
  67. * Action for Authorize.net SIM Relay Request.
  68. *
  69. * @param string $area
  70. * @return void
  71. */
  72. protected function _responseAction($area = 'frontend')
  73. {
  74. $helper = $this->dataFactory->create($area);
  75. $params = [];
  76. $data = $this->getRequest()->getParams();
  77. /* @var $paymentMethod \Magento\Authorizenet\Model\DirectPost */
  78. $paymentMethod = $this->_objectManager->create(\Magento\Authorizenet\Model\Directpost::class);
  79. $result = [];
  80. if (!empty($data['x_invoice_num'])) {
  81. $result['x_invoice_num'] = $data['x_invoice_num'];
  82. $params['order_success'] = $helper->getSuccessOrderUrl($result);
  83. }
  84. try {
  85. if (!empty($data['store_id'])) {
  86. $paymentMethod->setStore($data['store_id']);
  87. }
  88. $paymentMethod->process($data);
  89. $result['success'] = 1;
  90. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  91. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  92. $result['success'] = 0;
  93. $result['error_msg'] = $e->getMessage();
  94. } catch (\Exception $e) {
  95. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  96. $result['success'] = 0;
  97. $result['error_msg'] = __('We can\'t process your order right now. Please try again later.');
  98. }
  99. if (!empty($data['controller_action_name'])
  100. && strpos($data['controller_action_name'], 'sales_order_') === false
  101. ) {
  102. if (!empty($data['key'])) {
  103. $result['key'] = $data['key'];
  104. }
  105. $result['controller_action_name'] = $data['controller_action_name'];
  106. $result['is_secure'] = isset($data['is_secure']) ? $data['is_secure'] : false;
  107. $params['redirect'] = $helper->getRedirectIframeUrl($result);
  108. }
  109. //registering parameter for iframe content
  110. $this->_coreRegistry->register(Iframe::REGISTRY_KEY, $params);
  111. }
  112. /**
  113. * Return customer quote
  114. *
  115. * @param bool $cancelOrder
  116. * @param string $errorMsg
  117. * @return void
  118. */
  119. protected function _returnCustomerQuote($cancelOrder = false, $errorMsg = '')
  120. {
  121. $incrementId = $this->_getDirectPostSession()->getLastOrderIncrementId();
  122. if ($incrementId && $this->_getDirectPostSession()->isCheckoutOrderIncrementIdExist($incrementId)) {
  123. /* @var $order \Magento\Sales\Model\Order */
  124. $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId($incrementId);
  125. if ($order->getId()) {
  126. try {
  127. /** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
  128. $quoteRepository = $this->_objectManager->create(\Magento\Quote\Api\CartRepositoryInterface::class);
  129. /** @var \Magento\Quote\Model\Quote $quote */
  130. $quote = $quoteRepository->get($order->getQuoteId());
  131. $quote->setIsActive(1)->setReservedOrderId(null);
  132. $quoteRepository->save($quote);
  133. $this->_getCheckout()->replaceQuote($quote);
  134. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  135. }
  136. $this->_getDirectPostSession()->removeCheckoutOrderIncrementId($incrementId);
  137. $this->_getDirectPostSession()->unsetData('quote_id');
  138. if ($cancelOrder) {
  139. $order->registerCancellation($errorMsg)->save();
  140. }
  141. }
  142. }
  143. }
  144. }