99 */
1010class SIMCompleteAuthorizeRequest extends AbstractRequest
1111{
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+
1221 public function getData ()
1322 {
14- if (strtolower ($ this ->httpRequest ->request ->get ('x_MD5_Hash ' )) !== $ this ->getHash ()) {
23+ // The hash sent in the callback from the Authorize.Net gateway.
24+ $ hash_posted = strtolower ($ this ->httpRequest ->request ->get ('x_MD5_Hash ' ));
25+
26+ // The transaction reference generated by the Authorize.Net gateway and sent in the callback.
27+ $ posted_transaction_reference = $ this ->httpRequest ->request ->get ('x_trans_id ' );
28+
29+ // The amount that the callback has authorized.
30+ $ posted_amount = $ this ->httpRequest ->request ->get ('x_amount ' );
31+
32+ // Calculate the hash locally, using the shared "hash secret" and login ID.
33+ $ hash_calculated = $ this ->getHash ($ posted_transaction_reference , $ posted_amount );
34+
35+ if ($ hash_posted !== $ hash_calculated ) {
36+ // If the hash is incorrect, then we can't trust the source nor anything sent.
37+ // Throwing exceptions here is probably a bad idea. We are trying to get the data,
38+ // and if it is invalid, then we need to be able to log that data for analysis.
39+ // Except we can't, baceuse the exception means we can't get to the data.
40+ // For now, this is consistent with other OmniPay gateway drivers.
41+
1542 throw new InvalidRequestException ('Incorrect hash ' );
1643 }
1744
45+ // The hashes have passed, but the amount should also be validated against the
46+ // amount in the stored and retrieved transaction. If the application has the
47+ // ability to retrieve the transaction (using the transaction_id sent as a custom
48+ // form field, or perhaps in an otherwise unused field such as x_invoice_id.
49+
50+ $ amount = $ this ->getAmount ();
51+
52+ if (isset ($ amount ) && $ amount != $ posted_amount ) {
53+ // The amounts don't match. Someone may have been playing with the
54+ // transaction references.
55+
56+ throw new InvalidRequestException ('Incorrect amount ' );
57+ }
58+
1859 return $ this ->httpRequest ->request ->all ();
1960 }
2061
@@ -23,9 +64,16 @@ public function getData()
2364 * The transaction reference and the amount are both sent by the remote gateway (x_trans_id
2465 * and x_amount) and it is those that should be checked against.
2566 */
26- public function getHash ()
67+ public function getHash ($ transaction_reference , $ amount )
2768 {
28- return md5 ($ this ->getHashSecret ().$ this ->getApiLoginId ().$ this ->getTransactionId ().$ this ->getAmount ());
69+ $ key = array (
70+ $ this ->getHashSecret (),
71+ $ this ->getApiLoginId (),
72+ $ transaction_reference ,
73+ $ amount ,
74+ );
75+
76+ return md5 (implode ('' , $ key ));
2977 }
3078
3179 public function sendData ($ data )
0 commit comments