Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for calling the invoicing API #52

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 28 additions & 5 deletions QuickPay/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Client
*/
public $ch;

/**
* Base url for the selected API.
*
* @var string
*/
public $base_url;

/**
* Contains the authentication string
*
Expand All @@ -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')) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion QuickPay/API/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
}
3 changes: 2 additions & 1 deletion QuickPay/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Request
* Contains QuickPay_Client instance
*
* @access protected
* @var Client
*/
protected $client;

Expand Down Expand Up @@ -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, '/'));
}

/**
Expand Down
23 changes: 12 additions & 11 deletions QuickPay/QuickPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}