-
-
Save summersab/ef0a9151457b2a3506f2c267282b495f to your computer and use it in GitHub Desktop.
Order Desk PHP Client
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 | |
/*********** | |
* CURRENTLY NON-FUNCTIONAL - NEEDING TO MAKE SOME SERVER CHANGES | |
* | |
* I've never written an API before, so I'm sure that my extension class is | |
* riddled with issues and doesn't conform to proper RESTful standards. | |
* Nevertheless, I wanted to provide some additional API calls to the Order | |
* Desk API client. | |
* | |
* In addition, my API endpoint provides a queue for calls that | |
* are made in order to provide pseudo-locking of the Order Desk resources. | |
* This prevents collisions from occuring when API calls take a little longer | |
* than usual to respond. The basic structure of this queueing framework is | |
* found here: | |
* https://gist.github.com/summersab/366694873ccd786d1cd6461f707846d6#file-orderdesk-queue-php | |
* | |
* Below are the available API calls provided by my endpoint. Note that you | |
* HAVE to use my server ($url) in order for these calls to work; otherwise, | |
* Order Desk's API won't recognize the request. Also, my calls allow you to | |
* look up items based on the code/SKU field instead of requiring the Order | |
* Desk ID (my code takes care of that in the background). You can't use the | |
* Order Desk IDs with my calls, however. | |
* | |
* Lastly, I make no guarantees about the uptime, reliability, etc of this | |
* code. I personally use it in production for my small business. Considering | |
* I doubt anyone else is going to use it but me, if it breaks, I'm the only | |
* one who should care. I haven't set up a proper cert for this server, so | |
* everything is currently http-only (calls to Order Desk from my server use | |
* https). However, if someone out there actually finds this to be helpful, | |
* I might do something to make it more reliable. | |
* | |
*********** | |
* Increment/Decrement | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $args = array( | |
* "stock" => -1 | |
* ); | |
* | |
* $headers = array( | |
* "Content-Type: application:x-counter" | |
* ); | |
* | |
* $result = $od->patch("inventory-items/codes/[CODE]"); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
*********** | |
* Set metadata value | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $args = array( | |
* "[KEY]" => "[VALUE]" | |
* ); | |
* | |
* $result = $od->put("inventory-items/codes/[CODE]/metadata"); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
*********** | |
* Lazy set metadata value based on key (only set the metadata if the provided | |
* key does not already exist) | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $args = array( | |
* "[KEY]" => "[VALUE]" | |
* ); | |
* | |
* $headers = array( | |
* "If-None-Exists: key" //Literal word key, not the key you're setting | |
* ); | |
* | |
* $result = $od->post("inventory-items/codes/[CODE]/metadata", $args, $headers); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
*********** | |
* Lazy set metadata value based on value (only set the metadata if the provided | |
* value does not exist for any existing keys) | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $args = array( | |
* "[KEY]" => "[VALUE]" | |
* ); | |
* | |
* $headers = array( | |
* "If-None-Exists: value" //Literal word value, not the value you're setting | |
* ); | |
* | |
* $result = $od->post("inventory-items/codes/[CODE]/metadata", $args, $headers); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
*********** | |
* Get all metadata for an item | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $result = $od->get("inventory-items/codes/[CODE]/metadata"); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
*********** | |
* Get the value of a specific metadata field for an item based on the key | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $result = $od->get("inventory-items/codes/[CODE]/metadata[KEY]"); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
*********** | |
* Get an array of all metadata fields for all items that contain the metadata | |
* field [KEY] => [VALUE] | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $result = $od->get("inventory-items/codes/metadata/keys/[KEY]/[VALUE]"); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
*********** | |
* Get an array of all metadata fields for all items that contain a metadata | |
* key of [KEY] | |
* | |
* <?php | |
* include "order-desk-api-client-ng.php"; | |
* $od = new OrderDeskApiClient($storeid, $apikey); | |
* | |
* $result = $od->get("inventory-items/codes/metadata/keys/[KEY]"); | |
* echo "<pre>" . print_r($result, 1) . "</pre>"; | |
* ?> | |
* | |
***********/ | |
class OrderDeskApiClient | |
{ | |
private $store_id; | |
private $api_key; | |
private $base_url = "http://order-desk-api-ng.tklapp.com/api/v1"; | |
public $last_status_code = ""; | |
public function __construct($store_id, $api_key) { | |
$this->store_id = $store_id; | |
$this->api_key = $api_key; | |
} | |
public function get($url = "", $post = null) { | |
return $this->go("GET", $url, $post); | |
} | |
public function post($url, $post = null, $headers = null) { | |
return $this->go("POST", $url, $post, $headers); | |
} | |
public function put($url, $post = null, $headers = null) { | |
return $this->go("PUT", $url, $post, $headers); | |
} | |
public function delete($url, $post = null, $headers = null) { | |
return $this->go("DELETE", $url, $post, $headers); | |
} | |
public function patch($url, $post = null, $headers = null) { | |
return $this->go("PATCH", $url, $post, $headers); | |
} | |
public function go($method, $url, $post, $headers = NULL) { | |
if (!is_array($post)) { | |
$post = null; | |
} | |
if (!is_array($headers) && !isset($headers)) { | |
$headers = array(); | |
} | |
if (!$url) { | |
throw new \Exception("Please enter a destination url"); | |
} | |
$url = $this->base_url . "/" . $url; | |
$headers = array_merge($this->getHeaders(), $headers); | |
//GET Override | |
if ($method == "GET" && $post !== null) { | |
$url .= (strpos($url, "?") === false ? "?" : "") . http_build_query($post); | |
$post = ""; | |
} | |
//Setup cURL | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | |
if ($post) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); | |
} | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_USERAGENT, "orderdesk/orderdesk_client"); | |
//Send To Order Desk and Parse Response | |
$response = trim(curl_exec($ch)); | |
$info = curl_getinfo($ch); | |
$json = json_decode($response, 1); | |
if (!is_array($json)) { | |
return $response; | |
} | |
$this->last_status_code = $info['http_code']; | |
return $json; | |
} | |
//Get auth headers for this call | |
public function getHeaders() { | |
return array( | |
"ORDERDESK-STORE-ID: {$this->store_id}", | |
"ORDERDESK-API-KEY: {$this->api_key}", | |
"Content-Type: application/json", | |
); | |
} | |
//Check Post JSON | |
public function validatePostedJson() { | |
if (!isset($_POST['order'])) { | |
header(':', true, 400); | |
die('No Data Found'); | |
} | |
//Check Store ID | |
if (!isset($_SERVER['HTTP_X_ORDER_DESK_STORE_ID']) || $_SERVER['HTTP_X_ORDER_DESK_STORE_ID'] != $this->store_id) { | |
header(':', true, 403); | |
die('Unauthorized Request'); | |
} | |
//Check the Hash | |
if (!isset($_SERVER['HTTP_X_ORDER_DESK_HASH']) || hash_hmac('sha256', rawurldecode($_POST['order']), $this->api_key) != $_SERVER['HTTP_X_ORDER_DESK_HASH']) { | |
header(':', true, 403); | |
die('Unauthorized Request'); | |
} | |
//Check Order Data | |
$order = json_decode($_POST['order'], 1); | |
if (!is_array($order)) { | |
header(':', true, 400); | |
die('Invalid Order Data'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment