shipping_name = "United Parcel Service (XML)";
$this->shipping_site = "http://www.burnwave.com/";
$this->author_name = "Michael K. Squires";
$this->author_site = "http://www.burnwave.com/";
$this->checkout_options = 1;
$this->ups_access_license = $this->shipping_config('license_number');
$this->ups_user_id = $this->shipping_config('user_id');
$this->ups_user_password = $this->shipping_config('user_password');
$this->ups_server = "https://www.ups.com/ups.app/xml/Rate";
$this->_CURL = true;
}
function shipping_config($config_name){
global $prefix, $db;
$shipping_sql = $db->sql_query("SELECT config_value FROM ".$prefix."_cart_shipping_options_ups WHERE config_name = '".$config_name."'");
list($value) = $db->sql_fetchrow($shipping_sql);
return $value;
}
function shipping_update($config_name, $config_value){
global $prefix, $db;
$db->sql_query("UPDATE ".$prefix."_cart_shipping_options_ups SET config_value = '".$config_value."' WHERE config_name = '".$config_name."'");
}
function admin($function){
global $prefix, $db, $cartconfig;
switch($function){
case "install": $this->admin_install(); break;
case "options": $this->admin_options(); break;
case "update": $this->admin_update(); break;
case "order_info": $this->admin_orderinfo(); break;
case "uninstall": $this->admin_uninstall(); break;
}
}
function admin_install() {
global $prefix, $db;
$db->sql_query("CREATE TABLE ".$prefix."_cart_shipping_options_ups (config_name varchar(255) NOT NULL default '', config_value text NOT NULL)");
$db->sql_query("INSERT INTO ".$prefix."_cart_shipping_options_ups VALUES ('origin_country_code', 'US')");
$db->sql_query("INSERT INTO ".$prefix."_cart_shipping_options_ups VALUES ('origin_postal_code', '21784')");
$db->sql_query("INSERT INTO ".$prefix."_cart_shipping_options_ups VALUES ('service_types', '1DM,1DA,1DAPI,1DP,2DM,2DA,3DS,GND,GNDRES,GNDCOM,STD,XPR,XDM,XPD')");
$db->sql_query("INSERT INTO ".$prefix."_cart_shipping_options_ups VALUES ('user_id', 'emporiumdev')");
$db->sql_query("INSERT INTO ".$prefix."_cart_shipping_options_ups VALUES ('user_password', 'empdev')");
$db->sql_query("INSERT INTO ".$prefix."_cart_shipping_options_ups VALUES ('license_number', '7BB0DE52C40237F0')");
$db->sql_query("INSERT INTO ".$prefix."_cart_shipping_options_ups VALUES ('service_default', 'GNDRES')");
}
function admin_options() {
global $prefix, $db, $cartconfig, $admin_file;
echo "
";
OpenTable();
echo "
"._EMPORIUM_MODULE_PAYMENT_UPS_UPSSERVICE.": ".$this->get_service_string($orderInfo['shipMethodData'])."";
CloseTable();
}
function admin_uninstall() {
global $prefix, $db;
$db->sql_query("DROP TABLE ".$prefix."_cart_shipping_options_ups");
}
###################
## UPS FUNCTIONS ##
###################
function ups_get_quote($upsProductCode,$packageWeight, $originPostalCode, $originCountryCode, $destPostalCode, $destCountryCode, $destPostalCity = '',
$UPSPickupTypeCode='03',$PackageType = '02',
$packageValue = '',$packageLength = '',$packageWidth = '',
$packageHeight = '', $ResidentialAddressIndicator = '0'){
global $iso_countries,$UPS_Server,$return_code;
if(empty($upsProductCode)){
return ("Missing UPS service with the UPS product code.");
}
if(empty($packageWeight)){
return ("Missing package weight.");
}
if(empty($originPostalCode)){
return ("Missing the five digit postal ZIP code of the shipment origin.");
}
if(empty($originCountryCode)){
return ("Missing the country of the shipment origin.");
}
if(strlen($originCountryCode) > 3){
$originCountryCode = preg_replace("'&'si","and",$originCountryCode);
$originCountryCode = preg_replace("'St. 'si","Saint ",$originCountryCode);
reset($iso_countries);
while (list($key, $value) = each($iso_countries)) {
if(strcasecmp($originCountryCode,$value)==0){
$originCountryCode = strtoupper($key);
break;
}
}
}
$code = strtolower($originCountryCode);
$country = $iso_countries["$code"];
/*
if(empty($country)){
return ("Missing the ISO country code of the shipment origin country $originCountryCode .");
}
*/
if(empty($destPostalCode)){
return ("Missing the one to six alphanumeric-character postal code of the destination country, if the destination country uses postal codes.");
}
if(empty($destCountryCode)){
return ("Missing the country of the destination country.");
}
if(strlen($destCountryCode) > 3){
reset($iso_countries);
$destCountryCode = preg_replace("'&'si","and",$destCountryCode);
$destCountryCode = preg_replace("'ST. 'si","Saint ",$destCountryCode);
while (list($key, $value) = each($iso_countries)) {
if(strcasecmp($destCountryCode,$value)==0){
$destCountryCode = strtoupper($key);
break;
}
}
}
$code = strtolower($destCountryCode);
$country = $iso_countries["$code"];
/*
if(empty($country)){
return ("Missing the ISO country code of the destination country $destCountryCode .");
}
*/
if(is_numeric($ups_quote)){
$UPSServiceCode = $upsProductCode;
} else {
$UPSServiceCode = $this->get_service_code($upsProductCode);
}
$Dimensions='';
$InSure='';
if(!empty($packageLength) && !empty($packageWidth) && !empty($packageHeight)){
$Dimensions = "IN
"
. " ".$UPSPickupTypeCode."
"
. " ".$UPSServiceCode."
"
. " ".$PackageType."
"
. " LBS
"
. "
$work_string"; if($this->_CURL == true){ $result = $this->ups_post_curl($this->ups_server, $work_string); } else { $result = $this->ups_post_php($this->ups_server, $work_string); } //echo "
$result";
if(! $result) { return "Error connecting to UPS shipping rates calculator server."; }
if($return_code > 200) { return "Error connecting to UPS shipping rates calculator server."; }
$attributes = array();
$attributes = $this->xml_parser($result);
$rate = 0;
reset($attributes);
while (list ($key, $val) = each ($attributes)){
switch($val['tag']){
case 'ResponseStatusCode':
$ResponseStatusCode =$val['value'];
break;
case 'Error':
while((list($key,$val)=each($attributes)) && ($val['tag']!='Error')){
if($val['tag']=='ErrorDescription'){
$error_description=$val['value'];
}
}
if($error_description){
return $error_description;
}
break;
case 'RatedShipment':
while((list($key,$val)=each($attributes)) && ($val['tag']!='RatedShipment')){
switch($val['tag']){
case 'TotalCharges':
while((list($key,$val)=each($attributes)) && ($val['tag']!='TotalCharges')){
if($val['tag']=='MonetaryValue'){
if($rate == 0){$rate=$val['value'];}
}
}
break;
}
}
break;
}
}
return $rate;
}
function ups_post_php($url,$post_string){
$header = "POST /ups.app/xml/Rate HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen ($post_string) . "\r\n\r\n";
$fp = fsockopen ("ssl://www.ups.com", 443, $errno, $errstr, 15);
if (!$fp) {
// https error
return "$errstr ($errno)";
} else {
fputs ($fp, $header . $post_string);
$results = fread($fp,4096);
$bytes_left = socket_get_status($fp);
if ($bytes_left > 0) {
$results .= fread($fp, $bytes_left);
}
fclose ($fp);
}
if(!eregi(' 200 ', $results)) {
echo "HTTPS error connecting to UPS Online Tools";
return;
}
if(!eregi('close', $results)) {
echo "HTTP HEAD error connecting to UPS Online Tools";
return;
}
list($junk,$xml) = split("close",$results);
$xml = trim($xml);
return "$xml";
}
function ups_post_curl($url,$post_string) {
global $return_code;
$return_code = 200;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt ($ch, CURLOPT_POSTFIELDSIZE, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_SSLVERSION, 3);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch);
$array1 = array();
$array1 = curl_getinfo($ch);
$code = $array1['http_code'];
$codestring = "http_code " .$code ." " .curl_error ($ch);
curl_close ($ch);
if (strstr($code,"200")){
$return_code = 200;
return $result;
}
if (strstr($code,"404")){
$return_code = 404;
return ("404 Error. The $url server responded with a File Not Found message.");
}
if (strstr($code,"40")){
$return_code = 404;
return ("$code Error. The $url server responded with an Unauthorized Access Forbidden Error.");
}
if (strstr($code,"30")){
$return_code = 300;
return("$code Error. The $url server responded with redirects.");
}
if (strstr($code,"50")){
$return_code = 500;
return("$code Error. The $url server responded with a Server Error.");
}
return ("Error. $url cannot be accessed at this time.
$codestring");
}
function get_service_types(){
$services = array('1DM', '1DA', '1DAPI', '1DP', '2DM', '2DA', '3DS', 'GND', 'GNDRES', 'GNDCOM', 'STD', 'XPR', 'XDM', 'XPD');
return $services;
}
function get_service_string($service){
if(empty($service)){
return;
}
switch($service){
case "1DM": $shipping_method = "UPS Next Day Early AM"; break;
case "1DA": $shipping_method = "UPS Next Day Air"; break;
case "1DAPI": $shipping_method = "UPS Next Day Air Intra (Puerto Rico)"; break;
case "1DP": $shipping_method = "UPS Next Day Air Saver"; break;
case "2DM": $shipping_method = "UPS 2nd Day Air AM"; break;
case "2DA": $shipping_method = "UPS 2nd Day Air"; break;
case "3DS": $shipping_method = "UPS 3 Day Select"; break;
case "GND": $shipping_method = "UPS Ground"; break;
case "GNDRES": $shipping_method = "UPS Ground Residential"; break;
case "GNDCOM": $shipping_method = "UPS Ground Commercial"; break;
case "STD": $shipping_method = "Canada Standard"; break;
case "XPR": $shipping_method = "UPS Worldwide Express"; break;
case "XDM": $shipping_method = "UPS Worldwide Express Plus";break;
case "XPD": $shipping_method = "UPS Worldwide Expedited"; break;
}
return $shipping_method;
}
function get_service_code($service){
if(empty($service)){
return;
}
switch($service){
case "1DM": $shipping_method = "14"; break;
case "1DA": $shipping_method = "01"; break;
case "1DAPI": $shipping_method = "01"; break;
case "1DP": $shipping_method = "13"; break;
case "2DM": $shipping_method = "59"; break;
case "2DA": $shipping_method = "02"; break;
case "3DS": $shipping_method = "12"; break;
case "GND": $shipping_method = "03"; break;
case "GNDRES": $shipping_method = "03"; break;
case "GNDCOM": $shipping_method = "03"; break;
case "STD": $shipping_method = "11"; break;
case "XPR": $shipping_method = "07"; break;
case "XDM": $shipping_method = "54"; break;
case "XPD": $shipping_method = "08"; break;
default: $shipping_method = "03"; break;
}
return $shipping_method;
}
function xml_parser($post_results){
if ( !($xml_parser = xml_parser_create()) ){
die("Couldn't create XML Parser!");
}
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xml_parser, $post_results, $vals, $index);
xml_parser_free($xml_parser);
return $vals;
}
#####################
# End UPS FUNCTIONS #
#####################
function shipping($function){
switch($function){
case "default_options": $this->default_options(); break;
case "shipping_options": $this->shipping_options(); break;
case "update_shipping_options": $this->update_shipping_options(); break;
case "confirmation_display": $this->confirmation_display(); break;
case "receipt_display": $this->receipt_display(); break;
}
}
function default_options(){
global $prefix, $db;
$userinfo = cartuserinfo();
$checkout = init_class_checkout();
if($checkout->shipMethodData == ''){
$checkout->update('shipMethodData', $this->shipping_config('service_default'));
}
}
function shipping_options(){
global $prefix, $db;
$userinfo = cartuserinfo();
$checkout = init_class_checkout();
OpenTable();
$origin_country_code = $this->shipping_config('origin_country_code');
$origin_postal_code = $this->shipping_config('origin_postal_code');
$country = cartcountryinfo($checkout->shipCountry);
$selectable_services = explode(",", $this->shipping_config('service_types'));
while(list($service_type) = each($selectable_services)){
$service_cost = $this->ups_get_quote($selectable_services[$service_type],$checkout->order_weight(), $origin_postal_code, $origin_country_code, $checkout->shipZip, $country['countryCode'], $checkout->shipCity,'03','02','','','','', 1);
if(is_numeric($service_cost)){
if($checkout->shipMethodData == $selectable_services[$service_type]){ $CHECKED = "checked"; } else { $CHECKED = ''; }
if($checkout->shipMethodData == $selectable_services[$service_type]){ echo ""; }
echo " ".$this->get_service_string($selectable_services[$service_type])." - ".price_format($service_cost)."
";
if($checkout->shipMethodData == $selectable_services[$service_type]){ echo ""; }
}
}
CloseTable();
echo "
";
}
function update_shipping_options(){
global $prefix, $db, $ups_service_code;
$userinfo = cartuserinfo();
$checkout = init_class_checkout();
$selected_service_types = explode(",", $this->shipping_config('service_types'));
if(in_array($ups_service_code, $selected_service_types)){
$checkout->update('shipMethodData', $ups_service_code);
} else {
die(header("Location: modules.php?name=Shopping_Cart&file=checkout&c_op=Checkout_PaymentMethod"));
}
}
function confirmation_display(){
global $prefix, $db;
$userinfo = cartuserinfo();
$checkout = init_class_checkout();
OpenTable();
echo "
"._EMPORIUM_MODULE_PAYMENT_UPS_UPSSERVICE.": ".$this->get_service_string($checkout->shipMethodData)."";
CloseTable();
echo "
";
}
function receipt_display(){
global $prefix, $db;
$userinfo = cartuserinfo();
$checkout = init_class_checkout();
$orderInfo = cartorderinfo($this->order_id);
OpenTable();
echo "
"._EMPORIUM_MODULE_PAYMENT_UPS_UPSSERVICE.": ".$this->get_service_string($orderInfo['shipMethodData'])."";
CloseTable();
echo "
";
}
function quote(){
global $prefix, $db;
$userinfo = cartuserinfo();
$checkout = init_class_checkout();
$shipping_cost = 0;
$origin_country_code = $this->shipping_config('origin_country_code');
$origin_postal_code = $this->shipping_config('origin_postal_code');
$country = cartcountryinfo($checkout->shipCountry);
$shipping_cost = $this->ups_get_quote($checkout->shipMethodData, $checkout->order_weight(), $origin_postal_code, $origin_country_code, $checkout->shipZip, $country['countryCode'], $checkout->shipCity,'03','02','','','','', 1);
return $shipping_cost;
}
}
?>