00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Usa_Model_Shipping_Carrier_Fedex
00035 extends Mage_Usa_Model_Shipping_Carrier_Abstract
00036 implements Mage_Shipping_Model_Carrier_Interface
00037 {
00038
00039 protected $_code = 'fedex';
00040
00041 protected $_request = null;
00042
00043 protected $_result = null;
00044
00045 protected $_gatewayUrl = 'https://gateway.fedex.com/GatewayDC';
00046
00047 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
00048 {
00049 if (!$this->getConfigFlag('active')) {
00050 return false;
00051 }
00052
00053 $this->setRequest($request);
00054
00055 $this->_result = $this->_getQuotes();
00056
00057 $this->_updateFreeMethodQuote($request);
00058
00059 return $this->getResult();
00060 }
00061
00062 public function setRequest(Mage_Shipping_Model_Rate_Request $request)
00063 {
00064 $this->_request = $request;
00065
00066 $r = new Varien_Object();
00067
00068 if ($request->getLimitMethod()) {
00069 $r->setService($request->getLimitMethod());
00070 }
00071
00072 if ($request->getFedexAccount()) {
00073 $account = $request->getFedexAccount();
00074 } else {
00075 $account = $this->getConfigData('account');
00076 }
00077 $r->setAccount($account);
00078
00079 if ($request->getFedexDropoff()) {
00080 $dropoff = $request->getFedexDropoff();
00081 } else {
00082 $dropoff = $this->getConfigData('dropoff');
00083 }
00084 $r->setDropoffType($dropoff);
00085
00086 if ($request->getFedexPackaging()) {
00087 $packaging = $request->getFedexPackaging();
00088 } else {
00089 $packaging = $this->getConfigData('packaging');
00090 }
00091 $r->setPackaging($packaging);
00092
00093 if ($request->getOrigCountry()) {
00094 $origCountry = $request->getOrigCountry();
00095 } else {
00096 $origCountry = Mage::getStoreConfig('shipping/origin/country_id', $this->getStore());
00097 }
00098 $r->setOrigCountry(Mage::getModel('directory/country')->load($origCountry)->getIso2Code());
00099
00100 if ($request->getOrigPostcode()) {
00101 $r->setOrigPostal($request->getOrigPostcode());
00102 } else {
00103 $r->setOrigPostal(Mage::getStoreConfig('shipping/origin/postcode', $this->getStore()));
00104 }
00105
00106 if ($request->getDestCountryId()) {
00107 $destCountry = $request->getDestCountryId();
00108 } else {
00109 $destCountry = self::USA_COUNTRY_ID;
00110 }
00111 $r->setDestCountry(Mage::getModel('directory/country')->load($destCountry)->getIso2Code());
00112
00113 if ($request->getDestPostcode()) {
00114 $r->setDestPostal($request->getDestPostcode());
00115 } else {
00116
00117 }
00118
00119 $weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
00120 $r->setWeight($weight);
00121 if ($request->getFreeMethodWeight()!= $request->getPackageWeight()) {
00122 $r->setFreeMethodWeight($request->getFreeMethodWeight());
00123 }
00124
00125 $r->setValue($request->getPackageValue());
00126 $r->setValueWithDiscount($request->getPackageValueWithDiscount());
00127
00128 $this->_rawRequest = $r;
00129
00130 return $this;
00131 }
00132
00133 public function getResult()
00134 {
00135 return $this->_result;
00136 }
00137
00138 protected function _getQuotes()
00139 {
00140 return $this->_getXmlQuotes();
00141 }
00142
00143 protected function _setFreeMethodRequest($freeMethod)
00144 {
00145 $r = $this->_rawRequest;
00146 $weight = $this->getTotalNumOfBoxes($r->getFreeMethodWeight());
00147 $r->setWeight($weight);
00148 $r->setService($freeMethod);
00149 }
00150
00151 protected function _getXmlQuotes()
00152 {
00153 $r = $this->_rawRequest;
00154 $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><FDXRateAvailableServicesRequest/>');
00155
00156 $xml->addAttribute('xmlns:api', 'http://www.fedex.com/fsmapi');
00157 $xml->addAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
00158 $xml->addAttribute('xsi:noNamespaceSchemaLocation', 'FDXRateAvailableServicesRequest.xsd');
00159
00160 $requestHeader = $xml->addChild('RequestHeader');
00161
00162 $requestHeader->addChild('AccountNumber', $r->getAccount());
00163
00164 $requestHeader->addChild('MeterNumber', '0');
00165
00166
00167
00168
00169
00170
00171
00172 $xml->addChild('ShipDate', date('Y-m-d'));
00173
00174
00175
00176
00177
00178
00179 $xml->addChild('DropoffType', $r->getDropoffType());
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 if ($r->hasService()) {
00190 $xml->addChild('Service', $r->getService());
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 $xml->addChild('Packaging', $r->getPackaging());
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 $xml->addChild('WeightUnits', 'LBS');
00226
00227
00228
00229
00230
00231 $xml->addChild('Weight', $r->getWeight());
00232
00233
00234
00235
00236
00237
00238
00239 $originAddress = $xml->addChild('OriginAddress');
00240
00241 $originAddress->addChild('PostalCode', $r->getOrigPostal());
00242 $originAddress->addChild('CountryCode', $r->getOrigCountry());
00243
00244 $destinationAddress = $xml->addChild('DestinationAddress');
00245
00246 $destinationAddress->addChild('PostalCode', $r->getDestPostal());
00247 $destinationAddress->addChild('CountryCode', $r->getDestCountry());
00248
00249 $payment = $xml->addChild('Payment');
00250 $payment->addChild('PayorType', 'SENDER');
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 $declaredValue = $xml->addChild('DeclaredValue');
00288 $declaredValue->addChild('Value', $r->getValue());
00289
00290 $declaredValue->addChild('CurrencyCode', $this->getCurrencyCode());
00291
00292 if ($this->getConfigData('residence_delivery')) {
00293 $specialServices = $xml->addChild('SpecialServices');
00294 $specialServices->addChild('ResidentialDelivery', 'true');
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 $xml->addChild('PackageCount', '1');
00394
00395 $request = $xml->asXML();
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 try {
00406 $url = $this->getConfigData('gateway_url');
00407 if (!$url) {
00408 $url = $this->_defaultGatewayUrl;
00409 }
00410 $ch = curl_init();
00411 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00412 curl_setopt($ch, CURLOPT_URL, $url);
00413 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
00414 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
00415 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
00416 $responseBody = curl_exec($ch);
00417 curl_close ($ch);
00418 } catch (Exception $e) {
00419 $responseBody = '';
00420 }
00421
00422 return $this->_parseXmlResponse($responseBody);
00423 }
00424
00425 protected function _parseXmlResponse($response)
00426 {
00427 $costArr = array();
00428 $priceArr = array();
00429 $errorTitle = 'Unable to retrieve quotes';
00430 if (strlen(trim($response))>0) {
00431 if (strpos(trim($response), '<?xml')===0) {
00432 $xml = simplexml_load_string($response);
00433 if (is_object($xml)) {
00434 if (is_object($xml->Error) && is_object($xml->Error->Message)) {
00435 $errorTitle = (string)$xml->Error->Message;
00436 } elseif (is_object($xml->SoftError) && is_object($xml->SoftError->Message)) {
00437 $errorTitle = (string)$xml->SoftError->Message;
00438 } else {
00439 $errorTitle = 'Unknown error';
00440 }
00441 $allowedMethods = explode(",", $this->getConfigData('allowed_methods'));
00442 foreach ($xml->Entry as $entry) {
00443 if (in_array((string)$entry->Service, $allowedMethods)) {
00444 $costArr[(string)$entry->Service] = (string)$entry->EstimatedCharges->DiscountedCharges->NetCharge;
00445 $priceArr[(string)$entry->Service] = $this->getMethodPrice((string)$entry->EstimatedCharges->DiscountedCharges->NetCharge, (string)$entry->Service);
00446 }
00447 }
00448 asort($priceArr);
00449 }
00450 } else {
00451 $errorTitle = 'Response is in the wrong format';
00452 }
00453 }
00454
00455 $result = Mage::getModel('shipping/rate_result');
00456 $defaults = $this->getDefaults();
00457 if (empty($priceArr)) {
00458 $error = Mage::getModel('shipping/rate_result_error');
00459 $error->setCarrier('fedex');
00460 $error->setCarrierTitle($this->getConfigData('title'));
00461
00462 $error->setErrorMessage($this->getConfigData('specificerrmsg'));
00463 $result->append($error);
00464 } else {
00465 foreach ($priceArr as $method=>$price) {
00466 $rate = Mage::getModel('shipping/rate_result_method');
00467 $rate->setCarrier('fedex');
00468 $rate->setCarrierTitle($this->getConfigData('title'));
00469 $rate->setMethod($method);
00470 $rate->setMethodTitle($this->getCode('method', $method));
00471 $rate->setCost($costArr[$method]);
00472 $rate->setPrice($price);
00473 $result->append($rate);
00474 }
00475 }
00476 return $result;
00477 }
00478
00479
00480
00481
00482
00483
00484
00485
00486 public function getCode($type, $code='')
00487 {
00488 $codes = array(
00489
00490 'method'=>array(
00491 'PRIORITYOVERNIGHT' => Mage::helper('usa')->__('Priority Overnight'),
00492 'STANDARDOVERNIGHT' => Mage::helper('usa')->__('Standard Overnight'),
00493 'FIRSTOVERNIGHT' => Mage::helper('usa')->__('First Overnight'),
00494 'FEDEX2DAY' => Mage::helper('usa')->__('2Day'),
00495 'FEDEXEXPRESSSAVER' => Mage::helper('usa')->__('Express Saver'),
00496 'INTERNATIONALPRIORITY' => Mage::helper('usa')->__('International Priority'),
00497 'INTERNATIONALECONOMY' => Mage::helper('usa')->__('International Economy'),
00498 'INTERNATIONALFIRST' => Mage::helper('usa')->__('International First'),
00499 'FEDEX1DAYFREIGHT' => Mage::helper('usa')->__('1 Day Freight'),
00500 'FEDEX2DAYFREIGHT' => Mage::helper('usa')->__('2 Day Freight'),
00501 'FEDEX3DAYFREIGHT' => Mage::helper('usa')->__('3 Day Freight'),
00502 'FEDEXGROUND' => Mage::helper('usa')->__('Ground'),
00503 'GROUNDHOMEDELIVERY' => Mage::helper('usa')->__('Home Delivery'),
00504 'INTERNATIONALPRIORITY FREIGHT' => Mage::helper('usa')->__('Intl Priority Freight'),
00505 'INTERNATIONALECONOMY FREIGHT' => Mage::helper('usa')->__('Intl Economy Freight'),
00506 'EUROPEFIRSTINTERNATIONALPRIORITY' => Mage::helper('usa')->__('Europe First Priority'),
00507 ),
00508
00509 'dropoff'=>array(
00510 'REGULARPICKUP' => Mage::helper('usa')->__('Regular Pickup'),
00511 'REQUESTCOURIER' => Mage::helper('usa')->__('Request Courier'),
00512 'DROPBOX' => Mage::helper('usa')->__('Drop Box'),
00513 'BUSINESSSERVICECENTER' => Mage::helper('usa')->__('Business Service Center'),
00514 'STATION' => Mage::helper('usa')->__('Station'),
00515 ),
00516
00517 'packaging'=>array(
00518 'FEDEXENVELOPE' => Mage::helper('usa')->__('FedEx Envelope'),
00519 'FEDEXPAK' => Mage::helper('usa')->__('FedEx Pak'),
00520 'FEDEXBOX' => Mage::helper('usa')->__('FedEx Box'),
00521 'FEDEXTUBE' => Mage::helper('usa')->__('FedEx Tube'),
00522 'FEDEX10KGBOX' => Mage::helper('usa')->__('FedEx 10kg Box'),
00523 'FEDEX25KGBOX' => Mage::helper('usa')->__('FedEx 25kg Box'),
00524 'YOURPACKAGING' => Mage::helper('usa')->__('Your Packaging'),
00525 ),
00526
00527 );
00528
00529 if (!isset($codes[$type])) {
00530
00531 return false;
00532 } elseif (''===$code) {
00533 return $codes[$type];
00534 }
00535
00536 if (!isset($codes[$type][$code])) {
00537
00538 return false;
00539 } else {
00540 return $codes[$type][$code];
00541 }
00542 }
00543
00544
00545
00546
00547
00548
00549 public function getCurrencyCode ()
00550 {
00551 $codes = array(
00552 'DOP' => 'RDD',
00553 'XCD' => 'ECD',
00554 'ARS' => 'ARN',
00555 'SGD' => 'SID',
00556 'KRW' => 'WON',
00557 'JMD' => 'JAD',
00558 'CHF' => 'SFR',
00559 'JPY' => 'JYE',
00560 'KWD' => 'KUD',
00561 'GBP' => 'UKL',
00562 'AED' => 'DHS',
00563 'MXN' => 'NMP',
00564 'UYU' => 'UYP',
00565 'CLP' => 'CHP',
00566 'TWD' => 'NTD',
00567 );
00568 $currencyCode = Mage::app()->getBaseCurrencyCode();
00569 return isset($codes[$currencyCode]) ? $codes[$currencyCode] : $currencyCode;
00570 }
00571
00572
00573 public function getTracking($trackings)
00574 {
00575 $this->setTrackingReqeust();
00576
00577 if (!is_array($trackings)) {
00578 $trackings=array($trackings);
00579 }
00580
00581 foreach($trackings as $tracking){
00582 $this->_getXMLTracking($tracking);
00583 }
00584
00585 return $this->_result;
00586 }
00587
00588 protected function setTrackingReqeust()
00589 {
00590 $r = new Varien_Object();
00591
00592 $account = $this->getConfigData('account');
00593 $r->setAccount($account);
00594
00595 $this->_rawTrackingRequest = $r;
00596
00597 }
00598 protected function _getXMLTracking($tracking)
00599 {
00600 $r = $this->_rawTrackingRequest;
00601
00602 $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><FDXTrack2Request/>');
00603 $xml->addAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
00604 $xml->addAttribute('xsi:noNamespaceSchemaLocation', 'FDXTrack2Request.xsd');
00605
00606 $requestHeader = $xml->addChild('RequestHeader');
00607 $requestHeader->addChild('AccountNumber', $r->getAccount());
00608
00609
00610
00611
00612 $requestHeader->addChild('MeterNumber', '0');
00613
00614 $packageIdentifier = $xml->addChild('PackageIdentifier');
00615 $packageIdentifier->addChild('Value', $tracking);
00616
00617
00618
00619
00620
00621 $xml->addChild('DetailScans', '1');
00622
00623 $request = $xml->asXML();
00624 try {
00625 $url = $this->getConfigData('gateway_url');
00626 if (!$url) {
00627 $url = $this->_defaultGatewayUrl;
00628 }
00629 $ch = curl_init();
00630 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00631 curl_setopt($ch, CURLOPT_URL, $url);
00632 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
00633 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
00634 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
00635 $responseBody = curl_exec($ch);
00636 curl_close ($ch);
00637 } catch (Exception $e) {
00638 $responseBody = '';
00639 }
00640
00641 #echo "<xmp>".$responseBody."</xmp>";
00642 $this->_parseXmlTrackingResponse($tracking, $responseBody);
00643 }
00644
00645 protected function _parseXmlTrackingResponse($trackingvalue,$response)
00646 {
00647 $resultArr=array();
00648 if (strlen(trim($response))>0) {
00649 if (strpos(trim($response), '<?xml')===0) {
00650 $xml = simplexml_load_string($response);
00651 if (is_object($xml)) {
00652 if (is_object($xml->Error) && is_object($xml->Error->Message)) {
00653 $errorTitle = (string)$xml->Error->Message;
00654 } elseif (is_object($xml->SoftError) && is_object($xml->SoftError->Message)) {
00655 $errorTitle = (string)$xml->SoftError->Message;
00656 }
00657 }else{
00658 $errorTitle = 'Error in loading response';
00659 }
00660
00661 if (!isset($errorTitle)) {
00662 $resultArr['status'] = (string)$xml->Package->StatusDescription;
00663 $resultArr['service'] = (string)$xml->Package->Service;
00664 $resultArr['deliverydate'] = (string)$xml->Package->DeliveredDate;
00665 $resultArr['deliverytime'] = (string)$xml->Package->DeliveredTime;
00666 $resultArr['deliverylocation'] = (string)$xml->TrackProfile->DeliveredLocationDescription;
00667 $resultArr['signedby'] = (string)$xml->Package->SignedForBy;
00668 $resultArr['shippeddate'] = (string)$xml->Package->ShipDate;
00669 $weight = (string)$xml->Package->Weight;
00670 $unit = (string)$xml->Package->WeightUnits;
00671 $resultArr['weight'] = "{$weight} {$unit}";
00672
00673 $packageProgress = array();
00674 if (isset($xml->Package->Event)) {
00675 foreach ($xml->Package->Event as $event) {
00676 $tempArr=array();
00677 $tempArr['activity'] = (string)$event->Description;
00678 $tempArr['deliverydate'] = (string)$event->Date;
00679 $tempArr['deliverytime'] = (string)$event->Time;
00680 $addArr=array();
00681 if (isset($event->Address->City)) {
00682 $addArr[] = (string)$event->Address->City;
00683 }
00684 if (isset($event->Address->StateProvinceCode)) {
00685 $addArr[] = (string)$event->Address->StateProvinceCode;
00686 }
00687 if (isset($event->Address->CountryCode)) {
00688 $addArr[] = (string)$event->Address->CountryCode;
00689 }
00690 if ($addArr) {
00691 $tempArr['deliverylocation']=implode(', ',$addArr);
00692 }
00693 $packageProgress[] = $tempArr;
00694 }
00695 }
00696
00697 $resultArr['progressdetail'] = $packageProgress;
00698 }
00699 } else {
00700 $errorTitle = 'Response is in the wrong format';
00701 }
00702 } else {
00703 $errorTitle = false;
00704 }
00705
00706 if(!$this->_result){
00707 $this->_result = Mage::getModel('shipping/tracking_result');
00708 }
00709 $defaults = $this->getDefaults();
00710
00711 if($resultArr){
00712 $tracking = Mage::getModel('shipping/tracking_result_status');
00713 $tracking->setCarrier('fedex');
00714 $tracking->setCarrierTitle($this->getConfigData('title'));
00715 $tracking->setTracking($trackingvalue);
00716 $tracking->addData($resultArr);
00717 $this->_result->append($tracking);
00718 }else{
00719 $error = Mage::getModel('shipping/tracking_result_error');
00720 $error->setCarrier('fedex');
00721 $error->setCarrierTitle($this->getConfigData('title'));
00722 $error->setTracking($trackingvalue);
00723 $error->setErrorMessage($errorTitle ? $errorTitle : Mage::helper('usa')->__('Unable to retrieve tracking'));
00724 $this->_result->append($error);
00725 }
00726 }
00727
00728 public function getResponse()
00729 {
00730 $statuses = '';
00731 if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result){
00732 if ($trackings = $this->_result->getAllTrackings()) {
00733 foreach ($trackings as $tracking){
00734 if($data = $tracking->getAllData()){
00735 if (!empty($data['status'])) {
00736 $statuses .= Mage::helper('usa')->__($data['status'])."\n<br/>";
00737 } else {
00738 $statuses .= Mage::helper('usa')->__('Empty response')."\n<br/>";
00739 }
00740 }
00741 }
00742 }
00743 }
00744 if (empty($statuses)) {
00745 $statuses = Mage::helper('usa')->__('Empty response');
00746 }
00747 return $statuses;
00748 }
00749
00750
00751
00752
00753
00754
00755 public function getAllowedMethods()
00756 {
00757 $allowed = explode(',', $this->getConfigData('allowed_methods'));
00758 $arr = array();
00759 foreach ($allowed as $k) {
00760 $arr[$k] = $this->getCode('method', $k);
00761 }
00762 return $arr;
00763 }
00764
00765 }