Response.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Response as AuthorizenetResponse;
  9. use Magento\Framework\Encryption\Helper\Security;
  10. /**
  11. * Authorize.net response model for DirectPost model
  12. * @deprecated 100.3.1 Authorize.net is removing all support for this payment method
  13. */
  14. class Response extends AuthorizenetResponse
  15. {
  16. /**
  17. * Generates an Md5 hash to compare against AuthNet's.
  18. *
  19. * @param string $merchantMd5
  20. * @param string $merchantApiLogin
  21. * @param string $amount
  22. * @param string $transactionId
  23. * @return string
  24. */
  25. public function generateHash($merchantMd5, $merchantApiLogin, $amount, $transactionId)
  26. {
  27. return strtoupper(md5($merchantMd5 . $merchantApiLogin . $transactionId . $amount));
  28. }
  29. /**
  30. * Return if is valid order id.
  31. *
  32. * @param string $storedHash
  33. * @param string $merchantApiLogin
  34. * @return bool
  35. */
  36. public function isValidHash($storedHash, $merchantApiLogin)
  37. {
  38. if (empty($this->getData('x_amount'))) {
  39. $this->setData('x_amount', '0.00');
  40. }
  41. if (!empty($this->getData('x_SHA2_Hash'))) {
  42. $hash = $this->generateSha2Hash($storedHash);
  43. return Security::compareStrings($hash, $this->getData('x_SHA2_Hash'));
  44. } elseif (!empty($this->getData('x_MD5_Hash'))) {
  45. $hash = $this->generateHash($storedHash, $merchantApiLogin, $this->getXAmount(), $this->getXTransId());
  46. return Security::compareStrings($hash, $this->getData('x_MD5_Hash'));
  47. }
  48. return false;
  49. }
  50. /**
  51. * Return if this is approved response from Authorize.net auth request.
  52. *
  53. * @return bool
  54. */
  55. public function isApproved()
  56. {
  57. return $this->getXResponseCode() == \Magento\Authorizenet\Model\Directpost::RESPONSE_CODE_APPROVED;
  58. }
  59. /**
  60. * Generates an SHA2 hash to compare against AuthNet's.
  61. *
  62. * @param string $signatureKey
  63. * @return string
  64. * @see https://support.authorize.net/s/article/MD5-Hash-End-of-Life-Signature-Key-Replacement
  65. */
  66. private function generateSha2Hash(string $signatureKey): string
  67. {
  68. $hashFields = [
  69. 'x_trans_id',
  70. 'x_test_request',
  71. 'x_response_code',
  72. 'x_auth_code',
  73. 'x_cvv2_resp_code',
  74. 'x_cavv_response',
  75. 'x_avs_code',
  76. 'x_method',
  77. 'x_account_number',
  78. 'x_amount',
  79. 'x_company',
  80. 'x_first_name',
  81. 'x_last_name',
  82. 'x_address',
  83. 'x_city',
  84. 'x_state',
  85. 'x_zip',
  86. 'x_country',
  87. 'x_phone',
  88. 'x_fax',
  89. 'x_email',
  90. 'x_ship_to_company',
  91. 'x_ship_to_first_name',
  92. 'x_ship_to_last_name',
  93. 'x_ship_to_address',
  94. 'x_ship_to_city',
  95. 'x_ship_to_state',
  96. 'x_ship_to_zip',
  97. 'x_ship_to_country',
  98. 'x_invoice_num',
  99. ];
  100. $message = '^';
  101. foreach ($hashFields as $field) {
  102. $message .= ($this->getData($field) ?? '') . '^';
  103. }
  104. return strtoupper(hash_hmac('sha512', $message, pack('H*', $signatureKey)));
  105. }
  106. }