Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 23 additions & 11 deletions QuickPay/api/Request.php → QuickPay/API/Request.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace QuickPay\API;

use Quickpay\API\Constants;
use Quickpay\API\Response;
use QuickPay\API\Constants;
use QuickPay\API\Response;

/**
* @class QuickPay_Request
Expand All @@ -20,7 +20,6 @@ class Request
*/
protected $client;


/**
* __construct function.
*
Expand All @@ -43,7 +42,7 @@ public function __construct( $client )
* @access public
* @param string $path
* @param array $query
* @return object
* @return Response
*/
public function get( $path, $query = array() )
{
Expand Down Expand Up @@ -74,7 +73,7 @@ public function get( $path, $query = array() )
* Performs an API POST request
*
* @access public
* @return object
* @return Response
*/
public function post( $path, $form = array() )
{
Expand All @@ -92,7 +91,7 @@ public function post( $path, $form = array() )
* Performs an API PUT request
*
* @access public
* @return object
* @return Response
*/
public function put( $path, $form = array() )
{
Expand All @@ -110,7 +109,7 @@ public function put( $path, $form = array() )
* Performs an API PATCH request
*
* @access public
* @return object
* @return Response
*/
public function patch( $path, $form = array() )
{
Expand All @@ -128,7 +127,7 @@ public function patch( $path, $form = array() )
* Performs an API DELETE request
*
* @access public
* @return object
* @return Response
*/
public function delete( $path, $form = array() )
{
Expand Down Expand Up @@ -162,7 +161,7 @@ protected function set_url( $params )
* @access protected
* @param string $request_type
* @param array $form
* @return object
* @return Response
*/
protected function execute( $request_type, $form = array() )
{
Expand All @@ -175,18 +174,31 @@ protected function execute( $request_type, $form = array() )
curl_setopt( $this->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 );
}

}
163 changes: 163 additions & 0 deletions QuickPay/API/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php
namespace QuickPay\API;
/**
* @class QuickPay_Response
* @since 1.0.0
* @package QuickPay
* @category Class
* @author Patrick Tolvstein, Perfect Solution ApS
* @docs http://tech.quickpay.net/api/
*/
class Response
{
/**
* HTTP status code of request.
*
* @var integer
*/
protected $status_code;

/**
* The headers sent during the request.
*
* @var string
*/
protected $sent_headers;

/**
* The headers received during the request.
*
* @var string
*/
protected $received_headers;

/**
* Response body of last request.
*
* @var string
*/
protected $response_data;

/**
* __construct
*
* Instantiates a new response object
*
* @param int $status_code the HTTP status code
* @param string $sent_headers the headers sent
* @param string $received_headers the headers received
* @param string $response_data the http response body
*/
public function __construct( $status_code, $sent_headers, $received_headers, $response_data )
{
$this->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: <hidden by default>';
}
}
$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;
}
}
10 changes: 5 additions & 5 deletions QuickPay/QuickPay.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace QuickPay;

require_once( 'api/Constants.php' );
require_once( 'api/Exception.php' );
require_once( 'api/Client.php' );
require_once( 'api/Request.php' );
require_once( 'api/Response.php' );
require_once( 'API/Constants.php' );
require_once( 'API/Exception.php' );
require_once( 'API/Client.php' );
require_once( 'API/Request.php' );
require_once( 'API/Response.php' );

use QuickPay\API\Client;
use QuickPay\API\Request;
Expand Down
2 changes: 1 addition & 1 deletion QuickPay/Tests/api/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace QuickPay\Tests;

use Quickpay\API\Exception;
use QuickPay\API\Exception;

class ExceptionTest extends \PHPUnit_Framework_TestCase
{
Expand Down
6 changes: 3 additions & 3 deletions QuickPay/Tests/api/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace QuickPay\Tests;

use Quickpay\API\Client;
use Quickpay\API\Request;
use Quickpay\API\Response;
use QuickPay\API\Client;
use QuickPay\API\Request;
use QuickPay\API\Response;

class RequestTest extends \PHPUnit_Framework_TestCase
{
Expand Down
20 changes: 11 additions & 9 deletions QuickPay/Tests/api/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace QuickPay\Tests;

use Quickpay\API\Response;
use QuickPay\API\Response;

class ResponseTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -17,7 +17,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
*/
public function testSuccessResponseHTTPCodes($httpCode, $expectedResult)
{
$response = new Response($httpCode, '');
$response = new Response($httpCode, '', '', '');

$result = $response->is_success();

Expand All @@ -43,7 +43,7 @@ public function providerTestSuccessResponseHTTPCodes()
*/
public function testReturnOfHTTPStatusCodes($httpCode, $expectedCode)
{
$response = new Response($httpCode, '');
$response = new Response($httpCode, '', '', '');

$statusCode = $response->http_status();

Expand All @@ -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();

Expand All @@ -70,7 +70,7 @@ public function testReturnOfResponseDataAsArray()

public function testReturnOfEmptyResponseDataAsArray()
{
$response = new Response(200, '');
$response = new Response(200, '', '', '');

$responseArray = $response->as_array();

Expand All @@ -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();

Expand All @@ -88,7 +88,7 @@ public function testReturnOfResponseDataAsObject()

public function testReturnOfEmptyResponseDataAsObject()
{
$response = new Response(200, '');
$response = new Response(200, '', '', '');

$responseObject = $response->as_object();

Expand All @@ -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) );
}

Expand Down
Loading