Request.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\Model\Directpost;
  8. use Magento\Authorizenet\Model\Request as AuthorizenetRequest;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\Intl\DateTimeFactory;
  11. /**
  12. * Authorize.net request model for DirectPost model
  13. * @deprecated 100.3.1 Authorize.net is removing all support for this payment method
  14. */
  15. class Request extends AuthorizenetRequest
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $_transKey = null;
  21. /**
  22. * Hexadecimal signature key.
  23. *
  24. * @var string
  25. */
  26. private $signatureKey = '';
  27. /**
  28. * @var DateTimeFactory
  29. */
  30. private $dateTimeFactory;
  31. /**
  32. * @param array $data
  33. * @param DateTimeFactory $dateTimeFactory
  34. */
  35. public function __construct(
  36. array $data = [],
  37. DateTimeFactory $dateTimeFactory = null
  38. ) {
  39. $this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()
  40. ->get(DateTimeFactory::class);
  41. parent::__construct($data);
  42. }
  43. /**
  44. * Return merchant transaction key.
  45. *
  46. * Needed to generate MD5 sign.
  47. *
  48. * @return string
  49. */
  50. protected function _getTransactionKey()
  51. {
  52. return $this->_transKey;
  53. }
  54. /**
  55. * Set merchant transaction key.
  56. *
  57. * Needed to generate MD5 sign.
  58. *
  59. * @param string $transKey
  60. * @return $this
  61. */
  62. protected function _setTransactionKey($transKey)
  63. {
  64. $this->_transKey = $transKey;
  65. return $this;
  66. }
  67. /**
  68. * Generates the MD5 fingerprint for request.
  69. *
  70. * @param string $merchantApiLoginId
  71. * @param string $merchantTransactionKey
  72. * @param string $amount
  73. * @param string $currencyCode
  74. * @param string $fpSequence An invoice number or random number.
  75. * @param string $fpTimestamp
  76. * @return string The fingerprint.
  77. */
  78. public function generateRequestSign(
  79. $merchantApiLoginId,
  80. $merchantTransactionKey,
  81. $amount,
  82. $currencyCode,
  83. $fpSequence,
  84. $fpTimestamp
  85. ) {
  86. return hash_hmac(
  87. "md5",
  88. $merchantApiLoginId . '^' . $fpSequence . '^' . $fpTimestamp . '^' . $amount . '^' . $currencyCode,
  89. $merchantTransactionKey
  90. );
  91. }
  92. /**
  93. * Set Authorizenet data to request.
  94. *
  95. * @param \Magento\Authorizenet\Model\Directpost $paymentMethod
  96. * @return $this
  97. */
  98. public function setConstantData(\Magento\Authorizenet\Model\Directpost $paymentMethod)
  99. {
  100. $this->setXVersion('3.1')->setXDelimData('FALSE')->setXRelayResponse('TRUE');
  101. $this->setSignatureKey($paymentMethod->getConfigData('signature_key'));
  102. $this->setXLogin($paymentMethod->getConfigData('login'))
  103. ->setXMethod(\Magento\Authorizenet\Model\Authorizenet::REQUEST_METHOD_CC)
  104. ->setXRelayUrl($paymentMethod->getRelayUrl());
  105. $this->_setTransactionKey($paymentMethod->getConfigData('trans_key'));
  106. return $this;
  107. }
  108. /**
  109. * Set entity data to request
  110. *
  111. * @param \Magento\Sales\Model\Order $order
  112. * @param \Magento\Authorizenet\Model\Directpost $paymentMethod
  113. * @return $this
  114. */
  115. public function setDataFromOrder(
  116. \Magento\Sales\Model\Order $order,
  117. \Magento\Authorizenet\Model\Directpost $paymentMethod
  118. ) {
  119. $payment = $order->getPayment();
  120. $this->setXType($payment->getAnetTransType());
  121. $this->setXFpSequence($order->getQuoteId());
  122. $this->setXInvoiceNum($order->getIncrementId());
  123. $this->setXAmount($payment->getBaseAmountAuthorized());
  124. $this->setXCurrencyCode($order->getBaseCurrencyCode());
  125. $this->setXTax(
  126. sprintf('%.2F', $order->getBaseTaxAmount())
  127. )->setXFreight(
  128. sprintf('%.2F', $order->getBaseShippingAmount())
  129. );
  130. //need to use (string) because NULL values IE6-8 decodes as "null" in JSON in JavaScript,
  131. //but we need "" for null values.
  132. $billing = $order->getBillingAddress();
  133. if (!empty($billing)) {
  134. $this->setXFirstName((string)$billing->getFirstname())
  135. ->setXLastName((string)$billing->getLastname())
  136. ->setXCompany((string)$billing->getCompany())
  137. ->setXAddress((string)$billing->getStreetLine(1))
  138. ->setXCity((string)$billing->getCity())
  139. ->setXState((string)$billing->getRegion())
  140. ->setXZip((string)$billing->getPostcode())
  141. ->setXCountry((string)$billing->getCountryId())
  142. ->setXPhone((string)$billing->getTelephone())
  143. ->setXFax((string)$billing->getFax())
  144. ->setXCustId((string)$billing->getCustomerId())
  145. ->setXCustomerIp((string)$order->getRemoteIp())
  146. ->setXCustomerTaxId((string)$billing->getTaxId())
  147. ->setXEmail((string)$order->getCustomerEmail())
  148. ->setXEmailCustomer((string)$paymentMethod->getConfigData('email_customer'))
  149. ->setXMerchantEmail((string)$paymentMethod->getConfigData('merchant_email'));
  150. }
  151. $shipping = $order->getShippingAddress();
  152. if (!empty($shipping)) {
  153. $this->setXShipToFirstName(
  154. (string)$shipping->getFirstname()
  155. )->setXShipToLastName(
  156. (string)$shipping->getLastname()
  157. )->setXShipToCompany(
  158. (string)$shipping->getCompany()
  159. )->setXShipToAddress(
  160. (string)$shipping->getStreetLine(1)
  161. )->setXShipToCity(
  162. (string)$shipping->getCity()
  163. )->setXShipToState(
  164. (string)$shipping->getRegion()
  165. )->setXShipToZip(
  166. (string)$shipping->getPostcode()
  167. )->setXShipToCountry(
  168. (string)$shipping->getCountryId()
  169. );
  170. }
  171. $this->setXPoNum((string)$payment->getPoNumber());
  172. return $this;
  173. }
  174. /**
  175. * Set sign hash into the request object.
  176. *
  177. * All needed fields should be placed in the object fist.
  178. *
  179. * @return $this
  180. */
  181. public function signRequestData()
  182. {
  183. $fpDate = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
  184. $fpTimestamp = $fpDate->getTimestamp();
  185. if (!empty($this->getSignatureKey())) {
  186. $hash = $this->generateSha2RequestSign(
  187. (string)$this->getXLogin(),
  188. (string)$this->getSignatureKey(),
  189. (string)$this->getXAmount(),
  190. (string)$this->getXCurrencyCode(),
  191. (string)$this->getXFpSequence(),
  192. $fpTimestamp
  193. );
  194. } else {
  195. $hash = $this->generateRequestSign(
  196. $this->getXLogin(),
  197. $this->_getTransactionKey(),
  198. $this->getXAmount(),
  199. $this->getXCurrencyCode(),
  200. $this->getXFpSequence(),
  201. $fpTimestamp
  202. );
  203. }
  204. $this->setXFpTimestamp($fpTimestamp);
  205. $this->setXFpHash($hash);
  206. return $this;
  207. }
  208. /**
  209. * Generates the SHA2 fingerprint for request.
  210. *
  211. * @param string $merchantApiLoginId
  212. * @param string $merchantSignatureKey
  213. * @param string $amount
  214. * @param string $currencyCode
  215. * @param string $fpSequence An invoice number or random number.
  216. * @param int $fpTimestamp
  217. * @return string The fingerprint.
  218. */
  219. private function generateSha2RequestSign(
  220. string $merchantApiLoginId,
  221. string $merchantSignatureKey,
  222. string $amount,
  223. string $currencyCode,
  224. string $fpSequence,
  225. int $fpTimestamp
  226. ): string {
  227. $message = $merchantApiLoginId . '^' . $fpSequence . '^' . $fpTimestamp . '^' . $amount . '^' . $currencyCode;
  228. return strtoupper(hash_hmac('sha512', $message, pack('H*', $merchantSignatureKey)));
  229. }
  230. /**
  231. * Return merchant hexadecimal signature key.
  232. *
  233. * Needed to generate SHA2 sign.
  234. *
  235. * @return string
  236. */
  237. private function getSignatureKey(): string
  238. {
  239. return $this->signatureKey;
  240. }
  241. /**
  242. * Set merchant hexadecimal signature key.
  243. *
  244. * Needed to generate SHA2 sign.
  245. *
  246. * @param string $signatureKey
  247. * @return void
  248. */
  249. private function setSignatureKey(string $signatureKey)
  250. {
  251. $this->signatureKey = $signatureKey;
  252. }
  253. }