diff --git a/QuickPay/API/Client.php b/QuickPay/API/Client.php index 3cad955..8a17980 100644 --- a/QuickPay/API/Client.php +++ b/QuickPay/API/Client.php @@ -18,6 +18,13 @@ class Client */ public $ch; + /** + * Base url for the selected API. + * + * @var string + */ + public $base_url; + /** * Contains the authentication string * @@ -31,8 +38,11 @@ class Client * Instantiate object * * @access public + * @param string $auth_string Format 'username:password' or ':apiKey' + * @param string $base_url The API to call. Use on of the constants. + * @throws Exception */ - public function __construct($auth_string = '') + public function __construct($auth_string = '', $base_url = Constants::API_URL) { // Check if lib cURL is enabled if (!function_exists('curl_init')) { @@ -42,6 +52,9 @@ public function __construct($auth_string = '') // Set auth string property $this->auth_string = $auth_string; + // Set base url of selected API + $this->base_url = $base_url; + // Instantiate cURL object $this->authenticate(); } @@ -71,10 +84,20 @@ protected function authenticate() { $this->ch = curl_init(); - $headers = array( - 'Accept-Version: v10', - 'Accept: application/json', - ); + $headers = array(); + switch ($this->base_url) { + case Constants::API_URL_INVOICING: + $headers[] = 'Accept: application/vnd.api+json'; + break; + + case Constants::API_URL: + $headers[] = 'Accept-Version: v' . Constants::API_VERSION; + $headers[] = 'Accept: application/json'; + break; + + default: + break; + } if (!empty($this->auth_string)) { $headers[] = 'Authorization: Basic ' . base64_encode($this->auth_string); diff --git a/QuickPay/API/Constants.php b/QuickPay/API/Constants.php index ecbe5b2..4e60325 100644 --- a/QuickPay/API/Constants.php +++ b/QuickPay/API/Constants.php @@ -12,8 +12,13 @@ class Constants { /** - * API DEFINITIONS + * Primary API */ const API_URL = 'https://api.quickpay.net/'; const API_VERSION = '10'; + + /** + * Invoicing API + */ + const API_URL_INVOICING = 'https://invoicing.quickpay.net/'; } diff --git a/QuickPay/API/Request.php b/QuickPay/API/Request.php index 9c40ee9..094f43b 100644 --- a/QuickPay/API/Request.php +++ b/QuickPay/API/Request.php @@ -18,6 +18,7 @@ class Request * Contains QuickPay_Client instance * * @access protected + * @var Client */ protected $client; @@ -139,7 +140,7 @@ public function delete($path, $form = array()) */ protected function setUrl($params) { - curl_setopt($this->client->ch, CURLOPT_URL, Constants::API_URL . trim($params, '/')); + curl_setopt($this->client->ch, CURLOPT_URL, $this->client->base_url . trim($params, '/')); } /** diff --git a/QuickPay/QuickPay.php b/QuickPay/QuickPay.php index 98ba3f3..8c0f24f 100644 --- a/QuickPay/QuickPay.php +++ b/QuickPay/QuickPay.php @@ -14,18 +14,19 @@ class QuickPay public $request; /** - * __construct function. - * - * Instantiates the main class. - * Creates a client which is passed to the request construct. - * - * @auth_string string Authentication string for QuickPay - * - * @access public - */ - public function __construct($auth_string = '') + * __construct function. + * + * Instantiates the main class. + * Creates a client which is passed to the request construct. + * + * @param string $auth_string Authentication string for QuickPay. Format 'username:password' or ':apiKey' + * @param string $base_url Optional: Use a secondary API (eg billing) + * + * @access public + */ + public function __construct($auth_string = '', $base_url = \QuickPay\API\Constants::API_URL) { - $client = new Client($auth_string); + $client = new Client($auth_string, $base_url); $this->request = new Request($client); } }