File: D:/HostingSpaces/Velosophe/dev.abloc.cc/wwwroot/app/plugins/universal_nonshipping/dabba.class.php
<?php
/* Connect to dabba */
class Dabba
{
private $username;
private $password;
private static $api_base;
/*
* Constructor
* @_db Dabba username
* @_order Dabba password
*/
function __construct($_username,$_password)
{
self::$api_base = 'https://' . ( get_option( 'fietskoeriers_dabba_live_mode' ) === 'yes' ? 'dabba' : 'test' ) . '.fietskoeriers.nl/api';
$this->username = $_username;
$this->password = $_password;
} // end constructor
public function getOrders()
{
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$api_base . "/order");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic " . base64_encode($this->username.":".$this->password)
));
$content = curl_exec($ch);
curl_close($ch);
$result = json_decode($content,true);
return $result;
} catch(Exception $ex)
{
$msg = "Dabba getOrders: " . $ex->getMessage() . "Trace: " . $ex->getTraceAsString();
echo $msg;
return false;
}
}
public function getOrder($id)
{
$id = filter_var ($id, FILTER_SANITIZE_STRING,FILTER_SANITIZE_SPECIAL_CHARS);
$url = self::$api_base . "/order/{$id}";
//echo $url;
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic " . base64_encode($this->username.":".$this->password)
));
$content = curl_exec($ch);
curl_close($ch);
$result = json_decode($content,true);
return $result;
} catch(Exception $ex)
{
$msg = "Dabba getOrder (ID: ".$id."): " . $ex->getMessage() . "Trace: " . $ex->getTraceAsString();
echo $msg;
return false;
}
}
public function createOrder($post_fields)
{
try {
//echo $username.":".$password. "<pre>"; print_r($post_fields); echo "</pre>";
$json = json_encode($post_fields);
//echo "<br /><br />".$json."<br /><br />";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$api_base . "/order");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
"Authorization: Basic " . base64_encode($this->username.":".$this->password)
));
$content = curl_exec($ch);
curl_close($ch);
$result = json_decode($content,true);
return $result;
} catch(Exception $ex)
{
$msg = "Dabba createOrder: " . $ex->getMessage() . "Trace: " . $ex->getTraceAsString();
echo $msg;
return false;
}
}
public function getLabel( $orderId )
{
$orderId = filter_var ($orderId, FILTER_SANITIZE_STRING, FILTER_SANITIZE_SPECIAL_CHARS);
$url = self::$api_base . "/label/{$orderId}";
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic " . base64_encode($this->username.":".$this->password)
));
$content = curl_exec($ch);
curl_close($ch);
$result = json_decode($content,true);
$result = base64_decode( $result[ 'label' ] );
return $result;
} catch(Exception $ex)
{
$msg = "Dabba getLabel: " . $ex->getMessage() . "Trace: " . $ex->getTraceAsString();
return false;
}
}
}