The code was not added to the correct page. You need to add this to your checkout failure page that you are getting the error on.
On your checkout failure page select a portion of the page you would like to see the information, then go into split or code view and add in the php code. When you save the page and get to it after failing the checkout it should give you the full details of the request and response. We can use these details to help understand what the error is and why it is occurring.