Data.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\Helper;
  8. use Magento\Framework\App\Helper\AbstractHelper;
  9. use Magento\Framework\App\Helper\Context;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\Sales\Model\OrderFactory;
  12. use Magento\Authorizenet\Model\Directpost;
  13. use Magento\Authorizenet\Model\Authorizenet;
  14. /**
  15. * Authorize.net Data Helper
  16. *
  17. * @api
  18. * @since 100.0.2
  19. * @deprecated 100.3.1 Authorize.net is removing all support for this payment method
  20. */
  21. class Data extends AbstractHelper
  22. {
  23. /**
  24. * @var \Magento\Store\Model\StoreManagerInterface
  25. */
  26. protected $storeManager;
  27. /**
  28. * @var \Magento\Sales\Model\OrderFactory
  29. */
  30. protected $orderFactory;
  31. /**
  32. * Allowed currencies
  33. *
  34. * @var array
  35. */
  36. protected $allowedCurrencyCodes = ['USD'];
  37. /**
  38. * Transaction statuses key to value map
  39. *
  40. * @var array
  41. */
  42. protected $transactionStatuses = [
  43. 'authorizedPendingCapture' => 'Authorized/Pending Capture',
  44. 'capturedPendingSettlement' => 'Captured/Pending Settlement',
  45. 'refundSettledSuccessfully' => 'Refund/Settled Successfully',
  46. 'refundPendingSettlement' => 'Refund/Pending Settlement',
  47. 'declined' => 'Declined',
  48. 'expired' => 'Expired',
  49. 'voided' => 'Voided',
  50. 'FDSPendingReview' => 'FDS - Pending Review',
  51. 'FDSAuthorizedPendingReview' => 'FDS - Authorized/Pending Review'
  52. ];
  53. /**
  54. * Fraud filter actions key to value map
  55. *
  56. * @var array
  57. */
  58. protected $fdsFilterActions = [
  59. 'decline ' => 'Decline',
  60. 'hold' => 'Hold',
  61. 'authAndHold' => 'Authorize and Hold',
  62. 'report' => 'Report Only'
  63. ];
  64. /**
  65. * @param \Magento\Framework\App\Helper\Context $context
  66. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  67. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  68. */
  69. public function __construct(
  70. Context $context,
  71. StoreManagerInterface $storeManager,
  72. OrderFactory $orderFactory
  73. ) {
  74. $this->storeManager = $storeManager;
  75. $this->orderFactory = $orderFactory;
  76. parent::__construct($context);
  77. }
  78. /**
  79. * Set secure url checkout is secure for current store.
  80. *
  81. * @param string $route
  82. * @param array $params
  83. * @return string
  84. */
  85. protected function _getUrl($route, $params = [])
  86. {
  87. $params['_type'] = \Magento\Framework\UrlInterface::URL_TYPE_LINK;
  88. if (isset($params['is_secure'])) {
  89. $params['_secure'] = (bool)$params['is_secure'];
  90. } elseif ($this->storeManager->getStore()->isCurrentlySecure()) {
  91. $params['_secure'] = true;
  92. }
  93. return parent::_getUrl($route, $params);
  94. }
  95. /**
  96. * Retrieve save order url params
  97. *
  98. * @param string $controller
  99. * @return array
  100. */
  101. public function getSaveOrderUrlParams($controller)
  102. {
  103. $route = [];
  104. switch ($controller) {
  105. case 'onepage':
  106. $route['action'] = 'saveOrder';
  107. $route['controller'] = 'onepage';
  108. $route['module'] = 'checkout';
  109. break;
  110. case 'sales_order_create':
  111. case 'sales_order_edit':
  112. $route['action'] = 'save';
  113. $route['controller'] = 'sales_order_create';
  114. $route['module'] = 'admin';
  115. break;
  116. default:
  117. break;
  118. }
  119. return $route;
  120. }
  121. /**
  122. * Retrieve redirect iframe url
  123. *
  124. * @param array $params
  125. * @return string
  126. */
  127. public function getRedirectIframeUrl($params)
  128. {
  129. return $this->_getUrl('authorizenet/directpost_payment/redirect', $params);
  130. }
  131. /**
  132. * Retrieve place order url
  133. *
  134. * @param array $params
  135. * @return string
  136. */
  137. public function getSuccessOrderUrl($params)
  138. {
  139. return $this->_getUrl('checkout/onepage/success', $params);
  140. }
  141. /**
  142. * Update all child and parent order's edit increment numbers.
  143. *
  144. * Needed for Admin area.
  145. *
  146. * @param \Magento\Sales\Model\Order $order
  147. * @return void
  148. */
  149. public function updateOrderEditIncrements(\Magento\Sales\Model\Order $order)
  150. {
  151. if ($order->getId() && $order->getOriginalIncrementId()) {
  152. $collection = $order->getCollection();
  153. $quotedIncrId = $collection->getConnection()->quote($order->getOriginalIncrementId());
  154. $collection->getSelect()->where(
  155. "original_increment_id = {$quotedIncrId} OR increment_id = {$quotedIncrId}"
  156. );
  157. foreach ($collection as $orderToUpdate) {
  158. $orderToUpdate->setEditIncrement($order->getEditIncrement());
  159. $orderToUpdate->save();
  160. }
  161. }
  162. }
  163. /**
  164. * Converts a lot of messages to message
  165. *
  166. * @param array $messages
  167. * @return string
  168. */
  169. public function convertMessagesToMessage($messages)
  170. {
  171. return implode(' | ', $messages);
  172. }
  173. /**
  174. * Return message for gateway transaction request
  175. *
  176. * @param \Magento\Payment\Model\InfoInterface $payment
  177. * @param string $requestType
  178. * @param string $lastTransactionId
  179. * @param \Magento\Framework\DataObject $card
  180. * @param bool|float $amount
  181. * @param bool|string $exception
  182. * @param bool|string $additionalMessage
  183. * @return bool|string
  184. */
  185. public function getTransactionMessage(
  186. $payment,
  187. $requestType,
  188. $lastTransactionId,
  189. $card,
  190. $amount = false,
  191. $exception = false,
  192. $additionalMessage = false
  193. ) {
  194. $message[] = __('Credit Card: xxxx-%1', $card->getCcLast4());
  195. if ($amount) {
  196. $message[] = __('amount %1', $this->formatPrice($payment, $amount));
  197. }
  198. $operation = $this->getOperation($requestType);
  199. if (!$operation) {
  200. return false;
  201. } else {
  202. $message[] = $operation;
  203. }
  204. $message[] = ($exception) ? '- ' . __('failed.') : '- ' . __('successful.');
  205. if ($lastTransactionId !== null) {
  206. $message[] = __('Authorize.Net Transaction ID %1.', $lastTransactionId);
  207. }
  208. if ($additionalMessage) {
  209. $message[] = $additionalMessage;
  210. }
  211. if ($exception) {
  212. $message[] = $exception;
  213. }
  214. return implode(' ', $message);
  215. }
  216. /**
  217. * Return operation name for request type
  218. *
  219. * @param string $requestType
  220. * @return \Magento\Framework\Phrase|bool
  221. */
  222. protected function getOperation($requestType)
  223. {
  224. switch ($requestType) {
  225. case Authorizenet::REQUEST_TYPE_AUTH_ONLY:
  226. return __('authorize');
  227. case Authorizenet::REQUEST_TYPE_AUTH_CAPTURE:
  228. return __('authorize and capture');
  229. case Authorizenet::REQUEST_TYPE_PRIOR_AUTH_CAPTURE:
  230. return __('capture');
  231. case Authorizenet::REQUEST_TYPE_CREDIT:
  232. return __('refund');
  233. case Authorizenet::REQUEST_TYPE_VOID:
  234. return __('void');
  235. default:
  236. return false;
  237. }
  238. }
  239. /**
  240. * Format price with currency sign
  241. *
  242. * @param \Magento\Payment\Model\InfoInterface $payment
  243. * @param float $amount
  244. * @return string
  245. */
  246. protected function formatPrice($payment, $amount)
  247. {
  248. return $payment->getOrder()->getBaseCurrency()->formatTxt($amount);
  249. }
  250. /**
  251. * Get payment method step html
  252. *
  253. * @param \Magento\Framework\App\ViewInterface $view
  254. * @return string
  255. */
  256. public function getPaymentMethodsHtml(\Magento\Framework\App\ViewInterface $view)
  257. {
  258. $layout = $view->getLayout();
  259. $update = $layout->getUpdate();
  260. $update->load('checkout_onepage_paymentmethod');
  261. $layout->generateXml();
  262. $layout->generateElements();
  263. $output = $layout->getOutput();
  264. return $output;
  265. }
  266. /**
  267. * Get direct post relay url
  268. *
  269. * @param null|int|string $storeId
  270. * @return string
  271. */
  272. public function getRelayUrl($storeId = null)
  273. {
  274. $baseUrl = $this->storeManager->getStore($storeId)
  275. ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
  276. return $baseUrl . 'authorizenet/directpost_payment/response';
  277. }
  278. /**
  279. * Get allowed currencies
  280. *
  281. * @return array
  282. */
  283. public function getAllowedCurrencyCodes()
  284. {
  285. return $this->allowedCurrencyCodes;
  286. }
  287. /**
  288. * Get translated filter action label
  289. *
  290. * @param string $key
  291. * @return \Magento\Framework\Phrase|string
  292. */
  293. public function getFdsFilterActionLabel($key)
  294. {
  295. return isset($this->fdsFilterActions[$key]) ? __($this->fdsFilterActions[$key]) : $key;
  296. }
  297. /**
  298. * Get translated transaction status label
  299. *
  300. * @param string $key
  301. * @return \Magento\Framework\Phrase|string
  302. */
  303. public function getTransactionStatusLabel($key)
  304. {
  305. return isset($this->transactionStatuses[$key]) ? __($this->transactionStatuses[$key]) : $key;
  306. }
  307. /**
  308. * Gateway error response wrapper
  309. *
  310. * @param string $text
  311. * @return \Magento\Framework\Phrase
  312. */
  313. public function wrapGatewayError($text)
  314. {
  315. return __('Gateway error: %1', $text);
  316. }
  317. }