|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\AuthorizeNet\Message; |
| 4 | + |
| 5 | +use Omnipay\Common\Exception\InvalidRequestException; |
| 6 | + |
| 7 | +/** |
| 8 | + * Authorize.Net SIM Complete Authorize Request |
| 9 | + */ |
| 10 | +class SIMCompleteRequest extends SIMAbstractRequest |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Get the transaction ID passed in through the custom field. |
| 14 | + * This is used to look up the transaction in storage. |
| 15 | + */ |
| 16 | + public function getTransactionId() |
| 17 | + { |
| 18 | + return $this->httpRequest->request->get(static::TRANSACTION_ID_PARAM); |
| 19 | + } |
| 20 | + |
| 21 | + public function getData() |
| 22 | + { |
| 23 | + // The hash sent in the callback from the Authorize.Net gateway. |
| 24 | + $hashPosted = $this->getPostedHash(); |
| 25 | + |
| 26 | + // Calculate the hash locally, using the shared "hash secret" and login ID. |
| 27 | + $hashCalculated = $this->getHash(); |
| 28 | + |
| 29 | + if ($hashPosted !== $hashCalculated) { |
| 30 | + // If the hash is incorrect, then we can't trust the source nor anything sent. |
| 31 | + // Throwing exceptions here is probably a bad idea. We are trying to get the data, |
| 32 | + // and if it is invalid, then we need to be able to log that data for analysis. |
| 33 | + // Except we can't, baceuse the exception means we can't get to the data. |
| 34 | + // For now, this is consistent with other OmniPay gateway drivers. |
| 35 | + |
| 36 | + throw new InvalidRequestException('Incorrect hash'); |
| 37 | + } |
| 38 | + |
| 39 | + // The hashes have passed, but the amount should also be validated against the |
| 40 | + // amount in the stored and retrieved transaction. If the application has the |
| 41 | + // ability to retrieve the transaction (using the transaction_id sent as a custom |
| 42 | + // form field, or perhaps in an otherwise unused field such as x_invoice_id. |
| 43 | + |
| 44 | + $amount = $this->getAmount(); |
| 45 | + $postedAmount = $this->httpRequest->request->get('x_amount'); |
| 46 | + |
| 47 | + if (isset($amount) && $amount != $postedAmount) { |
| 48 | + // The amounts don't match. Someone may have been playing with the |
| 49 | + // transaction references. |
| 50 | + |
| 51 | + throw new InvalidRequestException('Incorrect amount'); |
| 52 | + } |
| 53 | + |
| 54 | + return $this->httpRequest->request->all(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return string |
| 59 | + */ |
| 60 | + public function getHash() |
| 61 | + { |
| 62 | + if ($this->getSignatureKey()) { |
| 63 | + return $this->getSha512Hash(); |
| 64 | + } else { |
| 65 | + return $this->getMd5Hash(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Generate md5 hash. |
| 71 | + * |
| 72 | + * @param $transaction_reference |
| 73 | + * @param $amount |
| 74 | + * @return string |
| 75 | + */ |
| 76 | + public function getMd5Hash() |
| 77 | + { |
| 78 | + $transactionReference = $this->httpRequest->request->get('x_trans_id'); |
| 79 | + $amount = $this->httpRequest->request->get('x_amount'); |
| 80 | + |
| 81 | + $key = array( |
| 82 | + $this->getHashSecret(), |
| 83 | + $this->getApiLoginId(), |
| 84 | + $transactionReference, |
| 85 | + $amount, |
| 86 | + ); |
| 87 | + |
| 88 | + return md5(implode('', $key)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Generate sha512 hash. |
| 93 | + * Required fields are provided in Table 18 in |
| 94 | + * https://www.authorize.net/content/dam/authorize/documents/SIM_guide.pdf#page=73 |
| 95 | + * |
| 96 | + * @return string hash generated from server request transformed to upper case |
| 97 | + */ |
| 98 | + public function getSha512Hash() |
| 99 | + { |
| 100 | + $signatureKey = $this->getSignatureKey(); |
| 101 | + $request = $this->httpRequest->request; |
| 102 | + |
| 103 | + $hashData = '^' . implode('^', [ |
| 104 | + $request->get('x_trans_id'), |
| 105 | + $request->get('x_test_request'), |
| 106 | + $request->get('x_response_code'), |
| 107 | + $request->get('x_auth_code'), |
| 108 | + $request->get('x_cvv2_resp_code'), |
| 109 | + $request->get('x_cavv_response'), |
| 110 | + $request->get('x_avs_code'), |
| 111 | + $request->get('x_method'), |
| 112 | + $request->get('x_account_number'), |
| 113 | + $request->get('x_amount'), |
| 114 | + $request->get('x_company'), |
| 115 | + $request->get('x_first_name'), |
| 116 | + $request->get('x_last_name'), |
| 117 | + $request->get('x_address'), |
| 118 | + $request->get('x_city'), |
| 119 | + $request->get('x_state'), |
| 120 | + $request->get('x_zip'), |
| 121 | + $request->get('x_country'), |
| 122 | + $request->get('x_phone'), |
| 123 | + $request->get('x_fax'), |
| 124 | + $request->get('x_email'), |
| 125 | + $request->get('x_ship_to_company'), |
| 126 | + $request->get('x_ship_to_first_name'), |
| 127 | + $request->get('x_ship_to_last_name'), |
| 128 | + $request->get('x_ship_to_address'), |
| 129 | + $request->get('x_ship_to_city'), |
| 130 | + $request->get('x_ship_to_state'), |
| 131 | + $request->get('x_ship_to_zip'), |
| 132 | + $request->get('x_ship_to_country'), |
| 133 | + $request->get('x_invoice_num'), |
| 134 | + ]) . '^'; |
| 135 | + $hash = hash_hmac('sha512', $hashData, hex2bin($signatureKey)); |
| 136 | + |
| 137 | + return strtoupper($hash); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get posted hash from the callback from the Authorize.Net gateway. |
| 142 | + * |
| 143 | + * @return string|null |
| 144 | + */ |
| 145 | + public function getPostedHash() |
| 146 | + { |
| 147 | + if ($signatureKey = $this->getSignatureKey()) { |
| 148 | + return strtoupper($this->httpRequest->request->get('x_SHA2_Hash')); |
| 149 | + } |
| 150 | + |
| 151 | + return strtolower($this->httpRequest->request->get('x_MD5_Hash')); |
| 152 | + } |
| 153 | + |
| 154 | + public function sendData($data) |
| 155 | + { |
| 156 | + return $this->response = new SIMCompleteResponse($this, $data); |
| 157 | + } |
| 158 | +} |
0 commit comments