[+] MO : Shipwire module

This commit is contained in:
jObregon
2012-04-26 16:58:28 +00:00
parent bb6b2bf3b7
commit 09278c582d
18 changed files with 2067 additions and 0 deletions
+111
View File
@@ -0,0 +1,111 @@
<?php
/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2012 PrestaShop SA
* @version Release: $Revision: 1.4 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/// @todo document and handle errors
class SWCurl
{
/** @var _ch Curl Handler Resource */
private $_ch;
/** @var _url Url to call */
private $_url;
/** @var _lastResponse Last HTTP response fetched */
private $_lastResponse;
/** @var _params Array containing data for Methods */
private $_params;
public function __construct($url = null, $sslCheck = true)
{
$this->_ch = curl_init();
$this->_url = $url;
/* $this->_setOpt(CURLOPT_HEADER, true); */
/* $this->_setOpt(CURLINFO_HEADER_OUT, true); */
$this->_setOpt(CURLOPT_RETURNTRANSFER, true);
if (!$sslCheck)
$this->_setOpt(CURLOPT_SSL_VERIFYPEER, false);
}
public function __destruct()
{
curl_close($this->_ch);
}
private function _setOpt($flag, $value)
{
curl_setopt($this->_ch, $flag, $value);
}
public function setPostMethod(Array $params)
{
$this->_params['POST'] = http_build_query($params);
curl_setopt($this->_ch, CURLOPT_POST, true);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $this->_params['POST']);
}
public function setCustomPost($paramString)
{
curl_setopt($this->_ch, CURLOPT_POST, true);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $paramString);
$this->_params['POST'] = $paramString;
}
public function setHttpHeader(Array $headers)
{
curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $headers);
}
public function setGetParams(Array $params)
{
$this->_params['GET'] = http_build_query($params);
}
public function send($url = null)
{
if (!$url)
$url = $this->_url;
if (!$url)
return false;
if (isset($this->_params['GET']))
$url .= '?'.$this->_params['GET'];
curl_setopt($this->_ch, CURLOPT_URL, $url);
$this->_lastResponse = curl_exec($this->_ch);
if ($this->_lastResponse === false)
{
trigger_error('Curl error: '.curl_error($this->_ch), E_USER_WARNING);
return false;
}
return $this->_lastResponse;
}
public function getLastResponse()
{
return $this->_lastResponse;
}
}
+133
View File
@@ -0,0 +1,133 @@
<?php
/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2012 PrestaShop SA
* @version Release: $Revision: 1.4 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/* Security */
if (!defined('_PS_VERSION_'))
exit;
class ShipwireApi
{
protected $_configVars = array(
'SHIPWIRE_API_SERVER' => '',
'SHIPWIRE_API_USER' => '',
'SHIPWIRE_API_PASSWD' => '',
'SHIPWIRE_API_CONNECTED' => '',
'SHIPWIRE_API_MODE' => '',
'SHIPWIRE_WAREHOUSE' => '',
'SHIPWIRE_ACCOUNT_NAME' => '',
'SHIPWIRE_TRACKING_LAST_DATE' => '',
'SHIPWIRE_COMMIT_ID' => '',
'SHIPWIRE_SENT_ID' => '',
'SHIPWIRE_DELIVERED_ID' => '',
);
/** @var _xml Array with header body and footer of the final xml */
public $_xml = array();
/** @var _mode Sets the connection mode: Test | Production */
protected $_mode = 'Production';
/** @var _urlConn URL to connect to at Shipwire's end */
protected $_url = array(
'inventoryUpdate' => array(
'Test' => 'https://api.beta.shipwire.com/exec/InventoryServices.php',
'Production' => 'https://api.shipwire.com/exec/InventoryServices.php',
'prefix' => 'InventoryUpdateXML',
),
'fulfillmentServices' => array(
'Test' => 'https://api.beta.shipwire.com/exec/FulfillmentServices.php',
'Production' => 'https://api.shipwire.com/exec/FulfillmentServices.php',
'prefix' => 'OrderListXML',
),
'trackingUpdate' => array(
'Test' => 'https://api.beta.shipwire.com/exec/TrackingServices.php',
'Production' => 'https://api.shipwire.com/exec/TrackingServices.php',
'prefix' => 'TrackingUpdateXML',
),
);
// protected $_url = array(
// 'inventoryUpdate' => array(
// 'Test' => 'https://api.beta.shipwire.com/exec/InventoryServices.php',
// 'Production' => 'https://api.beta.shipwire.com/exec/InventoryServices.php',
// 'prefix' => 'InventoryUpdateXML',
// ),
// 'fulfillmentServices' => array(
// 'Test' => 'https://api.beta.shipwire.com/exec/FulfillmentServices.php',
// 'Production' => 'https://api.beta.shipwire.com/exec/FulfillmentServices.php',
// 'prefix' => 'OrderListXML',
// ),
// 'trackingUpdate' => array(
// 'Test' => 'https://api.beta.shipwire.com/exec/TrackingServices.php',
// 'Production' => 'https://api.beta.shipwire.com/exec/TrackingServices.php',
// 'prefix' => 'TrackingUpdateXML',
// ),
// );
public function __construct()
{
foreach ($this->_configVars as $key => $v)
$this->_configVars[$key] = Configuration::get($key);
if (Configuration::get('PS_CIPHER_ALGORITHM'))
$this->_cipherTool = new Rijndael(_RIJNDAEL_KEY_, _RIJNDAEL_IV_);
else
$this->_cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_);
$this->_configVars['SHIPWIRE_API_PASSWD'] = Tools::safeOutput($this->_cipherTool->decrypt($this->_configVars['SHIPWIRE_API_PASSWD']));
$this->_configVars['SHIPWIRE_API_MODE'] = $this->_mode;
}
public function sendData($xmlTagFields = array())
{
$xmlDocument = $this->_buildXMLDoc();
$urlConn = new SWCurl($this->_url[$this->_apiType][$this->_mode], false);
$urlConn->setCustomPost($this->_url[$this->_apiType]['prefix'].'='.urlencode($xmlDocument));
$action = $urlConn->send();
$xml = simplexml_load_string($urlConn->getLastResponse());
$json = json_encode($xml);
return json_decode($json,TRUE);
}
private function _buildXMLDoc()
{
$xmlDocument = '';
foreach ($this->_xml['header'] as $header)
$xmlDocument .= $header;
foreach ($this->_xml['body'] as $body)
$xmlDocument .= $body;
foreach ($this->_xml['footer'] as $footer)
$xmlDocument .= $footer;
return $xmlDocument;
}
}
@@ -0,0 +1,61 @@
<?php
/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2012 PrestaShop SA
* @version Release: $Revision: 1.4 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/* Security */
if (!defined('_PS_VERSION_'))
exit;
class ShipwireInventoryUpdate extends ShipwireApi
{
protected $_apiType = 'inventoryUpdate';
public function __construct($apiUser, $apiPasswd)
{
$this->_xml['header'][] =
'<InventoryUpdate>'.
'<EmailAddress>'.$apiUser.'</EmailAddress>'.
'<Password>'.$apiPasswd.'</Password>'.
'<Server>'.$this->_configVars['SHIPWIRE_API_MODE'].'</Server>'.
'<AffiliateId>7403</AffiliateId>'.
'<Warehouse></Warehouse>
<ProductCode></ProductCode>
<IncludeEmpty/>';
$this->_xml['body'][] = '';
$this->_xml['footer'][] = '</InventoryUpdate>';
}
public function getInventory($field = NULL)
{
$result = $this->sendData();
if (!empty($field) && isset($result[$field]))
$result = $result[$field];
return $result;
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2012 PrestaShop SA
* @version Release: $Revision: 1.4 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/* Security */
if (!defined('_PS_VERSION_'))
exit;
class ShipwireOrder extends ShipwireApi
{
protected $_apiType = 'fulfillmentServices';
public function __construct()
{
parent::__construct();
$this->_configVars['SHIPWIRE_ACCOUNT_NAME'] = '';
$this->_xml['header'] = array(
'<OrderList>',// StorAccountName="'.$this->_configVars['SHIPWIRE_ACCOUNT_NAME'].'">',
'<Username>'.$this->_configVars['SHIPWIRE_API_USER'].'</Username>',
'<Password>'.$this->_configVars['SHIPWIRE_API_PASSWD'].'</Password>',
'<Server>'.$this->_configVars['SHIPWIRE_API_MODE'].'</Server>',
'<AffiliateId>7403</AffiliateId>',
);
$this->_xml['body'][] = '';
$this->_xml['footer'][] = '</OrderList>';
}
public function addOrder($values, $refresh = false)
{
foreach ($values as &$v)
if (!empty($v) && !is_array($v))
$v = pSQL($v);
if (!$refresh)
$this->_xml['body'] = array(
'<Order id="'.$values['orderId'].'">',
'<Warehouse>'.$this->_configVars['SHIPWIRE_WAREHOUSE'].'</Warehouse>',
// '<SameDay>'.$values['sameDay'].'</SameDay>',
'<AddressInfo type="ship">',
'<Name><Full>'.$values['name'].'</Full></Name>',
'<Address1>'.$values['address1'].'</Address1>',
'<Address2>'.$values['address2'].'</Address2>',
'<City>'.$values['city'].'</City>',
'<Country>'.$values['country'].'</Country>',
'<Zip>'.$values['zip'].'</Zip>',
'<Phone>'.$values['phone'].'</Phone>',
'<Email>'.$values['mail'].'</Email>',
'</AddressInfo>',
'<Shipping>'.$values['shippingType'].'</Shipping>',
);
else
$this->_xml['body'] = array('<Order id="'.(int)$values['orderId'].'">');
$i = 0;
foreach ($values['packageList'] as $item)
{
$this->_xml['body'][] = '<Item num="'.($i++).'">';
$this->_xml['body'][] = '<Code>'.$item['code'].'</Code>';
$this->_xml['body'][] = '<Quantity>'.$item['quantity'].'</Quantity>';
$this->_xml['body'][] = '</Item>';
}
$this->_xml['body'][] = '</Order>';
}
}
+188
View File
@@ -0,0 +1,188 @@
<?php
/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2012 PrestaShop SA
* @version Release: $Revision: 1.4 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
require(dirname(__FILE__).'/../../../config/config.inc.php');
if (!class_exists('Shipwire'))
require(dirname(__FILE__).'/../shipwire.php');
class ShipwireTracking extends ShipwireApi
{
protected $_apiType = 'trackingUpdate';
public function __construct()
{
parent::__construct();
$this->_configVars['SHIPWIRE_ACCOUNT_NAME'] = '';
$this->_xml['header'] = array(
'<TrackingUpdate>',
'<Username>'.$this->_configVars['SHIPWIRE_API_USER'].'</Username>',
'<Password>'.$this->_configVars['SHIPWIRE_API_PASSWD'].'</Password>',
'<Server>'.$this->_configVars['SHIPWIRE_API_MODE'].'</Server>',
'<AffiliateId>7403</AffiliateId>',
);
$this->_xml['body'] = array();
$this->_xml['footer'][] = '</TrackingUpdate>';
}
public function retrieveFromTransacId($transactionId)
{
$this->_xml['body'][] = '<ShipwireId>'.pSQL($transactionId).'</ShipwireId>';
}
public function retrieveFromOrderId($orderId)
{
$this->_xml['body'][] = '<OrderNo>'.pSQL($orderId).'</OrderNo>';
}
public function retrieveFull()
{
$this->_xml['body'][] = '<Bookmark>1</Bookmark>';
}
public function retrieveNew()
{
$this->_xml['body'][] = '<Bookmark>3</Bookmark>';
}
/*
* @brief Updates tracking info in local database.
* Declared as a static method so shipwire.php can access it thru autoload
*/
public static function updateTracking($static = false)
{
return updateTracking($static);
}
}
function updateTracking($static = false)
{
$api = new ShipwireTracking();
$api->retrieveFull();
$d = $api->sendData();
if ($d['Status'])
if ($static)
return false;
else
die('KO');
if ($d['TotalOrders'] > 0)
{
foreach ($d['Order'] as $order)
{
$o = array();
if (isset($order['@attributes']))
$o = $order['@attributes'];
if (!isset($o['id']))
{
Logger::addLog('Shipwire: Order ID not defined. >>>>'.print_r($d, true).'<<<<', 4);
continue;
}
$orderExists = Db::getInstance()->ExecuteS('SELECT `id_order`
FROM `'._DB_PREFIX_.'shipwire_order`
WHERE `id_order` = '.(int)$o['id'].' LIMIT 1');
if (isset($orderExists[0]['id_order']) && !empty($orderExists[0]['id_order']))
{
Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'shipwire_order` SET '
.(isset($order['TrackingNumber']) ? '`tracking_number` = \''.pSQL($order['TrackingNumber']).'\',' : '')
.(isset($o['shipped']) ? '`shipped` = \''.pSQL($o['shipped']).'\'' : '')
.(isset($o['shipper']) ? ',`shipper` = \''.pSQL($o['shipper']).'\'' : '')
.(isset($o['shipDate']) ? ',`shipDate` = \''.pSQL($o['shipDate']).'\'' : '')
.(isset($o['expectedDeliveryDate']) ? ',`expectedDeliveryDate` = \''.pSQL($o['expectedDeliveryDate']).'\'' : '')
.(isset($o['href']) ? ',`href` = \''.pSQL($o['href']).'\'' : '')
.(isset($o['shipperFullName']) ? ',`shipperFullName` = \''.pSQL($o['shipperFullName']).'\'' : '')
.' WHERE `id_order` = '.(int)$o['id']);
}
else
{
Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'shipwire_order`
(`id_order`, `tracking_number`, `shipped`, `shipper`, `shipDate`, `expectedDeliveryDate`, `href`, `shipperFullName`)
VALUES (
\''.pSQL($o['id']).'\''
.(isset($order['TrackingNumber']) ? ',\''.pSQL($order['TrackingNumber']).'\'' : ',\'\'')
.(isset($o['shipped']) ? ',\''.pSQL($o['shipped']).'\'' : ',\'\'')
.(isset($o['shipper']) ? ',\''.pSQL($o['shipper']).'\'' : ',\'\'')
.(isset($o['shipDate']) ? ',\''.pSQL($o['shipDate']).'\'' : ',\'\'')
.(isset($o['expectedDeliveryDate']) ? ',\''.pSQL($o['expectedDeliveryDate']).'\'' : ',\'\'')
.(isset($o['href']) ? ',\''.pSQL($o['href']).'\'' : ',\'\'')
.(isset($o['shipperFullName']) ? ',\''.pSQL($o['shipperFullName']).'\'' : ',\'\'')
.')');
}
$result = Db::getInstance()->getValue('SELECT `transaction_ref`
FROM `'._DB_PREFIX_.'shipwire_order`
WHERE `id_order` = '.(int)$o['id']);
if (empty($result))
{
$module = new Shipwire();
$module->updateOrderStatus((int)$o['id'], true);
}
if (isset($order['TrackingNumber']))
{
Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'orders`
SET `shipping_number` = \''.pSQL($order['TrackingNumber']).'\'
WHERE `id_order` = '.(int)$o['id']);
if ($o['id'])
{
$psOrder = new Order($o['id']);
if ($psOrder->id)
{
$history = new OrderHistory();
$history->id_order = $o['id'];
if (isset($o['shipped']) && $o['shipped'] == 'YES')
$history->changeIdOrderState(Configuration::get('SHIPWIRE_SENT_ID'), $o['id']);
$history->addWithemail();
}
}
}
}
}
if (Configuration::get('PS_CIPHER_ALGORITHM'))
$cipherTool = new Rijndael(_RIJNDAEL_KEY_, _RIJNDAEL_IV_);
else
$cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_);
$shipWireInventoryUpdate = new ShipwireInventoryUpdate(Configuration::get('SHIPWIRE_API_USER'),
$cipherTool->decrypt(Configuration::get('SHIPWIRE_API_PASSWD')));
$shipWireInventoryUpdate->getInventory();
if ($static)
return true;
else
die('OK');
}
+36
View File
@@ -0,0 +1,36 @@
<?php
/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2012 PrestaShop SA
* @version Release: $Revision: 14009 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;