File: /var/www/vhosts/paiskincare.com/httpdocs2/monkey75/shipwire_fulfillment.php
<?php
/**
* shipwire_fulfillment.php
* Process Shipwire order fulfillment by orders_id(oID).
* -- Create Shipwire XML request.
* -- Send and Capture response.
* -- Log response and parse error, send email to admin for error
* by chang@shipwire.com March 19, 2008
*/
require('includes/application_top.php');
$oID = zen_db_prepare_input($_GET['oID']);
include(DIR_WS_CLASSES . 'order.php');
$order = new order($oID);
$xml_submit_url="https://api.shipwire.com/exec/FulfillmentServices.ZenCart.php";
$shipwire_order = array('cust_name' => $order->delivery['name'],
'address1' => $order->delivery['street_address'],
'address2' => $order->delivery['suburb'],
'city' => $order->delivery['city'],
'state' => $order->delivery['state'],
'country' => $order->delivery['country'],
'zip' => $order->delivery['postcode'],
'phone' => $order->customer['telephone'],
'email' => $order->customer['email_address'],
'shipping' => $order->info['shipping_method'],
'shipping_carrier' => $order->info['shipping_module_code']);
$carrier_name = $shipwire_order['shipping_carrier'];
$shipping_method = $shipwire_order['shipping'];
$shipwire_order['shipping'] = getShipwireServiceLevel($carrier_name, $shipping_method);
$email = trim(MODULE_SHIPWIRE_EMAIL);
$passwd = trim(MODULE_SHIPWIRE_PASSWORD);
$shipwire_key = trim(MODULE_SHIPWIRE_KEY);
$server = trim(MODULE_SHIPWIRE_SERVER_MODE); // "Test" or "Production"
$warehouse = "00"; // Leave "00" if you want Shipwire to determine the warehouse
$order_no = $oID;
$OrderList='<OrderList StoreAccountName="'.$shipwire_key.'">
<EmailAddress>'.$email.'</EmailAddress>
<Password>'.$passwd.'</Password>
<Server>'.$server.'</Server>
<Referer>zencart</Referer>
<Order id="'.$order_no.'">
<Warehouse>'.$warehouse.'</Warehouse>
<AddressInfo type="ship">
<Name>
<Full>'.$shipwire_order['cust_name'].'</Full>
</Name>
<Address1>'.$shipwire_order['address1'].'</Address1>
<Address2>'.$shipwire_order['address2'].'</Address2>
<City>'.$shipwire_order['city'].'</City>
<State>'.$shipwire_order['state'].'</State>
<Country>'.$shipwire_order['country'].'</Country>
<Zip>'.$shipwire_order['zip'].'</Zip>
<Phone>'.$shipwire_order['phone'].'</Phone>
<Email>'.$shipwire_order['email'].'</Email>
</AddressInfo>
<Shipping>'.$shipwire_order['shipping'].'</Shipping>';
$item_count = count($order->products);
for ($i=0; $i<$item_count; $i++)
{
$OrderList .='<Item num="'.$i.'">
<Code>'.$order->products[$i]['model'].'</Code>
<Quantity>'.$order->products[$i]['qty'].'</Quantity>
<Description>'.$order->products[$i]['name'].'</Description>
<Length></Length>
<Width></Width>
<Height></Height>
<Weight></Weight>
<DeclaredValue>'.$order->products[$i]['price'].'</DeclaredValue>
</Item>';
}
$OrderList .='</Order>
</OrderList>';
//Convert characters to proper format for post
//$OrderList = urlencode($OrderList);
// open synchronous connection to Shipwire servlet
$url = $xml_submit_url;
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array("Content-type","application/x-www-form-urlencoded"));
curl_setopt($session, CURLOPT_POSTFIELDS, $OrderList);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($session, CURLOPT_TIMEOUT, 360);
$orderSubmitted = curl_exec($session);
//Check if there is error in response.
$regex = '/<ErrorMessage>(.+?)</';
preg_match($regex,$orderSubmitted,$match);
curl_close($session);
if(isset($match[1]))
{
$response_error = $match[1];
}else{
$response_error = "";
}
// if there is no error, set orders_status to 'Shipwire Fulfilled'
if($response_error==""){
$shipwire_status = $db->Execute("select orders_status_id from " . TABLE_ORDERS_STATUS . " where orders_status_name='Shipwire Fulfilled'");
$shipwire_status_id = $shipwire_status->fields['orders_status_id'];
$db->Execute("update " . TABLE_ORDERS . " set orders_status = " . $shipwire_status_id . ", last_modified = now() where orders_id = '" . (int)$oID . "'");
$db->Execute("insert into " . TABLE_ORDERS_STATUS_HISTORY . "
(orders_id, orders_status_id, date_added, customer_notified, comments)
values ('" . (int)$oID . "',
'" . zen_db_input($shipwire_status_id) . "',
now(),'1','')");
$redirection_URL = "orders.php?oID=".$oID;
header("Location: $redirection_URL");
// in case of there is error, send out error message.
}else{
echo("<script language='javascript'>");
echo("alert('".$response_error."');");
echo("history.back(-1);");
echo("</script>");
}
function getShipwireServiceLevel($carrier_name, $shipping_method)
{
global $db;
if($carrier_name == 'ups'){
$ups_types = array('1DA' => 'Next Day Air',
'1DM' => 'Next Day Air Early AM',
'1DML' => 'Next Day Air Early AM Letter',
'1DAL' => 'Next Day Air Letter',
'1DAPI' => 'Next Day Air Intra (Puerto Rico)',
'1DP' => 'Next Day Air Saver',
'1DPL' => 'Next Day Air Saver Letter',
'2DA' => '2nd Day Air',
'2DM' => '2nd Day Air AM',
'2DML' => '2nd Day Air AM Letter',
'2DAL' => '2nd Day Air Letter',
'3DS' => '3 Day Select',
'GND' => 'Ground',
'GNDCOM' => 'Ground Commercial',
'GNDRES' => 'Ground Residential',
'STD' => 'Canada Standard',
'XPR' => 'Worldwide Express',
'XPRL' => 'worldwide Express Letter',
'XDM' => 'Worldwide Express Plus',
'XDML' => 'Worldwide Express Plus Letter',
'XPD' => 'Worldwide Expedited');
$key = "";
while ($ups_type = current($ups_types)) {
if(strpos($shipping_method, $ups_type) !== FALSE){
$key = key($ups_types);
}
next($ups_types);
}
//if there is no usp shipping key exists
if($key != ""){
$sql = "select * FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'MODULE_SHIPWIRE_UPS_$key'";
$shipwire_ups_shipping = $db->Execute($sql);
if(!$shipwire_ups_shipping->EOF)
{
$shipwire_level = $shipwire_ups_shipping->fields['configuration_value'];
return $shipwire_level;
}
}
}elseif($carrier_name == 'usps'){
$usps_types = array('Express' => 'EXPRESS',
'First_Class' => 'FIRST CLASS',
'Priority' => 'PRIORITY',
'Parcel' => 'PARCEL',
'Media' => 'MEDIA',
'BPM' => 'Bound Printed Material',
'Library' => 'LIBRARY'
);
$key = "";
while ($usps_type = current($usps_types)) {
if(strpos($shipping_method, $usps_type) !== FALSE){
$key = key($usps_types);
}
next($usps_types);
}
//if there is no usp shipping key exists
if($key != ""){
$shipwire_usps_shipping = $db->Execute("select * FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'MODULE_SHIPWIRE_USPS_$key'");
if(!$shipwire_usps_shipping->EOF)
{
$shipwire_level = $shipwire_usps_shipping->fields['configuration_value'];
return $shipwire_level;
}
}
}else{
return $shipping_method;
}
}
?>