tribulant

FedEx Class file

Jun 8th, 2011
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.12 KB | None | 0 0
  1. <?php
  2.  
  3. require_once(dirname(__FILE__) . DS . "xmlparser.php");
  4.  
  5. class wpcofedex extends wpCheckoutPlugin {
  6.    
  7.     // Variables
  8.     var $server = "https://gatewaybeta.fedex.com/GatewayDC";
  9.     var $accountNumber;
  10.     var $meterNumber;
  11.     var $carrierCode = "FDXG";
  12.     var $dropoffType = "REGULARPICKUP";
  13.     var $service;
  14.     var $serviceName;
  15.     var $packaging = "YOURPACKAGING";
  16.     var $weightUnits = "LBS";
  17.     var $weight;
  18.     // Origin Address
  19.     var $originStateOrProvinceCode;
  20.     var $originPostalCode;
  21.     var $originCountryCode;
  22.     // Destination Address
  23.     var $destStateOrProvinceCode;
  24.     var $destPostalCode;
  25.     var $destCountryCode;
  26.     var $payorType = "SENDER";
  27.    
  28.     function wpcofedex() {
  29.         return;
  30.     }
  31.    
  32.     // Functions    
  33.     function setServer($server) {
  34.         $this->server = $server;
  35.         $this -> server = "https://gateway.fedex.com/GatewayDC";
  36.         return;
  37.     }
  38.  
  39.     function setAccountNumber($accountNumber) {
  40.         $this -> accountNumber = $accountNumber;
  41.     }
  42.  
  43.     function setMeterNumber($meterNumber) {
  44.         $this -> meterNumber = $meterNumber;
  45.     }
  46.  
  47.     function setCarrierCode($carrierCode) {
  48.         $this -> carrierCode = $carrierCode;
  49.     }
  50.    
  51.     function setDropoffType($dropoffType) {
  52.         $this -> dropoffType = $dropoffType;
  53.     }
  54.  
  55.     function setService($service, $name) {
  56.         $this->service = $service;
  57.         $this->serviceName = $name;
  58.     }
  59.  
  60.     function setPackaging($packaging) {
  61.         $this->packaging = $packaging;
  62.     }
  63.    
  64.     function setWeightUnits($units) {
  65.         $this->weightUnits = $units;
  66.     }
  67.    
  68.     function setWeight($weight) {
  69.         $this->weight = $weight;
  70.     }
  71.    
  72.     function setOriginStateOrProvinceCode($code) {
  73.         $this->originStateOrProvinceCode = $code;
  74.     }
  75.    
  76.     function setOriginPostalCode($code) {
  77.         $this->originPostalCode = $code;
  78.     }
  79.    
  80.     function setOriginCountryCode($code) {
  81.         $this->originCountryCode = $code;
  82.     }
  83.    
  84.     function setDestStateOrProvinceCode($code) {
  85.         $this->destStateOrProvinceCode = $code;
  86.     }
  87.    
  88.     function setDestPostalCode($code) {
  89.         $this->destPostalCode = $code;
  90.     }
  91.    
  92.     function setDestCountryCode($code) {
  93.         $this->destCountryCode = $code;
  94.     }
  95.    
  96.     function setPayorType($type) {
  97.         $this->payorType = $type;
  98.     }
  99.    
  100.     function getPrice() {
  101.        
  102.         $str = '<?xml version="1.0" encoding="UTF-8" ?>';
  103.         $str .= '    <FDXRateRequest xmlns:api="http://www.fedex.com/fsmapi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FDXRateRequest.xsd">';
  104.         $str .= '        <RequestHeader>';
  105.         $str .= '            <CustomerTransactionIdentifier>Express Rate</CustomerTransactionIdentifier>';
  106.         $str .= '            <AccountNumber>' . $this -> accountNumber . '</AccountNumber>';
  107.         $str .= '            <MeterNumber>' . $this -> meterNumber . '</MeterNumber>';
  108.         $str .= '            <CarrierCode>' . $this -> carrierCode . '</CarrierCode>';
  109.         $str .= '        </RequestHeader>';
  110.         $str .= '        <DropoffType>' . $this -> dropoffType . '</DropoffType>';
  111.         $str .= '        <Service>'.$this->service.'</Service>';
  112.         $str .= '        <Packaging>'.$this->packaging.'</Packaging>';
  113.         $str .= '        <WeightUnits>'.$this->weightUnits.'</WeightUnits>';
  114.         $str .= '        <Weight>'.number_format($this->weight, 1, '.', '').'</Weight>';
  115.         $str .= '        <OriginAddress>';
  116.         $str .= '            <StateOrProvinceCode>'.$this->originStateOrProvinceCode.'</StateOrProvinceCode>';
  117.         $str .= '            <PostalCode>'.$this->originPostalCode.'</PostalCode>';
  118.         $str .= '            <CountryCode>'.$this->originCountryCode.'</CountryCode>';
  119.         $str .= '        </OriginAddress>';
  120.         $str .= '        <DestinationAddress>';
  121.         //$str .= '            <StateOrProvinceCode>'.$this->destStateOrProvinceCode.'</StateOrProvinceCode>';
  122.         $str .= '            <PostalCode>'.$this->destPostalCode.'</PostalCode>';
  123.         $str .= '            <CountryCode>'.$this->destCountryCode.'</CountryCode>';
  124.         $str .= '        </DestinationAddress>';
  125.         $str .= '        <Payment>';
  126.         $str .= '            <PayorType>' . $this -> payorType . '</PayorType>';
  127.         $str .= '        </Payment>';
  128.         $str .= '        <PackageCount>' . ceil(bcdiv(number_format($this -> weight, 1, '.', ''), '150', 3)).'</PackageCount>';
  129.         $str .= '    </FDXRateRequest>';
  130.         //print($str);
  131.         $header[] = "Host: www.smart-shop.com";
  132.         $header[] = "MIME-Version: 1.0";
  133.         $header[] = "Content-type: multipart/mixed; boundary=----doc";
  134.         $header[] = "Accept: text/xml";
  135.         $header[] = "Content-length: ".strlen($str);
  136.         $header[] = "Cache-Control: no-cache";
  137.         $header[] = "Connection: close \r\n";
  138.         $header[] = $str;
  139.  
  140.         $ch = curl_init();
  141.         //Disable certificate check.
  142.         // uncomment the next line if you get curl error 60: error setting certificate verify locations
  143.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  144.         // uncommenting the next line is most likely not necessary in case of error 60
  145.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  146.         //-------------------------
  147.         //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  148.         //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  149.         //curl_setopt($ch, CURLOPT_CAINFO, "c:/ca-bundle.crt");
  150.         //-------------------------
  151.         curl_setopt($ch, CURLOPT_URL, $this -> server);
  152.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  153.         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  154.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  155.         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  156.            
  157.         $data = curl_exec($ch);
  158.              
  159.         if (curl_errno($ch)) {
  160.             $this -> getPrice();
  161.         } else {
  162.             $this -> debug($data);
  163.            
  164.             // close curl resource, and free up system resources
  165.             curl_close($ch);
  166.             $xmlParser = new xmlparser();
  167.             $array = $xmlParser -> GetXMLTree($data);
  168.             //$xmlParser->printa($array);
  169.             if(count($array['FDXRATEREPLY'][0]['ERROR'])) { // If it is error
  170.                 $error = new fedexError();
  171.                 $error -> number = $array['FDXRATEREPLY'][0]['ERROR'][0]['CODE'][0]['VALUE'];
  172.                 $error -> description = $array['FDXRATEREPLY'][0]['ERROR'][0]['MESSAGE'][0]['VALUE'];
  173.                 $error -> response = $array;
  174.                 $this ->error = $error;
  175.             } else if (count($array['FDXRATEREPLY'][0]['ESTIMATEDCHARGES'][0]['DISCOUNTEDCHARGES'][0]['NETCHARGE'])) {
  176.                 $price = new fedexPrice();
  177.                 $price -> rate = $array['FDXRATEREPLY'][0]['ESTIMATEDCHARGES'][0]['DISCOUNTEDCHARGES'][0]['NETCHARGE'][0]['VALUE'];
  178.                 $price -> service = $this->serviceName;
  179.                 $price -> response = $array;
  180.                 $this -> price = $price;            
  181.             }
  182.            
  183.             return $this;
  184.         }
  185.     }
  186.    
  187.     function getAllPrices($shipmethod = null, $order_id = null) {
  188.         global $Order;
  189.         $prices = array();
  190.        
  191.         if (!empty($shipmethod) && !empty($order_id)) {
  192.             //if ($fedex = $this -> vendor("fedex", "shipping" . DS . "fedex" . DS)) {
  193.                 $api_options = maybe_unserialize($shipmethod -> api_options);
  194.                 $userdata = $this -> userdata();
  195.                
  196.                 if ($fedex_services = $api_options['Service']) {
  197.                     $fedex_services_all = $this -> get_option('fedex_services');
  198.                     $prices = array();
  199.                    
  200.                     foreach ($fedex_services as $fedex_servicecode) {                      
  201.                         $fedex = $this -> vendor("fedex", "shipping" . DS . "fedex" . DS);
  202.                         $fedex -> setServer("https://gatewaybeta.fedex.com/GatewayDC");
  203.                         $fedex -> setAccountNumber($api_options['AccountNumber']);
  204.                         $fedex -> setMeterNumber($api_options['MeterNumber']);
  205.                         $fedex -> setCarrierCode($api_options['CarrierCode']);
  206.                         $fedex -> setDropoffType($api_options['DropoffType']);
  207.                         $fedex -> setService($fedex_servicecode, $fedex_services_all[$fedex_servicecode]);
  208.                         $fedex -> setPackaging($api_options['Packaging']);
  209.                         $fedex -> setWeightUnits($api_options['WeightUnits']);
  210.                         $fedex -> setWeight($Order -> weight($order_id));
  211.                         if (!empty($api_options['orig_StateOrProvinceCode'])) { $fedex -> setOriginStateOrProvinceCode($api_options['orig_StateOrProvinceCode']); }
  212.                         $fedex -> setOriginPostalCode($api_options['orig_PostalCode']);
  213.                         $fedex -> setOriginCountryCode($api_options['orig_CountryCode']);
  214.                         //$fedex -> setDestStateOrProvinceCode();
  215.                         $fedex -> setDestPostalCode($userdata -> ship_zipcode);
  216.                         $fedex -> setDestCountryCode($userdata -> ship_countrycode);
  217.                         $fedex -> setPayorType("SENDER");  
  218.                         $price = $this -> getPrice();
  219.                         $prices[] = $price;
  220.                     }  
  221.                 }
  222.             //}
  223.         }
  224.        
  225.         return $prices;
  226.     }
  227. }
  228. class fedexError
  229. {
  230.     var $number;
  231.     var $description;
  232.     var $response;
  233. }
  234. class fedexPrice
  235. {
  236.     var $service;
  237.     var $rate;
  238.     var $response;
  239. }
  240.  
  241. ?>
Advertisement
Add Comment
Please, Sign In to add comment