<?php
class Cache
{
public static function has( $name ){
return false;
}
public static function get( $name ){
return null;
}
public static function put( $name, $value, $time = 1440 ){
return true;
}
}
class Collivery
{
protected $token;
protected $client;
protected $config;
protected $errors = array();
protected $check_cache = 2;
protected $default_address_id;
protected $client_id;
protected $user_id;
/**
* Setup class with basic Config
*
* @param Array $config Configuration Array
*/
function __construct( array $config = array() )
{
$this->config = (object) array(
'app_name' => 'Default App Name', // Application Name
'app_version' => '0.0.1', // Application Version
'app_host' => '', // Framework/CMS name and version, eg 'Wordpress 3.8.1 WooCommerce 2.0.20' / 'Joomla! 2.5.17 VirtueMart 2.0.26d'
'app_url' => '', // URL your site is hosted on
'user_email' => 'demo@collivery.co.za',
'user_password' => 'demo',
'demo' => false,
);
foreach ( $config as $key => $value ) {
$this->config->$key = $value;
}
if ( $this->config->demo ){
$this->config->user_email = 'demo@collivery.co.za';
$this->config->user_password = 'demo';
}
$this->authenticate();
}
/**
* Setup the Soap Object
*
* @return SoapClient MDS Collivery Soap Client
*/
protected function init ()
{
if ( ! $this->client ){
try {
$this->client = new SoapClient( // Setup the soap client
'http://www.collivery.co.za/wsdl/v2', // URL to WSDL File
array( 'cache_wsdl' => WSDL_CACHE_NONE ) // Don't cache the WSDL file
);
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
}
return true;
}
/**
* Checks if the Soap Client has been set, and returns it.
*
* @return SoapClient Webserver Soap Client
*/
protected function client()
{
if ( ! $this->client ) {
$this->init();
}
if ( ! $this->token ) {
$this->authenticate();
}
return $this->client;
}
/**
* Authenticate and set the token
*
* @return string
*/
protected function authenticate()
{
if ( ( $this->check_cache == 2 ) && Cache::has('collivery.auth') ) {
$authenticate = Cache::get('collivery.auth');
$this->default_address_id = $authenticate['default_address_id'];
$this->client_id = $authenticate['client_id'];
$this->user_id = $authenticate['user_id'];
$this->token = $authenticate['token'];
return true;
} else {
if ( ! $this->init() ) return false;
$user_email = $this->config->user_email;
$user_password = $this->config->user_password;
try {
$authenticate = $this->client->authenticate($user_email, $user_password, $this->token,
array(
'name' => $this->config->app_name . ' mds/collivery/class',
'version' => $this->config->app_version,
'host' => $this->config->app_host,
'url' => $this->config->app_url,
'lang' => 'PHP '. phpversion(),
));
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( is_array( $authenticate ) && isset( $authenticate['token'] ) ){
if ( $this->check_cache != 0 ) Cache::put( 'collivery.auth', $authenticate, 50 );
$this->default_address_id = $authenticate['default_address_id'];
$this->client_id = $authenticate['client_id'];
$this->user_id = $authenticate['user_id'];
$this->token = $authenticate['token'];
return true;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Create a new Address and Contact
*
* @param array $data Address and Contact Information
* @return array Address ID and Contact ID
*/
public function addAddress( array $data )
{
$location_types = $this->getLocationTypes();
$towns = $this->getTowns();
$suburbs = $this->getSuburbs( $data['town_id'] );
if ( ! isset( $data['location_type'] ) )
$this->setError( 'missing_data', 'location_type not set.' );
elseif ( ! isset( $location_types[ $data['location_type'] ] ) )
$this->setError( 'invalid_data', 'Invalid location_type.' );
if ( ! isset( $data['town_id'] ) )
$this->setError( 'missing_data', 'town_id not set.' );
elseif ( ! isset( $towns[ $data['town_id'] ] ) )
$this->setError( 'invalid_data', 'Invalid town_id.' );
if ( ! isset( $data['suburb_id'] ) )
$this->setError( 'missing_data', 'suburb_id not set.' );
elseif ( ! isset( $suburbs[ $data['suburb_id'] ] ) )
$this->setError( 'invalid_data', 'Invalid suburb_id.' );
if ( ! isset( $data['street'] ) )
$this->setError( 'missing_data', 'street not set.' );
if ( ! isset( $data['full_name'] ) )
$this->setError( 'missing_data', 'full_name not set.' );
if ( isset( $data['phone'] ) || isset( $data['cellphone'] ) )
$this->setError( 'missing_data', 'Please supply ether a phone or cellphone number...' );
if ( ! $this->hasErrors() ) {
try {
$result = $this->client()->add_address( $data, $this->token );
Cache::forget( 'collivery.addresses.'. $this->client_id );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['address_id'] ) ) {
return $result;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Add's a contact person for a given Address ID
*
* @param array $data New Contact Data
* @return int New Contact ID
*/
public function addContact( array $data )
{
if ( ! isset( $data['address_id'] ) )
$this->setError( 'missing_data', 'address_id not set.' );
elseif ( ! is_array( $this->getAddress( $data['address_id'] ) ) )
$this->setError( 'invalid_data', 'Invalid address_id.' );
if ( ! isset( $data['street'] ) )
$this->setError( 'missing_data', 'street not set.' );
if ( ! isset( $data['full_name'] ) )
$this->setError( 'missing_data', 'full_name not set.' );
if ( isset( $data['phone'] ) || isset( $data['cellphone'] ) )
$this->setError( 'missing_data', 'Please supply ether a phone or cellphone number...' );
if ( ! $this->hasErrors() ) {
try {
$result = $this->client()->add_address( $data, $this->token );
Cache::forget( 'collivery.addresses.'. $this->client_id );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['address_id'] ) ) {
return $result;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Returns the price based on the data provided.
*
* @param array $data Your Collivery Details
* @return array Pricing for details supplied
*/
public function getPrice( array $data )
{
$towns = $this->getTowns();
if ( ! isset( $data['collivery_from'] ) && ! isset( $data['from_town_id'] ) )
$this->setError( 'missing_data', 'collivery_from/from_town_id not set.' );
elseif ( isset( $data['collivery_from'] ) && ! is_array( $this->getAddress( $data['collivery_from'] ) ) )
$this->setError( 'invalid_data', 'Invalid Address ID for: collivery_from.' );
elseif ( isset( $data['from_town_id'] ) && ! isset( $towns[ $data['from_town_id'] ] ) )
$this->setError( 'invalid_data', 'Invalid Town ID for: from_town_id.' );
if ( ! isset( $data['collivery_to'] ) && ! isset( $data['to_town_id'] ) )
$this->setError( 'missing_data', 'collivery_to/to_town_id not set.' );
elseif ( isset( $data['collivery_to'] ) && ! is_array( $this->getAddress( $data['collivery_to'] ) ) )
$this->setError( 'invalid_data', 'Invalid Address ID for: collivery_to.' );
elseif ( isset( $data['to_town_id'] ) && ! isset( $towns[ $data['to_town_id'] ] ) )
$this->setError( 'invalid_data', 'Invalid Town ID for: to_town_id.' );
if ( ! isset( $data['service'] ) )
$this->setError( 'missing_data', 'service not set.' );
if ( ! $this->hasErrors() ) {
try {
$result = $this->client()->get_price( $data, $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( is_array( $result ) ) {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
return $result;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Validate Collivery
*
* Returns the validated data array of all details pertaining to a collivery.
* This process validates the information based on services, time frames and parcel information.
* Dates and times may be altered during this process based on the collection and delivery towns service parameters.
* Certain towns are only serviced on specific days and between certain times.
* This function automatically alters the values.
* The parcels volumetric calculations are also done at this time.
* It is important that the data is first validated before a collivery can be added.
*
* @param array $data Properties of the new Collivery
* @return array The validated data
*/
public function validate( array $data )
{
$contacts_from = $this->getContacts( $data['collivery_from'] );
$contacts_to = $this->getContacts( $data['collivery_to'] );
$parcel_types = $this->getParcelTypes();
$services = $this->getServices();
if ( ! isset( $data['collivery_from'] ) )
$this->setError( 'missing_data', 'collivery_from not set.' );
elseif ( ! is_array( $this->getAddress( $data['collivery_from'] ) ) )
$this->setError( 'invalid_data', 'Invalid Address ID for: collivery_from.' );
if ( ! isset( $data['contact_from'] ) )
$this->setError( 'missing_data', 'contact_from not set.' );
elseif ( ! isset( $contacts_from[ $data['contact_from'] ] ) )
$this->setError( 'invalid_data', 'Invalid Contact ID for: contact_from.' );
if ( ! isset( $data['collivery_to'] ) )
$this->setError( 'missing_data', 'collivery_to not set.' );
elseif ( ! is_array( $this->getAddress( $data['collivery_to'] ) ) )
$this->setError( 'invalid_data', 'Invalid Address ID for: collivery_to.' );
if ( ! isset( $data['contact_to'] ) )
$this->setError( 'missing_data', 'contact_to not set.' );
elseif ( ! isset( $contacts_to[ $data['contact_to'] ] ) )
$this->setError( 'invalid_data', 'Invalid Contact ID for: contact_to.' );
if ( ! isset( $data['collivery_type'] ) )
$this->setError( 'missing_data', 'collivery_type not set.' );
elseif ( ! isset( $parcel_types[ $data['collivery_type'] ] ) )
$this->setError( 'invalid_data', 'Invalid collivery_type.' );
if ( ! isset( $data['service'] ) )
$this->setError( 'missing_data', 'service not set.' );
elseif ( ! isset( $services[ $data['service'] ] ) )
$this->setError( 'invalid_data', 'Invalid service.' );
if ( ! $this->hasErrors() ) {
try {
$result = $this->client()->validate_collivery( $data, $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( is_array( $result ) ) {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
return $result;
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Creates a new Collivery based on the data array provided.
* The array should first be validated before passing to this function.
* The Waybill No is return apon successful creation of the collivery.
*
* @param array $data Properties of the new Collivery
* @return int New Collivery ID
*/
public function addCollivery( array $data )
{
$contacts_from = $this->getContacts( $data['collivery_from'] );
$contacts_to = $this->getContacts( $data['collivery_to'] );
$parcel_types = $this->getParcelTypes();
$services = $this->getServices();
if ( ! isset( $data['collivery_from'] ) )
$this->setError( 'missing_data', 'collivery_from not set.' );
elseif ( ! is_array( $this->getAddress( $data['collivery_from'] ) ) )
$this->setError( 'invalid_data', 'Invalid Address ID for: collivery_from.' );
if ( ! isset( $data['contact_from'] ) )
$this->setError( 'missing_data', 'contact_from not set.' );
elseif ( ! isset( $contacts_from[ $data['contact_from'] ] ) )
$this->setError( 'invalid_data', 'Invalid Contact ID for: contact_from.' );
if ( ! isset( $data['collivery_to'] ) )
$this->setError( 'missing_data', 'collivery_to not set.' );
elseif ( ! is_array( $this->getAddress( $data['collivery_to'] ) ) )
$this->setError( 'invalid_data', 'Invalid Address ID for: collivery_to.' );
if ( ! isset( $data['contact_to'] ) )
$this->setError( 'missing_data', 'contact_to not set.' );
elseif ( ! isset( $contacts_to[ $data['contact_to'] ] ) )
$this->setError( 'invalid_data', 'Invalid Contact ID for: contact_to.' );
if ( ! isset( $data['collivery_type'] ) )
$this->setError( 'missing_data', 'collivery_type not set.' );
elseif ( ! isset( $parcel_types[ $data['collivery_type'] ] ) )
$this->setError( 'invalid_data', 'Invalid collivery_type.' );
if ( ! isset( $data['service'] ) )
$this->setError( 'missing_data', 'service not set.' );
elseif ( ! isset( $services[ $data['service'] ] ) )
$this->setError( 'invalid_data', 'Invalid service.' );
if ( ! $this->hasErrors() ) {
try {
$result = $this->client()->add_collivery( $data, $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['collivery_id'] ) ) {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
return $result['collivery_id'];
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
}
/**
* Accepts the newly created Collivery, moving it from Waiting Client Acceptance
* to Accepted so that it can be processed.
*
* @param int $collivery_id ID of the Collivery you wish to accept
* @return boolean Has the Collivery been accepted
*/
public function acceptCollivery( $collivery_id )
{
try {
$result = $this->client()->accept_collivery( $collivery_id, $this->token );
} catch (SoapFault $e) {
$this->catchSoapFault( $e );
return false;
}
if ( isset( $result['result'] ) ) {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
return $result['result'] == 'Accepted';
} else {
if ( isset( $result['error_id'] ) )
$this->setError( $result['error_id'], $result['error'] );
else
$this->setError( 'result_unexpected', 'No address_id returned.' );
return false;
}
}
/**
* Handle error messages in SoapFault
*
* @param SoapFault $e SoapFault Object
*/
protected function catchSoapFault( $e )
{
$this->setError( $e->faultcode, $e->faultstring );
}
/**
* Add a new error
*
* @param string $id Error ID
* @param string $text Error text
*/
protected function setError( $id, $text )
{
$this->errors[ $id ] = $text;
}
/**
* Retrieve errors
*/
public function getErrors()
{
return $this->errors;
}
/**
* Check if this instance has an error
*/
public function hasErrors()
{
return !empty($this->errors);
}
/**
* Clears all the Errors
*/
public function clearErrors()
{
$this->errors = array();
}
/**
* Disable Cached completely and retrieve data directly from the webservice
*/
public function disableCache()
{
$this->check_cache = 0;
}
/**
* Ignore Cached data and retrieve data directly from the webservice
* Save returned data to Cache
*/
public function ignoreCache()
{
$this->check_cache = 1;
}
/**
* Check if cache exists before querying the webservice
* If webservice was queried, save returned data to Cache
*/
public function enableCache()
{
$this->check_cache = 2;
}
}
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.25, 7.3.0 - 7.3.12, 7.4.0
preferences:
153.69 ms | 403 KiB | 269 Q