diff --git a/QuickPay/api/Client.php b/QuickPay/API/Client.php similarity index 100% rename from QuickPay/api/Client.php rename to QuickPay/API/Client.php diff --git a/QuickPay/api/Constants.php b/QuickPay/API/Constants.php similarity index 100% rename from QuickPay/api/Constants.php rename to QuickPay/API/Constants.php diff --git a/QuickPay/api/Exception.php b/QuickPay/API/Exception.php similarity index 100% rename from QuickPay/api/Exception.php rename to QuickPay/API/Exception.php diff --git a/QuickPay/api/Request.php b/QuickPay/API/Request.php similarity index 82% rename from QuickPay/api/Request.php rename to QuickPay/API/Request.php index c58c441..faf7ce7 100644 --- a/QuickPay/api/Request.php +++ b/QuickPay/API/Request.php @@ -1,8 +1,8 @@ client->ch, CURLOPT_POSTFIELDS, http_build_query($form) ); } + // Store received headers in temporary memory file, remember sent headers + $fh_header = fopen('php://memory', 'w+'); + curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header); + curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true); + // Execute the request $response_data = curl_exec( $this->client->ch ); - if (curl_errno($this->client->ch) !== 0) { + if (curl_errno($this->client->ch) !== 0) { //An error occurred + fclose($fh_header); throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch)); } + // Grab the headers + $sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT); + rewind($fh_header); + $received_headers = stream_get_contents($fh_header); + fclose($fh_header); + // Retrieve the HTTP response code $response_code = (int) curl_getinfo( $this->client->ch, CURLINFO_HTTP_CODE ); // Return the response object. - return new Response( $response_code, $response_data ); + return new Response( $response_code, $sent_headers, $received_headers, $response_data ); } + } diff --git a/QuickPay/API/Response.php b/QuickPay/API/Response.php new file mode 100644 index 0000000..56df190 --- /dev/null +++ b/QuickPay/API/Response.php @@ -0,0 +1,163 @@ +status_code = $status_code; + $this->sent_headers = $sent_headers; + $this->received_headers = $received_headers; + $this->response_data = $response_data; + } + + + /** + * as_raw + * + * Returns the HTTP status code, headers and response body. + * Usage: list($status_code, $headers, $response_body) = $response->as_raw(). + * + * @param boolan $keep_authorization_value Normally the value of the Authorization: header is masked. True keeps the sent value. + * @return array [integer, string[], string] + */ + public function as_raw( $keep_authorization_value = false ) + { + // To avoid unintentional logging of credentials the default is to mask the value of the Authorization: header + if ($keep_authorization_value ) + { + $sent_headers = $this->sent_headers; + } + else + { + // Avoid dependency on mbstring + $lines = explode( "\n", $this->sent_headers ); + foreach ( $lines as &$line ) + { + if (strpos($line, 'Authorization: ') === 0) { + $line = 'Authorization: '; + } + } + $sent_headers = implode("\n", $lines); + } + + return [ + $this->status_code, + [ 'sent' => $sent_headers, + 'received' => $this->received_headers, + ], + $this->response_data, + ]; + } + + /** + * as_array + * + * Returns the response body as an array + * + * @return array + */ + public function as_array() + { + if( $response = json_decode( $this->response_data, TRUE ) ) + { + return $response; + } + + return array(); + } + + + /** + * as_object + * + * Returns the response body as an array + * + * @return array + */ + public function as_object() + { + if( $response = json_decode( $this->response_data ) ) + { + return $response; + } + + return new \stdClass; + } + + + /** + * http_status + * + * Returns the http_status code + * + * @return int + */ + public function http_status() + { + return $this->status_code; + } + + + /** + * is_success + * + * Checks if the http status code indicates a succesful or an error response. + * + * @return boolean + */ + public function is_success() + { + if( $this->status_code > 299 ) + { + return false; + } + + return true; + } +} diff --git a/QuickPay/QuickPay.php b/QuickPay/QuickPay.php index ea16fd1..9decef0 100644 --- a/QuickPay/QuickPay.php +++ b/QuickPay/QuickPay.php @@ -1,11 +1,11 @@ is_success(); @@ -43,7 +43,7 @@ public function providerTestSuccessResponseHTTPCodes() */ public function testReturnOfHTTPStatusCodes($httpCode, $expectedCode) { - $response = new Response($httpCode, ''); + $response = new Response($httpCode, '', '', ''); $statusCode = $response->http_status(); @@ -61,7 +61,7 @@ public function providerTestReturnOfHTTPStatusCodes() public function testReturnOfResponseDataAsArray() { - $response = new Response(200, $this->responseTestData); + $response = new Response(200, '', '', $this->responseTestData); $responseArray = $response->as_array(); @@ -70,7 +70,7 @@ public function testReturnOfResponseDataAsArray() public function testReturnOfEmptyResponseDataAsArray() { - $response = new Response(200, ''); + $response = new Response(200, '', '', ''); $responseArray = $response->as_array(); @@ -79,7 +79,7 @@ public function testReturnOfEmptyResponseDataAsArray() public function testReturnOfResponseDataAsObject() { - $response = new Response(200, $this->responseTestData); + $response = new Response(200, '', '', $this->responseTestData); $responseObject = $response->as_object(); @@ -88,7 +88,7 @@ public function testReturnOfResponseDataAsObject() public function testReturnOfEmptyResponseDataAsObject() { - $response = new Response(200, ''); + $response = new Response(200, '', '', ''); $responseObject = $response->as_object(); @@ -97,10 +97,12 @@ public function testReturnOfEmptyResponseDataAsObject() public function testReturnOfResponseDataAsRaw() { - $response = new Response(200, $this->responseTestData); + $response = new Response(200, '', '', $this->responseTestData); - $responseRaw = $response->as_raw(); + list($statusCode, $headers, $responseRaw) = $response->as_raw(); + $this->assertTrue( is_int($statusCode) ); + $this->assertTrue( is_array($headers) ); $this->assertTrue( is_string($responseRaw) ); } diff --git a/QuickPay/api/Response.php b/QuickPay/api/Response.php deleted file mode 100644 index 9be4f9f..0000000 --- a/QuickPay/api/Response.php +++ /dev/null @@ -1,109 +0,0 @@ -response_code = $response_code; - $this->response_data = $response_data; - } - - - /** - * as_raw - * - * Returns the raw response body - * - * @return string - */ - public function as_raw() - { - return $this->response_data; - } - - - /** - * as_array - * - * Returns the response body as an array - * - * @return array - */ - public function as_array() - { - if( $response = json_decode( $this->response_data, TRUE ) ) - { - return $response; - } - - return array(); - } - - - /** - * as_object - * - * Returns the response body as an array - * - * @return array - */ - public function as_object() - { - if( $response = json_decode( $this->response_data ) ) - { - return $response; - } - - return new \stdClass; - } - - - /** - * http_status - * - * Returns the http_status code - * - * @return int - */ - public function http_status() - { - return $this->response_code; - } - - - /** - * is_success - * - * Checks if the http status code indicates a succesful or an error response. - * - * @return boolean - */ - public function is_success() - { - if( $this->response_code > 299 ) - { - return false; - } - - return true; - } -} diff --git a/README.md b/README.md index 74a5b76..797fcbe 100644 --- a/README.md +++ b/README.md @@ -99,8 +99,8 @@ if( $status == 200 ) { The returned response object supports 3 different ways of returning the response body, `as_raw()`, `as_object`, `as_array()`. ```php5 -// Get the raw response body -$response_body = $client->request->get('/payments')->as_raw(); +// Get the HTTP status code, headers and raw response body. +list($status_code, $headers, $response_body) = $client->request->get('/payments')->as_raw(); // Get the response body as an object $response_body = $client->request->get('/payments')->as_object(); @@ -118,4 +118,12 @@ foreach( $payments as $payment ) { ``` -You can read more about api responses at [http://tech.quickpay.net/api/](http://tech.quickpay.net/api). \ No newline at end of file +You can read more about api responses at [http://tech.quickpay.net/api/](http://tech.quickpay.net/api). + +## Tests + +Use composer to create an autoloader: +```command +$ composer update +$ phpunit --bootstrap vendor/autoload.php +``` diff --git a/composer.json b/composer.json index 7100635..3d1d90a 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "phpunit/phpunit": "4.8.*@dev" }, "autoload": { - "psr-0": { - "QuickPay": "" + "psr-4": { + "QuickPay\\": "QuickPay/" } } } \ No newline at end of file