@@ -11,10 +11,42 @@ class SIMCompleteAuthorizeRequest extends AbstractRequest
1111{
1212 public function getData ()
1313 {
14- if (strtolower ($ this ->httpRequest ->request ->get ('x_MD5_Hash ' )) !== $ this ->getHash ()) {
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 ->getHash ($ 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+
1533 throw new InvalidRequestException ('Incorrect hash ' );
1634 }
1735
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+
1850 return $ this ->httpRequest ->request ->all ();
1951 }
2052
@@ -23,9 +55,16 @@ public function getData()
2355 * The transaction reference and the amount are both sent by the remote gateway (x_trans_id
2456 * and x_amount) and it is those that should be checked against.
2557 */
26- public function getHash ()
58+ public function getHash ($ transaction_reference , $ amount )
2759 {
28- return md5 ($ this ->getHashSecret ().$ this ->getApiLoginId ().$ this ->getTransactionId ().$ this ->getAmount ());
60+ $ key = array (
61+ $ this ->getHashSecret (),
62+ $ this ->getApiLoginId (),
63+ $ transaction_reference ,
64+ $ amount ,
65+ );
66+
67+ return md5 (implode ('' , $ key ));
2968 }
3069
3170 public function sendData ($ data )
0 commit comments