1,'code' => 'FEDEX_GROUND', 'name' => 'FedEx Ground', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172') , 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>2,'code' => 'FEDEX_2_DAY', 'name' => 'FedEx 2 Day', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172'), 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>3,'code' => 'FEDEX_EXPRESS_SAVER', 'name' => 'FedEx Express Saver Time Pickup', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172'), 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>4,'code' => 'FIRST_OVERNIGHT', 'name' => 'FedEx First Overnight', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172'), 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>5,'code' => 'GROUND_HOME_DELIVERY', 'name' => 'FedEx Ground (Home Delivery)', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172'), 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>6,'code' => 'PRIORITY_OVERNIGHT', 'name' => 'FedEx Priority Overnight', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172'), 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>7,'code' => 'SMART_POST', 'name' => 'FedEx Smart Post', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172'), 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>8,'code' => 'STANDARD_OVERNIGHT', 'name' => 'FedEx Standard Overnight', 'countries' => 'USA, PUERTO RICO', 'zones' => array('country_United_States_of_America_223','country_Puerto_Rico_172'), 'destinations' => array('country_United_States_of_America_223','country_Puerto_Rico_172')),
array('key'=>9,'code' => 'INTERNATIONAL_GROUND', 'name' => 'FedEx International Ground'),
array('key'=>10,'code' => 'INTERNATIONAL_ECONOMY', 'name' => 'FedEx International Economy'),
array('key'=>11,'code' => 'INTERNATIONAL_ECONOMY_DISTRIBUTION', 'name' => 'FedEx International Economy Distribution'),
array('key'=>12,'code' => 'INTERNATIONAL_FIRST', 'name' => 'FedEx International First'),
array('key'=>13,'code' => 'INTERNATIONAL_PRIORITY', 'name' => 'FedEx International Priority'),
array('key'=>14,'code' => 'INTERNATIONAL_PRIORITY_DISTRIBUTION', 'name' => 'FedEx International Priority Distribution'),
array('key'=>15,'code' => 'EUROPE_FIRST_INTERNATIONAL_PRIORITY', 'name' => 'FedEx Europe First')
);
var $convertUnit=array(
'kg' => 'KGS',
'lb' => 'LBS',
'cm' => 'CM',
'in' => 'IN',
'kg2' => 'kg',
'lb2' => 'lb',
'cm2' => 'cm',
'in2' => 'in',
);
public $nbpackage = 0;
function shippingMethods(&$main){
$methods = array();
if(!empty($main->shipping_params->methodsList)){
$main->shipping_params->methods=unserialize($main->shipping_params->methodsList);
}
if(!empty($main->shipping_params->methods)){
foreach($main->shipping_params->methods as $method){
$selected = null;
foreach($this->fedex_methods as $fedex){
if($fedex['code']==$method) {
$selected = $fedex;
break;
}
}
if($selected){
$methods[$main->shipping_id . '-' . $selected['key']] = $selected['name'];
}
}
}
return $methods;
}
function onShippingDisplay(&$order,&$dbrates,&$usable_rates,&$messages){
if(!hikashop_loadUser())
return false;
$local_usable_rates = array();
$local_messages = array();
$ret = parent::onShippingDisplay($order, $dbrates, $local_usable_rates, $local_messages);
if($ret === false)
return false;
$currentShippingZone = null;
$currentCurrencyId = null;
$currencyClass=hikashop_get('class.currency');
foreach($local_usable_rates as $k => $rate){
if(empty($rate->shipping_params->methodsList)) {
$messages['no_shipping_methods_configured'] = 'No shipping methods configured in the FedEx shipping plugin options';
continue;
}
$rate->shipping_params->methods = unserialize($rate->shipping_params->methodsList);
if($order->weight <= 0 || ($order->volume <= 0 && @$rate->shipping_params->use_dimensions == 1))
continue;
$this->freight = false;
$this->classicMethod = false;
$heavyProduct = false;
$weightTotal = 0;
if(!empty($rate->shipping_params->methods)) {
foreach($rate->shipping_params->methods as $method) {
if($method=='TDCB' || $method=='TDA' || $method=='TDO' || $method=='308' || $method=='309' || $method=='310')
$this->freight = true;
else
$this->classicMethod = true;
}
}
$data = null;
if(empty($order->shipping_address)) {
$messages['no_shipping_methods_configured'] = 'No shipping address is configured.';
return true;
}
$this->shipping_currency_id=$currency= hikashop_getCurrency();
$db = JFactory::getDBO();
$query='SELECT currency_code FROM '.hikashop_table('currency').' WHERE currency_id IN ('.$this->shipping_currency_id.')';
$db->setQuery($query);
$this->shipping_currency_code = $db->loadResult();
$cart = hikashop_get('class.cart');
$null = null;
$cart->loadAddress($null,$order->shipping_address->address_id,'object', 'shipping');
$currency = hikashop_get('class.currency');
$receivedMethods=$this->_getRates($rate, $order, $heavyProduct, $null);
if(empty($receivedMethods)) {
$messages['no_rates'] = JText::_('NO_SHIPPING_METHOD_FOUND');
continue;
}
$i = 0;
$local_usable_rates = array();
foreach($receivedMethods as $method) {
$usableMethods[] = $method;
$local_usable_rates[$i]=(!HIKASHOP_PHP5) ? $rate : clone($rate);
$local_usable_rates[$i]->shipping_price += round($method['value'], 2);
$selected_method = '';
$name = '';
foreach($this->fedex_methods as $fedex_method) {
if($fedex_method['code'] == $method['code'] && ($method['old_currency_code'] == 'CAD' || !isset($fedex_method['double']))) {
$name = $fedex_method['name'];
$selected_method = $fedex_method['key'];
break;
}
}
$local_usable_rates[$i]->shipping_name=$name;
if(!empty($selected_method))
$local_usable_rates[$i]->shipping_id .= '-' . $selected_method;
$sep = '';
if(@$rate->shipping_params->show_eta) {
if(@$rate->shipping_params->show_eta_delay) {
//if not fedex ground method
if($method['delivery_delay']!=-1 && $method['day']>0){
$local_usable_rates[$i]->shipping_description.=$sep.JText::sprintf( 'ESTIMATED_TIME_AFTER_SEND', $method['delivery_delay']);
}else{
$local_usable_rates[$i]->shipping_description.=$sep.JText::_( 'NO_ESTIMATED_TIME_AFTER_SEND');
}
} else {
//if not fedex ground method
if($method['delivery_day']!=-1 && $method['day']>0){
$local_usable_rates[$i]->shipping_description.=$sep.JText::sprintf( 'ESTIMATED_TIME_AFTER_SEND', $method['delivery_day']);
}else{
$local_usable_rates[$i]->shipping_description.=$sep.JText::_( 'NO_ESTIMATED_TIME_AFTER_SEND');
}
}
$sep = '
';
if($method['delivery_time']!=-1 && $method['day']>0){
if(@$rate->shipping_params->show_eta_format == '12')
$local_usable_rates[$i]->shipping_description.=$sep.JText::sprintf( 'DELIVERY_HOUR', date('h:i:s a', strtotime($method['delivery_time'])));
else
$local_usable_rates[$i]->shipping_description.=$sep.JText::sprintf( 'DELIVERY_HOUR', $method['delivery_time']);
}else{
$local_usable_rates[$i]->shipping_description.=$sep.JText::_( 'NO_DELIVERY_HOUR');
}
}
if(@$rate->shipping_params->show_notes && !empty($method['notes'])) {
foreach($method['notes'] as $note){
if($note->Code != '820' && $note->Code != '819' && !empty($note->LocalizedMessage) ) {
$local_usable_rates[$i]->shipping_description.=$sep.implode('
', $note->LocalizedMessage);
$sep = '
';
}
}
}
if($rate->shipping_params->group_package && $this->nbpackage>0)
$local_usable_rates[$i]->shipping_description.='
'.JText::sprintf('X_PACKAGES', $this->nbpackage);
$i++;
}
foreach($local_usable_rates as $i => $finalRate){
if(isset($finalRate->shipping_price_orig) || isset($finalRate->shipping_currency_id_orig)){
if($finalRate->shipping_currency_id_orig == $finalRate->shipping_currency_id)
$finalRate->shipping_price_orig = $finalRate->shipping_price;
else
$finalRate->shipping_price_orig = $currencyClass->convertUniquePrice($finalRate->shipping_price, $finalRate->shipping_currency_id, $finalRate->shipping_currency_id_orig);
}
$usable_rates[$finalRate->shipping_id]=$finalRate;
}
}
// BOF: gpraceman added to sort rates from least to most expensive
$usable_rates = $this->array_sort($usable_rates, 'shipping_price', 'fedex', SORT_ASC);
// EOF: gpraceman added to sort rates from least to most expensive
}
function getShippingDefaultValues(&$element){
$element->shipping_name='FedEx';
$element->shipping_description='';
$element->group_package=0;
$element->debug=0;
$element->shipping_images='fedex';
$element->shipping_params->post_code='';
$element->shipping_currency_id = $this->main_currency;
$element->shipping_params->pickup_type='01';
$element->shipping_params->destination_type='auto';
}
function onShippingConfiguration(&$element){
$config =& hikashop_config();
$this->main_currency = $config->get('main_currency', 1);
$currencyClass = hikashop_get('class.currency');
$currency = hikashop_get('class.currency');
$this->currencyCode = $currency->get($this->main_currency)->currency_code;
$this->currencySymbol = $currency->get($this->main_currency)->currency_symbol;
$this->fedex = JRequest::getCmd('name','fedex');
$this->categoryType = hikashop_get('type.categorysub');
$this->categoryType->type = 'tax';
$this->categoryType->field = 'category_id';
parent::onShippingConfiguration($element);
$elements = array($element);
$key = key($elements);
if(empty($elements[$key]->shipping_params->lang_file_override)){
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
$folder = JLanguage::getLanguagePath(JPATH_ROOT).DS.'overrides';
$path = $folder.DS.'en-GB.override.ini';
$content_override='';
if(JFile::exists($path)){
$content_override=JFile::read($path);
}
$content_override .= '
FEDEX_METER_ID="Meter #"'.'
FEDEX_ACCOUNT_NUMBER="Account #"'.'
FEDEX_API_KEY="API Key"'.'
FEDEX_API_PASSWORD="API Password"'.'
FEDEX_SHOW_ETA="Show ETA?"'.'
FEDEX_SHOW_ETA_FORMAT="ETA Format"'.'
PACKAGING_TYPE="Packaging Type"'.'
BOX_DIMENSIONS="Box Dimensions"'.'
ORIGINATION_POSTCODE="Ship From Postcode"'.'
SENDER_COMPANY="Sender Company"'.'
SENDER_PHONE="Sender Phone"'.'
SENDER_ADDRESS="Sender Address"'.'
SENDER_CITY="Sender City"'.'
SENDER_STATE="Sender State"'.'
SENDER_POSTCODE="Sender Zip"';
if(!JFolder::exists($folder)){
JFolder::create($folder);
}
if(JFolder::exists($folder)){
$path = $folder.DS.'en-GB.override.ini';
$result = JFile::write($path, $content_override);
if(!$result){
hikashop_display(JText::sprintf('FAIL_SAVE',$path),'error');
}else {
$elements[$key]->shipping_params->lang_file_override = 1;
}
}
}
$js="
function checkAllBox(id, type){
var toCheck = document.getElementById(id).getElementsByTagName('input');
for (i = 0 ; i < toCheck.length ; i++) {
if (toCheck[i].type == 'checkbox') {
if(type == 'check'){
toCheck[i].checked = true;
}else{
toCheck[i].checked = false;
}
}
}
}";
if(!HIKASHOP_PHP5) {
$doc =& JFactory::getDocument();
} else {
$doc = JFactory::getDocument();
}
$doc->addScriptDeclaration( "\n" );
}
function onShippingConfigurationSave(&$element){
$app = JFactory::getApplication();
$methods=array();
if(empty($element->shipping_params->account_number) ||
empty($element->shipping_params->origination_postcode) ||
empty($element->shipping_params->meter_id) ||
empty($element->shipping_params->api_key) ||
empty($element->shipping_params->api_password) ||
empty($element->shipping_params->sender_company) ||
empty($element->shipping_params->sender_phone) ||
empty($element->shipping_params->sender_address) ||
empty($element->shipping_params->sender_city) ||
empty($element->shipping_params->sender_state) ||
empty($element->shipping_params->sender_country) ||
empty($element->shipping_params->sender_postcode)
){
$app->enqueueMessage(JText::sprintf('ENTER_INFO', 'FedEx', JText::_('SENDER_INFORMATIONS').' ('. JText::_( 'FEDEX_ORIGINATION_POSTCODE' ).', '.JText::_( 'FEDEX_ACCOUNT_NUMBER' ).', '.JText::_( 'FEDEX_METER_ID' ).', '.JText::_( 'FEDEX_API_KEY' ).', '.JText::_( 'HIKA_PASSWORD' ).', '.JText::_( 'COMPANY' ).', '.JText::_( 'TELEPHONE' ).', '.JText::_( 'ADDRESS' ).', '.JText::_( 'CITY' ).', '.JText::_( 'COUNTRY' ).', '.JText::_( 'POST_CODE' ).')'));
}
if(isset($_REQUEST['data']['shipping_methods'])){
foreach($_REQUEST['data']['shipping_methods'] as $method){
foreach($this->fedex_methods as $fedexMethod){
$name=strtolower($fedexMethod['name']);
$name=str_replace(' ','_', $name);
if($name==$method['name']){
$obj = new stdClass();
$methods[strip_tags($method['name'])]=strip_tags($fedexMethod['code']);
}
}
}
} else {
$app->enqueueMessage(JText::sprintf('CHOOSE_SHIPPING_SERVICE'));
}
$element->shipping_params->methodsList = serialize($methods);
return true;
}
// add all rates for each package together. apply any rate discounts.
// returns an array of merged rates
// TODO: move all fees, discounts, etc. calculations to class.Package
function _getRates(&$rate, &$order, $heavyProduct, $null){
$db = JFactory::getDBO();
$total_price = 0;
// order total
foreach($order->products as $k=>$v){
foreach($v->prices as $price){
$total_price = $total_price + $price->price_value;
}
}
$data['fedex_account_number']=@$rate->shipping_params->account_number;
$data['fedex_meter_number']=@$rate->shipping_params->meter_id;
$data['fedex_api_key']=@$rate->shipping_params->api_key;
$data['fedex_api_password']=@$rate->shipping_params->api_password;
$data['show_eta']=@$rate->shipping_params->show_eta;
$data['show_eta_format']=@$rate->shipping_params->show_eta_format;
$data['packaging_type']=@$rate->shipping_params->packaging_type;
$data['include_price']=@$rate->shipping_params->include_price;
$data['currency_code']= $this->shipping_currency_code;
$data['weight_approximation']=@$rate->shipping_params->weight_approximation;
$data['use_dimensions']=@$rate->shipping_params->use_dimensions;
$data['dim_approximation_l']=@$rate->shipping_params->dim_approximation_l;
$data['dim_approximation_w']=@$rate->shipping_params->dim_approximation_w;
$data['dim_approximation_h']=@$rate->shipping_params->dim_approximation_h;
$data['methods']=@$rate->shipping_params->methods;
$data['destZip']=@$null->shipping_address->address_post_code;
$data['destCountry']=@$null->shipping_address->address_country->zone_code_2;
$data['zip']=@$rate->shipping_params->origination_postcode;
$data['total_insured']=@$total_price;
$data['sender_company']=@$rate->shipping_params->sender_company;
$data['sender_phone']=@$rate->shipping_params->sender_phone;
$data['sender_address']=@$rate->shipping_params->sender_address;
$data['sender_city']=@$rate->shipping_params->sender_city;
$state_zone = '';
$state_zone=@$rate->shipping_params->sender_state;
$query="SELECT zone_id, zone_code_2, zone_code_3 FROM ".hikashop_table('zone')." WHERE zone_namekey IN (".$db->Quote($state_zone).")";
$db->setQuery($query);
$state = $db->loadObject();
$data['sender_state'] = '';
if(isset($state->zone_code_2) && strlen($state->zone_code_2) == 2)
$data['sender_state'] = $state->zone_code_2;
elseif(strlen($state->zone_code_3) == 2)
$data['sender_state']=$state->zone_code_3;
$data['sender_postcode']=$rate->shipping_params->sender_postcode;
$data['recipient']=$null->shipping_address;
$czone_code = '';
$czone_code=@$rate->shipping_params->sender_country;
$query="SELECT zone_id, zone_code_2 FROM ".hikashop_table('zone')." WHERE zone_namekey IN (".$db->Quote($czone_code).")";
$db->setQuery($query);
$czone = $db->loadObject();
$data['country'] = $czone->zone_code_2;
$data['XMLpackage']='';
/*$data['destType']='';
if(@$rate->shipping_params->destination_type=='res'){
$data['destType']=''.$data['dimension_unit'].'
02
'.$data['weight_unit'].'
Request $v :
";
echo '
' . var_export($request, true) . ''; } // echo( '
'.var_export($request, true).'' ); // try { $response = $client->getRates($request); // } // catch(SoapFault $e) { } // echo( '
'.var_export($response, true).'' ); if(isset($response->HighestSeverity) && $response->HighestSeverity == "ERROR") { static $notif = false; if(!$notif && isset($response->Notifications->Message) && $response->Notifications->Message == 'Authentication Failed') { $app = JFactory::getApplication(); $app->enqueueMessage('FEDEX Authentication Failed'); $notif = true; } if(!$notif && !empty($response->Notifications->Message) && $response->Notifications->Message != 'Service is not allowed') { $app = JFactory::getApplication(); $app->enqueueMessage('The FedEx request failed with the message : ' . $response->Notifications->Message); } } if(@$rate->shipping_params->debug){ echo "
' . var_export($response, true) . ''; } // NOTE and WARNING = SUCCESS + warning/note message if(!empty($response->HighestSeverity) && ($response->HighestSeverity == "SUCCESS" || $response->HighestSeverity == "NOTE" || $response->HighestSeverity == "WARNING")) { $code = ''; $notes = array(); if($response->HighestSeverity == "NOTE" || $response->HighestSeverity == "WARNING") { $notes = $response->Notifications; } foreach($this->fedex_methods as $k=>$v) { if($v['code'] == $response->RateReplyDetails->ServiceType){ $code = $v['code']; } } $delayType = hikashop_get('type.delay'); if(!empty($response->RateReplyDetails->DeliveryTimestamp)) $timestamp = strtotime($response->RateReplyDetails->DeliveryTimestamp); else { $timestamp = 0; $response->RateReplyDetails->DeliveryTimestamp=0; } $totalNetPrice = 0; $discountAmount = 0; if(is_array($response->RateReplyDetails->RatedShipmentDetails)){ $totalNetPrice = $response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount; if($request['RequestedShipment']['RateRequestTypes'] != 'ACCOUNT'){ $discountAmount = $response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalFreightDiscounts->Amount; } $shipment[] = array( 'value'=>$totalNetPrice + $discountAmount, 'code'=>$code, 'delivery_timestamp' => $timestamp, 'day'=>$response->RateReplyDetails->DeliveryTimestamp, 'delivery_day' => date("m/d/Y", $timestamp), 'delivery_delay' => parent::displayDelaySECtoDAY($timestamp - strtotime('now'),2), 'delivery_time' => date("H:i:s", $timestamp), 'currency_code'=>$response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Currency, 'old_currency_code' => $response->RateReplyDetails->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Currency, 'notes' => $notes ); } else if(is_object($response->RateReplyDetails->RatedShipmentDetails)){ $totalNetPrice = $response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Amount; if($request['RequestedShipment']['RateRequestTypes'] != 'ACCOUNT'){ $discountAmount = $response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalFreightDiscounts->Amount; } $shipment[] = array( 'value'=>$totalNetPrice + $discountAmount, 'code'=>$code, 'delivery_timestamp' => $timestamp, 'day'=>$response->RateReplyDetails->DeliveryTimestamp, 'delivery_day' => date("m/d/Y", $timestamp), 'delivery_delay' => parent::displayDelaySECtoDAY($timestamp - strtotime('now'),2), 'delivery_time' => date("H:i:s", $timestamp), 'currency_code'=>$response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Currency, 'old_currency_code' => $response->RateReplyDetails->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Currency, 'notes' => $notes ); } } else if(!empty($response->HighestSeverity) && ($response->HighestSeverity == "ERROR")) { static $errorsDisplayed = array(); //TODO add error_code+message $acceptedCodes = array(836); if(!empty($response->Notifications)) { foreach($response->Notifications as $notif) { if(!is_object($notif)) continue; $errorCode = $notif->Code; if(!in_array($errorCode, $acceptedCodes)) { if(!isset($errorsDisplayed[$errorCode])) { $app = JFactory::getApplication(); $app->enqueueMessage($notif->Message); } $errorsDisplayed[$errorCode] = true; } } } } } return $shipment; } function printSuccess($client, $response) { echo '
' . htmlspecialchars($client->__getLastRequest()). ''; echo "\n"; echo '
' . htmlspecialchars($client->__getLastResponse()). ''; echo "\n"; } /** * Print SOAP Fault */ function printFault($exception, $client) { echo '