@@ -5,7 +5,7 @@ class WePay {
5
5
/**
6
6
* Version number - sent in user agent string
7
7
*/
8
- const VERSION = '0.1.3 ' ;
8
+ const VERSION = '0.1.4 ' ;
9
9
10
10
/**
11
11
* Scope fields
@@ -183,7 +183,7 @@ public function __destruct() {
183
183
self ::$ ch = NULL ;
184
184
}
185
185
}
186
-
186
+
187
187
/**
188
188
* create the cURL request and execute it
189
189
*/
@@ -196,14 +196,14 @@ private static function make_request($endpoint, $values, $headers = array())
196
196
curl_setopt (self ::$ ch , CURLOPT_HTTPHEADER , $ headers );
197
197
curl_setopt (self ::$ ch , CURLOPT_TIMEOUT , 30 ); // 30-second timeout, adjust to taste
198
198
curl_setopt (self ::$ ch , CURLOPT_POST , !empty ($ values )); // WePay's API is not strictly RESTful, so all requests are sent as POST unless there are no request values
199
-
199
+
200
200
$ uri = self ::getDomain () . $ endpoint ;
201
201
curl_setopt (self ::$ ch , CURLOPT_URL , $ uri );
202
-
202
+
203
203
if (!empty ($ values )) {
204
204
curl_setopt (self ::$ ch , CURLOPT_POSTFIELDS , json_encode ($ values ));
205
205
}
206
-
206
+
207
207
$ raw = curl_exec (self ::$ ch );
208
208
if ($ errno = curl_errno (self ::$ ch )) {
209
209
// Set up special handling for request timeouts
@@ -213,26 +213,23 @@ private static function make_request($endpoint, $values, $headers = array())
213
213
throw new Exception ('cURL error while making API call to WePay: ' . curl_error (self ::$ ch ), $ errno );
214
214
}
215
215
$ result = json_decode ($ raw );
216
-
217
- $ error_code = null ;
218
- if (isset ($ result ->error_code )) {
219
- $ error_code = $ result ->error_code ;
220
- }
221
-
222
216
$ httpCode = curl_getinfo (self ::$ ch , CURLINFO_HTTP_CODE );
223
217
if ($ httpCode >= 400 ) {
218
+ if (!isset ($ result ->error_code )) {
219
+ throw new WePayServerException ("WePay returned an error response with no error_code, please alert api@wepay.com. Original message: $ result ->error_description " , $ httpCode , $ result , 0 );
220
+ }
224
221
if ($ httpCode >= 500 ) {
225
- throw new WePayServerException ($ result ->error_description , $ httpCode , $ result , $ error_code );
222
+ throw new WePayServerException ($ result ->error_description , $ httpCode , $ result , $ result -> error_code );
226
223
}
227
224
switch ($ result ->error ) {
228
225
case 'invalid_request ' :
229
- throw new WePayRequestException ($ result ->error_description , $ httpCode , $ result , $ error_code );
226
+ throw new WePayRequestException ($ result ->error_description , $ httpCode , $ result , $ result -> error_code );
230
227
case 'access_denied ' :
231
228
default :
232
- throw new WePayPermissionException ($ result ->error_description , $ httpCode , $ result , $ error_code );
229
+ throw new WePayPermissionException ($ result ->error_description , $ httpCode , $ result , $ result -> error_code );
233
230
}
234
231
}
235
-
232
+
236
233
return $ result ;
237
234
}
238
235
@@ -246,13 +243,13 @@ private static function make_request($endpoint, $values, $headers = array())
246
243
*/
247
244
public function request ($ endpoint , array $ values = array ()) {
248
245
$ headers = array ();
249
-
246
+
250
247
if ($ this ->token ) { // if we have an access_token, add it to the Authorization header
251
248
$ headers [] = "Authorization: Bearer $ this ->token " ;
252
249
}
253
-
250
+
254
251
$ result = self ::make_request ($ endpoint , $ values , $ headers );
255
-
252
+
256
253
return $ result ;
257
254
}
258
255
}
0 commit comments