8
8
use Omnipay \FirstAtlanticCommerce \Constants ;
9
9
use Omnipay \FirstAtlanticCommerce \Support \TransactionCode ;
10
10
use Omnipay \FirstAtlanticCommerce \Exception \GatewayHTTPException ;
11
+ use Psr \Http \Message \ResponseInterface ;
11
12
12
13
abstract class AbstractRequest extends \Omnipay \Common \Message \AbstractRequest
13
14
implements \Omnipay \FirstAtlanticCommerce \Support \FACParametersInterface
@@ -16,6 +17,7 @@ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
16
17
17
18
const PARAM_CACHE_TRANSACTION = 'cacheTransaction ' ;
18
19
const PARAM_CACHE_REQUEST = 'cacheRequest ' ;
20
+ const EXCEPTION_LOG = 'exception.log ' ;
19
21
20
22
protected $ data = [];
21
23
protected $ XMLDoc ;
@@ -95,34 +97,64 @@ public function sendData($data)
95
97
$ this ->XMLDoc ->asXML ($ this ->TransactionCacheDir .$ this ->getMessageClassName ().'Request_ ' .$ this ->getTransactionId ().'.xml ' );
96
98
}
97
99
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) {
103
101
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 " ];
106
106
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 );
112
126
}
113
- else
127
+
128
+ try {
129
+ $ responseXML = new \SimpleXMLElement ($ responseContent );
130
+ } catch (\Exception $ e )
114
131
{
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 ;
116
136
}
117
137
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
+ }
121
146
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 );
123
152
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. " );
126
158
}
127
159
}
128
160
0 commit comments