Advertisement
Guest User

Untitled

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