|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\AuthorizeNet\Message; |
| 4 | + |
| 5 | +use Omnipay\Common\Exception\InvalidRequestException; |
| 6 | + |
| 7 | +/** |
| 8 | + * Authorize.Net DPM Complete Authorize Request |
| 9 | + */ |
| 10 | +class DPMCompleteRequest extends SIMCompleteAuthorizeRequest |
| 11 | +{ |
| 12 | + public function getData() |
| 13 | + { |
| 14 | + // The hash sent in the callback from the Authorize.Net gateway. |
| 15 | + $hash_posted = strtolower($this->httpRequest->request->get('x_MD5_Hash')); |
| 16 | + |
| 17 | + // The transaction reference generated by the Authorize.Net gateway and sent in the callback. |
| 18 | + $posted_transaction_reference = $this->httpRequest->request->get('x_trans_id'); |
| 19 | + |
| 20 | + // The amount that the callback has authorized. |
| 21 | + $posted_amount = $this->httpRequest->request->get('x_amount'); |
| 22 | + |
| 23 | + // Calculate the hash locally, using the shared "hash secret" and login ID. |
| 24 | + $hash_calculated = $this->getDpmHash($posted_transaction_reference, $posted_amount); |
| 25 | + |
| 26 | + if ($hash_posted !== $hash_calculated) { |
| 27 | + // If the hash is incorrect, then we can't trust the source nor anything sent. |
| 28 | + // Throwing exceptions here is probably a bad idea. We are trying to get the data, |
| 29 | + // and if it is invalid, then we need to be able to log that data for analysis. |
| 30 | + // Except we can't, baceuse the exception means we can't get to the data. |
| 31 | + // For now, this is consistent with other OmniPay gateway drivers. |
| 32 | + |
| 33 | + throw new InvalidRequestException('Incorrect hash'); |
| 34 | + } |
| 35 | + |
| 36 | + // The hashes have passed, but the amount should also be validated against the |
| 37 | + // amount in the stored and retrieved transaction. If the application has the |
| 38 | + // ability to retrieve the transaction (using the transaction_id sent as a custom |
| 39 | + // form field, or perhaps in an otherwise unused field such as x_invoice_id. |
| 40 | + |
| 41 | + $amount = $this->getAmount(); |
| 42 | + |
| 43 | + if (isset($amount) && $amount != $posted_amount) { |
| 44 | + // The amounts don't match. Someone may have been playing with the |
| 45 | + // transaction references. |
| 46 | + |
| 47 | + throw new InvalidRequestException('Incorrect amount'); |
| 48 | + } |
| 49 | + |
| 50 | + return $this->httpRequest->request->all(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * This hash confirms the ransaction has come from the Authorize.Net gateway. |
| 55 | + * It confirms the sender knows ther shared hash secret and that the amount and |
| 56 | + * transaction reference has not been changed in transit. |
| 57 | + */ |
| 58 | + public function getDpmHash($transaction_reference, $amount) |
| 59 | + { |
| 60 | + $key = $this->getHashSecret() |
| 61 | + . $this->getApiLoginId() |
| 62 | + . $transaction_reference |
| 63 | + . $amount; |
| 64 | + |
| 65 | + return md5($key); |
| 66 | + } |
| 67 | + |
| 68 | + public function sendData($data) |
| 69 | + { |
| 70 | + return $this->response = new DPMCompleteResponse($this, $data); |
| 71 | + } |
| 72 | +} |
0 commit comments