Skip to content
Closed
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
110 changes: 61 additions & 49 deletions QuickPay/API/Client.php
Original file line number Diff line number Diff line change
@@ -1,94 +1,106 @@
<?php
namespace QuickPay\API;
/**
* @class QuickPay_Client
* @since 1.0.0
* @package QuickPay
* @category Class
* @author Patrick Tolvstein, Perfect Solution ApS
* @class QuickPay_Client
* @since 1.0.0
* @package QuickPay
* @category Class
* @author Patrick Tolvstein, Perfect Solution ApS
* @docs http://tech.quickpay.net/api/
*/
class Client
class Client
{
/**
* Contains cURL instance
* @access public
*/
*/
public $ch;

/**
* Contains the authentication string
* @access protected
*/
*/
protected $auth_string;


/**
* __construct function.
*
* Instantiate object
*
* @access public
*/

/**
* __construct function.
*
* Instantiate object
*
* @access public
*/
public function __construct( $auth_string = '' )
{
// Check if lib cURL is enabled
if( ! function_exists('curl_init') )
if( ! function_exists('curl_init') )
{
throw new Exception( 'Lib cURL must be enabled on the server' );
}

// Set auth string property
$this->auth_string = $auth_string;

// Instantiate cURL object
$this->authenticate();
}
/**
* shutdown function.
*
* Closes the current cURL connection
*
* @access public
*/

/**
* shutdown function.
*
* Closes the current cURL connection
*
* @access public
*/
public function shutdown()
{
if( ! empty( $this->ch ) )
{
curl_close( $this->ch );
}
}
/**
* authenticate function.
*
* Create a cURL instance with authentication headers
*
* @access public
*/


/**
* authenticate function.
*
* Create a cURL instance with authentication headers
*
* @access public
*/
protected function authenticate()
{
$this->ch = curl_init();


$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
);

curl_setopt_array( $this->ch, $options );
}

public function set_headers( $additional_headers = array() )
{
$headers = array(
'Accept-Version: v10',
'Accept: application/json',
'Accept: application/json',
'Content-Type: application/json',
);

if( ! empty($this->auth_string))
if( ! empty($this->auth_string))
{
$headers[] = 'Authorization: Basic ' . base64_encode( $this->auth_string );
}

$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HTTPHEADER => $headers
);

curl_setopt_array( $this->ch, $options );
if( ! empty( $additional_headers ) )
{
foreach( $additional_headers as $additional_header )
{
$headers[] = $additional_header;
}
}

curl_setopt( $this->ch, CURLOPT_HTTPHEADER, $headers);
}
}
12 changes: 11 additions & 1 deletion QuickPay/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,17 @@ protected function execute( $request_type, $form = array() )
// If additional data is delivered, we will send it along with the API request
if( is_array( $form ) && ! empty( $form ) )
{
curl_setopt( $this->client->ch, CURLOPT_POSTFIELDS, http_build_query($form) );
$json_data = json_encode( $form );
curl_setopt( $this->client->ch, CURLOPT_POSTFIELDS, $json_data );
$this->client->set_headers(
array(
'Content-Length: ' . strlen($json_data)
)
);
}
else
{
$this->client->set_headers();
}

// Store received headers in temporary memory file, remember sent headers
Expand Down