Last active
October 26, 2015 23:46
-
-
Save joeswann/818cc8fb6b33251f95ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Joeswann; | |
class NuorderConnector { | |
public $oauth_consumer_key; | |
public $oauth_consumer_secret; | |
public $endpoint; | |
public $oauth_verifier; | |
public $oauth_token; | |
public $oauth_token_secret; | |
public $application_name; | |
function __construct($options) { | |
$this->oauth_consumer_key = $options['consumer_key']; | |
$this->oauth_consumer_secret = $options['consumer_secret']; | |
$this->endpoint = $options['endpoint']; | |
$this->oauth_token = $options['oauth_token']; | |
$this->oauth_token_secret = $options['oauth_token_secret']; | |
$this->application_name = $options['application_name']; | |
$this->callback = $options['callback']; | |
$this->base_uri = $this->endpoint; | |
$this->guzzle = new \GuzzleHttp\Client(); | |
} | |
function api_initiate() { | |
$url = $this->base_uri . '/api/initiate'; | |
$oauth = $this->get_oauth('GET', '/api/initiate', [ 'oauth_callback' => $this->callback,'application_name' => $this->application_name ]); | |
$options = [ | |
'headers' => $oauth['headers'], | |
'query' => $oauth['query'], | |
]; | |
try { | |
return $response = $this->guzzle->request('GET',$url, $options); | |
} catch (\GuzzleHttp\Exception\ClientException $e) { | |
echo "<pre>"; | |
echo "Response: \n"; | |
if ($e->hasResponse()) { | |
echo $e->getResponse()->getBody(); | |
} | |
echo "\n\n\n"; | |
echo "Request: \n"; | |
echo var_export($e->getRequest(),1); | |
echo "</pre>"; | |
die(); | |
} | |
$this->oauth_token = $response['oauth_token']; | |
$this->oauth_token_secret = $response['oauth_token_secret']; | |
return $response; | |
} | |
function get_oauth_token() { | |
if(!$this->oauth_verifier) throw new Exception('No oauth_verifier'); | |
$options = [ | |
'headers' => $this->get_oauth('GET', '/api/token', [ 'oauth_verifier' => $this->oauth_verifier ])['headers'] | |
]; | |
$response = $this->guzzle->request('GET','/api/token', $options); | |
$this->oauth_token = $response['oauth_token']; | |
$this->oauth_token_secret = $response['oauth_token_secret']; | |
return $response; | |
} | |
function get($url, $params = null) { | |
$url = $this->base_uri . $url; | |
$options = [ | |
'headers' => $this->get_oauth('GET', $url)['headers'], | |
'params' => $params | |
]; | |
try { | |
return $response = $this->guzzle->request('GET',$url, $options); | |
} catch (\GuzzleHttp\Exception\ClientException $e) { | |
die('Uh oh! ' . $e->getMessage()); | |
} | |
} | |
function post($url, $params = null) { | |
$url = $this->base_uri . $url; | |
try { | |
return $response = $this->guzzle->request('POST',$url, $this->build_params('POST', $url, $params)); | |
} catch (\GuzzleHttp\Exception\ClientException $e) { | |
die('Uh oh! ' . $e->getMessage()); | |
} | |
} | |
function put($url, $params = null) { | |
$url = $this->base_uri . $url; | |
try { | |
return $response = $this->guzzle->request('PUT', $this->build_params('PUT', $url, $params)); | |
} catch (\Guzzl\Http\Exception\ClientException $e) { | |
die('Uh oh! ' . $e->getMessage()); | |
} | |
} | |
private function build_params($type, $url, $params = null) { | |
$options = [ | |
'headers' => $this->get_oauth($type, $url)['headers'], | |
]; | |
if($params) array_merge($options,[ 'body' => json_encode($params) ]); | |
return $options; | |
} | |
private function validate_response($response) { | |
if(in_array([200,201], (int)$response->StatusCode())) { | |
return $response; | |
} else { | |
throw new Exception("NuOrder API Error {$response['code']}. {$response["message"]}"); | |
} | |
} | |
private function get_oauth($method, $url, $addons = null) { | |
$url = $this->base_uri . $url; | |
$time = time(); | |
$nonce = $this->generateRandomString(16); | |
$signature = $this->build_signature($method, $url, $time, $nonce, $addons)['signature']; | |
$auth = $this->build_oauth($time, $nonce, $signature, $addons); | |
$query = $this->build_oauth($time, $nonce, $signature, $addons, false); | |
//Debugging | |
echo '<pre>'; | |
echo "Auth: \n"; | |
var_dump("OAuth {$auth}"); | |
echo "\n\n\n"; | |
echo "Signature: \n"; | |
var_dump($signature); | |
echo "\n\n\n"; | |
echo "Query: \n"; | |
var_dump($query); | |
echo "\n\n\n"; | |
echo '</pre>'; | |
return [ | |
'headers' => [ | |
'Authorization' => "OAuth {$auth}", | |
'Accept' => 'application/json', | |
'Content-Type' => 'application/json' | |
], | |
'query' => $query | |
]; | |
} | |
private function build_signature($method, $url, $time, $nonce, $addons) { | |
$key = rawurlencode($this->oauth_consumer_secret) . '&' . rawurlencode($this->oauth_token_secret); | |
$params = [ | |
'oauth_consumer_key' => $this->oauth_consumer_key, | |
'oauth_token' => $this->oauth_token, | |
'oauth_timestamp' => $time, | |
'oauth_nonce' => $nonce, | |
'oauth_version' => '1.0', | |
'oauth_signature_method' => 'HMAC-SHA1', | |
]; | |
if(isset($addons['oauth_callback'])) $params['oauth_callback'] = $addons['oauth_callback']; | |
//ksort($params); | |
$query = http_build_query($params); | |
$base = "{$method}&{$url}?{$query}"; | |
return [ | |
'base' => $base, | |
'signature' => hash_hmac('sha1', rawurlencode($base), $key) | |
]; | |
} | |
private function build_oauth($time, $nonce, $signature, $addons = false, $headers = true) { | |
$params = [ | |
'oauth_consumer_key' => $this->oauth_consumer_key, | |
'oauth_timestamp' => $time, | |
'oauth_nonce' => $nonce, | |
'oauth_version' => '1.0', | |
'oauth_signature_method' => 'HMAC-SHA1', | |
'oauth_token' => $this->oauth_token, | |
]; | |
if($headers) $params['oauth_signature'] = $signature; | |
if($addons['oauth_callback']) $params['oauth_callback'] = $addons['oauth_callback']; | |
if($headers) { | |
if($addons['application_name']) $params['application_name'] = $addons['application_name']; | |
$oauth_kv = []; | |
foreach($params as $k => $v) { | |
$oauth_kv[] = "{$k}={$v}"; | |
} | |
return join(',', $oauth_kv); | |
} else { | |
return $params; | |
} | |
} | |
private function generateRandomString($length = 10) { | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$charactersLength = strlen($characters); | |
$randomString = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$randomString .= $characters[rand(0, $charactersLength - 1)]; | |
} | |
return $randomString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment