88use Omnipay \FirstAtlanticCommerce \Constants ;
99use Omnipay \FirstAtlanticCommerce \Support \TransactionCode ;
1010use Omnipay \FirstAtlanticCommerce \Exception \GatewayHTTPException ;
11+ use Psr \Http \Message \ResponseInterface ;
1112
1213abstract class AbstractRequest extends \Omnipay \Common \Message \AbstractRequest
1314implements \Omnipay \FirstAtlanticCommerce \Support \FACParametersInterface
@@ -16,6 +17,7 @@ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
1617
1718 const PARAM_CACHE_TRANSACTION = 'cacheTransaction ' ;
1819 const PARAM_CACHE_REQUEST = 'cacheRequest ' ;
20+ const EXCEPTION_LOG = 'exception.log ' ;
1921
2022 protected $ data = [];
2123 protected $ XMLDoc ;
@@ -95,34 +97,64 @@ public function sendData($data)
9597 $ this ->XMLDoc ->asXML ($ this ->TransactionCacheDir .$ this ->getMessageClassName ().'Request_ ' .$ this ->getTransactionId ().'.xml ' );
9698 }
9799
98- switch ($ httpResponse ->getStatusCode ())
99- {
100- case "200 " :
101- $ responseContent = $ httpResponse ->getBody ()->getContents ();
102- $ responseClassName = __NAMESPACE__ ."\\" .$ this ->FACServices [$ this ->getMessageClassName ()]["response " ];
100+ if ($ httpResponse instanceof ResponseInterface) {
103101
104- $ responseXML = new \SimpleXMLElement ($ responseContent );
105- $ responseXML ->registerXPathNamespace ("fac " , Constants::PLATFORM_XML_NS );
102+ switch ($ httpResponse ->getStatusCode ()) {
103+ case "200 " :
104+ $ responseContent = $ httpResponse ->getBody ()->getContents ();
105+ $ responseClassName = __NAMESPACE__ . "\\" . $ this ->FACServices [$ this ->getMessageClassName ()]["response " ];
106106
107- if ($ this ->getCacheTransaction ())
108- {
109- if (!is_dir ($ this ->TransactionCacheDir ))
110- {
111- $ cacheDirExists = mkdir ($ this ->TransactionCacheDir );
107+ if ($ this ->getCacheTransaction ()) {
108+ if (!is_dir ($ this ->TransactionCacheDir )) {
109+ $ cacheDirExists = mkdir ($ this ->TransactionCacheDir );
110+ } else {
111+ $ cacheDirExists = true ;
112+ }
113+
114+ if ($ cacheDirExists ) {
115+ file_put_contents ($ this ->TransactionCacheDir . self ::EXCEPTION_LOG , date ("Y-m-d H:i:s " ) . " FAC Response String \n" , FILE_APPEND );
116+ file_put_contents ($ this ->TransactionCacheDir . self ::EXCEPTION_LOG , "START \n" . $ responseContent . "\nEND \n" , FILE_APPEND );
117+ file_put_contents ($ this ->TransactionCacheDir . self ::EXCEPTION_LOG , "HEADERS START \n" , FILE_APPEND );
118+ }
119+
120+ $ headers = $ httpResponse ->getHeaders ();
121+ foreach ($ headers as $ h => $ header ) {
122+ file_put_contents ($ this ->TransactionCacheDir . self ::EXCEPTION_LOG , strtoupper ($ h ) . ": " . implode ("\n" , $ header ) . "\n\n" , FILE_APPEND );
123+ }
124+
125+ file_put_contents ($ this ->TransactionCacheDir . self ::EXCEPTION_LOG , "HEADERS END \n" , FILE_APPEND );
112126 }
113- else
127+
128+ try {
129+ $ responseXML = new \SimpleXMLElement ($ responseContent );
130+ } catch (\Exception $ e )
114131 {
115- $ cacheDirExists = true ;
132+ if ($ this ->getCacheTransaction () && $ cacheDirExists )
133+ file_put_contents ($ this ->TransactionCacheDir .self ::EXCEPTION_LOG , "Exception: " .$ e ->getMessage ()."\n" , FILE_APPEND );
134+
135+ throw $ e ;
116136 }
117137
118- if ($ cacheDirExists )
119- $ responseXML ->asXML ($ this ->TransactionCacheDir .$ this ->getMessageClassName ().'Response_ ' .$ this ->getTransactionId ().'.xml ' );
120- }
138+ $ responseXML ->registerXPathNamespace ("fac " , Constants::PLATFORM_XML_NS );
139+
140+ if ($ this ->getCacheTransaction ()) {
141+ if (!is_dir ($ this ->TransactionCacheDir )) {
142+ $ cacheDirExists = mkdir ($ this ->TransactionCacheDir );
143+ } else {
144+ $ cacheDirExists = true ;
145+ }
121146
122- return $ this ->response = new $ responseClassName ($ this , $ responseXML );
147+ if ($ cacheDirExists )
148+ $ responseXML ->asXML ($ this ->TransactionCacheDir . $ this ->getMessageClassName () . 'Response_ ' . $ this ->getTransactionId () . '.xml ' );
149+ }
150+
151+ return $ this ->response = new $ responseClassName ($ this , $ responseXML );
123152
124- default :
125- throw new GatewayHTTPException ($ httpResponse ->getReasonPhrase (), $ httpResponse ->getStatusCode ());
153+ default :
154+ throw new GatewayHTTPException ($ httpResponse ->getReasonPhrase (), $ httpResponse ->getStatusCode ());
155+ }
156+ } else {
157+ throw new \Exception ("Invalid HTTP Response. " );
126158 }
127159 }
128160
0 commit comments